added selector and selection display to RepayMortage

This commit is contained in:
Yvonne Schmidt 2024-12-02 01:06:58 +01:00
parent e05ad71fff
commit 411f7ea25b

View File

@ -2,12 +2,17 @@ package pp.monopoly.client.gui.popups;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.simsilica.lemur.Axis;
import com.simsilica.lemur.Button; import com.simsilica.lemur.Button;
import com.simsilica.lemur.Container; import com.simsilica.lemur.Container;
import com.simsilica.lemur.Label; import com.simsilica.lemur.Label;
import com.simsilica.lemur.ListBox; import com.simsilica.lemur.ListBox;
import com.simsilica.lemur.Selector;
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.core.VersionedList; 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.dialog.Dialog; import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp; import pp.monopoly.client.MonopolyApp;
@ -17,7 +22,9 @@ import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.BuildingProperty; import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.notification.Sound; import pp.monopoly.notification.Sound;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -27,6 +34,12 @@ public class RepayMortage extends Dialog {
private final MonopolyApp app; private final MonopolyApp app;
private final Container repayMortageContainer; private final Container repayMortageContainer;
private final Container backgroundContainer; private final Container backgroundContainer;
private TextField selectionDisplay; // TextField to display selections
private VersionedReference<Set<Integer>> selectionRef;
private Selector<String> propertySelector;
private Set<String> selectedProperties = new HashSet<>();
private Label cost = new Label("0", new ElementId("label-Text"));
public RepayMortage(MonopolyApp app) { public RepayMortage(MonopolyApp app) {
@ -63,42 +76,11 @@ public class RepayMortage extends Dialog {
middleContainer.setPreferredSize(new Vector3f(100, 150, 0)); middleContainer.setPreferredSize(new Vector3f(100, 150, 0));
middleContainer.setBackground(new QuadBackgroundComponent(ColorRGBA.Orange)); middleContainer.setBackground(new QuadBackgroundComponent(ColorRGBA.Orange));
// Create a VersionedList for the ListBox model middleContainer.addChild(createPropertyDropdown());
VersionedList<BuildingProperty> listModel = new VersionedList<>();
// Retrieve current player and their properties //TODO hier Prüfen, ob abweichungen zur SellHouse-Klasse beachtet werden müssen
Player currentPlayer = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId());
BoardManager boardManager = app.getGameLogic().getBoardManager();
List<BuildingProperty> playerProperties = boardManager.getPropertyFields(
currentPlayer.getProperties()).stream()
.filter(property -> property instanceof BuildingProperty)
.map(property -> (BuildingProperty) property)
.filter(property -> property.getHouses() > 0 || property.getHotel() == 1)
.collect(Collectors.toList());
// Populate the list model
listModel.addAll(playerProperties);
// Create a ListBox with the "glass" style and the model
ListBox<BuildingProperty> listBox = new ListBox<>(listModel, "glass");
listBox.setPreferredSize(new Vector3f(300, 200, 0)); // Adjust size as needed
// Add selection listener
listBox.addClickCommands(item -> {
BuildingProperty selected = listBox.getSelectedItem(); // Correct method to retrieve the selected item
if (selected != null) {
System.out.println("Selected property: " + selected.getName());
}
});
// Add the ListBox to the middle container
middleContainer.addChild(listBox);
downContainer.addChild(new Label("", new ElementId("label-Text")));// Leerzeile downContainer.addChild(new Label("", new ElementId("label-Text")));// Leerzeile
downContainer.addChild(new Label("Kosten:", new ElementId("label-Text")));// Leerzeile downContainer.addChild(new Label("Kosten:", new ElementId("label-Text")));// Leerzeile
downContainer.addChild(new Label("Hier die tätsächliche Erstattung", new ElementId("label-Text"))); downContainer.addChild(cost);
downContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f))); downContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f)));
// Beenden-Button // Beenden-Button
@ -113,12 +95,9 @@ public class RepayMortage extends Dialog {
confirmButton.setFontSize(32); confirmButton.setFontSize(32);
confirmButton.addClickCommands(s -> ifTopDialog( () -> { confirmButton.addClickCommands(s -> ifTopDialog( () -> {
app.getGameLogic().playSound(Sound.BUTTON); app.getGameLogic().playSound(Sound.BUTTON);
BuildingProperty selected = listBox.getSelectedItem();
if (selected != null) {
System.out.println("Confirmed property: " + selected.getName());
// Send the "alter building" message to the server // Send the "alter building" message to the server
//app.getGameLogic().sendMessage(new AlterBuildingMessage(selected.getId(), false)); TODO Message an Server //app.getGameLogic().sendMessage(new AlterBuildingMessage(selected.getId(), false)); TODO Message an Server
}
})); }));
// Zentriere das Popup // Zentriere das Popup
@ -138,6 +117,82 @@ public class RepayMortage extends Dialog {
app.getGuiNode().attachChild(repayMortageContainer); app.getGuiNode().attachChild(repayMortageContainer);
} }
/**
* Creates a dropdown menu for selecting a property.
*
* @return The dropdown container.
*/
private Container createPropertyDropdown() {
Container dropdownContainer = new Container(new SpringGridLayout(Axis.Y, Axis.X));
dropdownContainer.setPreferredSize(new Vector3f(300, 200, 0));
dropdownContainer.setBackground(new QuadBackgroundComponent(ColorRGBA.Orange));
VersionedList<String> propertyOptions = new VersionedList<>();
List<BuildingProperty> playerProperties = getPlayerProperties();
// Populate the dropdown with property names
for (BuildingProperty property : playerProperties) {
propertyOptions.add(property.getName());
}
propertySelector = new Selector<>(propertyOptions, "glass");
dropdownContainer.addChild(propertySelector);
// Track selection changes
selectionRef = propertySelector.getSelectionModel().createReference();
// Initialize the selection display here
selectionDisplay = new TextField(""); // Create TextField for displaying selections
selectionDisplay.setPreferredSize(new Vector3f(300, 30, 0));
dropdownContainer.addChild(selectionDisplay); // Add it to the dropdown container
return dropdownContainer;
}
/**
* Retrieves the list of properties owned by the current player.
*
* @return List of BuildingProperty objects owned by the player.
*/
private List<BuildingProperty> getPlayerProperties() {
Player self = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId());
BoardManager boardManager = app.getGameLogic().getBoardManager();
return boardManager.getPropertyFields(self.getProperties()).stream()
.filter(property -> property instanceof BuildingProperty)
.map(property -> (BuildingProperty) property)
.collect(Collectors.toList());
}
@Override
public void update(float delta) {
if(selectionRef.update()) {
onDropdownSelectionChanged(propertySelector);
}
}
/**
* Handles property selection changes.
*/
private void onDropdownSelectionChanged(Selector<String> playerProperties) {
String selected = playerProperties.getSelectedItem();
app.getGameLogic().playSound(Sound.BUTTON);
if (selectedProperties.contains(selected)) {
selectedProperties.remove(selected);
} else {
selectedProperties.add(selected);
}
int cost = 0;
for (String s : selectedProperties) {
cost += ((BuildingProperty) app.getGameLogic().getBoardManager().getFieldByName(s)).getHousePrice();
}
String display = String.join(" | ", selectedProperties);
selectionDisplay.setText(display);
this.cost.setText(cost+"");
}
/** /**
* Schließt das Menü und entfernt die GUI-Elemente. * Schließt das Menü und entfernt die GUI-Elemente.
*/ */