working selector

This commit is contained in:
Johannes Schmelz 2024-12-02 00:47:06 +01:00
parent 3b729c9a04
commit 5bb438c221

View File

@ -6,8 +6,8 @@ import com.simsilica.lemur.Axis;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Container;
import com.simsilica.lemur.Label;
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.SpringGridLayout;
import com.simsilica.lemur.core.VersionedList;
@ -17,11 +17,12 @@ import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.client.gui.SettingsMenu;
import pp.monopoly.game.server.Player;
import pp.monopoly.message.client.AlterProperty;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.model.fields.PropertyField;
import pp.monopoly.notification.Sound;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@ -33,9 +34,12 @@ public class BuyHouse extends Dialog {
private final MonopolyApp app;
private final Container buyHouseContainer;
private final Container backgroundContainer;
private TextField selectionDisplay; // TextField to display selections
private VersionedReference<Set<Integer>> selectionRef;
private Selector<String> propertySelector;
private BuildingProperty selectedProperty;
private Set<String> selectedProperties = new HashSet<>();
private Label cost = new Label("0", new ElementId("label-Text"));
public BuyHouse(MonopolyApp app) {
super(app.getDialogManager());
@ -76,7 +80,7 @@ public class BuyHouse extends Dialog {
downContainer.addChild(new Label("", new ElementId("label-Text"))); // Empty line
downContainer.addChild(new Label("Kosten:", new ElementId("label-Text"))); // Label for cost
downContainer.addChild(new Label("Hier die tatsächlichen Kosten", new ElementId("label-Text"))); // Cost details
downContainer.addChild(cost); // Cost details
downContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f)));
// Cancel button
@ -92,11 +96,9 @@ public class BuyHouse extends Dialog {
confirmButton.setFontSize(32);
confirmButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
if (selectedProperty != null) {
System.out.println("Confirmed property: " + selectedProperty.getName());
// Send the "alter building" message to the server
// app.getGameLogic().sendMessage(new AlterBuildingMessage(selectedProperty.getId(), false)); // TODO
}
AlterProperty msg = new AlterProperty("BuyHouse");
msg.setProperties(selectedProperties.stream().map(p -> app.getGameLogic().getBoardManager().getFieldByName(p).getId()).map(p -> (Integer) p).collect(Collectors.toSet()));
app.getGameLogic().send(msg);
}));
// Center the popup
@ -130,22 +132,20 @@ public class BuyHouse extends Dialog {
// Populate the dropdown with property names
for (BuildingProperty property : playerProperties) {
propertyOptions.add(property.getName() + " (ID: " + property.getId() + ")");
propertyOptions.add(property.getName());
}
propertySelector = new Selector<>(propertyOptions, "glass");
dropdownContainer.setBackground(new QuadBackgroundComponent(ColorRGBA.Orange));
dropdownContainer.addChild(propertySelector);
// Set initial selection
if (!propertyOptions.isEmpty()) {
onDropdownSelectionChanged(propertyOptions.get(0), playerProperties);
}
// 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;
}
@ -156,36 +156,43 @@ public class BuyHouse extends Dialog {
* @return List of BuildingProperty objects owned by the player.
*/
private List<BuildingProperty> getPlayerProperties() {
Player currentPlayer = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId());
Player self = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId());
BoardManager boardManager = app.getGameLogic().getBoardManager();
return boardManager.getPropertyFields(currentPlayer.getProperties()).stream()
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(String selected, List<BuildingProperty> playerProperties) {
private void onDropdownSelectionChanged(Selector<String> playerProperties) {
String selected = playerProperties.getSelectedItem();
app.getGameLogic().playSound(Sound.BUTTON);
// Extract the ID from the selected string
int idStart = selected.indexOf("(ID: ") + 5;
int idEnd = selected.indexOf(")", idStart);
String idStr = selected.substring(idStart, idEnd);
int propertyId = Integer.parseInt(idStr);
// Find the selected property
selectedProperty = playerProperties.stream()
.filter(property -> property.getId() == propertyId)
.findFirst()
.orElse(null);
if (selectedProperty != null) {
System.out.println("Selected property: " + selectedProperty.getName());
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+"");
}
@Override