Added infinite boolean, fixed loading behaviour at startup

This commit is contained in:
lieght
2025-12-03 18:56:08 +01:00
parent 740d2cf3db
commit 040287ad70
12 changed files with 47 additions and 30 deletions

View File

@@ -45,6 +45,7 @@ public final class App extends Application {
public void start(Stage stage) throws Exception {
// Start loading localization
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/localization"));
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/style"));
final StackPane root = WidgetContainer.setup();
final Scene scene = new Scene(root);
@@ -67,8 +68,6 @@ public final class App extends Application {
stage.setScene(scene);
stage.setResizable(true);
stage.show();
App.stage = stage;
App.scene = scene;
@@ -77,8 +76,10 @@ public final class App extends Application {
App.isQuitting = false;
AppSettings.applySettings();
LoadingWidget loading = new LoadingWidget(Primitive.text(
"Loading...", false), 0, 0, Integer.MAX_VALUE // Just set a high default
"Loading...", false), 0, 0, Integer.MAX_VALUE, false // Just set a high default
);
WidgetContainer.add(Pos.CENTER, loading);
@@ -88,7 +89,7 @@ public final class App extends Application {
EventFlow loadingFlow = new EventFlow();
loadingFlow
.listen(AssetLoaderEvents.LoadingProgressUpdate.class, e -> {
loading.setMaxAmount(e.isLoadingAmount()-1);
loading.setMaxAmount(e.isLoadingAmount());
try {
loading.setAmount(e.hasLoadedAmount());
@@ -96,7 +97,7 @@ public final class App extends Application {
throw new RuntimeException(ex);
}
if (e.hasLoadedAmount() >= e.isLoadingAmount()-1) {
if (e.hasLoadedAmount() >= e.isLoadingAmount()) {
loading.triggerSuccess();
loadingFlow.unsubscribe("init_loading");
}
@@ -104,15 +105,17 @@ public final class App extends Application {
}, false, "init_loading");
// Start loading assets
ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/assets"));
new Thread(() -> ResourceManager.loadAssets(new ResourceLoader("app/src/main/resources/assets")))
.start();
stage.show();
}
private void setOnLoadingSuccess(LoadingWidget loading) {
loading.setOnSuccess(() -> {
initSystems();
AppSettings.applySettings();
new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent();
loading.hide();
AppSettings.applyMusicVolumeSettings();
new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).postEvent();
loading.hide();
WidgetContainer.add(Pos.CENTER, new MainView());
WidgetContainer.add(Pos.BOTTOM_RIGHT, new SongDisplay());
stage.setOnCloseRequest(event -> {
@@ -145,7 +148,14 @@ public final class App extends Application {
).initListeners("medium-button-click.wav");
}).start();
}
// Threads must be ready, before continue, TODO use latch instead
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public static void startQuit() {
if (isQuitting) {

View File

@@ -73,10 +73,10 @@ public final class Server {
return;
}
final int reconnectAttempts = 5;
final int reconnectAttempts = 10;
LoadingWidget loading = new LoadingWidget(
Primitive.text("connecting"), 0, 0, reconnectAttempts
Primitive.text("connecting"), 0, 0, reconnectAttempts, true
);
WidgetContainer.getCurrentView().transitionNext(loading);

View File

@@ -127,7 +127,6 @@ public final class Primitive {
slider.setOnMouseReleased(event -> {
playButtonSound();
System.out.println("I got called!");
});
return slider;
@@ -150,7 +149,6 @@ public final class Primitive {
choice.valueProperty().addListener((_, _, newValue) -> {
onValueChanged.accept(newValue);
playButtonSound();
System.out.println("hi i got called choice");
});
}

View File

@@ -11,13 +11,13 @@ 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 Text loadingText; // TODO Make changeable
private final ProgressBar progressBar;
private final Text loadingText;
private final AtomicBoolean successTriggered = new AtomicBoolean(false);
private final AtomicBoolean failureTriggered = new AtomicBoolean(false);
private Runnable success = () -> {};
private Runnable failure = () -> {};
private AtomicBoolean successTriggered = new AtomicBoolean(false);
private AtomicBoolean failureTriggered = new AtomicBoolean(false);
private int maxAmount;
private int minAmount;
private int amount;
@@ -25,6 +25,8 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
private Callable<Boolean> failureTrigger = () -> (amount < minAmount);
private float percentage = 0.0f;
private boolean isInfiniteBar = false;
/**
*
* Widget that shows a loading bar.
@@ -34,13 +36,15 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
* @param startAmount The starting amount.
* @param maxAmount The max amount.
*/
public LoadingWidget(Text loadingText, int minAmount, int startAmount, int maxAmount) {
public LoadingWidget(Text loadingText, int minAmount, int startAmount, int maxAmount, boolean infiniteBar) {
isInfiniteBar = infiniteBar;
this.maxAmount = maxAmount;
this.minAmount = minAmount;
amount = startAmount;
progressBar = new ProgressBar();
this.loadingText = loadingText;
progressBar = new ProgressBar();
VBox box = Primitive.vbox(this.loadingText, progressBar);
add(Pos.CENTER, box);
@@ -144,7 +148,6 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
if (maxAmount != 0) {
percentage = (float) amount / maxAmount;
}
progressBar.setProgress(percentage);
if (!isInfiniteBar) progressBar.setProgress(percentage);
}
}

View File

@@ -27,18 +27,24 @@ public class AppSettings {
AppContext.setLocale(Locale.of(settingsData.locale));
App.setFullscreen(settingsData.fullScreen);
new EventFlow()
.addPostEvent(new AudioEvents.ChangeVolume(settingsData.volume, VolumeControl.MASTERVOLUME))
.asyncPostEvent();
new EventFlow()
.addPostEvent(new AudioEvents.ChangeVolume(settingsData.fxVolume, VolumeControl.FX))
.asyncPostEvent();
new EventFlow()
.addPostEvent(new AudioEvents.ChangeVolume(settingsData.musicVolume, VolumeControl.MUSIC))
.asyncPostEvent();
App.setStyle(settingsAsset.getTheme(), settingsAsset.getLayoutSize());
}
public static void applyMusicVolumeSettings() {
Settings settingsData = settingsAsset.getContent();
new EventFlow()
.addPostEvent(new AudioEvents.ChangeVolume(settingsData.volume, VolumeControl.MASTERVOLUME))
.asyncPostEvent();
new EventFlow()
.addPostEvent(new AudioEvents.ChangeVolume(settingsData.fxVolume, VolumeControl.FX))
.asyncPostEvent();
new EventFlow()
.addPostEvent(new AudioEvents.ChangeVolume(settingsData.musicVolume, VolumeControl.MUSIC))
.asyncPostEvent();
}
public static SettingsAsset getPath() {
if (settingsAsset == null) {
String os = System.getProperty("os.name").toLowerCase();