This commit is contained in:
Johannes Schmelz 2024-12-01 21:55:10 +01:00
commit 93152f4073

View File

@ -2,12 +2,16 @@ package pp.monopoly.client.gui.popups;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
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.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;
@ -15,9 +19,11 @@ import pp.monopoly.client.gui.SettingsMenu;
import pp.monopoly.game.server.Player;
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.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
@ -27,7 +33,9 @@ public class BuyHouse extends Dialog {
private final MonopolyApp app;
private final Container buyHouseContainer;
private final Container backgroundContainer;
private VersionedReference<Set<Integer>> selectionRef;
private Selector<String> propertySelector;
private BuildingProperty selectedProperty;
public BuyHouse(MonopolyApp app) {
super(app.getDialogManager());
@ -38,100 +46,69 @@ public class BuyHouse extends Dialog {
backgroundContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.8657f, 0.8735f, 0.8892f, 1.0f))); // Darker background
attachChild(backgroundContainer);
// Hauptcontainer für das Menü
// Main container for the menu
buyHouseContainer = new Container();
buyHouseContainer.setPreferredSize(new Vector3f(800, 600, 0));
float padding = 10; // Passt den backgroundContainer an die Größe des sellhouseContainers an
float padding = 10; // Adjust backgroundContainer size to match buyHouseContainer
backgroundContainer.setPreferredSize(buyHouseContainer.getPreferredSize().addLocal(padding, padding, 0));
// Titel
Label title = buyHouseContainer.addChild(new Label( "Gebäude Kaufen", new ElementId("warining-Bold")));
// Title
Label title = buyHouseContainer.addChild(new Label("Gebäude Kaufen", new ElementId("warning-Bold")));
title.setFontSize(48);
title.setColor(ColorRGBA.Black);
//Unterteilund des sellHouseContainer in drei "Untercontainer"
// Divide buyHouseContainer into three sub-containers
Container upContainer = buyHouseContainer.addChild(new Container());
Container middleContainer = buyHouseContainer.addChild(new Container());
Container downContainer = buyHouseContainer.addChild(new Container());
// Text, der auf der Karte steht
upContainer.addChild(new Label("„Grundstück wählen:", new ElementId("label-Text"))); //TODO hier überall die entsprechenden Variablen einfügen
upContainer.addChild(new Label("", new ElementId("label-Text")));// Leerzeile
// Upper section text
upContainer.addChild(new Label("„Grundstück wählen:", new ElementId("label-Text")));
upContainer.addChild(new Label("", new ElementId("label-Text"))); // Empty line
upContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f)));
middleContainer.setPreferredSize(new Vector3f(100, 150, 0));
middleContainer.setBackground(new QuadBackgroundComponent(ColorRGBA.Orange));
// Create a VersionedList for the ListBox model
VersionedList<BuildingProperty> listModel = new VersionedList<>();
// Add the property selector dropdown
middleContainer.addChild(createPropertyDropdown());
// 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("Kosten:", new ElementId("label-Text")));// Leerzeile
downContainer.addChild(new Label("Hier die tätsächliche Erstattung", new ElementId("label-Text")));
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.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f)));
// Beenden-Button
// Cancel button
Button cancelButton = buyHouseContainer.addChild(new Button("Abbrechen", new ElementId("button")));
cancelButton.setFontSize(32);
cancelButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
close();
}));
// Kaufen-Button
// Confirm button
Button confirmButton = buyHouseContainer.addChild(new Button("Bestätigen", new ElementId("button")));
confirmButton.setFontSize(32);
confirmButton.addClickCommands(s -> ifTopDialog( () -> {
confirmButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
BuildingProperty selected = listBox.getSelectedItem();
if (selected != null) {
System.out.println("Confirmed property: " + selected.getName());
if (selectedProperty != null) {
System.out.println("Confirmed property: " + selectedProperty.getName());
// 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(selectedProperty.getId(), false)); // TODO
}
}));
// Zentriere das Popup
// Center the popup
buyHouseContainer.setLocalTranslation(
(app.getCamera().getWidth() - buyHouseContainer.getPreferredSize().x) / 2,
(app.getCamera().getHeight() + buyHouseContainer.getPreferredSize().y) / 2,
9
);
// Zentriere das Popup
backgroundContainer.setLocalTranslation(
(app.getCamera().getWidth() - buyHouseContainer.getPreferredSize().x - padding) / 2,
(app.getCamera().getHeight() + buyHouseContainer.getPreferredSize().y+ padding) / 2,
(app.getCamera().getHeight() + buyHouseContainer.getPreferredSize().y + padding) / 2,
8
);
@ -139,12 +116,82 @@ public class BuyHouse extends Dialog {
}
/**
* Schließt das Menü und entfernt die GUI-Elemente.
* 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() + " (ID: " + property.getId() + ")");
}
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();
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 currentPlayer = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId());
BoardManager boardManager = app.getGameLogic().getBoardManager();
return boardManager.getPropertyFields(currentPlayer.getProperties()).stream()
.filter(property -> property instanceof BuildingProperty)
.map(property -> (BuildingProperty) property)
.collect(Collectors.toList());
}
/**
* Handles property selection changes.
*/
private void onDropdownSelectionChanged(String selected, List<BuildingProperty> playerProperties) {
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());
}
}
@Override
public void close() {
app.getGuiNode().detachChild(buyHouseContainer); // Entferne das Menü
app.getGuiNode().detachChild(backgroundContainer); //Entfernt Rand
app.getGuiNode().detachChild(buyHouseContainer);
app.getGuiNode().detachChild(backgroundContainer);
super.close();
}