Refactor scene management.
Init totem choice scene
This commit is contained in:
@@ -1,61 +1,21 @@
|
||||
package it.polimi.ingsw.gc14;
|
||||
|
||||
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.LoginView;
|
||||
import it.polimi.ingsw.gc14.View.IView;
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import static javafx.application.Application.launch;
|
||||
|
||||
public class ClientLauncherGUI extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) {
|
||||
LoginView loginView = new LoginView();
|
||||
public void start(Stage stage) throws Exception {
|
||||
GUI view = new GUI(stage);
|
||||
|
||||
ClientController controller = new ClientController(view);
|
||||
view.setController(controller);
|
||||
|
||||
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();
|
||||
view.start(stage);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,46 +1,76 @@
|
||||
package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
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.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 javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
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 String username;
|
||||
private MiniModel model;
|
||||
|
||||
public GUI(Stage stage) {
|
||||
this.stage = stage;
|
||||
this.mainView = new MainView();
|
||||
this.primaryStage = stage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setModel(MiniModel model) {
|
||||
this.model = model;
|
||||
mainView.setModel(model,controller, username);
|
||||
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);
|
||||
|
||||
Platform.runLater(() -> {
|
||||
stage.setScene(mainView.getScene());
|
||||
});
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Chiamato da onGameInit e onAction — sempre da thread RMI/TCP.
|
||||
* Platform.runLater garantisce che la UI venga aggiornata sul JavaFX thread.
|
||||
*/
|
||||
public void showLogin() throws IOException {
|
||||
primaryStage.setScene(loginScene);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setModel(MiniModel miniModel) {
|
||||
this.model=model;
|
||||
}
|
||||
|
||||
public void setController(ClientController controller) {
|
||||
this.controller=controller;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
Platform.runLater(() -> {
|
||||
mainView.render();
|
||||
});
|
||||
if(controller.miniModel.currentState.getGameStage()==TOTEM_CHOICE) {
|
||||
Platform.runLater(() -> {
|
||||
primaryStage.setScene(totemScene);
|
||||
});
|
||||
} else {
|
||||
Platform.runLater(() -> {
|
||||
primaryStage.setScene(mainScene);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
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.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
@@ -13,22 +16,27 @@ import java.util.List;
|
||||
|
||||
public class LoginFXMLController {
|
||||
|
||||
@FXML private TextField campoNome;
|
||||
@FXML private TextField campoNumPlayers;
|
||||
@FXML private TextField campoIP;
|
||||
@FXML private TextField campoNome;
|
||||
@FXML private TextField campoNumPlayers;
|
||||
@FXML private TextField campoIP;
|
||||
@FXML private RadioButton btnRMI;
|
||||
@FXML private RadioButton btnTCP;
|
||||
@FXML private Button btnAccedi;
|
||||
@FXML private Label labelErrore;
|
||||
@FXML private Button btnAccedi;
|
||||
@FXML private Label labelErrore;
|
||||
@FXML private ComboBox<String> comboInterfaccia;
|
||||
|
||||
private ClientController controller;
|
||||
|
||||
public void setController(ClientController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
ToggleGroup group = new ToggleGroup();
|
||||
btnRMI.setToggleGroup(group);
|
||||
btnTCP.setToggleGroup(group);
|
||||
|
||||
// Carica le interfacce in background
|
||||
new Thread(() -> {
|
||||
try {
|
||||
List<String> ips = new ArrayList<>();
|
||||
@@ -39,9 +47,8 @@ public class LoginFXMLController {
|
||||
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
InetAddress addr = addresses.nextElement();
|
||||
if (addr instanceof Inet4Address) {
|
||||
if (addr instanceof Inet4Address)
|
||||
ips.add(ni.getDisplayName() + " -> " + addr.getHostAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
Platform.runLater(() -> {
|
||||
@@ -49,34 +56,57 @@ public class LoginFXMLController {
|
||||
if (!ips.isEmpty()) comboInterfaccia.getSelectionModel().selectFirst();
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Platform.runLater(() -> setErrore("Errore nel rilevare le interfacce."));
|
||||
Platform.runLater(() -> labelErrore.setText("Errore nel rilevare le interfacce."));
|
||||
}
|
||||
}).start();
|
||||
btnAccedi.setOnAction(e -> onAccediClick());
|
||||
}
|
||||
|
||||
// --- Getter esposti a LoginView ---
|
||||
|
||||
public Button getBtnAccedi() { return btnAccedi; }
|
||||
|
||||
public String getNome() { return campoNome.getText().trim(); }
|
||||
|
||||
public int getNumPlayers() {
|
||||
@FXML
|
||||
private void onAccediClick() {
|
||||
String nome = campoNome.getText().trim();
|
||||
String ip = campoIP.getText().trim();
|
||||
int numPlayers;
|
||||
try {
|
||||
return Integer.parseInt(campoNumPlayers.getText().trim());
|
||||
numPlayers = Integer.parseInt(campoNumPlayers.getText().trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
labelErrore.setText("Numero giocatori non valido.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (nome.isEmpty() || ip.isEmpty()) {
|
||||
labelErrore.setText("Compila tutti i campi.");
|
||||
return;
|
||||
}
|
||||
|
||||
int networkType = btnRMI.isSelected() ? 0 : 1;
|
||||
String interfaccia = 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();
|
||||
|
||||
}
|
||||
|
||||
public int getNetworkType() { return btnRMI.isSelected() ? 0 : 1; }
|
||||
|
||||
public String getIP() { return campoIP.getText().trim(); }
|
||||
|
||||
public String getInterfaccia() {
|
||||
private String getInterfaccia() {
|
||||
String selected = comboInterfaccia.getValue();
|
||||
if (selected == null) return "";
|
||||
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