From e000dcfc51f1ad7a5463b942c009856053a66df8 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Thu, 28 Nov 2024 19:50:53 +0100 Subject: [PATCH] cleanup --- .../java/pp/monopoly/client/gui/Toolbar.java | 258 +++++++++--------- 1 file changed, 124 insertions(+), 134 deletions(-) diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java index 0e10258..7d5fe4c 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java @@ -3,7 +3,10 @@ package pp.monopoly.client.gui; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector2f; import com.jme3.math.Vector3f; -import com.simsilica.lemur.*; +import com.simsilica.lemur.Axis; +import com.simsilica.lemur.Button; +import com.simsilica.lemur.Container; +import com.simsilica.lemur.Label; import com.simsilica.lemur.component.IconComponent; import com.simsilica.lemur.component.QuadBackgroundComponent; import com.simsilica.lemur.component.SpringGridLayout; @@ -15,25 +18,22 @@ import pp.monopoly.game.server.Player; import pp.monopoly.game.server.PlayerHandler; import pp.monopoly.message.client.EndTurn; import pp.monopoly.message.client.RollDice; -import pp.monopoly.notification.DiceRollEvent; -import pp.monopoly.notification.ButtonStatusEvent; -import pp.monopoly.notification.GameEventListener; -import pp.monopoly.notification.Sound; -import pp.monopoly.notification.UpdatePlayerView; +import pp.monopoly.notification.*; public class Toolbar extends Dialog implements GameEventListener { private final MonopolyApp app; private final Container toolbarContainer; - private Label imageLabel; - private Label imageLabel2; private Container overviewContainer; private Container accountContainer; private PlayerHandler playerHandler; + private Label imageLabel; + private Label imageLabel2; private Button diceButton; private Button tradeButton; private Button propertyMenuButton; private Button endTurnButton; + private volatile boolean animatingDice = false; private volatile DiceRollEvent latestDiceRollEvent = null; @@ -42,52 +42,39 @@ public class Toolbar extends Dialog implements GameEventListener { this.app = app; app.getGameLogic().addListener(this); - playerHandler = app.getGameLogic().getPlayerHandler(); + this.playerHandler = app.getGameLogic().getPlayerHandler(); - // Erstelle die Toolbar - toolbarContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y), "toolbar"); - toolbarContainer.setLocalTranslation(0, 200, 0); - toolbarContainer.setPreferredSize(new Vector3f(app.getCamera().getWidth(), 200, 0)); + toolbarContainer = createToolbarContainer(); + app.getGuiNode().attachChild(toolbarContainer); + } - // Account- und Übersichtskontainer - accountContainer = toolbarContainer.addChild(new Container()); - overviewContainer = toolbarContainer.addChild(new Container()); + private Container createToolbarContainer() { + Container container = new Container(new SpringGridLayout(Axis.X, Axis.Y), "toolbar"); + container.setLocalTranslation(0, 200, 0); + container.setPreferredSize(new Vector3f(app.getCamera().getWidth(), 200, 0)); + // Create account and overview containers + accountContainer = container.addChild(new Container()); + overviewContainer = container.addChild(new Container()); receivedEvent(new UpdatePlayerView()); - overviewContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f))); + // Dice section + container.addChild(createDiceSection()); - // Würfelbereich - Container diceContainer = toolbarContainer.addChild(new Container()); - diceContainer.setLayout(new SpringGridLayout(Axis.X, Axis.Y)); + // Action menu + Container menuContainer = container.addChild(new Container()); + menuContainer.addChild(createTradeButton()); + menuContainer.addChild(createPropertyMenuButton()); + menuContainer.addChild(createEndTurnButton()); + menuContainer.setBackground(createBackground()); - Container horizontalContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y)); - horizontalContainer.setPreferredSize(new Vector3f(200, 150, 0)); + return container; + } - // Linker Würfel - Container leftContainer = new Container(); - leftContainer.setPreferredSize(new Vector3f(100, 150, 0)); - imageLabel = new Label(""); - IconComponent icon = new IconComponent("Pictures/dice/one.png"); - icon.setIconSize(new Vector2f(100, 100)); - imageLabel.setIcon(icon); + private Container createDiceSection() { + Container diceContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y)); + diceContainer.addChild(createDiceDisplay()); - // Rechter Würfel - Container rightContainer = new Container(); - rightContainer.setPreferredSize(new Vector3f(100, 150, 0)); - imageLabel2 = new Label(""); - IconComponent icon2 = new IconComponent("Pictures/dice/two.png"); - icon2.setIconSize(new Vector2f(100, 100)); - imageLabel2.setIcon(icon2); - - leftContainer.addChild(imageLabel); - rightContainer.addChild(imageLabel2); - - horizontalContainer.addChild(leftContainer); - horizontalContainer.addChild(rightContainer); - diceContainer.addChild(horizontalContainer); - - // Würfeln-Button diceButton = new Button("Würfeln", new ElementId("button-toolbar")); diceButton.setPreferredSize(new Vector3f(200, 50, 0)); diceButton.addClickCommands(s -> ifTopDialog(() -> { @@ -98,17 +85,38 @@ public class Toolbar extends Dialog implements GameEventListener { })); diceContainer.addChild(diceButton); - // Menü-Container für weitere Aktionen - Container menuContainer = toolbarContainer.addChild(new Container()); - menuContainer.addChild(addTradeMenuButton()); - menuContainer.addChild(addPropertyMenuButton()); - menuContainer.addChild(addEndTurnButton()); - menuContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f))); - - app.getGuiNode().attachChild(toolbarContainer); + return diceContainer; } - private Button addTradeMenuButton() { + private Container createDiceDisplay() { + Container horizontalContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y)); + horizontalContainer.setPreferredSize(new Vector3f(200, 150, 0)); + + imageLabel = createDiceLabel("Pictures/dice/one.png"); + imageLabel2 = createDiceLabel("Pictures/dice/two.png"); + + horizontalContainer.addChild(createDiceContainer(imageLabel)); + horizontalContainer.addChild(createDiceContainer(imageLabel2)); + + return horizontalContainer; + } + + private Label createDiceLabel(String iconPath) { + Label label = new Label(""); + IconComponent icon = new IconComponent(iconPath); + icon.setIconSize(new Vector2f(100, 100)); + label.setIcon(icon); + return label; + } + + private Container createDiceContainer(Label label) { + Container container = new Container(); + container.setPreferredSize(new Vector3f(100, 150, 0)); + container.addChild(label); + return container; + } + + private Button createTradeButton() { tradeButton = new Button("Handeln", new ElementId("button-toolbar")); tradeButton.setPreferredSize(new Vector3f(150, 50, 0)); tradeButton.addClickCommands(s -> ifTopDialog(() -> { @@ -118,19 +126,18 @@ public class Toolbar extends Dialog implements GameEventListener { return tradeButton; } - private Button addPropertyMenuButton() { + private Button createPropertyMenuButton() { propertyMenuButton = new Button("Grundstücke", new ElementId("button-toolbar")); propertyMenuButton.setPreferredSize(new Vector3f(150, 50, 0)); propertyMenuButton.setFontSize(30); propertyMenuButton.addClickCommands(s -> ifTopDialog(() -> { app.getGameLogic().playSound(Sound.BUTTON); - // new BuildingAdminMenu(app).open(); new PropertyOverviewMenu(app).open(); })); return propertyMenuButton; } - private Button addEndTurnButton() { + private Button createEndTurnButton() { endTurnButton = new Button("Zug beenden", new ElementId("button-toolbar")); endTurnButton.setPreferredSize(new Vector3f(150, 50, 0)); endTurnButton.addClickCommands(s -> ifTopDialog(() -> { @@ -141,77 +148,78 @@ public class Toolbar extends Dialog implements GameEventListener { return endTurnButton; } + private QuadBackgroundComponent createBackground() { + return new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f)); + } + private void startDiceAnimation() { animatingDice = true; - - // Starte die Animation und speichere die Startzeit long startTime = System.currentTimeMillis(); - - Thread diceAnimation = new Thread(() -> { - int[] currentFace = {1}; + + new Thread(() -> { try { - while (System.currentTimeMillis() - startTime < 2500) { // Animation läuft für 4 Sekunden - currentFace[0] = (currentFace[0] % 6) + 1; - - String rotatingImage1 = diceToString(currentFace[0]); - String rotatingImage2 = diceToString((currentFace[0] % 6) + 1); - - IconComponent newIcon1 = new IconComponent(rotatingImage1); - newIcon1.setIconSize(new Vector2f(100, 100)); - app.enqueue(() -> imageLabel.setIcon(newIcon1)); - - IconComponent newIcon2 = new IconComponent(rotatingImage2); - newIcon2.setIconSize(new Vector2f(100, 100)); - app.enqueue(() -> imageLabel2.setIcon(newIcon2)); - - // Warte 100 ms, bevor die Bilder wechseln - Thread.sleep(100); - } - - // Animation beenden + animateDice(startTime); animatingDice = false; - - // Zeige das finale Ergebnis if (latestDiceRollEvent != null) { showFinalDiceResult(latestDiceRollEvent); } - } catch (InterruptedException e) { - System.err.println("Würfelanimation unterbrochen: " + e.getMessage()); + System.err.println("Dice animation interrupted: " + e.getMessage()); } - }); - diceAnimation.start(); + }).start(); } - + + private void animateDice(long startTime) throws InterruptedException { + int[] currentFace = {1}; + while (System.currentTimeMillis() - startTime < 2500) { // Animation duration + currentFace[0] = (currentFace[0] % 6) + 1; + + String rotatingImage1 = diceToString(currentFace[0]); + String rotatingImage2 = diceToString((currentFace[0] % 6) + 1); + + app.enqueue(() -> { + setDiceIcon(imageLabel, rotatingImage1); + setDiceIcon(imageLabel2, rotatingImage2); + }); + + Thread.sleep(100); // Time between frame updates + } + } + private void showFinalDiceResult(DiceRollEvent event) { - int diceRoll1 = event.a(); - int diceRoll2 = event.b(); + app.enqueue(() -> { + setDiceIcon(imageLabel, diceToString(event.a())); + setDiceIcon(imageLabel2, diceToString(event.b())); + app.getGameLogic().playSound(Sound.BUTTON); + }); + } + + private void setDiceIcon(Label label, String imagePath) { + IconComponent icon = new IconComponent(imagePath); + icon.setIconSize(new Vector2f(80, 80)); // Set consistent dice size + label.setIcon(icon); + } + - String finalImage1 = diceToString(diceRoll1); - String finalImage2 = diceToString(diceRoll2); - - IconComponent finalIcon1 = new IconComponent(finalImage1); - finalIcon1.setIconSize(new Vector2f(100, 100)); - app.enqueue(() -> imageLabel.setIcon(finalIcon1)); - - IconComponent finalIcon2 = new IconComponent(finalImage2); - finalIcon2.setIconSize(new Vector2f(100, 100)); - app.enqueue(() -> imageLabel2.setIcon(finalIcon2)); - - app.getGameLogic().playSound(Sound.BUTTON); + private String diceToString(int i) { + switch (i) { + case 1: return "Pictures/dice/one.png"; + case 2: return "Pictures/dice/two.png"; + case 3: return "Pictures/dice/three.png"; + case 4: return "Pictures/dice/four.png"; + case 5: return "Pictures/dice/five.png"; + case 6: return "Pictures/dice/six.png"; + default: throw new IllegalArgumentException("Invalid dice number: " + i); + } } @Override public void receivedEvent(DiceRollEvent event) { - latestDiceRollEvent = event; // Speichere das Event + latestDiceRollEvent = event; } @Override public void receivedEvent(UpdatePlayerView event) { - playerHandler = app.getGameLogic().getPlayerHandler(); - System.err.println("Update Player View"); - - // Clear existing accountContainer and overviewContainer content accountContainer.clearChildren(); overviewContainer.clearChildren(); @@ -225,7 +233,7 @@ public class Toolbar extends Dialog implements GameEventListener { playerHandler.getPlayerById(app.getId()).getNumJailCard() + "", new ElementId("label-Text") )); - accountContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f))); + accountContainer.setBackground(createBackground()); overviewContainer.addChild(new Label("Übersicht", new ElementId("label-Bold"))); for (Player player : playerHandler.getPlayers()) { @@ -236,26 +244,16 @@ public class Toolbar extends Dialog implements GameEventListener { )); } } - overviewContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f))); + overviewContainer.setBackground(createBackground()); } - private String diceToString(int i) { - switch (i) { - case 1: - return "Pictures/dice/one.png"; - case 2: - return "Pictures/dice/two.png"; - case 3: - return "Pictures/dice/three.png"; - case 4: - return "Pictures/dice/four.png"; - case 5: - return "Pictures/dice/five.png"; - case 6: - return "Pictures/dice/six.png"; - default: - throw new IllegalArgumentException("Invalid dice number: " + i); - } + @Override + public void receivedEvent(ButtonStatusEvent event) { + boolean enabled = event.buttonsEnabled(); + diceButton.setEnabled(enabled); + tradeButton.setEnabled(enabled); + propertyMenuButton.setEnabled(enabled); + endTurnButton.setEnabled(enabled); } @Override @@ -268,12 +266,4 @@ public class Toolbar extends Dialog implements GameEventListener { public void escape() { new SettingsMenu(app).open(); } - - @Override - public void receivedEvent(ButtonStatusEvent event) { - propertyMenuButton.setEnabled(event.buttonsEnabled()); - diceButton.setEnabled(event.buttonsEnabled()); - endTurnButton.setEnabled(event.buttonsEnabled()); - tradeButton.setEnabled(event.buttonsEnabled()); - } }