CircuitPresetEditor.java

package ui.emulationsettings;

import javafx.beans.property.FloatProperty;
import javafx.collections.FXCollections;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.util.StringConverter;
import libsidplay.common.ChipModel;
import ui.entities.config.FilterSection;
import ui.entities.config.Configuration;

import java.util.List;
import java.util.ResourceBundle;

/** Edits the selected circuit preset using the same persisted properties as INI and web playback. */
final class CircuitPresetEditor extends VBox {
    private final ResourceBundle bundle;
    private final Runnable changed;
    private final ComboBox<FilterSection> presets = new ComboBox<>();
    private final VBox controls = new VBox(6);
    private final Button reset = new Button();

    CircuitPresetEditor(List<FilterSection> filters, ResourceBundle bundle, Runnable changed) {
        super(6);
        this.bundle = bundle;
        this.changed = changed;
        presets.setItems(FXCollections.observableArrayList(filters.stream()
                .filter(f -> f.getCircuitModel() != ChipModel.AUTO).toList()));
        presets.setConverter(new StringConverter<>() {
            @Override public String toString(FilterSection filter) { return filter == null ? "" : filter.getName(); }
            @Override public FilterSection fromString(String value) { throw new UnsupportedOperationException(); }
        });
        presets.valueProperty().addListener((observable, old, selected) -> show(selected));
        reset.setText(bundle.getString("CIRCUIT_RESET"));
        reset.setOnAction(event -> {
            FilterSection selected = presets.getValue();
            if (selected == null) return;
            Configuration.DEFAULT_FILTERS.stream().filter(f -> f.getName().equals(selected.getName())).findFirst().ifPresent(original -> {
                selected.setCircuitCurve(original.getCircuitCurve());
                selected.setCircuitRange(original.getCircuitRange());
                selected.setCircuitCapacitance(original.getCircuitCapacitance());
                selected.setVoiceNonlinearity(original.getVoiceNonlinearity());
                selected.setResonanceFactor(original.getResonanceFactor());
                show(selected);
                changed.run();
            });
        });
        getChildren().addAll(presets, reset, controls);
        presets.getSelectionModel().selectFirst();
    }

    private void show(FilterSection preset) {
        controls.getChildren().clear();
        reset.setDisable(preset == null || Configuration.DEFAULT_FILTERS.stream()
                .noneMatch(f -> f.getName().equals(preset.getName())));
        if (preset == null) return;
        slider("CIRCUIT_CURVE", preset.circuitCurveProperty(), 0, 1);
        if (preset.getCircuitModel() == ChipModel.MOS6581) {
            slider("CIRCUIT_RANGE", preset.circuitRangeProperty(), 0, 1);
            slider("CIRCUIT_CAPACITANCE", preset.circuitCapacitanceProperty(), 330, 2200);
            slider("CIRCUIT_VOICE_NONLINEARITY", preset.voiceNonlinearityProperty(), 0.8, 1);
        }
        slider("CIRCUIT_RESONANCE", preset.resonanceFactorProperty(), 0, 2);
    }

    private void slider(String key, FloatProperty property, double min, double max) {
        Label value = new Label();
        value.textProperty().bind(property.asString(bundle.getString(key) + ": %.3f"));
        Slider slider = new Slider(min, max, property.get());
        slider.setShowTickLabels(true);
        slider.setMajorTickUnit((max - min) / 4);
        // Update the property while dragging; apply the model once the drag ends.
        slider.valueProperty().addListener((observable, old, next) -> {
            property.set(next.floatValue());
            if (!slider.isValueChanging()) changed.run();
        });
        slider.valueChangingProperty().addListener((observable, old, dragging) -> {
            if (!dragging) changed.run();
        });
        controls.getChildren().addAll(value, slider);
    }
}