auswahllogik im dropdown implementiert

This commit is contained in:
Yvonne Schmidt 2024-11-23 17:03:56 +01:00
parent fd347c7780
commit 09b363f943
3 changed files with 165 additions and 15 deletions

View File

@ -258,3 +258,30 @@ selector("menu-button", "pp") {
buttonCommands = stdButtonCommands buttonCommands = stdButtonCommands
} }
// Style for Selector text
selector("selector.item.label") {
color = color(0, 0, 0, 1) // Black text
fontSize = 16 // Optional: Adjust the text size if needed
textHAlignment = HAlignment.Left // Optional: Align text to the left
insets = new Insets3f(2, 2, 2, 2) // Optional: Add padding around text
}
// Style the popup container background
selector("selector.popup") {
background = new QuadBackgroundComponent(new ColorRGBA(1, 1, 1, 0.8f)) // Translucent white background
insets = new Insets3f(5, 5, 5, 5) // Padding inside the popup container
}
// Style the text of dropdown options
selector("selector.item.label") {
color = color(0, 0, 0, 1) // Black text
fontSize = 16 // Optional: Adjust font size
textHAlignment = HAlignment.Left // Align text to the left
insets = new Insets3f(2, 5, 2, 5) // Add padding for each option
}
// Style the hover state of dropdown options
selector("selector.item.label", "hover") {
color = color(1, 1, 1, 1) // White text when hovered
background = new QuadBackgroundComponent(new ColorRGBA(0.2f, 0.6f, 1.0f, 0.9f)) // Highlighted background
}

View File

@ -1,5 +1,7 @@
package pp.monopoly.client.gui; package pp.monopoly.client.gui;
import com.jme3.app.Application;
import com.jme3.app.state.BaseAppState;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
@ -16,14 +18,19 @@ import com.simsilica.lemur.TextField;
import com.simsilica.lemur.component.QuadBackgroundComponent; import com.simsilica.lemur.component.QuadBackgroundComponent;
import com.simsilica.lemur.component.SpringGridLayout; 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 com.simsilica.lemur.style.ElementId;
import pp.monopoly.client.MonopolyApp; import pp.monopoly.client.MonopolyApp;
import java.util.Set;
public class LobbyMenu { public class LobbyMenu {
private final MonopolyApp app; private final MonopolyApp app;
private final Container menuContainer; private final Container menuContainer;
private Geometry background; private Geometry background;
private final Selector<String> dropdown;
public LobbyMenu(MonopolyApp app) { public LobbyMenu(MonopolyApp app) {
this.app = app; this.app = app;
@ -46,7 +53,7 @@ public class LobbyMenu {
horizontalContainer.setPreferredSize(new Vector3f(600, 50, 0)); // Adjust container size horizontalContainer.setPreferredSize(new Vector3f(600, 50, 0)); // Adjust container size
horizontalContainer.setBackground(null); horizontalContainer.setBackground(null);
Label title = horizontalContainer.addChild(new Label("Startkapital:",new ElementId("label-Bold"))); Label title = horizontalContainer.addChild(new Label("Startkapital:", new ElementId("label-Bold")));
title.setFontSize(48); title.setFontSize(48);
// Add a spacer between the title and the input field // Add a spacer between the title and the input field
@ -76,20 +83,20 @@ public class LobbyMenu {
playerListContainer.setBackground(null); playerListContainer.setBackground(null);
Label playersLabel = playerListContainer.addChild(new Label("Noch keine Spieler verbunden.")); // Beispieltext Label playersLabel = playerListContainer.addChild(new Label("Noch keine Spieler verbunden.")); // Beispieltext
// Dropdown menu for game options VersionedList<String> items = new VersionedList<>();
Selector<String> dropdown = new Selector<>(); items.add("Alpha");
for (int i = 1; i <= 10; i++) { // Generate 10 options items.add("Beta");
dropdown.getModel().add("Option " + i); items.add("Gamma");
} items.add("Back");
menuContainer.addChild(new Label("Game Options:")); // Add a label for clarity dropdown = new Selector<>(items);
dropdown.setBackground(new QuadBackgroundComponent(ColorRGBA.Black));
menuContainer.addChild(dropdown); menuContainer.addChild(dropdown);
// Adjust the dropdown's popup container size if necessary // Add the custom action listener
Vector3f dimens = menuContainer.getPreferredSize(); addSelectionActionListener(dropdown, this::onDropdownSelectionChanged);
Vector3f dimens2 = dropdown.getPopupContainer().getPreferredSize();
dimens2.setX(dimens.getX());
dropdown.getPopupContainer().setPreferredSize(dimens2);
// Buttons // Buttons
Container buttonContainer = menuContainer.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y))); Container buttonContainer = menuContainer.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y)));
@ -146,4 +153,71 @@ public class LobbyMenu {
app.getGuiNode().detachChild(background); // Entfernt das Hintergrundbild app.getGuiNode().detachChild(background); // Entfernt das Hintergrundbild
new CreateGameMenu(app); new CreateGameMenu(app);
} }
/**
* Adds a custom action listener to the Selector.
*/
private void addSelectionActionListener(Selector<String> selector, SelectionActionListener<String> listener) {
VersionedReference<Set<Integer>> selectionRef = selector.getSelectionModel().createReference();
app.getStateManager().attach(new BaseAppState() {
@Override
public void update(float tpf) {
if (selectionRef.update()) {
String selected = selectionRef.get().toString();
System.out.println(selected);
listener.onSelectionChanged(selected);
}
}
@Override
protected void initialize(Application app) {
}
@Override
protected void cleanup(Application app) {
}
@Override
protected void onEnable() {
}
@Override
protected void onDisable() {
}
});
}
/**
* Callback for when the dropdown selection changes.
*/
private void onDropdownSelectionChanged(String selected) {
System.out.println("Selected: " + selected);
switch (selected) {
case "[0]":
System.out.println("Alpha selected");
break;
case "[1]":
System.out.println("Beta selected");
break;
case "[2]":
System.out.println("Gamma selected");
break;
case "[3]":
goBackToCreateGame();
break;
default:
System.out.println("Unknown selection");
}
}
/**
* Functional interface for a selection action listener.
*/
@FunctionalInterface
private interface SelectionActionListener<T> {
void onSelectionChanged(T selection);
}
} }

View File

@ -0,0 +1,49 @@
package pp.monopoly.client.gui.popups;
/*
* $Id$
*
* Copyright (c) 2013-2013 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import com.jme3.scene.Spatial;
/**
* Notified when the current selection changes.
*
* @author Paul Speed
*/
public interface SelectionListener {
public void selectionChanged( Spatial selection, Spatial previous );
}