From 9cef4617cdc72c7459c73030e190d96bfb3d69e8 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 22 May 2026 13:49:30 +0200 Subject: [PATCH] Initial refactor of GUI --- .../RMI/Client/ClientCallbackImpl.java | 12 +++++++++--- .../gc14/Network/TCP/Client/TCPClient.java | 10 +++++++--- .../it/polimi/ingsw/gc14/View/GUI/GUI.java | 19 ++++--------------- .../gc14/View/GUI/MainFXMLController.java | 4 ---- .../java/it/polimi/ingsw/gc14/View/IView.java | 1 - .../it/polimi/ingsw/gc14/View/TUI/TUI.java | 4 ---- 6 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java index 74aa52b..9d2648b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java @@ -5,6 +5,7 @@ import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; +import javafx.application.Platform; import java.io.Serializable; import java.rmi.RemoteException; @@ -39,8 +40,10 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa */ @Override public void onGameInit(MiniModel model) throws RemoteException { - clientController.setModel(model); - clientController.view.render(); + Platform.runLater(() -> { + clientController.setModel(model); + clientController.view.render(); + }); } @@ -57,7 +60,10 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa if(event.getIsError()) { clientController.view.showError(event.toString()); } else { - clientController.view.applyAndRender(event); + Platform.runLater(() -> { + event.apply(clientController.miniModel); + clientController.view.render(); + }); } } } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java index 28598d4..7ae8872 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java @@ -6,6 +6,7 @@ import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Network.IClient; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.*; +import javafx.application.Platform; import java.io.*; import java.net.*; @@ -163,14 +164,17 @@ public class TCPClient implements IClient { if (event.getIsError()) { controller.view.showError(event.toString()); } else { - controller.view.applyAndRender(event); + Platform.runLater(() -> { + event.apply(controller.miniModel); + controller.view.render(); + }); } } else if (read instanceof MiniModel model) { - synchronized (controller) { + Platform.runLater(() -> { controller.setModel(model); controller.view.render(); - } + }); } } catch (ClassNotFoundException e) { diff --git a/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java b/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java index 8d6bd27..98822ce 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java @@ -109,25 +109,17 @@ public class GUI extends Application implements IView { public void applyAndRender(NetworkEvent event) { Platform.runLater(() -> { - event.apply(miniModel); + if (event!=null) { + event.apply(miniModel); + } if (controller.miniModel.currentState.getGameStage() == TOTEM_CHOICE) { primaryStage.setScene(totemScene); controllerTotem.render(); } else if (controller.miniModel.currentState.getGameStage() == GameStages.ENDED) { - boolean sceneChange = primaryStage.getScene() != leaderboardScene; primaryStage.setScene(leaderboardScene); - if (sceneChange) { - primaryStage.setMaximized(false); - Platform.runLater(() -> primaryStage.setMaximized(true)); - } controllerLeaderboard.render(); } else { - boolean sceneChange = primaryStage.getScene() != mainScene; // ← era un'altra scena? primaryStage.setScene(mainScene); - if (sceneChange) { - primaryStage.setMaximized(false); - Platform.runLater(() -> primaryStage.setMaximized(true)); - } controllerMain.render(event); controllerMain.initPopup(mainScene); } @@ -135,10 +127,6 @@ public class GUI extends Application implements IView { } public void render() { - if (!Platform.isFxApplicationThread()) { - Platform.runLater(this::render); - return; - } if (controller.miniModel.currentState.getGameStage() == TOTEM_CHOICE) { primaryStage.setScene(totemScene); controllerTotem.render(); @@ -147,6 +135,7 @@ public class GUI extends Application implements IView { controllerLeaderboard.render(); }else { primaryStage.setScene(mainScene); + controllerMain.initPopup(mainScene); controllerMain.render(); } } diff --git a/src/main/java/it/polimi/ingsw/gc14/View/GUI/MainFXMLController.java b/src/main/java/it/polimi/ingsw/gc14/View/GUI/MainFXMLController.java index 47c6834..5c1f126 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/GUI/MainFXMLController.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/GUI/MainFXMLController.java @@ -105,10 +105,6 @@ public class MainFXMLController { // ==== RENDER ==== public void render() { - if (mainHBox.getWidth() == 0 || mainHBox.getHeight() == 0) { - Platform.runLater(() -> render()); - return; - } renderUpper(); renderBoard(); renderLower(); diff --git a/src/main/java/it/polimi/ingsw/gc14/View/IView.java b/src/main/java/it/polimi/ingsw/gc14/View/IView.java index f10b639..dc0aaa4 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/IView.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/IView.java @@ -7,7 +7,6 @@ import it.polimi.ingsw.gc14.Network.NetworkEvent; public interface IView { public void setModel(MiniModel miniModel); public void render(); - public void applyAndRender(NetworkEvent event); public void showMessage(String message); public void showError(String message); diff --git a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java index c79faa7..6b318fa 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java @@ -59,10 +59,6 @@ public class TUI implements IView { this.model = model; this.username = ""; } - public void applyAndRender(NetworkEvent event) { - event.apply(model); - render(); - } /** * Sets the username of the local player.