diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherGUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherGUI.java new file mode 100644 index 0000000..b982f08 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherGUI.java @@ -0,0 +1,61 @@ +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 javafx.application.Application; +import javafx.application.Platform; +import javafx.stage.Stage; + +public class ClientLauncherGUI extends Application { + + @Override + public void start(Stage stage) { + LoginView loginView = new LoginView(); + GUI view = new GUI(stage); + + ClientController controller = new ClientController(view); + + 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; + } + + // Connessione in un thread separato — non bloccare il JavaFX thread! + new Thread(() -> { + if (networkType == 0) { + RMIClient client = new RMIClient(controller, ip, 1099); + 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); + if (client.connect(nome, numPlayer)) { + controller.setClient(client); + } else { + Platform.runLater(() -> loginView.setErrore("Connessione TCP fallita.")); + } + } + }).start(); + }); + + stage.setTitle("La mia app"); + stage.setScene(loginView.getScene()); + stage.show(); + } + + public static void main(String[] args) { + launch(); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..b9a9d63 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java @@ -0,0 +1,54 @@ +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.View.GUI.MainView; +import it.polimi.ingsw.gc14.View.IView; +import javafx.application.Platform; +import javafx.stage.Stage; + +public class GUI implements IView { // stessa interfaccia che implementa TUI + + private final Stage stage; + private final MainView mainView = new MainView(); + private Game model; + private String username; + + public GUI(Stage stage) { + this.stage = stage; + } + + + @Override + public void update(Game model) { + this.model = model; + } + + /** + * Chiamato da onGameInit e onAction — sempre da thread RMI/TCP. + * Platform.runLater garantisce che la UI venga aggiornata sul JavaFX thread. + */ + @Override + public void render() { + Platform.runLater(() -> { + stage.setScene(mainView.getScene()); + }); + } + + @Override + public void showMessage(String message) { + + } + + @Override + public void showError(String message) { + + } + + public void setUsername(String username) { + this.username=username; + } + + // Getter per il launcher + public Stage getStage() { return stage; } +} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/View/GUI/LoginView.java b/src/main/java/it/polimi/ingsw/gc14/View/GUI/LoginView.java new file mode 100644 index 0000000..1067009 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/View/GUI/LoginView.java @@ -0,0 +1,75 @@ +package it.polimi.ingsw.gc14.View.GUI; + +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.scene.layout.VBox; + +public class LoginView { + + private final Scene scene; + + private final TextField campoNome = new TextField(); + private final TextField campoNumPlayers = new TextField(); + private final TextField campoIP = new TextField(); + private final ToggleGroup networkGroup = new ToggleGroup(); + private final RadioButton btnRMI = new RadioButton("RMI"); + private final RadioButton btnTCP = new RadioButton("TCP"); + private final Button btnAccedi = new Button("Connetti"); + private final Label labelErrore = new Label(""); + + public LoginView() { + campoNome.setPromptText("Nome utente"); + campoNumPlayers.setPromptText("Numero di giocatori"); + campoIP.setPromptText("IP server"); + + btnRMI.setToggleGroup(networkGroup); + btnTCP.setToggleGroup(networkGroup); + btnRMI.setSelected(true); // RMI selezionato di default + + labelErrore.setStyle("-fx-text-fill: red;"); + + VBox layout = new VBox(10, + new Label("Nome utente:"), campoNome, + new Label("Num. giocatori:"), campoNumPlayers, + new Label("Protocollo:"), btnRMI, btnTCP, + new Label("IP server:"), campoIP, + labelErrore, + btnAccedi + ); + layout.setAlignment(Pos.CENTER_LEFT); + layout.setPadding(new Insets(40)); + + scene = new Scene(layout, 350, 380); + } + + // --- Getter esposti al launcher --- + + public Scene getScene() { return scene; } + public Button getBtnAccedi() { return btnAccedi; } + + public String getNome() { + return campoNome.getText().trim(); + } + + public int getNumPlayers() { + try { + return Integer.parseInt(campoNumPlayers.getText().trim()); + } catch (NumberFormatException e) { + return -1; // il launcher controlla questo e mostra errore + } + } + + public int getNetworkType() { + return btnRMI.isSelected() ? 0 : 1; + } + + public String getIP() { + return campoIP.getText().trim(); + } + + public void setErrore(String messaggio) { + labelErrore.setText(messaggio); + } +} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/View/GUI/MainView.java b/src/main/java/it/polimi/ingsw/gc14/View/GUI/MainView.java new file mode 100644 index 0000000..96c8e33 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/View/GUI/MainView.java @@ -0,0 +1,23 @@ +package it.polimi.ingsw.gc14.View.GUI; + +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Label; +import javafx.scene.layout.VBox; + +public class MainView { + + private final Scene scene; + private final Label labelStato = new Label("In attesa di aggiornamenti..."); + + public MainView() { + VBox layout = new VBox(16, labelStato); + layout.setAlignment(Pos.CENTER); + layout.setPadding(new Insets(40)); + scene = new Scene(layout, 800, 600); + } + + public Scene getScene() { return scene; } + public void setStato(String testo) { labelStato.setText(testo); } +} \ No newline at end of file