Initial refactor of GUI

This commit is contained in:
2026-05-22 13:49:30 +02:00
parent 6e96459152
commit 9cef4617cd
6 changed files with 20 additions and 30 deletions
@@ -5,6 +5,7 @@ import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Model.MiniModel;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import javafx.application.Platform;
import java.io.Serializable; import java.io.Serializable;
import java.rmi.RemoteException; import java.rmi.RemoteException;
@@ -39,8 +40,10 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
*/ */
@Override @Override
public void onGameInit(MiniModel model) throws RemoteException { public void onGameInit(MiniModel model) throws RemoteException {
clientController.setModel(model); Platform.runLater(() -> {
clientController.view.render(); clientController.setModel(model);
clientController.view.render();
});
} }
@@ -57,7 +60,10 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
if(event.getIsError()) { if(event.getIsError()) {
clientController.view.showError(event.toString()); clientController.view.showError(event.toString());
} else { } else {
clientController.view.applyAndRender(event); Platform.runLater(() -> {
event.apply(clientController.miniModel);
clientController.view.render();
});
} }
} }
} }
@@ -6,6 +6,7 @@ import it.polimi.ingsw.gc14.Model.MiniModel;
import it.polimi.ingsw.gc14.Network.IClient; import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.*; import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import javafx.application.Platform;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
@@ -163,14 +164,17 @@ public class TCPClient implements IClient {
if (event.getIsError()) { if (event.getIsError()) {
controller.view.showError(event.toString()); controller.view.showError(event.toString());
} else { } else {
controller.view.applyAndRender(event); Platform.runLater(() -> {
event.apply(controller.miniModel);
controller.view.render();
});
} }
} else if (read instanceof MiniModel model) { } else if (read instanceof MiniModel model) {
synchronized (controller) { Platform.runLater(() -> {
controller.setModel(model); controller.setModel(model);
controller.view.render(); controller.view.render();
} });
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@@ -109,25 +109,17 @@ public class GUI extends Application implements IView {
public void applyAndRender(NetworkEvent event) { public void applyAndRender(NetworkEvent event) {
Platform.runLater(() -> { Platform.runLater(() -> {
event.apply(miniModel); if (event!=null) {
event.apply(miniModel);
}
if (controller.miniModel.currentState.getGameStage() == TOTEM_CHOICE) { if (controller.miniModel.currentState.getGameStage() == TOTEM_CHOICE) {
primaryStage.setScene(totemScene); primaryStage.setScene(totemScene);
controllerTotem.render(); controllerTotem.render();
} else if (controller.miniModel.currentState.getGameStage() == GameStages.ENDED) { } else if (controller.miniModel.currentState.getGameStage() == GameStages.ENDED) {
boolean sceneChange = primaryStage.getScene() != leaderboardScene;
primaryStage.setScene(leaderboardScene); primaryStage.setScene(leaderboardScene);
if (sceneChange) {
primaryStage.setMaximized(false);
Platform.runLater(() -> primaryStage.setMaximized(true));
}
controllerLeaderboard.render(); controllerLeaderboard.render();
} else { } else {
boolean sceneChange = primaryStage.getScene() != mainScene; // ← era un'altra scena?
primaryStage.setScene(mainScene); primaryStage.setScene(mainScene);
if (sceneChange) {
primaryStage.setMaximized(false);
Platform.runLater(() -> primaryStage.setMaximized(true));
}
controllerMain.render(event); controllerMain.render(event);
controllerMain.initPopup(mainScene); controllerMain.initPopup(mainScene);
} }
@@ -135,10 +127,6 @@ public class GUI extends Application implements IView {
} }
public void render() { public void render() {
if (!Platform.isFxApplicationThread()) {
Platform.runLater(this::render);
return;
}
if (controller.miniModel.currentState.getGameStage() == TOTEM_CHOICE) { if (controller.miniModel.currentState.getGameStage() == TOTEM_CHOICE) {
primaryStage.setScene(totemScene); primaryStage.setScene(totemScene);
controllerTotem.render(); controllerTotem.render();
@@ -147,6 +135,7 @@ public class GUI extends Application implements IView {
controllerLeaderboard.render(); controllerLeaderboard.render();
}else { }else {
primaryStage.setScene(mainScene); primaryStage.setScene(mainScene);
controllerMain.initPopup(mainScene);
controllerMain.render(); controllerMain.render();
} }
} }
@@ -105,10 +105,6 @@ public class MainFXMLController {
// ==== RENDER ==== // ==== RENDER ====
public void render() { public void render() {
if (mainHBox.getWidth() == 0 || mainHBox.getHeight() == 0) {
Platform.runLater(() -> render());
return;
}
renderUpper(); renderUpper();
renderBoard(); renderBoard();
renderLower(); renderLower();
@@ -7,7 +7,6 @@ import it.polimi.ingsw.gc14.Network.NetworkEvent;
public interface IView { public interface IView {
public void setModel(MiniModel miniModel); public void setModel(MiniModel miniModel);
public void render(); public void render();
public void applyAndRender(NetworkEvent event);
public void showMessage(String message); public void showMessage(String message);
public void showError(String message); public void showError(String message);
@@ -59,10 +59,6 @@ public class TUI implements IView {
this.model = model; this.model = model;
this.username = ""; this.username = "";
} }
public void applyAndRender(NetworkEvent event) {
event.apply(model);
render();
}
/** /**
* Sets the username of the local player. * Sets the username of the local player.