aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/gui/SettingsEditor.java
blob: c5233b52bf086393e9edfab59703539fb6e23b78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package net.sowgro.npehero.gui;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import net.sowgro.npehero.Driver;
import net.sowgro.npehero.main.*;

import java.io.IOException;

public class SettingsEditor extends Page
{
    private final HBox content = new HBox();
    private final CheckBox forceDefaultColors;
    private final Slider gameOpacity;
    private final ColorPicker[] colorPickers;

    record OptionEntry(String label, Node action) { }

    public SettingsEditor()
    {
        Slider musicSlider = new Slider(0.0, 1.0, 0);
        musicSlider.valueProperty().bindBidirectional(Settings.musicVol);

        CheckBox enableMenuMusic = new CheckBox("Enable Menu Music");
        enableMenuMusic.selectedProperty().bindBidirectional(Settings.enableMenuMusic);

        VBox musicBox = new VBox(musicSlider, enableMenuMusic);

        Slider SFXSlider = new Slider(0.0, 1.0, 0);
        SFXSlider.valueProperty().bindBidirectional(Settings.effectsVol);

        Button fullscreen = new Button();
        fullscreen.setText("Toggle (F11)");
        fullscreen.setOnAction(e -> {
            Sound.playSfx(Sound.FORWARD);
            Driver.primaryStage.setFullScreen(!Driver.primaryStage.isFullScreen());
        });

        Button controlsButton = new Button();
        controlsButton.setText("Edit");
        controlsButton.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            Driver.setMenu(new ControlEditor());
        });

        ToggleButton[] scaleOptions = new ToggleButton[4];
        for (int i = 0; i < scaleOptions.length; i++) {
            var val = i * 0.5 + 0.5;
            ToggleButton tb = new ToggleButton((int)(val * 100) + "%");
            tb.setUserData(val);
            tb.getStyleClass().remove("radio-button");
            tb.getStyleClass().add("button");
            if (val == Settings.guiScale.get()) {
                tb.setSelected(true);
            }
            scaleOptions[i] = tb;
        }

        ToggleGroup tg = new ToggleGroup();
        tg.getToggles().addAll(scaleOptions);
        tg.selectedToggleProperty().addListener((_, _, newt) -> {
            Settings.guiScale.set((Double)newt.getUserData());
        });
        HBox scaleSlider = new HBox(scaleOptions);

        colorPickers = new ColorPicker[] {
                new ColorPicker(Settings.defaultColors[0]),
                new ColorPicker(Settings.defaultColors[1]),
                new ColorPicker(Settings.defaultColors[2]),
                new ColorPicker(Settings.defaultColors[3]),
                new ColorPicker(Settings.defaultColors[4])
        };
        for (ColorPicker cp : colorPickers) {
            cp.getStyleClass().add("button");
            cp.setMinHeight(60);
            cp.setMinWidth(60);
        }

        HBox colorPickerBox = new HBox();
        colorPickerBox.getChildren().addAll(colorPickers);
        colorPickerBox.setSpacing(10);

        forceDefaultColors = new CheckBox("Force Default Colors");
        forceDefaultColors.setSelected(Settings.forceDefaultColors);

        Button resetColors = new Button("Reset");
        resetColors.setOnAction(_ -> {
            for (int i = 0; i < colorPickers.length; i++) {
                colorPickers[i].setValue(Settings.DEFAULT_DEFAULT_COLORS[i]);
            }
        });

        VBox defaultColors = new VBox(colorPickerBox, forceDefaultColors, resetColors);
        defaultColors.setSpacing(10);

        gameOpacity = new Slider(0.5, 1.0, 0.75);
        gameOpacity.setMajorTickUnit(0.25);
        gameOpacity.setMinorTickCount(1);
        gameOpacity.setSnapToTicks(true);

        OptionEntry[][] optionEntries = {
                {
                        new OptionEntry("Music Volume", musicBox),
                        new OptionEntry("Sound Effects Volume", SFXSlider),
                        new OptionEntry("Fullscreen Mode", fullscreen),
                        new OptionEntry("Key Bindings", controlsButton),
                },
                {
                        new OptionEntry("GUI Scale", scaleSlider),
                        new OptionEntry("Default Block Colors", defaultColors),
                        new OptionEntry("Game Opacity", gameOpacity)
                }
        };
        HBox options = new HBox();
        options.setSpacing(10);
        for (OptionEntry[] col : optionEntries) {
            VBox colBox = new VBox();
            colBox.setSpacing(10);
            colBox.setPrefWidth(420);

            for (OptionEntry option : col) {
                VBox optionBox = new VBox(new Label(option.label), option.action);
                optionBox.setPadding(new Insets(10));
                optionBox.setSpacing(5);
                optionBox.getStyleClass().add("box");
                colBox.getChildren().add(optionBox);
            }
            options.getChildren().add(colBox);
        }
        ScrollPane optionsScroll = new ScrollPane(options);
        optionsScroll.getStyleClass().remove("scroll-pane");
        optionsScroll.setFitToWidth(true);
//        optionsScroll.setPrefWidth(450);

        HBox main = new HBox();
        main.getChildren().addAll(optionsScroll);
        main.setSpacing(10);
        main.maxWidthProperty().bind(content.widthProperty().multiply(0.95));
        main.maxHeightProperty().bind(content.heightProperty().multiply(0.75));

        Button exit = new Button();
        exit.setText("Back");
        exit.setOnAction(_ -> {
            Sound.playSfx(Sound.BACKWARD);
            Driver.setMenu(new MainMenu());
        });

        VBox centerBox = new VBox();
        centerBox.getChildren().addAll(main, exit);
        centerBox.setSpacing(10);
        centerBox.setAlignment(Pos.CENTER);

        content.getChildren().add(centerBox);
        content.setAlignment(Pos.CENTER);
    }

    @Override
    public Pane getContent() {
        return content;
    }

    @Override
    public void onLeave() {
        Settings.forceDefaultColors = forceDefaultColors.isSelected();
        Settings.defaultColors[0] = colorPickers[0].getValue();
        Settings.defaultColors[1] = colorPickers[1].getValue();
        Settings.defaultColors[2] = colorPickers[2].getValue();
        Settings.defaultColors[3] = colorPickers[3].getValue();
        Settings.defaultColors[4] = colorPickers[4].getValue();
        Settings.gameOpacity = gameOpacity.getValue();
        try {
            Settings.save();
        } catch (IOException ex) {
            Driver.setMenu(new ErrorDisplay("Failed to save settings", ex, this));
        }
    }
}