Main menu loader (#277)

* LoadingWidget main menu

* fixed garbage code

* Fixed garbage code 2

* LoadWidget fix, added loading to starting the game. Removed unnecessary console output

---------

Co-authored-by: ramollia <>
This commit is contained in:
Bas Antonius de Jong
2025-12-03 14:49:59 +01:00
committed by GitHub
parent bc84171029
commit 628e4f30b5
17 changed files with 68 additions and 26 deletions

View File

@@ -13,11 +13,10 @@ public final class Main {
static void main(String[] args) {
initSystems();
App.run(args);
}
private static void initSystems() {
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/assets"));
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/localization"));
new Thread(() -> new NetworkingClientEventListener(new NetworkingClientManager())).start();
new Thread(() -> {

View File

@@ -1,12 +1,17 @@
package org.toop.app;
import org.toop.app.widget.Primitive;
import org.toop.app.widget.Widget;
import org.toop.app.widget.WidgetContainer;
import org.toop.app.widget.complex.LoadingWidget;
import org.toop.app.widget.display.SongDisplay;
import org.toop.app.widget.popup.QuitPopup;
import org.toop.app.widget.view.MainView;
import org.toop.framework.audio.events.AudioEvents;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.resource.ResourceLoader;
import org.toop.framework.resource.ResourceManager;
import org.toop.framework.resource.events.AssetLoaderEvents;
import org.toop.framework.resource.resources.CssAsset;
import org.toop.local.AppContext;
import org.toop.local.AppSettings;
@@ -47,7 +52,7 @@ public final class App extends Application {
stage.setMinHeight(720);
stage.setOnCloseRequest(event -> {
event.consume();
startQuit();
quit();
});
stage.setScene(scene);
@@ -63,11 +68,44 @@ public final class App extends Application {
App.isQuitting = false;
AppSettings.applySettings();
new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent();
var loading = new LoadingWidget(Primitive.text(
"Loading...", false), 0, 0, 9999
);
WidgetContainer.add(Pos.CENTER, new MainView());
WidgetContainer.add(Pos.BOTTOM_RIGHT, new SongDisplay());
WidgetContainer.add(Pos.CENTER, loading);
loading.setOnSuccess(() -> {
AppSettings.applySettings();
loading.hide();
WidgetContainer.add(Pos.CENTER, new MainView());
WidgetContainer.add(Pos.BOTTOM_RIGHT, new SongDisplay());
stage.setOnCloseRequest(event -> {
event.consume();
startQuit();
});
});
var loadingFlow = new EventFlow();
loadingFlow
.listen(AssetLoaderEvents.LoadingProgressUpdate.class, e -> {
loading.setMaxAmount(e.isLoadingAmount()-1);
try {
loading.setAmount(e.hasLoadedAmount());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
if (e.hasLoadedAmount() >= e.isLoadingAmount()-1) {
loading.triggerSuccess();
loadingFlow.unsubscribe("initloading");
}
}, false, "initloading");
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/assets"));
new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent();
}
public static void startQuit() {

View File

@@ -99,16 +99,9 @@ public final class Server {
a.onResponse(NetworkEvents.StartClientResponse.class, e -> {
if (!e.successful()) {
// loading.triggerFailure();
return;
}
try {
TimeUnit.MILLISECONDS.sleep(500); // TODO temp fix for index bug
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
WidgetContainer.getCurrentView().transitionPrevious();
a.unsubscribe("connecting");
@@ -131,6 +124,9 @@ public final class Server {
() -> {
try {
loading.setAmount(e.amount());
if (e.amount() >= loading.getMaxAmount()) {
loading.triggerFailure();
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
@@ -253,7 +249,7 @@ public final class Server {
} else {
stopScheduler();
}
}, 0, 5, TimeUnit.SECONDS);
}, 0, 1, TimeUnit.SECONDS);
}
private void stopScheduler() {

View File

@@ -23,7 +23,7 @@ import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.util.StringConverter;
public final class Primitive {
public final class Primitive {
public static Text header(String key, boolean localize) {
var header = new Text();
header.getStyleClass().add("header");
@@ -78,7 +78,6 @@ public final class Primitive {
button.setOnAction(_ -> {
onAction.run();
playButtonSound();
System.out.println("HI I got called button");
});
}

View File

@@ -1,5 +1,6 @@
package org.toop.app.widget.complex;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
@@ -7,6 +8,7 @@ import javafx.scene.text.Text;
import org.toop.app.widget.Primitive;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
public class LoadingWidget extends ViewWidget implements Update { // TODO make of widget type
private final ProgressBar progressBar;
@@ -14,8 +16,8 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
private Runnable success = () -> {};
private Runnable failure = () -> {};
private boolean successTriggered = false;
private boolean failureTriggered = false;
private AtomicBoolean successTriggered = new AtomicBoolean(false);
private AtomicBoolean failureTriggered = new AtomicBoolean(false);
private int maxAmount;
private int minAmount;
private int amount;
@@ -66,7 +68,7 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
}
public boolean isTriggered() {
return (failureTriggered || successTriggered);
return (failureTriggered.get() || successTriggered.get());
}
/**
@@ -105,21 +107,27 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
* Forcefully trigger success.
*/
public void triggerSuccess() {
successTriggered = true; // TODO, else it will double call... why?
success.run();
if (successTriggered.compareAndSet(false, true)) {
Platform.runLater(() -> {
if (success != null) success.run();
});
}
}
/**
* Forcefully trigger failure.
*/
public void triggerFailure() {
failureTriggered = true; // TODO, else it will double call... why?
failure.run();
if (failureTriggered.compareAndSet(false, true)) {
Platform.runLater(() -> {
if (failure != null) failure.run();
});
}
}
@Override
public void update() throws Exception { // TODO Better exception
if (successTriggered || failureTriggered) { // If already triggered, throw exception.
if (successTriggered.get() || failureTriggered.get()) { // If already triggered, throw exception.
throw new RuntimeException();
}
@@ -133,7 +141,9 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
return;
}
percentage = (float) amount / maxAmount;
if (maxAmount != 0) {
percentage = (float) amount / maxAmount;
}
progressBar.setProgress(percentage);
}