-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
429 lines (398 loc) · 17.1 KB
/
Main.java
File metadata and controls
429 lines (398 loc) · 17.1 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.Label;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import java.util.Optional;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
public class Main extends Application {
private BorderPane root;
private Flashcards currentSet;
// Default flashcard set
ArrayList<String> default_terms = new ArrayList<>(Arrays.asList("CPU", "RAM", "SSD", "GPU"));
ArrayList<String> default_definitions = new ArrayList<>(Arrays.asList("Central Processing Unit", "Random Access Memory", "Solid State Drive", "Graphics Processing Unit"));
Flashcards flashcards = new Flashcards(default_terms, default_definitions);
private EXPBarUI expBar;
Image appIcon = null;
Font honk;
@Override
public void start(Stage stage) {
// font loading
String[] fonts = {"microwave", "honk"};
for (String fontName : fonts) {
java.net.URL fontUrl = getClass().getResource("/fonts/" + fontName + ".ttf");
if (fontUrl != null) {
Font.loadFont(fontUrl.toExternalForm(), 10);
}
}
stage.setTitle("FlashQuiz");
// load application icon (try classpath first, then local file)
try {
java.net.URL iconUrl = getClass().getResource("/icon.png");
if (iconUrl != null) {
appIcon = new Image(iconUrl.toExternalForm());
} else {
File f = new File("icon.png");
if (f.exists()) appIcon = new Image(f.toURI().toString());
}
if (appIcon != null) stage.getIcons().add(appIcon);
} catch (Exception ignored) {}
flashcards.addFlashcardSet(flashcards);
if (!Flashcards.IDs.isEmpty()) {
Flashcards.titles.set(Flashcards.IDs.size()-1, "Default Set");
}
VBox menu = new VBox(10);
menu.setPadding(new Insets(12));
Button homeBtn = new Button("Home");
Button setsBtn = new Button("Sets");
Button learnBtn = new Button("Learn");
Button matchingBtn = new Button("Matching");
Button bossBtn = new Button("Boss Battle");
Button accuracyBtn = new Button("Accuracy");
homeBtn.getStyleClass().addAll("nav-button", "primary");
setsBtn.getStyleClass().addAll("nav-button", "danger");
learnBtn.getStyleClass().addAll("nav-button", "accent");
matchingBtn.getStyleClass().addAll("nav-button", "accent");
bossBtn.getStyleClass().addAll("nav-button", "accent");
accuracyBtn.getStyleClass().addAll("nav-button", "accent");
// add hover animations to nav buttons
Animations.applyButtonHover(homeBtn);
Animations.applyButtonHover(setsBtn);
Animations.applyButtonHover(learnBtn);
Animations.applyButtonHover(matchingBtn);
Animations.applyButtonHover(accuracyBtn);
Animations.applyButtonHover(bossBtn);
menu.getChildren().addAll(homeBtn, setsBtn, learnBtn, matchingBtn, accuracyBtn, bossBtn);
root = new BorderPane();
root.getStyleClass().add("root");
expBar = EXPBarUI.getInstance();
root.setTop(expBar);
root.setLeft(menu);
root.setCenter(buildHomeScreen());
homeBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
root.setCenter(buildHomeScreen());
}
});
learnBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
root.setCenter(buildLearnScreen());
}
});
matchingBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
root.setCenter(buildMatchingScreen());
}
});
bossBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
root.setCenter(buildBossScreen());
}
});
accuracyBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
root.setCenter(buildAccuracyScreen());
}
});
setsBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
root.setCenter(buildSetsScreen());
}
});
Scene scene = new Scene(root, 900, 650);
try {
java.net.URL cssUrl = getClass().getResource("/styles.css");
if (cssUrl != null) {
scene.getStylesheets().add(cssUrl.toExternalForm());
} else {
java.io.File css = new java.io.File("styles.css");
java.io.File cssApp = new java.io.File("app/styles.css");
if (css.exists()) {
scene.getStylesheets().add(css.toURI().toString());
} else if (cssApp.exists()) {
scene.getStylesheets().add(cssApp.toURI().toString());
}
}
} catch (Exception ignored) {
}
stage.setScene(scene);
stage.show();
}
private Node buildHomeScreen() {
// temp button functionality test;
// File soundFile = new File("sounds/vineBoom.mp3");
// Button playSoundBtn = new Button("Play Sound");
// String uri = soundFile.toURI().toString();
// Media media = new Media(uri);
// MediaPlayer mediaPlayer = new MediaPlayer(media);
// playSoundBtn.setOnAction(new EventHandler<ActionEvent>() {
// @Override
// public void handle(ActionEvent e) {
// mediaPlayer.stop();
// mediaPlayer.seek(Duration.ZERO);
// mediaPlayer.play();
// }
// });
// box.getChildren().add(playSoundBtn);
// end temp button functionality test
Label title = new Label("FlashQuiz");
Label subtitle = new Label("Selected Set");
honk = Font.loadFont("file:fonts/honk.ttf", 48);
// Label warning = new Label("WARNING: The XP bar will reset every time you close the Program.");
VBox box = new VBox(12);
box.getStyleClass().add("app-container");
title.getStyleClass().add("app-header");
title.setFont(honk);
subtitle.getStyleClass().add("sub-title");
box.setPadding(new Insets(16));
FlowPane wrap = new FlowPane();
wrap.getStyleClass().add("card-grid");
wrap.setHgap(12);
wrap.setVgap(12);
int selectedIndex = 0;
if (currentSet != null) {
int idx = Flashcards.IDs.indexOf(currentSet);
if (idx >= 0) selectedIndex = idx;
}
for (int i = 0; i < Flashcards.IDs.size(); i++) {
Flashcards fc = Flashcards.IDs.get(i);
int index = i;
int count = 0;
try {
var s = fc.getFlashcardSet();
if (s != null && s.size() >= 2 && s.get(0) != null) {
count = s.get(0).size();
}
} catch (Exception ignored) {}
String name = (index < Flashcards.titles.size()) ? Flashcards.titles.get(index) : ("Set " + (index + 1));
Button card = new Button(name + " - " + count + " cards");
card.getStyleClass().add("set-card");
Animations.applyCardHover(card);
Animations.applyButtonHover(card);
if (index == selectedIndex) {
card.getStyleClass().add("selected");
}
card.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
currentSet = fc;
for (Node n : wrap.getChildren()) {
n.getStyleClass().remove("selected");
}
card.getStyleClass().add("selected");
}
});
wrap.getChildren().add(card);
}
if (!wrap.getChildren().isEmpty()) {
if (currentSet == null) {
currentSet = Flashcards.IDs.get(selectedIndex);
}
int idx = Flashcards.IDs.indexOf(currentSet);
if (idx < 0) idx = selectedIndex;
}
// warning.getStyleClass().add("levels-warning");
// box.getChildren().addAll(title, subtitle, wrap, warning);
box.getChildren().addAll(title, subtitle, wrap);
Animations.fadeIn(box);
return box;
}
private Node buildLearnScreen() {
VBox box = new VBox(10);
box.setPadding(new Insets(16));
box.getStyleClass().add("app-container");
Label title = new Label("Learn");
title.getStyleClass().add("app-header");
if (currentSet == null || currentSet.getFlashcardSet() == null || currentSet.getFlashcardSet().size() < 2) {
box.getChildren().addAll(title, new Label("Pick a set on Home first."));
return box;
}
ArrayList<ArrayList<String>> cs = currentSet.getFlashcardSet();
ArrayList<String> terms = cs.get(0);
ArrayList<String> definitions = cs.get(1);
ArrayList<Double> weights = currentSet.getWeights();
Learn learnView = new Learn(terms, weights, definitions, expBar);
return learnView;
}
private Node buildMatchingScreen() {
VBox box = new VBox(10);
box.setPadding(new Insets(16));
box.getStyleClass().add("app-container");
Label title = new Label("Matching");
title.getStyleClass().add("app-header");
if (currentSet == null || currentSet.getFlashcardSet() == null || currentSet.getFlashcardSet().size() < 2) {
return box;
}
int count = 0;
try {
ArrayList<ArrayList<String>> s = currentSet.getFlashcardSet();
if (s != null && s.size() >= 2 && s.get(0) != null) {
count = s.get(0).size();
}
} catch (Exception ignored) {}
ArrayList<ArrayList<String>> cs = currentSet.getFlashcardSet();
Matching matchview = new Matching(cs.get(0), cs.get(1), expBar);
return matchview;
}
private Node buildBossScreen() {
VBox box = new VBox(10);
box.setPadding(new Insets(16));
Label title = new Label("Boss Battle");
if (currentSet == null || currentSet.getFlashcardSet() == null || currentSet.getFlashcardSet().size() < 2) {
box.getChildren().addAll(title, new Label("Pick a set on Home first."));
return box;
}
int count = 0;
try {
ArrayList<ArrayList<String>> s = currentSet.getFlashcardSet();
if (s != null && s.size() >= 2 && s.get(0) != null) {
count = s.get(0).size();
}
} catch (Exception ignored) {}
ArrayList<ArrayList<String>> cs = currentSet.getFlashcardSet();
ArrayList<String> terms = cs.get(0);
ArrayList<String> definitions = cs.get(1);
ArrayList<Double> weights = currentSet.getWeights();
boolean isMCQ = false; // placeholder mode selection
Boss bossview = new Boss(terms, definitions, weights, expBar);
return bossview;
}
private Node buildAccuracyScreen() {
VBox box = new VBox(10);
box.setPadding(new Insets(16));
box.getStyleClass().add("app-container");
Label title = new Label("Accuracy Challenge");
title.getStyleClass().add("app-header");
if (currentSet == null || currentSet.getFlashcardSet() == null || currentSet.getFlashcardSet().size() < 2) {
box.getChildren().addAll(title, new Label("Pick a set on Home first."));
return box;
}
ArrayList<ArrayList<String>> cs = currentSet.getFlashcardSet();
ArrayList<String> terms = cs.get(0);
ArrayList<String> definitions = cs.get(1);
ArrayList<Double> weights = currentSet.getWeights();
Accuracy accuracy = new Accuracy(terms, definitions, weights, expBar);
return accuracy;
}
private Node buildSetsScreen() {
VBox box = new VBox(10);
box.setPadding(new Insets(16));
Label title = new Label("Sets");
title.getStyleClass().add("app-header");
ListView<String> list = new ListView<>();
list.getStyleClass().add("sets-list");
int index = 1;
for (Flashcards fc : Flashcards.IDs) {
int count = 0;
try {
var s = fc.getFlashcardSet();
if (s != null && s.size() >= 2 && s.get(0) != null) {
count = s.get(0).size();
}
} catch (Exception ignored) {}
String name = (index-1 < Flashcards.titles.size()) ? Flashcards.titles.get(index-1) : ("Set " + index);
list.getItems().add(name + " - " + count + " cards");
index++;
}
int curIdx = Flashcards.IDs.indexOf(currentSet);
if (curIdx >= 0) {
list.getSelectionModel().select(curIdx);
}
list.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> obs, Number oldVal, Number newVal) {
int sel = newVal == null ? -1 : newVal.intValue();
if (sel >= 0 && sel < Flashcards.IDs.size()) {
currentSet = Flashcards.IDs.get(sel);
}
}
});
Button createBtn = new Button("Create Set");
createBtn.getStyleClass().addAll("nav-button", "accent");
createBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
root.setCenter(new SetsEditor());
}
});
Button editBtn = new Button("Edit Set");
editBtn.getStyleClass().addAll("nav-button", "accent");
editBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
int sel = list.getSelectionModel().getSelectedIndex();
if (sel >= 0 && sel < Flashcards.IDs.size()) {
root.setCenter(new SetsEditor(Flashcards.IDs.get(sel)));
} else {
Alert info = new Alert(Alert.AlertType.INFORMATION);
info.setTitle("Edit Set");
info.setHeaderText(null);
info.setContentText("Please select a set from the list to edit.");
info.showAndWait();
}
}
});
Button deleteSetsBtn = new Button("Delete Set");
deleteSetsBtn.getStyleClass().addAll("nav-button", "danger");
deleteSetsBtn.setOnAction(e -> {
int sel = list.getSelectionModel().getSelectedIndex();
if (sel < 0 || sel >= Flashcards.IDs.size()) return;
Alert confirm = new Alert(Alert.AlertType.CONFIRMATION);
confirm.setTitle("Delete Set");
confirm.setHeaderText("Delete selected set");
confirm.setContentText("Are you sure you want to delete the selected set? This cannot be undone.");
Optional<ButtonType> res = confirm.showAndWait();
if (res.isPresent() && res.get() == ButtonType.OK) {
// Remove from model
Flashcards.IDs.remove(sel);
if (sel < Flashcards.titles.size()) Flashcards.titles.remove(sel);
// Remove from view
list.getItems().remove(sel);
// Adjust currentSet selection
if (currentSet != null && !Flashcards.IDs.contains(currentSet)) {
if (!Flashcards.IDs.isEmpty()) {
int newSel = Math.min(sel, Flashcards.IDs.size() - 1);
currentSet = Flashcards.IDs.get(newSel);
list.getSelectionModel().select(newSel);
} else {
currentSet = null;
}
} else {
int currentIdx = Flashcards.IDs.indexOf(currentSet);
if (currentIdx >= 0) list.getSelectionModel().select(currentIdx);
}
}
});
HBox hbox = new HBox(8);
hbox.getChildren().addAll(createBtn, editBtn, deleteSetsBtn);
box.getChildren().addAll(title, list, hbox);
Animations.fadeIn(box);
return box;
}
public static void main(String[] args) {
launch(args);
}
}