Refactor scene management.
Init totem choice scene
This commit is contained in:
@@ -1,61 +1,21 @@
|
|||||||
package it.polimi.ingsw.gc14;
|
package it.polimi.ingsw.gc14;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||||
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
|
|
||||||
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
|
|
||||||
import it.polimi.ingsw.gc14.View.GUI.GUI;
|
import it.polimi.ingsw.gc14.View.GUI.GUI;
|
||||||
import it.polimi.ingsw.gc14.View.GUI.LoginView;
|
import it.polimi.ingsw.gc14.View.IView;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
|
||||||
import javafx.scene.text.Font;
|
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
import static javafx.application.Application.launch;
|
||||||
|
|
||||||
public class ClientLauncherGUI extends Application {
|
public class ClientLauncherGUI extends Application {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) {
|
public void start(Stage stage) throws Exception {
|
||||||
LoginView loginView = new LoginView();
|
|
||||||
GUI view = new GUI(stage);
|
GUI view = new GUI(stage);
|
||||||
|
|
||||||
ClientController controller = new ClientController(view);
|
ClientController controller = new ClientController(view);
|
||||||
view.setController(controller);
|
view.setController(controller);
|
||||||
|
view.start(stage);
|
||||||
loginView.getBtnAccedi().setOnAction(e -> {
|
|
||||||
String nome = loginView.getNome();
|
|
||||||
int numPlayer = loginView.getNumPlayers();
|
|
||||||
int networkType = loginView.getNetworkType();
|
|
||||||
String ip = loginView.getIP();
|
|
||||||
|
|
||||||
if (nome.isEmpty() || ip.isEmpty()) {
|
|
||||||
loginView.setErrore("Compila tutti i campi.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
view.setUsername(nome);
|
|
||||||
|
|
||||||
// Connessione in un thread separato — non bloccare il JavaFX thread!
|
|
||||||
new Thread(() -> {
|
|
||||||
if (networkType == 0) {
|
|
||||||
RMIClient client = new RMIClient(controller, ip, 1099, loginView.getInterfaccia());
|
|
||||||
if (client.connect(nome, numPlayer)) {
|
|
||||||
controller.setClient(client);
|
|
||||||
// Da qui in poi il server chiamerà onGameInit → render()
|
|
||||||
} else {
|
|
||||||
Platform.runLater(() -> loginView.setErrore("Connessione RMI fallita."));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
TCPClient client = new TCPClient(controller, ip, 8080,8081);
|
|
||||||
if (client.connect(nome, numPlayer)) {
|
|
||||||
controller.setClient(client);
|
|
||||||
} else {
|
|
||||||
Platform.runLater(() -> loginView.setErrore("Connessione TCP fallita."));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
});
|
|
||||||
|
|
||||||
stage.setTitle("Mesos");
|
|
||||||
stage.setScene(loginView.getScene());
|
|
||||||
stage.show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -1,46 +1,76 @@
|
|||||||
package it.polimi.ingsw.gc14.View.GUI;
|
package it.polimi.ingsw.gc14.View.GUI;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||||
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.View.GUI.MainView;
|
import it.polimi.ingsw.gc14.View.GUI.LoginFXMLController;
|
||||||
|
import it.polimi.ingsw.gc14.View.GUI.MainFXMLController;
|
||||||
import it.polimi.ingsw.gc14.View.IView;
|
import it.polimi.ingsw.gc14.View.IView;
|
||||||
|
import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
|
import javafx.fxml.FXMLLoader;
|
||||||
|
import javafx.scene.Scene;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
public class GUI implements IView { // stessa interfaccia che implementa TUI
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static it.polimi.ingsw.gc14.Model.GamePackage.GameStages.TOTEM_CHOICE;
|
||||||
|
|
||||||
|
public class GUI extends Application implements IView {
|
||||||
|
private Stage primaryStage;
|
||||||
|
Scene loginScene;
|
||||||
|
Scene totemScene;
|
||||||
|
Scene mainScene;
|
||||||
|
|
||||||
private final Stage stage;
|
|
||||||
private final MainView mainView;
|
|
||||||
private MiniModel model;
|
|
||||||
private ClientController controller;
|
private ClientController controller;
|
||||||
private String username;
|
private MiniModel model;
|
||||||
|
|
||||||
public GUI(Stage stage) {
|
public GUI(Stage stage) {
|
||||||
this.stage = stage;
|
this.primaryStage = stage;
|
||||||
this.mainView = new MainView();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setModel(MiniModel model) {
|
public void start(Stage stage) throws Exception {
|
||||||
|
FXMLLoader loaderLogin = new FXMLLoader(getClass().getResource("/GUIScene/login.fxml"));
|
||||||
|
loginScene = new Scene(loaderLogin.load());
|
||||||
|
LoginFXMLController controllerLogin = loaderLogin.getController();
|
||||||
|
controllerLogin.setController(controller);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FXMLLoader loaderTotem = new FXMLLoader(getClass().getResource("/GUIScene/totem.fxml"));
|
||||||
|
totemScene = new Scene(loaderTotem.load());
|
||||||
|
FXMLLoader loaderMain = new FXMLLoader(getClass().getResource("/GUIScene/main.fxml"));
|
||||||
|
mainScene = new Scene(loaderMain.load());
|
||||||
|
|
||||||
|
showLogin();
|
||||||
|
stage.setTitle("Mesos");
|
||||||
|
primaryStage.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showLogin() throws IOException {
|
||||||
|
primaryStage.setScene(loginScene);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setModel(MiniModel miniModel) {
|
||||||
this.model=model;
|
this.model=model;
|
||||||
mainView.setModel(model,controller, username);
|
|
||||||
|
|
||||||
Platform.runLater(() -> {
|
|
||||||
stage.setScene(mainView.getScene());
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void setController(ClientController controller) {
|
||||||
* Chiamato da onGameInit e onAction — sempre da thread RMI/TCP.
|
this.controller=controller;
|
||||||
* Platform.runLater garantisce che la UI venga aggiornata sul JavaFX thread.
|
}
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void render() {
|
public void render() {
|
||||||
|
if(controller.miniModel.currentState.getGameStage()==TOTEM_CHOICE) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
mainView.render();
|
primaryStage.setScene(totemScene);
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
primaryStage.setScene(mainScene);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,12 +83,4 @@ public class GUI implements IView { // stessa interfaccia che implementa TUI
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUsername(String username) {
|
|
||||||
this.username=username;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setController(ClientController controller) { this.controller = controller;}
|
|
||||||
|
|
||||||
// Getter per il launcher
|
|
||||||
public Stage getStage() { return stage; }
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package it.polimi.ingsw.gc14.View.GUI;
|
package it.polimi.ingsw.gc14.View.GUI;
|
||||||
|
|
||||||
|
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||||
|
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
@@ -22,13 +25,18 @@ public class LoginFXMLController {
|
|||||||
@FXML private Label labelErrore;
|
@FXML private Label labelErrore;
|
||||||
@FXML private ComboBox<String> comboInterfaccia;
|
@FXML private ComboBox<String> comboInterfaccia;
|
||||||
|
|
||||||
|
private ClientController controller;
|
||||||
|
|
||||||
|
public void setController(ClientController controller) {
|
||||||
|
this.controller = controller;
|
||||||
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
ToggleGroup group = new ToggleGroup();
|
ToggleGroup group = new ToggleGroup();
|
||||||
btnRMI.setToggleGroup(group);
|
btnRMI.setToggleGroup(group);
|
||||||
btnTCP.setToggleGroup(group);
|
btnTCP.setToggleGroup(group);
|
||||||
|
|
||||||
// Carica le interfacce in background
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
List<String> ips = new ArrayList<>();
|
List<String> ips = new ArrayList<>();
|
||||||
@@ -39,44 +47,66 @@ public class LoginFXMLController {
|
|||||||
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
||||||
while (addresses.hasMoreElements()) {
|
while (addresses.hasMoreElements()) {
|
||||||
InetAddress addr = addresses.nextElement();
|
InetAddress addr = addresses.nextElement();
|
||||||
if (addr instanceof Inet4Address) {
|
if (addr instanceof Inet4Address)
|
||||||
ips.add(ni.getDisplayName() + " -> " + addr.getHostAddress());
|
ips.add(ni.getDisplayName() + " -> " + addr.getHostAddress());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
comboInterfaccia.getItems().addAll(ips);
|
comboInterfaccia.getItems().addAll(ips);
|
||||||
if (!ips.isEmpty()) comboInterfaccia.getSelectionModel().selectFirst();
|
if (!ips.isEmpty()) comboInterfaccia.getSelectionModel().selectFirst();
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Platform.runLater(() -> setErrore("Errore nel rilevare le interfacce."));
|
Platform.runLater(() -> labelErrore.setText("Errore nel rilevare le interfacce."));
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
btnAccedi.setOnAction(e -> onAccediClick());
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Getter esposti a LoginView ---
|
@FXML
|
||||||
|
private void onAccediClick() {
|
||||||
public Button getBtnAccedi() { return btnAccedi; }
|
String nome = campoNome.getText().trim();
|
||||||
|
String ip = campoIP.getText().trim();
|
||||||
public String getNome() { return campoNome.getText().trim(); }
|
int numPlayers;
|
||||||
|
|
||||||
public int getNumPlayers() {
|
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(campoNumPlayers.getText().trim());
|
numPlayers = Integer.parseInt(campoNumPlayers.getText().trim());
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return -1;
|
labelErrore.setText("Numero giocatori non valido.");
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNetworkType() { return btnRMI.isSelected() ? 0 : 1; }
|
if (nome.isEmpty() || ip.isEmpty()) {
|
||||||
|
labelErrore.setText("Compila tutti i campi.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
public String getIP() { return campoIP.getText().trim(); }
|
int networkType = btnRMI.isSelected() ? 0 : 1;
|
||||||
|
String interfaccia = getInterfaccia();
|
||||||
|
|
||||||
public String getInterfaccia() {
|
// Stesso thread di rete che era in ClientLauncher
|
||||||
|
new Thread(() -> {
|
||||||
|
if (networkType == 0) {
|
||||||
|
RMIClient client = new RMIClient(controller, ip, 1099, interfaccia);
|
||||||
|
if (client.connect(nome, numPlayers)) {
|
||||||
|
controller.setClient(client);
|
||||||
|
// Da qui il server chiamerà gui.render() → cambio scena
|
||||||
|
} else {
|
||||||
|
Platform.runLater(() -> labelErrore.setText("Connessione RMI fallita."));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TCPClient client = new TCPClient(controller, ip, 8080, 8081);
|
||||||
|
if (client.connect(nome, numPlayers)) {
|
||||||
|
controller.setClient(client);
|
||||||
|
} else {
|
||||||
|
Platform.runLater(() -> labelErrore.setText("Connessione TCP fallita."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getInterfaccia() {
|
||||||
String selected = comboInterfaccia.getValue();
|
String selected = comboInterfaccia.getValue();
|
||||||
if (selected == null) return "";
|
if (selected == null) return "";
|
||||||
return selected.contains("->") ? selected.split("->")[1].trim() : selected;
|
return selected.contains("->") ? selected.split("->")[1].trim() : selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setErrore(String messaggio) { labelErrore.setText(messaggio); }
|
|
||||||
}
|
}
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
package it.polimi.ingsw.gc14.View.GUI;
|
|
||||||
|
|
||||||
import javafx.fxml.FXMLLoader;
|
|
||||||
import javafx.scene.Scene;
|
|
||||||
import javafx.scene.control.Button;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class LoginView {
|
|
||||||
|
|
||||||
private final Scene scene;
|
|
||||||
private final LoginFXMLController fxmlController;
|
|
||||||
|
|
||||||
public LoginView() {
|
|
||||||
try {
|
|
||||||
FXMLLoader loader = new FXMLLoader(
|
|
||||||
getClass().getResource("/GUIScene/login.fxml")
|
|
||||||
);
|
|
||||||
scene = new Scene(loader.load(), 350, 380);
|
|
||||||
fxmlController = loader.getController();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Impossibile caricare login.fxml", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Delega tutto al controller FXML ---
|
|
||||||
|
|
||||||
public Scene getScene() { return scene; }
|
|
||||||
public Button getBtnAccedi() { return fxmlController.getBtnAccedi(); }
|
|
||||||
public String getNome() { return fxmlController.getNome(); }
|
|
||||||
public int getNumPlayers() { return fxmlController.getNumPlayers(); }
|
|
||||||
public int getNetworkType() { return fxmlController.getNetworkType(); }
|
|
||||||
public String getIP() { return fxmlController.getIP(); }
|
|
||||||
public String getInterfaccia() {return fxmlController.getInterfaccia();}
|
|
||||||
public void setErrore(String messaggio) { fxmlController.setErrore(messaggio); }
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.scene.layout.HBox?>
|
||||||
|
<?import javafx.scene.text.Text?>
|
||||||
|
|
||||||
|
|
||||||
|
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/26" xmlns:fx="http://javafx.com/fxml/1">
|
||||||
|
<children>
|
||||||
|
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" wrappingWidth="295.0152587890625" />
|
||||||
|
</children>
|
||||||
|
</HBox>
|
||||||
Reference in New Issue
Block a user