Merge 'feat/lobby-ui' back into 'main' #172
+38
-5
@@ -1,5 +1,10 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
|
||||
|
||||
/**
|
||||
* Controller for the Casono main UI lobby.
|
||||
* Handles UI initialization and user actions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Controller for the Casono main UI lobby.
|
||||
* Handles UI initialization and user actions.
|
||||
@@ -30,11 +35,12 @@ import javafx.scene.layout.AnchorPane;
|
||||
public class CasinomainuiController {
|
||||
|
||||
/**
|
||||
* Standardkonstruktor.
|
||||
* Default constructor for the controller.
|
||||
*/
|
||||
public CasinomainuiController() {
|
||||
// Standardkonstruktor
|
||||
// Default constructor
|
||||
}
|
||||
|
||||
/** Root pane of the UI. */
|
||||
@FXML
|
||||
private AnchorPane rootPane;
|
||||
@@ -53,9 +59,13 @@ public class CasinomainuiController {
|
||||
/** Exit button for closing the application. */
|
||||
@FXML
|
||||
private Button exitbutton;
|
||||
/** Casino table VBox for grid rendering. */
|
||||
@FXML
|
||||
private javafx.scene.layout.VBox casinoTable;
|
||||
|
||||
|
||||
|
||||
private LobbyButtonTranslationManager translationManager;
|
||||
private LobbyButtonGridManager gridManager;
|
||||
private int nextButtonId = 1;
|
||||
|
||||
/**
|
||||
* Initializes the UI components and sets default values.
|
||||
@@ -63,8 +73,12 @@ public class CasinomainuiController {
|
||||
public void initialize() {
|
||||
titleLabel.setText("Casono");
|
||||
subtitleLabel.setText("Texas Hold'em Poker");
|
||||
// Logo laden
|
||||
logoView.setImage(new javafx.scene.image.Image(getClass().getResource("/images/logo.png").toExternalForm()));
|
||||
|
||||
translationManager = new LobbyButtonTranslationManager();
|
||||
gridManager = new LobbyButtonGridManager(new javafx.scene.layout.GridPane(), translationManager);
|
||||
casinoTable.getChildren().add(gridManager.getGridPane());
|
||||
gridManager.renderLobbyButtons();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,5 +89,24 @@ public class CasinomainuiController {
|
||||
Platform.exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles creation of a new lobby button.
|
||||
*/
|
||||
@FXML
|
||||
public void handleCreateLobbyButton() {
|
||||
if (translationManager.isFull()) {
|
||||
System.out.println("Grid voll! Keine weiteren Lobbys moeglich.");
|
||||
return;
|
||||
}
|
||||
int buttonId = nextButtonId++;
|
||||
int lobbyId = gridManager.createLobby();
|
||||
try {
|
||||
translationManager.addLobbyButton(buttonId, lobbyId);
|
||||
System.out.println("ButtonID: " + buttonId + ", LobbyID: " + lobbyId);
|
||||
gridManager.renderLobbyButtons();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Fehler beim Hinzufügen: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
|
||||
|
||||
/**
|
||||
* Manages the grid for lobby buttons and rendering.
|
||||
* Uses LobbyButtonTranslationManager for mapping ButtonID to LobbyID.
|
||||
*/
|
||||
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Manages the grid for lobby buttons and rendering.
|
||||
* Uses LobbyButtonTranslationManager for mapping ButtonID to LobbyID.
|
||||
*/
|
||||
public class LobbyButtonGridManager {
|
||||
|
||||
/** GridPane for the button grid. */
|
||||
private final GridPane gridPane;
|
||||
/** Manager for mapping ButtonID to LobbyID. */
|
||||
private final LobbyButtonTranslationManager translationManager;
|
||||
/** Number of rows in the grid. */
|
||||
private final int rows = 2;
|
||||
/** Number of columns in the grid. */
|
||||
private final int cols = 4;
|
||||
/** Path to the button image. */
|
||||
private final String buttonImagePath = "/images/logo.png";
|
||||
|
||||
/**
|
||||
* Constructor for the GridManager.
|
||||
*
|
||||
* @param gridPane the GridPane for rendering
|
||||
* @param translationManager the manager for mapping ButtonID to LobbyID
|
||||
*/
|
||||
public LobbyButtonGridManager(GridPane gridPane, LobbyButtonTranslationManager translationManager) {
|
||||
this.gridPane = gridPane;
|
||||
this.translationManager = translationManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders all lobby buttons in the grid.
|
||||
* Creates a button for each mapping with image and event handler.
|
||||
*/
|
||||
public void renderLobbyButtons() {
|
||||
gridPane.getChildren().clear();
|
||||
int index = 0;
|
||||
Map<Integer, Integer> mapping = translationManager.getButtonIdToLobbyId();
|
||||
if (mapping.isEmpty()) {
|
||||
// No buttons to render
|
||||
return;
|
||||
}
|
||||
for (Map.Entry<Integer, Integer> entry : mapping.entrySet()) {
|
||||
int buttonId = entry.getKey();
|
||||
Button btn = new Button();
|
||||
btn.setId("lobbyBtn-" + buttonId);
|
||||
btn.setGraphic(new ImageView(new Image(getClass().getResourceAsStream(buttonImagePath))));
|
||||
btn.setOnAction(e -> {
|
||||
Integer lobbyId = translationManager.getLobbyIdForButton(buttonId);
|
||||
if (lobbyId != null)
|
||||
joinLobby(lobbyId);
|
||||
});
|
||||
int row = index / cols;
|
||||
int col = index % cols;
|
||||
gridPane.add(btn, col, row);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Placeholder for lobby creation logic. Returns a generated lobbyId.
|
||||
*
|
||||
* @return The generated lobbyId
|
||||
*/
|
||||
public int createLobby() {
|
||||
// TODO: Replace with actual lobby creation logic
|
||||
int lobbyId = (int) (Math.random() * 10000 + 1);
|
||||
System.out.println("Lobby created: " + lobbyId);
|
||||
return lobbyId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Placeholder for joining a lobby.
|
||||
*
|
||||
* @param lobbyId The lobbyId to join
|
||||
*/
|
||||
public void joinLobby(int lobbyId) {
|
||||
// TODO: Replace with actual join logic
|
||||
System.out.println("Joining lobby: " + lobbyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the GridPane.
|
||||
*
|
||||
* @return The GridPane for the button grid
|
||||
*/
|
||||
public javafx.scene.layout.GridPane getGridPane() {
|
||||
return gridPane;
|
||||
}
|
||||
}
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Handles JSON translation between ButtonID and LobbyID.
|
||||
* <p>
|
||||
* Verwaltet das Mapping zwischen Button-IDs und Lobby-IDs, speichert und lädt die Zuordnung aus einer JSON-Datei.
|
||||
*/
|
||||
public class LobbyButtonTranslationManager {
|
||||
/**
|
||||
* Removes all lobbies from the mapping and saves the file.
|
||||
*/
|
||||
public void clearAllLobbies() {
|
||||
buttonIdToLobbyId.clear();
|
||||
saveTranslation();
|
||||
}
|
||||
/** Pfad zur JSON-Translationsdatei. */
|
||||
private final String translationFilePath = "src/main/resources/ui-structure/lobby_button_translation.json";
|
||||
/** Zuordnung ButtonID → LobbyID. */
|
||||
private final Map<Integer, Integer> buttonIdToLobbyId = new HashMap<>();
|
||||
/** Maximale Anzahl an Buttons im Grid. */
|
||||
private final int maxButtons = 8;
|
||||
|
||||
/**
|
||||
* Konstruktor: lädt die Zuordnung aus der JSON-Datei.
|
||||
*/
|
||||
public LobbyButtonTranslationManager() {
|
||||
loadTranslation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lädt die Zuordnung ButtonID → LobbyID aus der JSON-Datei.
|
||||
* Falls die Datei nicht existiert, bleibt die Zuordnung leer.
|
||||
*/
|
||||
public void loadTranslation() {
|
||||
try {
|
||||
Path path = Path.of(translationFilePath);
|
||||
if (!Files.exists(path)) return;
|
||||
String json = Files.readString(path);
|
||||
JSONObject obj = new JSONObject(json);
|
||||
buttonIdToLobbyId.clear();
|
||||
for (String key : obj.keySet()) {
|
||||
buttonIdToLobbyId.put(Integer.parseInt(key), obj.getInt(key));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Speichert die aktuelle Zuordnung ButtonID → LobbyID in die JSON-Datei.
|
||||
*/
|
||||
public void saveTranslation() {
|
||||
try {
|
||||
JSONObject obj = new JSONObject();
|
||||
for (Map.Entry<Integer, Integer> entry : buttonIdToLobbyId.entrySet()) {
|
||||
obj.put(String.valueOf(entry.getKey()), entry.getValue());
|
||||
}
|
||||
Files.write(Path.of(translationFilePath), obj.toString(2).getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft, ob das Grid voll ist (maxButtons erreicht).
|
||||
* @return true, wenn Grid voll; sonst false
|
||||
*/
|
||||
public boolean isFull() {
|
||||
return buttonIdToLobbyId.size() >= maxButtons;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt eine Zuordnung ButtonID → LobbyID hinzu und speichert sie.
|
||||
* @param buttonId Die ID des Buttons
|
||||
* @param lobbyId Die ID der Lobby
|
||||
* @throws Exception wenn das Grid voll ist
|
||||
*/
|
||||
public void addLobbyButton(int buttonId, int lobbyId) throws Exception {
|
||||
if (isFull()) throw new Exception("Grid is full!");
|
||||
buttonIdToLobbyId.put(buttonId, lobbyId);
|
||||
saveTranslation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Entfernt eine Zuordnung für die gegebene ButtonID und speichert.
|
||||
* @param buttonId Die ID des zu entfernenden Buttons
|
||||
*/
|
||||
public void removeLobbyButton(int buttonId) {
|
||||
buttonIdToLobbyId.remove(buttonId);
|
||||
saveTranslation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die LobbyID für eine gegebene ButtonID zurück.
|
||||
* @param buttonId Die ButtonID
|
||||
* @return Die zugehoerige LobbyID oder null, falls nicht vorhanden
|
||||
*/
|
||||
public Integer getLobbyIdForButton(int buttonId) {
|
||||
return buttonIdToLobbyId.get(buttonId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die gesamte Zuordnung ButtonID → LobbyID zurück.
|
||||
* @return Map aller Zuordnungen
|
||||
*/
|
||||
public Map<Integer, Integer> getButtonIdToLobbyId() {
|
||||
return buttonIdToLobbyId;
|
||||
}
|
||||
}
|
||||
@@ -120,6 +120,7 @@
|
||||
-fx-padding: 0 0 40 0;
|
||||
}
|
||||
|
||||
/* EXIT BUTTON */
|
||||
.button-exit {
|
||||
-fx-background-radius: 12;
|
||||
-fx-border-radius: 12;
|
||||
@@ -128,17 +129,17 @@
|
||||
-fx-border-width: 2;
|
||||
-fx-padding: 6 15;
|
||||
-fx-cursor: hand;
|
||||
-fx-background-color: #333333;
|
||||
-fx-border-color: #666666;
|
||||
-fx-text-fill: #CCCCCC;
|
||||
}
|
||||
|
||||
.button-exit:hover {
|
||||
-fx-translate-y: -3;
|
||||
-fx-effect: dropshadow(one-pass-box, rgba(0, 0, 0, 0.8), 0, 0, 3, 3);
|
||||
}
|
||||
|
||||
.button-exit {
|
||||
-fx-background-color: #333333;
|
||||
-fx-border-color: #666666;
|
||||
-fx-text-fill: #CCCCCC;
|
||||
/* LOBBY ERSTELLEN BUTTON */
|
||||
.button-create-lobby {
|
||||
-fx-background-radius: 12;
|
||||
-fx-border-radius: 12;
|
||||
-fx-font-family: "Courier New";
|
||||
@@ -146,6 +147,14 @@
|
||||
-fx-border-width: 2;
|
||||
-fx-padding: 6 15;
|
||||
-fx-cursor: hand;
|
||||
-fx-background-color: #1a7f2e;
|
||||
-fx-border-color: #2ecc71;
|
||||
-fx-text-fill: #ffffff;
|
||||
}
|
||||
.button-create-lobby:hover {
|
||||
-fx-translate-y: -3;
|
||||
-fx-effect: dropshadow(one-pass-box, rgba(0, 0, 0, 0.8), 0, 0, 3, 3);
|
||||
-fx-background-color: #27ae60;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -68,6 +68,18 @@
|
||||
<Insets top="20" left="20" />
|
||||
</GridPane.margin>
|
||||
</Button>
|
||||
<Button text="Lobby erstellen"
|
||||
styleClass="button-create-lobby"
|
||||
fx:id="createLobbyButton"
|
||||
onAction="#handleCreateLobbyButton"
|
||||
GridPane.rowIndex="0"
|
||||
GridPane.columnIndex="0"
|
||||
GridPane.halignment="LEFT"
|
||||
GridPane.valignment="TOP">
|
||||
<GridPane.margin>
|
||||
<Insets top="60" left="20" />
|
||||
</GridPane.margin>
|
||||
</Button>
|
||||
|
||||
<VBox fx:id="casinoTable"
|
||||
alignment="CENTER"
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"1": 1240,
|
||||
"2": 3640,
|
||||
"3": 7589,
|
||||
"4": 3982,
|
||||
"5": 5655,
|
||||
"6": 6418,
|
||||
"7": 8586,
|
||||
"8": 7378
|
||||
}
|
||||
Reference in New Issue
Block a user