rudimentary function for trade

This commit is contained in:
Johannes Schmelz
2024-11-29 03:07:17 +01:00
parent 333f27a016
commit 94ba9f0af2
10 changed files with 271 additions and 411 deletions

View File

@@ -23,6 +23,7 @@ import com.simsilica.lemur.style.ElementId;
import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player;
import pp.monopoly.message.client.ViewAssetsRequest;
import pp.monopoly.model.TradeHandler;
import pp.monopoly.notification.Sound;
@@ -48,6 +49,7 @@ public class ChoosePartner extends Dialog {
super(app.getDialogManager());
this.app = app;
tradeHandler = new TradeHandler(app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()));
app.getGameLogic().send(new ViewAssetsRequest());
// Background Image
addBackgroundImage();
@@ -140,7 +142,7 @@ public class ChoosePartner extends Dialog {
confirmButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
close();
new TradeMenu(app).open();
new TradeMenu(app, tradeHandler).open();
}));
lowerRightMenu.addChild(confirmButton);

View File

@@ -13,15 +13,12 @@ import com.simsilica.lemur.style.ElementId;
import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.model.fields.BuildingProperty;
import pp.monopoly.model.fields.Field;
import pp.monopoly.model.fields.FoodField;
import pp.monopoly.model.fields.GateField;
import pp.monopoly.model.fields.PropertyField;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@@ -43,9 +40,6 @@ public class PropertyOverviewMenu extends Dialog {
super(app.getDialogManager());
this.app = app;
// Assign 10 random buildings to the player
//TODO Aufruf für Demo löschen
assignRandomPropertiesToPlayer();
// Make the menu fullscreen
Vector3f screenSize = new Vector3f(app.getCamera().getWidth(), app.getCamera().getHeight(), 0);
@@ -102,7 +96,7 @@ public class PropertyOverviewMenu extends Dialog {
Player currentPlayer = app.getGameLogic().getPlayerHandler().getPlayers().get(0);
// Iterate through the player's properties
for (PropertyField property : currentPlayer.getProperties()) {
for (PropertyField property : currentPlayer.getPropertyFields()) {
if (property instanceof BuildingProperty) {
BuildingProperty building = (BuildingProperty) property;
cards.add(createBuildingCard(building));
@@ -247,27 +241,4 @@ public class PropertyOverviewMenu extends Dialog {
app.getGuiNode().detachChild(mainContainer);
super.close();
}
//TODO Demo Funktion nach implementierung löschen
private void assignRandomPropertiesToPlayer() {
// Get the board from the BoardManager
BoardManager boardManager = app.getGameLogic().getBoardManager();
List<Field> fields = boardManager.getBoard();
// Filter for BuildingProperty, FoodField, and GateField
List<PropertyField> propertyFields = new ArrayList<>();
for (Field field : fields) {
if (field instanceof PropertyField) {
propertyFields.add((PropertyField) field);
}
}
// Shuffle and select up to 10 properties (mix of BuildingProperty, FoodField, GateField)
Collections.shuffle(propertyFields);
List<PropertyField> randomProperties = propertyFields.subList(0, Math.min(10, propertyFields.size()));
// Assign the properties to the current player
Player currentPlayer = app.getGameLogic().getPlayerHandler().getPlayers().get(0);
currentPlayer.getProperties().clear();
currentPlayer.getProperties().addAll(randomProperties);
}
}

View File

@@ -1,7 +1,8 @@
package pp.monopoly.client.gui;
import static java.lang.Math.min;
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;
@@ -12,55 +13,218 @@ 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 com.simsilica.lemur.text.DocumentModel;
import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.model.TradeHandler;
import pp.monopoly.model.fields.PropertyField;
import pp.monopoly.notification.Sound;
import java.util.Set;
public class TradeMenu extends Dialog {
private final MonopolyApp app;
private final TradeHandler tradeHandler;
private final Container mainContainer;
private Selector<String> leftBuildingSelector, leftCurrencySelector, leftSpecialCardSelector;
private Selector<String> rightBuildingSelector, rightCurrencySelector, rightSpecialCardSelector;
private final Button cancelButton = new Button("Abbrechen");
private final Button tradeButton = new Button("Handeln");
private Container lowerLeftMenu, lowerRightMenu;
private TextField leftSelectionsField;
private TextField rightSelectionsField;
private TextField leftCurrencyInput;
private TextField rightCurrencyInput;
private Selector<String> leftBuildingSelector, leftSpecialCardSelector;
private Selector<String> rightBuildingSelector, rightSpecialCardSelector;
private TextField leftSelectionsField, rightSelectionsField;
private TextField leftCurrencyInput, rightCurrencyInput;
QuadBackgroundComponent translucentWhiteBackground =
new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White));
private static final ColorRGBA TRANSLUCENT_WHITE = new ColorRGBA(1, 1, 1, 0.5f);
/**
* Constructs the TradeMenu dialog.
*
* @param app The Monopoly application instance.
*/
public TradeMenu(MonopolyApp app) {
public TradeMenu(MonopolyApp app, TradeHandler tradeHandler) {
super(app.getDialogManager());
this.app = app;
this.tradeHandler = tradeHandler;
// Background Image
addBackgroundImage();
// Main container for the UI components
mainContainer = new Container(new SpringGridLayout(Axis.Y, Axis.X));
mainContainer.setPreferredSize(new Vector3f(1200, 800, 0));
mainContainer.setBackground(translucentWhiteBackground);
// Add header container
mainContainer.addChild(createHeaderContainer());
// Add main content (three columns: left, middle, right)
mainContainer.addChild(createMainContent());
// Attach main container to GUI node
mainContainer = createMainContainer();
app.getGuiNode().attachChild(mainContainer);
positionMainContainer();
}
private Container createMainContainer() {
Container container = new Container(new SpringGridLayout(Axis.Y, Axis.X));
container.setPreferredSize(new Vector3f(1200, 800, 0));
container.setBackground(new QuadBackgroundComponent(TRANSLUCENT_WHITE));
container.addChild(createHeader());
container.addChild(createMainContent());
return container;
}
private Label createHeader() {
Label header = new Label("Handelsmenü", new ElementId("label-Bold"));
header.setFontSize(50);
header.setInsets(new Insets3f(10, 10, 10, 10));
header.setBackground(new QuadBackgroundComponent(TRANSLUCENT_WHITE));
return header;
}
private Container createMainContent() {
Container mainContent = new Container(new SpringGridLayout(Axis.X, Axis.Y));
mainContent.setPreferredSize(new Vector3f(1200, 700, 0));
mainContent.addChild(createTradeColumn("Wähle Handelsobjekt:", true));
mainContent.addChild(createMiddleSection());
mainContent.addChild(createTradeColumn("Wähle Zielobjekt:", false));
Container buttons = mainContent.addChild(new Container(new SpringGridLayout()));
Button cancel = new Button("Abbrechen");
cancel.addClickCommands(s-> ifTopDialog( () -> {
close();
app.getGameLogic().playSound(Sound.BUTTON);
}));
Button trade = new Button("Handeln");
trade.addClickCommands(s-> ifTopDialog( () -> {
close();
app.getGameLogic().playSound(Sound.BUTTON);
}));
buttons.addChild(cancel);
buttons.addChild(trade);
return mainContent;
}
private Container createTradeColumn(String label, boolean isLeft) {
Container column = new Container(new SpringGridLayout(Axis.Y, Axis.X));
column.setBackground(new QuadBackgroundComponent(ColorRGBA.White));
Label columnLabel = column.addChild(new Label(label));
columnLabel.setFontSize(24);
columnLabel.setBackground(new QuadBackgroundComponent(TRANSLUCENT_WHITE));
column.addChild(new Label("Gebäude:"));
Selector<String> buildingSelector = createPropertySelector(isLeft);
column.addChild(buildingSelector);
column.addChild(new Label("Währung:"));
TextField currencyInput = createCurrencyInput();
column.addChild(currencyInput);
column.addChild(new Label("Sonderkarten:"));
Selector<String> specialCardSelector = createSpecialCardSelector();
styleSelector(specialCardSelector);
column.addChild(specialCardSelector);
assignSelectors(buildingSelector, specialCardSelector, currencyInput, isLeft);
return column;
}
private Selector<String> createPropertySelector(boolean isLeft) {
VersionedList<String> properties = new VersionedList<>();
for (PropertyField field : getPropertyFields(isLeft)) {
properties.add(field.getName());
}
Selector<String> selector = new Selector<>(properties, "glass");
styleSelector(selector);
return selector;
}
private Selector<String> createSpecialCardSelector() {
VersionedList<String> numbers = new VersionedList<>();
for (int i = 0; i <= app.getGameLogic().getPlayerHandler().getPlayerById(tradeHandler.getReceiver().getId()).getNumJailCard(); i++) {
numbers.add(i+"");
}
Selector<String> selector = new Selector<>(numbers, "glass");
styleSelector(selector);
return selector;
}
private Iterable<PropertyField> getPropertyFields(boolean isLeft) {
return app.getGameLogic()
.getBoardManager()
.getPropertyFields(app.getGameLogic()
.getPlayerHandler()
.getPlayerById(isLeft ? tradeHandler.getReceiver().getId() : tradeHandler.getSender().getId())
.getProperties());
}
private TextField createCurrencyInput() {
TextField currencyInput = new TextField("");
styleTextField(currencyInput);
return currencyInput;
}
/**
* Creates the middle section with selection fields and arrows.
*
* @return The middle section container.
*/
private Container createMiddleSection() {
Container middleSection = new Container(new SpringGridLayout(Axis.Y, Axis.X));
middleSection.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.8f, 0.8f, 0.8f, 1.0f)));
// Add combined label for the top
Label middleLabelTop = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabelTop.setFontSize(24);
middleLabelTop.setInsets(new Insets3f(5, 5, 5, 5));
// Add the left selection field (Quellobjekte)
leftSelectionsField = middleSection.addChild(new TextField(""));
leftSelectionsField.setPreferredSize(new Vector3f(600, 50, 0));
// Add arrows
Label arrows = middleSection.addChild(new Label(""));
arrows.setFontSize(40);
// Add the right selection field (Zielobjekte)
rightSelectionsField = middleSection.addChild(new TextField(""));
rightSelectionsField.setPreferredSize(new Vector3f(600, 50, 0));
// Add combined label for the bottom
Label middleLabelBottom = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabelBottom.setFontSize(24);
middleLabelBottom.setInsets(new Insets3f(5, 5, 5, 5));
// Spacer
Label spacer = middleSection.addChild(new Label(""));
spacer.setPreferredSize(new Vector3f(1, 50, 0));
return middleSection;
}
private TextField createSelectionsField(Container parent, boolean isLeft) {
TextField field = new TextField("");
field.setPreferredSize(new Vector3f(600, 50, 0));
parent.addChild(field);
return field;
}
private Label createArrowLabel() {
Label arrowLabel = new Label("");
arrowLabel.setFontSize(40);
return arrowLabel;
}
private void styleSelector(Selector<String> selector) {
selector.setInsets(new Insets3f(5, 10, 5, 10));
selector.setBackground(new QuadBackgroundComponent(ColorRGBA.Black));
}
private void styleTextField(TextField textField) {
textField.setInsets(new Insets3f(5, 10, 5, 10));
textField.setBackground(new QuadBackgroundComponent(ColorRGBA.Black));
}
private void assignSelectors(Selector<String> buildingSelector, Selector<String> specialCardSelector, TextField currencyInput, boolean isLeft) {
if (isLeft) {
leftBuildingSelector = buildingSelector;
leftSpecialCardSelector = specialCardSelector;
leftCurrencyInput = currencyInput;
} else {
rightBuildingSelector = buildingSelector;
rightSpecialCardSelector = specialCardSelector;
rightCurrencyInput = currencyInput;
}
}
private void positionMainContainer() {
mainContainer.setLocalTranslation(
(app.getCamera().getWidth() - mainContainer.getPreferredSize().x) / 2,
(app.getCamera().getHeight() + mainContainer.getPreferredSize().y) / 2,
@@ -68,183 +232,6 @@ public class TradeMenu extends Dialog {
);
}
/**
* Creates a container for the header with a fixed size.
*
* @return The header container.
*/
private Container createHeaderContainer() {
// Create a container for the header
Container headerContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y));
headerContainer.setPreferredSize(new Vector3f(200, 100, 0)); // Set fixed width and height
// Add the header label
Label headerLabel = headerContainer.addChild(new Label("Handelsmenü", new ElementId("label-Bold")));
headerLabel.setFontSize(50); // Adjust font size as needed
headerLabel.setInsets(new Insets3f(10, 10, 10, 10)); // Add padding around the label
headerLabel.setBackground(new QuadBackgroundComponent(new ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f))); // Optional background
return headerContainer;
}
/**
* Creates the main content layout (left, middle, right columns).
*
* @return The main content container.
*/
private Container createMainContent() {
Container mainContent = new Container(new SpringGridLayout(Axis.X, Axis.Y));
mainContent.setPreferredSize(new Vector3f(1200, 700, 0));
// Left Column
mainContent.addChild(createTradeColumn("Wähle Handelsobjekt:", true));
// Middle Section
Container middleSection = mainContent.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X)));
middleSection.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.8f, 0.8f, 0.8f, 1.0f)));
// Add combined label
Label middleLabel = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabel.setFontSize(24); // Adjust font size as needed
middleLabel.setInsets(new Insets3f(5, 5, 5, 5)); // Add padding around the label
// Add the Quellobjekte TextField
leftSelectionsField = middleSection.addChild(new TextField(""));
leftSelectionsField.setPreferredSize(new Vector3f(600, 50, 0)); // Larger width to fit the split sections
addCustomSelectionListener(leftBuildingSelector, newSelection -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
addCustomSelectionListener(leftSpecialCardSelector, newSelection -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
// Add change listener for the currency input
monitorTextFieldChanges(leftCurrencyInput, () -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
Label arrows = middleSection.addChild(new Label(""));
arrows.setFontSize(40);
// Right Column
mainContent.addChild(createTradeColumn("Wähle Zielobjekt:", false));
// Add combined label
middleLabel = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabel.setFontSize(24); // Adjust font size as needed
middleLabel.setInsets(new Insets3f(5, 5, 5, 5)); // Add padding around the label
// Add the Zielobjekte TextField
rightSelectionsField = middleSection.addChild(new TextField(""));
rightSelectionsField.setPreferredSize(new Vector3f(600, 50, 0));
addCustomSelectionListener(rightBuildingSelector, newSelection -> updateSelectionsField(rightSelectionsField, rightBuildingSelector, rightCurrencyInput, rightSpecialCardSelector));
addCustomSelectionListener(rightSpecialCardSelector, newSelection -> updateSelectionsField(rightSelectionsField, rightBuildingSelector, rightCurrencyInput, rightSpecialCardSelector));
// Add change listener for the currency input
monitorTextFieldChanges(rightCurrencyInput, () -> updateSelectionsField(rightSelectionsField, rightBuildingSelector, rightCurrencyInput, rightSpecialCardSelector));
// "Bestätigen" button
lowerRightMenu = new Container();
tradeButton.setPreferredSize(new Vector3f(200, 60, 0));
tradeButton.setFontSize(30);
tradeButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
close();
new TradeMenu(app).open();
}));
lowerRightMenu.addChild(tradeButton);
// Position the container near the bottom-right corner
lowerRightMenu.setLocalTranslation(new Vector3f(app.getCamera().getWidth() - 680, 100, 8)); // X: 220px from the right, Y: 50px above the bottom
app.getGuiNode().attachChild(lowerRightMenu);
Label spacer = middleSection.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 50, 0));
return mainContent;
}
/**
* Creates a column for trade objects (left or right side).
*
* @param label The label for the column.
* @param isLeft Whether this column is the left side.
* @return The column container.
*/
private Container createTradeColumn(String label, boolean isLeft) {
Container column = new Container(new SpringGridLayout(Axis.Y, Axis.X));
column.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White)));
Label columnLabel = column.addChild(new Label(label));
columnLabel.setFontSize(24);
columnLabel.setBackground(translucentWhiteBackground);
// Add dropdowns
column.addChild(new Label("Gebäude:"));
Selector<String> buildingSelector = column.addChild(new Selector<>(getSampleItems(),"glass"));
buildingSelector.setInsets(new Insets3f(5, 10, 5, 10));
buildingSelector.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
column.addChild(new Label("Währung:"));
TextField currencyInput = column.addChild(new TextField(""));
currencyInput.setInsets(new Insets3f(5, 10, 5, 10));
currencyInput.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
column.addChild(new Label("Sonderkarten:"));
Selector<String> specialCardSelector = column.addChild(new Selector<>(getSampleItems(),"glass"));
specialCardSelector.setInsets(new Insets3f(5, 10, 5, 10));
specialCardSelector.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
// Assign selectors to corresponding fields
if (isLeft) {
leftBuildingSelector = buildingSelector;
leftSpecialCardSelector = specialCardSelector;
leftCurrencyInput = currencyInput;
// "Abbrechen" button
lowerLeftMenu = new Container();
cancelButton.setPreferredSize(new Vector3f(200, 60, 0));
cancelButton.setFontSize(30);
cancelButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
this.close();
}));
lowerLeftMenu.addChild(cancelButton);
// Position the container near the bottom-left corner
lowerLeftMenu.setLocalTranslation(new Vector3f(50, 100, 8)); // Adjust X and Y to align with the bottom-left corner
app.getGuiNode().attachChild(lowerLeftMenu);
Label spacer = column.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 130, 0));
} else {
rightBuildingSelector = buildingSelector;
rightSpecialCardSelector = specialCardSelector;
rightCurrencyInput = currencyInput;
Label spacer = column.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 130, 0));
}
return column;
}
/**
* Provides sample dropdown items.
*/
private VersionedList<String> getSampleItems() {
VersionedList<String> items = new VersionedList<>();
items.add("Option 1");
items.add("Option 2");
items.add("Option 3");
return items;
}
/**
* Adds a background image to the dialog.
*/
private void addBackgroundImage() {
Texture backgroundImage = app.getAssetManager().loadTexture("Pictures/unibw-Bib2.png");
Quad quad = new Quad(app.getCamera().getWidth(), app.getCamera().getHeight());
@@ -252,131 +239,18 @@ public class TradeMenu extends Dialog {
Material backgroundMaterial = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
backgroundMaterial.setTexture("ColorMap", backgroundImage);
background.setMaterial(backgroundMaterial);
background.setLocalTranslation(0, 0, 6); // Position behind other GUI elements
background.setLocalTranslation(0, 0, 6);
app.getGuiNode().attachChild(background);
}
private String truncateText(String text, int maxLength) {
return text.length() > maxLength ? text.substring(0, maxLength) + "..." : text;
}
private void updateSelectionsField(TextField selectionsField, Selector<String> buildingSelector, TextField currencyInput, Selector<String> specialCardSelector) {
// Get selections from the building selector
String buildingSelections = buildingSelector != null && buildingSelector.getSelectedItem() != null
? buildingSelector.getSelectedItem().trim()
: "";
// Get text from the currency input
String currencySelections = currencyInput != null
? currencyInput.getText().trim()
: "";
// Get selections from the special card selector
String specialCardSelections = specialCardSelector != null && specialCardSelector.getSelectedItem() != null
? specialCardSelector.getSelectedItem().trim()
: "";
// Build the combined text without adding unnecessary spaces
StringBuilder combinedText = new StringBuilder();
if (!buildingSelections.isEmpty()) {
combinedText.append(buildingSelections);
}
if (!currencySelections.isEmpty()) {
if (combinedText.length() > 0) {
combinedText.append(" | "); // Add a separator if there's already text
}
combinedText.append(currencySelections);
}
if (!specialCardSelections.isEmpty()) {
if (combinedText.length() > 0) {
combinedText.append(" | "); // Add a separator if there's already text
}
combinedText.append(specialCardSelections);
}
// Update the content of the TextField
selectionsField.setText(combinedText.toString());
}
/**
* Handles the escape action for the dialog.
*/
@Override
public void escape() {
new SettingsMenu(app).open();
}
/**
* Updates the dialog periodically, called by the dialog manager.
*
* @param delta The time elapsed since the last update.
*/
@Override
public void update(float delta) {
// Periodic updates (if needed) can be implemented here
public void close() {
app.getGuiNode().detachChild(mainContainer);
super.close();
}
/**
* Functional interface for a selection action listener.
*/
@FunctionalInterface
private interface SelectionActionListener<T> {
void onSelectionChanged(T selection);
}
/**
* Adds a custom action listener to the Selector.
*/
private void addCustomSelectionListener(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 = selector.getSelectedItem();
listener.onSelectionChanged(selected);
}
}
@Override
protected void initialize(Application app) {}
@Override
protected void cleanup(Application app) {}
@Override
protected void onEnable() {}
@Override
protected void onDisable() {}
});
}
private void monitorTextFieldChanges(TextField textField, Runnable onChange) {
VersionedReference<DocumentModel> ref = textField.getDocumentModel().createReference();
app.getStateManager().attach(new BaseAppState() {
@Override
public void update(float tpf) {
if (ref.update()) {
onChange.run();
}
}
@Override
protected void initialize(Application app) {}
@Override
protected void cleanup(Application app) {}
@Override
protected void onEnable() {}
@Override
protected void onDisable() {}
});
}
}