@@ -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) {
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ public class ClientController {
|
|||||||
/** Network client (either TCP or RMI) */
|
/** Network client (either TCP or RMI) */
|
||||||
private IClient client;
|
private IClient client;
|
||||||
|
|
||||||
|
public String myUsername;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs the ClientController.
|
* Constructs the ClientController.
|
||||||
@@ -56,6 +58,10 @@ public class ClientController {
|
|||||||
view.setModel(miniModel);
|
view.setModel(miniModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMyUsername (String username) {
|
||||||
|
this.myUsername=username;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays an error message in the view.
|
* Displays an error message in the view.
|
||||||
|
|||||||
@@ -1,46 +1,92 @@
|
|||||||
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;
|
||||||
|
|
||||||
|
FXMLLoader loaderLogin;
|
||||||
|
Scene loginScene;
|
||||||
|
LoginFXMLController controllerLogin;
|
||||||
|
|
||||||
|
FXMLLoader loaderTotem;
|
||||||
|
Scene totemScene;
|
||||||
|
TotemFXMLController controllerTotem;
|
||||||
|
|
||||||
|
FXMLLoader loaderMain;
|
||||||
|
Scene mainScene;
|
||||||
|
MainFXMLController controllerMain;
|
||||||
|
|
||||||
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 {
|
||||||
this.model = model;
|
loaderLogin = new FXMLLoader(getClass().getResource("/GUIScene/login.fxml"));
|
||||||
mainView.setModel(model,controller, username);
|
loginScene = new Scene(loaderLogin.load());
|
||||||
|
controllerLogin = loaderLogin.getController();
|
||||||
|
controllerLogin.setController(controller);
|
||||||
|
|
||||||
Platform.runLater(() -> {
|
|
||||||
stage.setScene(mainView.getScene());
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
loaderTotem = new FXMLLoader(getClass().getResource("/GUIScene/totem.fxml"));
|
||||||
|
totemScene = new Scene(loaderTotem.load());
|
||||||
|
controllerTotem = loaderTotem.getController();
|
||||||
|
controllerTotem.setController(controller);
|
||||||
|
|
||||||
|
|
||||||
|
loaderMain = new FXMLLoader(getClass().getResource("/GUIScene/main.fxml"));
|
||||||
|
mainScene = new Scene(loaderMain.load());
|
||||||
|
controllerMain = loaderMain.getController();
|
||||||
|
controllerMain.setController(controller);
|
||||||
|
|
||||||
|
showLogin();
|
||||||
|
stage.setTitle("Mesos");
|
||||||
|
primaryStage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void showLogin() throws IOException {
|
||||||
* Chiamato da onGameInit e onAction — sempre da thread RMI/TCP.
|
primaryStage.setScene(loginScene);
|
||||||
* Platform.runLater garantisce che la UI venga aggiornata sul JavaFX thread.
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
public void setModel(MiniModel miniModel) {
|
||||||
|
this.model=model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setController(ClientController controller) {
|
||||||
|
this.controller=controller;
|
||||||
|
}
|
||||||
|
|
||||||
public void render() {
|
public void render() {
|
||||||
Platform.runLater(() -> {
|
if(controller.miniModel.currentState.getGameStage()==TOTEM_CHOICE) {
|
||||||
mainView.render();
|
Platform.runLater(() -> {
|
||||||
});
|
controllerTotem.render();
|
||||||
|
primaryStage.setScene(totemScene);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
primaryStage.setScene(mainScene);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,12 +99,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.*;
|
||||||
@@ -13,22 +16,27 @@ import java.util.List;
|
|||||||
|
|
||||||
public class LoginFXMLController {
|
public class LoginFXMLController {
|
||||||
|
|
||||||
@FXML private TextField campoNome;
|
@FXML private TextField campoNome;
|
||||||
@FXML private TextField campoNumPlayers;
|
@FXML private TextField campoNumPlayers;
|
||||||
@FXML private TextField campoIP;
|
@FXML private TextField campoIP;
|
||||||
@FXML private RadioButton btnRMI;
|
@FXML private RadioButton btnRMI;
|
||||||
@FXML private RadioButton btnTCP;
|
@FXML private RadioButton btnTCP;
|
||||||
@FXML private Button btnAccedi;
|
@FXML private Button btnAccedi;
|
||||||
@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,9 +47,8 @@ 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(() -> {
|
||||||
@@ -49,34 +56,57 @@ public class LoginFXMLController {
|
|||||||
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();
|
||||||
|
controller.setMyUsername(nome);
|
||||||
public String getNome() { return campoNome.getText().trim(); }
|
String ip = campoIP.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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
} 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; }
|
private String getInterfaccia() {
|
||||||
|
|
||||||
public String getIP() { return campoIP.getText().trim(); }
|
|
||||||
|
|
||||||
public 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,39 @@
|
|||||||
|
package it.polimi.ingsw.gc14.View.GUI;
|
||||||
|
|
||||||
|
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||||
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
import it.polimi.ingsw.gc14.Model.Totems;
|
||||||
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.scene.image.Image;
|
||||||
|
import javafx.scene.image.ImageView;
|
||||||
|
import javafx.scene.layout.HBox;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class TotemFXMLController {
|
||||||
|
@FXML private HBox mainHBox;
|
||||||
|
|
||||||
|
private ClientController controller;
|
||||||
|
|
||||||
|
public void setController(ClientController controller) {
|
||||||
|
this.controller=controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void initialize() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
for (Totems totem : controller.miniModel.availableTotems) {
|
||||||
|
ImageView img = new ImageView(new Image(getClass().getResourceAsStream("/GUIImages/Totems/totem_"+ String.valueOf(totem).toLowerCase(Locale.ROOT) +".png")));
|
||||||
|
img.setFitHeight(240);
|
||||||
|
img.setPreserveRatio(true);
|
||||||
|
img.setOnMouseClicked(e -> controller.totemChoice(controller.myUsername));
|
||||||
|
mainHBox.getChildren().add(img);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 9.0 KiB After Width: | Height: | Size: 9.0 KiB |
|
Before Width: | Height: | Size: 9.0 KiB After Width: | Height: | Size: 9.0 KiB |
|
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 9.9 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<?import javafx.scene.layout.HBox?>
|
||||||
|
|
||||||
|
|
||||||
|
<HBox fx:id="mainHBox" 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" fx:controller="it.polimi.ingsw.gc14.View.GUI.TotemFXMLController">
|
||||||
|
</HBox>
|
||||||