@@ -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("Mesos");
|
||||
stage.setScene(loginView.getScene());
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public class ClientController {
|
||||
*/
|
||||
public void setModel(Game model) {
|
||||
localController.setModel(model);
|
||||
view.update(localController.getModel());
|
||||
view.setModel(localController.getModel());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
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 setModel(Game model) {
|
||||
this.model = model;
|
||||
mainView.setModel(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(() -> {
|
||||
mainView.render();
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
|
||||
public class LoginFXMLController {
|
||||
|
||||
@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
|
||||
public void initialize() {
|
||||
// I RadioButton vanno collegati a un ToggleGroup a mano
|
||||
// perché FXML non lo fa automaticamente
|
||||
ToggleGroup group = new ToggleGroup();
|
||||
btnRMI.setToggleGroup(group);
|
||||
btnTCP.setToggleGroup(group);
|
||||
}
|
||||
|
||||
// --- Getter esposti a LoginView ---
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public int getNetworkType() { return btnRMI.isSelected() ? 0 : 1; }
|
||||
|
||||
public String getIP() { return campoIP.getText().trim(); }
|
||||
|
||||
public void setErrore(String messaggio) { labelErrore.setText(messaggio); }
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
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 void setErrore(String messaggio) { fxmlController.setErrore(messaggio); }
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.image.*;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.scene.shape.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MainFXMLController {
|
||||
|
||||
@FXML private HBox board;
|
||||
@FXML private HBox upperList;
|
||||
@FXML private HBox lowerList;
|
||||
@FXML private HBox myHand;
|
||||
@FXML private VBox playersHand;
|
||||
|
||||
private Game model;
|
||||
|
||||
public void setModel(Game model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
Image placeholder = new Image(getClass().getResourceAsStream("/GUIImages/Fronts/card-001.png"));
|
||||
|
||||
for (TribeCard card : model.getUpperListTribeCards()) {
|
||||
ImageView view = new ImageView(placeholder);
|
||||
view.setFitHeight(250);
|
||||
view.setPreserveRatio(true);
|
||||
upperList.getChildren().add(view);
|
||||
}
|
||||
|
||||
for (TribeCard card : model.getLowerListTribeCards()) {
|
||||
ImageView view = new ImageView(placeholder);
|
||||
view.setFitHeight(250);
|
||||
view.setPreserveRatio(true);
|
||||
lowerList.getChildren().add(view);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.VBox;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MainView {
|
||||
|
||||
private final Scene scene;
|
||||
private final MainFXMLController fxmlController;
|
||||
private Game model;
|
||||
|
||||
public MainView() {
|
||||
try {
|
||||
FXMLLoader loader = new FXMLLoader(
|
||||
getClass().getResource("/GUIScene/main.fxml")
|
||||
);
|
||||
scene = new Scene(loader.load(), 1920, 1080);
|
||||
fxmlController = loader.getController();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Impossibile caricare login.fxml", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Scene getScene() { return scene; }
|
||||
public void setModel(Game model) {
|
||||
this.model = model;
|
||||
fxmlController.setModel(model);
|
||||
}
|
||||
public void render() {
|
||||
fxmlController.render();
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
|
||||
public interface IView {
|
||||
public void update(Game game);
|
||||
public void setModel(Game game);
|
||||
public void render();
|
||||
public void showMessage(String message);
|
||||
public void showError(String message);
|
||||
|
||||
@@ -77,7 +77,7 @@ public class TUI implements IView {
|
||||
* @param model the new {@link Game} model; must not be {@code null}
|
||||
*/
|
||||
@Override
|
||||
public void update(Game model) {
|
||||
public void setModel(Game model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,4 +18,8 @@ module it.polimi.ingsw.gc14 {
|
||||
exports it.polimi.ingsw.gc14.Network.RMI.Client to java.rmi;
|
||||
exports it.polimi.ingsw.gc14.Network to java.rmi;
|
||||
exports it.polimi.ingsw.gc14.Model to java.rmi, com.google.gson;
|
||||
|
||||
// GUI
|
||||
opens it.polimi.ingsw.gc14.View.GUI to javafx.fxml;
|
||||
exports it.polimi.ingsw.gc14.View.GUI;
|
||||
}
|
||||
Reference in New Issue
Block a user