aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/editor/LevelEditor.java
blob: 1f300065a1e82321a6dd9c9a1f588d719ceefa34 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package net.sowgro.npehero.editor;

import java.io.IOException;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import net.sowgro.npehero.Driver;
import net.sowgro.npehero.levelapi.Level;
import net.sowgro.npehero.main.*;

public class LevelEditor extends Page
{

    private final ValidIndicator songValid = new ValidIndicator();
    private final ValidIndicator diffsInvalid = new ValidIndicator();
    private final TextField titleEntry;
    private final TextField artistEntry;
    private final TextField descEntry;
    private final ColorPicker[] colorsPickers;
    private final Level level;

    private final HBox content = new HBox();

    record OptionEntry(String label, Node action, ValidIndicator vi) {
        OptionEntry(String label, Node action) {
            this(label, action, null);
        }
    }


    public LevelEditor(Level level, Page prev) {
        this.level = level;

        TextField folderName = new TextField();
        if (level.dir != null) {
            folderName.setText(level.dir.getName());
            folderName.setDisable(true);
        }

        titleEntry = new TextField(level.title);

        artistEntry = new TextField(level.artist);

        descEntry = new TextField(level.desc);

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

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

        Node songFile = createFileImportBox(
                Level.SONG_FILE,
                level.song,
                "Audio Files (*.wav, *.mp3, *.aac)",
                new String[]{"*.wav", "*.mp3", "*.aac"}
        );

        Node previewImage = createFileImportBox(
                Level.PREVIEW_FILE,
                level.preview,
                "Image Files (*.png, *.jpg, *.gif)",
                new String[]{"*.png", "*.jpg", "*.gif"}
        );

        Node backgroundImage = createFileImportBox(
                Level.BACKGROUND_FILE,
                level.background,
                "Image Files (*.png, *.jpg, *.gif)",
                new String[]{"*.png", "*.jpg", "*.gif"}
        );

        Button newDiffs = new Button("Edit difficulties");
        newDiffs.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            Driver.setMenu(new DiffList(level, this));
        });

        OptionEntry[][] optionEntries = {
                {
                        new OptionEntry("Folder", folderName),
                        new OptionEntry("Title", titleEntry),
                        new OptionEntry("Artist", artistEntry),
                        new OptionEntry("Description", descEntry),
                        new OptionEntry("Difficulties", newDiffs, diffsInvalid)
                },
                {
                        new OptionEntry("Song File", songFile, songValid),
                        new OptionEntry("Preview Image", previewImage),
                        new OptionEntry("Background Image", backgroundImage),
                        new OptionEntry("Block Colors", colorPickerBox),
                }
        };
        HBox options = new HBox();
        for (OptionEntry[] col : optionEntries) {
            VBox colBox = new VBox();
            colBox.setSpacing(10);
            colBox.setPrefWidth(400);
            for (OptionEntry option : col) {
                Label label = new Label(option.label);
                HBox labelBox = new HBox(label);
                labelBox.setSpacing(5);
                if (option.vi != null) {
                    labelBox.getChildren().add(option.vi);
                }
                VBox optionBox = new VBox(labelBox, option.action);
                optionBox.setPadding(new Insets(10));
                optionBox.setSpacing(5);
                optionBox.getStyleClass().add("box");
                colBox.getChildren().add(optionBox);
            }
            options.getChildren().add(colBox);
        }
        options.setSpacing(10);

        ScrollPane optionsScroll = new ScrollPane(options);
        optionsScroll.getStyleClass().remove("scroll-pane");
        optionsScroll.setFitToWidth(true);
//        optionsScroll.setPrefWidth(1100);

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

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

        HBox bottom = new HBox(exit);
        bottom.setAlignment(Pos.CENTER);
        bottom.setSpacing(10);

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

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

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

    @Override
    public void onView() {
        // validate
        if (level.difficulties.getValidList().isEmpty()) {
            diffsInvalid.setInvalid("This level contains no valid difficulties!");
        } else {
            diffsInvalid.setValid();
        }

        if (level.song == null) {
            songValid.setInvalid("Missing song file!");
        } else {
            songValid.setValid();
        }
    }

    @Override
    public void onLeave() {
        level.title = titleEntry.getText();
        level.artist = artistEntry.getText();
        level.desc = descEntry.getText();
        level.colors[0] = colorsPickers[0].getValue();
        level.colors[1] = colorsPickers[1].getValue();
        level.colors[2] = colorsPickers[2].getValue();
        level.colors[3] = colorsPickers[3].getValue();
        level.colors[4] = colorsPickers[4].getValue();
        try {
            level.writeMetadata();
        } catch (IOException e) {
            e.printStackTrace(); // TODO
        }
    }

    private Node createFileImportBox(String filename, Object dest, String extDesc, String[] extensions) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.getExtensionFilters().add(new ExtensionFilter(extDesc, extensions));
        Button importButton = new Button("Import");
        Button removeButton = new Button("Remove");
        importButton.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            var f = fileChooser.showOpenDialog(Driver.primaryStage);
            if (f == null) {
                return;
            }
            try {
                level.addFile(f, filename);
            } catch (IOException e) {
                e.printStackTrace(); // TODO
            }
            removeButton.setDisable(dest == null);
        });
        removeButton.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            try {
                level.removeFile(filename);
            } catch (IOException e) {
                e.printStackTrace(); // TODO
            }
            removeButton.setDisable(dest == null);
        });
        removeButton.setDisable(dest == null);

        var b1 = new HBox(importButton, removeButton);
        b1.setSpacing(10);
        return b1;
    }
}