mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
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:
committed by
GitHub
parent
bc84171029
commit
628e4f30b5
@@ -13,11 +13,10 @@ public final class Main {
|
|||||||
static void main(String[] args) {
|
static void main(String[] args) {
|
||||||
initSystems();
|
initSystems();
|
||||||
App.run(args);
|
App.run(args);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initSystems() {
|
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(() -> new NetworkingClientEventListener(new NetworkingClientManager())).start();
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
package org.toop.app;
|
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.WidgetContainer;
|
||||||
|
import org.toop.app.widget.complex.LoadingWidget;
|
||||||
import org.toop.app.widget.display.SongDisplay;
|
import org.toop.app.widget.display.SongDisplay;
|
||||||
import org.toop.app.widget.popup.QuitPopup;
|
import org.toop.app.widget.popup.QuitPopup;
|
||||||
import org.toop.app.widget.view.MainView;
|
import org.toop.app.widget.view.MainView;
|
||||||
import org.toop.framework.audio.events.AudioEvents;
|
import org.toop.framework.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
import org.toop.framework.resource.ResourceLoader;
|
||||||
import org.toop.framework.resource.ResourceManager;
|
import org.toop.framework.resource.ResourceManager;
|
||||||
|
import org.toop.framework.resource.events.AssetLoaderEvents;
|
||||||
import org.toop.framework.resource.resources.CssAsset;
|
import org.toop.framework.resource.resources.CssAsset;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
import org.toop.local.AppSettings;
|
import org.toop.local.AppSettings;
|
||||||
@@ -47,7 +52,7 @@ public final class App extends Application {
|
|||||||
stage.setMinHeight(720);
|
stage.setMinHeight(720);
|
||||||
stage.setOnCloseRequest(event -> {
|
stage.setOnCloseRequest(event -> {
|
||||||
event.consume();
|
event.consume();
|
||||||
startQuit();
|
quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
stage.setScene(scene);
|
stage.setScene(scene);
|
||||||
@@ -63,11 +68,44 @@ public final class App extends Application {
|
|||||||
|
|
||||||
App.isQuitting = false;
|
App.isQuitting = false;
|
||||||
|
|
||||||
AppSettings.applySettings();
|
var loading = new LoadingWidget(Primitive.text(
|
||||||
new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent();
|
"Loading...", false), 0, 0, 9999
|
||||||
|
);
|
||||||
|
|
||||||
WidgetContainer.add(Pos.CENTER, new MainView());
|
WidgetContainer.add(Pos.CENTER, loading);
|
||||||
WidgetContainer.add(Pos.BOTTOM_RIGHT, new SongDisplay());
|
|
||||||
|
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() {
|
public static void startQuit() {
|
||||||
|
|||||||
@@ -99,16 +99,9 @@ public final class Server {
|
|||||||
a.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
a.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
||||||
|
|
||||||
if (!e.successful()) {
|
if (!e.successful()) {
|
||||||
// loading.triggerFailure();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
TimeUnit.MILLISECONDS.sleep(500); // TODO temp fix for index bug
|
|
||||||
} catch (InterruptedException ex) {
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
WidgetContainer.getCurrentView().transitionPrevious();
|
WidgetContainer.getCurrentView().transitionPrevious();
|
||||||
|
|
||||||
a.unsubscribe("connecting");
|
a.unsubscribe("connecting");
|
||||||
@@ -131,6 +124,9 @@ public final class Server {
|
|||||||
() -> {
|
() -> {
|
||||||
try {
|
try {
|
||||||
loading.setAmount(e.amount());
|
loading.setAmount(e.amount());
|
||||||
|
if (e.amount() >= loading.getMaxAmount()) {
|
||||||
|
loading.triggerFailure();
|
||||||
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
@@ -253,7 +249,7 @@ public final class Server {
|
|||||||
} else {
|
} else {
|
||||||
stopScheduler();
|
stopScheduler();
|
||||||
}
|
}
|
||||||
}, 0, 5, TimeUnit.SECONDS);
|
}, 0, 1, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopScheduler() {
|
private void stopScheduler() {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import javafx.scene.layout.VBox;
|
|||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
import javafx.util.StringConverter;
|
import javafx.util.StringConverter;
|
||||||
|
|
||||||
public final class Primitive {
|
public final class Primitive {
|
||||||
public static Text header(String key, boolean localize) {
|
public static Text header(String key, boolean localize) {
|
||||||
var header = new Text();
|
var header = new Text();
|
||||||
header.getStyleClass().add("header");
|
header.getStyleClass().add("header");
|
||||||
@@ -78,7 +78,6 @@ public final class Primitive {
|
|||||||
button.setOnAction(_ -> {
|
button.setOnAction(_ -> {
|
||||||
onAction.run();
|
onAction.run();
|
||||||
playButtonSound();
|
playButtonSound();
|
||||||
System.out.println("HI I got called button");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.toop.app.widget.complex;
|
package org.toop.app.widget.complex;
|
||||||
|
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.control.ProgressBar;
|
import javafx.scene.control.ProgressBar;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
@@ -7,6 +8,7 @@ import javafx.scene.text.Text;
|
|||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public class LoadingWidget extends ViewWidget implements Update { // TODO make of widget type
|
public class LoadingWidget extends ViewWidget implements Update { // TODO make of widget type
|
||||||
private final ProgressBar progressBar;
|
private final ProgressBar progressBar;
|
||||||
@@ -14,8 +16,8 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
|
|||||||
|
|
||||||
private Runnable success = () -> {};
|
private Runnable success = () -> {};
|
||||||
private Runnable failure = () -> {};
|
private Runnable failure = () -> {};
|
||||||
private boolean successTriggered = false;
|
private AtomicBoolean successTriggered = new AtomicBoolean(false);
|
||||||
private boolean failureTriggered = false;
|
private AtomicBoolean failureTriggered = new AtomicBoolean(false);
|
||||||
private int maxAmount;
|
private int maxAmount;
|
||||||
private int minAmount;
|
private int minAmount;
|
||||||
private int amount;
|
private int amount;
|
||||||
@@ -66,7 +68,7 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTriggered() {
|
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.
|
* Forcefully trigger success.
|
||||||
*/
|
*/
|
||||||
public void triggerSuccess() {
|
public void triggerSuccess() {
|
||||||
successTriggered = true; // TODO, else it will double call... why?
|
if (successTriggered.compareAndSet(false, true)) {
|
||||||
success.run();
|
Platform.runLater(() -> {
|
||||||
|
if (success != null) success.run();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forcefully trigger failure.
|
* Forcefully trigger failure.
|
||||||
*/
|
*/
|
||||||
public void triggerFailure() {
|
public void triggerFailure() {
|
||||||
failureTriggered = true; // TODO, else it will double call... why?
|
if (failureTriggered.compareAndSet(false, true)) {
|
||||||
failure.run();
|
Platform.runLater(() -> {
|
||||||
|
if (failure != null) failure.run();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() throws Exception { // TODO Better exception
|
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();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +141,9 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
percentage = (float) amount / maxAmount;
|
if (maxAmount != 0) {
|
||||||
|
percentage = (float) amount / maxAmount;
|
||||||
|
}
|
||||||
progressBar.setProgress(percentage);
|
progressBar.setProgress(percentage);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user