aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/sowgro/npehero/editor/DiffEditor.java
blob: 0ae8f5fc444b586a7a815e3c9f87471fed39d7e2 (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.editor;

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 net.sowgro.npehero.Driver;
import net.sowgro.npehero.gui.LeaderboardView;
import net.sowgro.npehero.gui.LevelSurround;
import javafx.scene.layout.VBox;
import net.sowgro.npehero.levelapi.Difficulty;
import net.sowgro.npehero.main.*;

import java.io.IOException;

public class DiffEditor extends Page
{
    private final TextField titleEntry;
    private final Difficulty diff;
    private final ValidIndicator validNotes = new ValidIndicator();

    private final HBox content = new HBox();

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

    public DiffEditor(Difficulty diff, Page prev) {
        this.diff = diff;

        TextField folderName = new TextField(diff.thisDir.getName());
        folderName.setDisable(true);

        titleEntry = new TextField(diff.title);

        Button clearLeaderboard = new Button("Clear");
        clearLeaderboard.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            diff.leaderboard.entries.clear();
            try {
                diff.leaderboard.write();
            } catch (IOException e) {
                Driver.setMenu(new ErrorDisplay("Failed to clear the leaderboard:\n"+e, this));
            }
            clearLeaderboard.setDisable(diff.leaderboard.entries.isEmpty());
        });
        clearLeaderboard.setDisable(diff.leaderboard.entries.isEmpty());

        Button viewLeaderboard = new Button("View");
        viewLeaderboard.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            Driver.setMenu(new LeaderboardView(diff, this));
        });

        HBox leaderboardActions = new HBox(viewLeaderboard, clearLeaderboard);
        leaderboardActions.setSpacing(10);

        Button playLevel = new Button("Play");
        playLevel.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            if (diff.isValid() && diff.level.isValid()) {
                Driver.setMenu(new LevelSurround(diff, this));
            }
            else {
                Driver.setMenu(new ErrorDisplay("This Level is not valid!\nCheck that all required fields are populated.", this));
            }
        });

        Button editNotes = new Button("Edit Notes");
        editNotes.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            if (diff.level.song == null) {
                Driver.setMenu(new ErrorDisplay("You must import a song file before editing the notes!", this));
                return;
            }
//            if (diff.bpm != 0.0) {
//                Driver.setMenu(new ErrorDisplay(
//                        "Note:\nThe new notes editor does not support bpm and beat based songs. If you continue, the notes will be converted.",
//                        this,
//                        new NotesEditor2(diff, this)
//                ));
//                return;
//            }
            Driver.setMenu(new NotesEditor2(diff, this));
        });

        Button oldEditNotes = new Button("Edit legacy");
        oldEditNotes.setOnAction(_ -> {
            Sound.playSfx(Sound.FORWARD);
            Driver.setMenu(new ErrorDisplay(
                "Warning: \nThe legacy editor will overwrite all existing notes!",
                this,
                new NotesEditor(diff, this))
            );
        });

        HBox noteActions = new HBox(playLevel, editNotes/*, oldEditNotes*/);
        noteActions.setSpacing(10);

        OptionEntry[] optionsEntries = {
                new OptionEntry("Folder name", folderName),
                new OptionEntry("Title", titleEntry),
                new OptionEntry("Scores", leaderboardActions),
                new OptionEntry("Notes", noteActions, validNotes),
        };
        VBox options = new VBox();
        for (OptionEntry option : optionsEntries) {
            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");
            options.getChildren().add(optionBox);
        }
        options.setSpacing(10);

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

        HBox main = new HBox();
        main.getChildren().addAll(optionsScroll);
        main.setSpacing(10);
        main.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.setSpacing(10);
        bottom.setAlignment(Pos.CENTER);

        VBox centerBox = new VBox();
        centerBox.getChildren().addAll(main, 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() {
        update();
    }

    @Override
    public void onLeave() {
        diff.title = titleEntry.getText();
        try {
            diff.writeMetadata();
        } catch (IOException e) {
            e.printStackTrace(); //TODO
        }
    }

    private void update() {
        if (diff.notes.list.isEmpty()) {
            validNotes.setInvalid("This difficulty does not contain any notes!");
        } else {
            validNotes.setValid();
        }
    }
}