Add: initialized GUI using FXML file and controller
This commit is contained in:
@@ -18,10 +18,10 @@ public class GUI implements IView { // stessa interfaccia che implementa TUI
|
||||
this.stage = stage;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(Game model) {
|
||||
public void setModel(Game model) {
|
||||
this.model = model;
|
||||
mainView.setModel(model);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,6 +31,7 @@ public class GUI implements IView { // stessa interfaccia che implementa TUI
|
||||
@Override
|
||||
public void render() {
|
||||
Platform.runLater(() -> {
|
||||
mainView.render();
|
||||
stage.setScene(mainView.getScene());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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); }
|
||||
}
|
||||
@@ -1,75 +1,35 @@
|
||||
package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.control.Button;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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("");
|
||||
private final Scene scene;
|
||||
private final LoginFXMLController fxmlController;
|
||||
|
||||
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
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public int getNetworkType() {
|
||||
return btnRMI.isSelected() ? 0 : 1;
|
||||
}
|
||||
// --- Delega tutto al controller FXML ---
|
||||
|
||||
public String getIP() {
|
||||
return campoIP.getText().trim();
|
||||
}
|
||||
|
||||
public void setErrore(String messaggio) {
|
||||
labelErrore.setText(messaggio);
|
||||
}
|
||||
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,47 @@
|
||||
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.setFitWidth(80);
|
||||
view.setFitHeight(120);
|
||||
view.setPreserveRatio(true);
|
||||
upperList.getChildren().add(view);
|
||||
}
|
||||
|
||||
for (TribeCard card : model.getLowerListTribeCards()) {
|
||||
ImageView view = new ImageView(placeholder);
|
||||
view.setFitWidth(80);
|
||||
view.setFitHeight(120);
|
||||
view.setPreserveRatio(true);
|
||||
lowerList.getChildren().add(view);
|
||||
}
|
||||
System.out.println("AGGIORNO GAGAGAGA");
|
||||
}
|
||||
}
|
||||
@@ -1,23 +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 Label labelStato = new Label("In attesa di aggiornamenti...");
|
||||
private final MainFXMLController fxmlController;
|
||||
private Game model;
|
||||
|
||||
public MainView() {
|
||||
VBox layout = new VBox(16, labelStato);
|
||||
layout.setAlignment(Pos.CENTER);
|
||||
layout.setPadding(new Insets(40));
|
||||
scene = new Scene(layout, 800, 600);
|
||||
try {
|
||||
FXMLLoader loader = new FXMLLoader(
|
||||
getClass().getResource("/GUIScene/main.fxml")
|
||||
);
|
||||
scene = new Scene(loader.load(), 350, 380);
|
||||
fxmlController = loader.getController();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Impossibile caricare login.fxml", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Scene getScene() { return scene; }
|
||||
public void setStato(String testo) { labelStato.setText(testo); }
|
||||
public void setModel(Game model) {
|
||||
this.model = model;
|
||||
fxmlController.setModel(model);
|
||||
}
|
||||
public void render() {
|
||||
fxmlController.render();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user