From 48b1cf817a9034794812911f8e985c639c8736d0 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Fri, 29 Nov 2024 04:32:57 +0100 Subject: [PATCH] refactor --- .../pp/monopoly/client/gui/ChoosePartner.java | 93 ++++++------------- 1 file changed, 27 insertions(+), 66 deletions(-) diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java index b0f89e0..452770f 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java @@ -2,24 +2,19 @@ package pp.monopoly.client.gui; import java.util.Set; -import com.jme3.app.Application; -import com.jme3.app.state.BaseAppState; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Quad; import com.jme3.texture.Texture; -import com.simsilica.lemur.Axis; -import com.simsilica.lemur.Button; -import com.simsilica.lemur.Container; -import com.simsilica.lemur.Label; -import com.simsilica.lemur.Selector; +import com.simsilica.lemur.*; import com.simsilica.lemur.component.QuadBackgroundComponent; import com.simsilica.lemur.component.SpringGridLayout; import com.simsilica.lemur.core.VersionedList; import com.simsilica.lemur.core.VersionedReference; import com.simsilica.lemur.style.ElementId; + import pp.dialog.Dialog; import pp.monopoly.client.MonopolyApp; import pp.monopoly.game.server.Player; @@ -37,6 +32,9 @@ public class ChoosePartner extends Dialog { private Container lowerRightMenu; private Geometry background; private TradeHandler tradeHandler; + private VersionedReference> selectionRef; // Reference to track selector changes + private String lastSelected = ""; // To keep track of the last selected value + QuadBackgroundComponent translucentWhiteBackground = new QuadBackgroundComponent(new ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f)); @@ -70,8 +68,6 @@ public class ChoosePartner extends Dialog { // Add buttons mainContainer.addChild(createButtonContainer()); - addSelectionActionListener(playerSelector, this::onDropdownSelectionChanged); - // Attach main container to GUI node app.getGuiNode().attachChild(mainContainer); mainContainer.setLocalTranslation( @@ -79,6 +75,9 @@ public class ChoosePartner extends Dialog { (app.getCamera().getHeight() + mainContainer.getPreferredSize().y) / 2, 4 ); + + // Initialize selection reference for tracking changes + selectionRef = playerSelector.getSelectionModel().createReference(); } /** @@ -95,7 +94,7 @@ public class ChoosePartner extends Dialog { for (Player player : app.getGameLogic().getPlayerHandler().getPlayers()) { if (player.getId() != app.getId()) { - playerOptions.add(player.getName() + " (ID: "+player.getId()+")"); + playerOptions.add(player.getName() + " (ID: " + player.getId() + ")"); } } @@ -103,10 +102,14 @@ public class ChoosePartner extends Dialog { dropdownContainer.addChild(playerSelector); Vector3f dimens = dropdownContainer.getPreferredSize(); Vector3f dimens2 = playerSelector.getPopupContainer().getPreferredSize(); - dimens2.setX( dimens.getX() ); - playerSelector.getPopupContainer().setPreferredSize(new Vector3f(200,200,3)); - playerSelector.setLocalTranslation(0,0,5); - onDropdownSelectionChanged(playerOptions.get(0)); + dimens2.setX(dimens.getX()); + playerSelector.getPopupContainer().setPreferredSize(new Vector3f(200, 200, 3)); + playerSelector.setLocalTranslation(0, 0, 5); + + // Set initial selection + if (!playerOptions.isEmpty()) { + onDropdownSelectionChanged(playerOptions.get(0)); + } return dropdownContainer; } @@ -134,7 +137,6 @@ public class ChoosePartner extends Dialog { lowerLeftMenu.setLocalTranslation(new Vector3f(120, 170, 5)); // Adjust X and Y to align with the bottom-left corner app.getGuiNode().attachChild(lowerLeftMenu); - // "Bestätigen" button lowerRightMenu = new Container(); confirmButton.setPreferredSize(new Vector3f(200, 60, 0)); @@ -150,7 +152,6 @@ public class ChoosePartner extends Dialog { lowerRightMenu.setLocalTranslation(new Vector3f(app.getCamera().getWidth() - 320, 170, 5)); // X: 220px from the right, Y: 50px above the bottom app.getGuiNode().attachChild(lowerRightMenu); - return buttonContainer; } @@ -183,7 +184,14 @@ public class ChoosePartner extends Dialog { */ @Override public void update(float delta) { - // Periodic updates (if needed) can be implemented here + // Check if the selection has changed + if (selectionRef.update()) { + String selected = playerSelector.getSelectedItem(); + if (!selected.equals(lastSelected)) { + lastSelected = selected; + onDropdownSelectionChanged(selected); + } + } } @Override @@ -196,42 +204,6 @@ public class ChoosePartner extends Dialog { super.close(); } - /** - * Adds a custom action listener to the Selector. - */ - private void addSelectionActionListener(Selector selector, SelectionActionListener listener) { - VersionedReference> selectionRef = selector.getSelectionModel().createReference(); - - app.getStateManager().attach(new BaseAppState() { - @Override - public void update(float tpf) { - if (selectionRef.update()) { - String selected = selectionRef.get().toString(); - listener.onSelectionChanged(selected); - } - } - - @Override - protected void initialize(Application app) { - update(1); - } - - @Override - protected void cleanup(Application app) { - } - - @Override - protected void onEnable() { - - } - - @Override - protected void onDisable() { - - } - }); - } - /** * Callback for when the dropdown selection changes. */ @@ -241,23 +213,12 @@ public class ChoosePartner extends Dialog { int idEnd = selected.indexOf(")", idStart); // Find end of the ID String idStr = selected.substring(idStart, idEnd); // Extract the ID as a string int playerId = Integer.parseInt(idStr); // Convert the ID to an integer - + // Find the player by ID Player selectedPlayer = app.getGameLogic().getPlayerHandler().getPlayerById(playerId); - + if (selectedPlayer != null) { tradeHandler.setReceiver(selectedPlayer); // Set the receiver in TradeHandler } } - - /** - * Functional interface for a selection action listener. - */ - @FunctionalInterface - private interface SelectionActionListener { - void onSelectionChanged(T selection); - } } - - -