Compare commits

...

5 Commits

Author SHA1 Message Date
Johannes Schmelz
e56cdb1dcb Merge branch 'gui' of https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02 into gui 2024-11-26 20:00:54 +01:00
Johannes Schmelz
9107a08011 dynamically set players in ChoosePatner 2024-11-26 20:00:50 +01:00
Yvonne Schmidt
0242587a5f added groundwork for selection listeners 2024-11-26 19:54:02 +01:00
Yvonne Schmidt
3ce27023c2 Merge remote-tracking branch 'origin/gui' into gui 2024-11-26 19:42:01 +01:00
Yvonne Schmidt
ada067a2a0 added and centered buttons 2024-11-26 18:25:23 +01:00
3 changed files with 294 additions and 28 deletions

View File

@ -1,5 +1,9 @@
package pp.monopoly.client.gui;
import java.util.Set;
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;
@ -14,9 +18,12 @@ 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;
import pp.monopoly.game.server.Player;
import pp.monopoly.model.TradeHandler;
import pp.monopoly.notification.Sound;
public class ChoosePartner extends Dialog {
@ -28,6 +35,7 @@ public class ChoosePartner extends Dialog {
private Container lowerLeftMenu;
private Container lowerRightMenu;
private Geometry background;
private TradeHandler tradeHandler;
QuadBackgroundComponent translucentWhiteBackground =
new QuadBackgroundComponent(new ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f));
@ -39,7 +47,7 @@ public class ChoosePartner extends Dialog {
public ChoosePartner(MonopolyApp app) {
super(app.getDialogManager());
this.app = app;
tradeHandler = new TradeHandler(app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()));
// Background Image
addBackgroundImage();
@ -60,6 +68,8 @@ public class ChoosePartner extends Dialog {
// Add buttons
mainContainer.addChild(createButtonContainer());
addSelectionActionListener(playerSelector, this::onDropdownSelectionChanged);
// Attach main container to GUI node
app.getGuiNode().attachChild(mainContainer);
mainContainer.setLocalTranslation(
@ -80,10 +90,12 @@ public class ChoosePartner extends Dialog {
dropdownContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
VersionedList<String> playerOptions = new VersionedList<>();
playerOptions.add("Spieler 1");
playerOptions.add("Spieler 2");
playerOptions.add("Spieler 3");
playerOptions.add("Spieler 4");
for (Player player : app.getGameLogic().getPlayerHandler().getPlayers()) {
if (player.getId() != app.getId()) {
playerOptions.add(player.getName() + " (ID: "+player.getId()+")");
}
}
playerSelector = new Selector<>(playerOptions, "glass");
dropdownContainer.addChild(playerSelector);
@ -126,8 +138,6 @@ public class ChoosePartner extends Dialog {
confirmButton.setFontSize(30);
confirmButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
String selectedPlayer = playerSelector.getSelectedItem();
System.out.println("Selected player: " + selectedPlayer);
close();
new TradeMenu(app).open();
}));
@ -182,6 +192,72 @@ public class ChoosePartner extends Dialog {
app.getGuiNode().detachChild(background);
super.close();
}
/**
* 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);
app.getGameLogic().playSound(Sound.BUTTON);
int idStart = selected.indexOf("(ID: ") + 5; // Find start of the ID
int idEnd = selected.indexOf(")", idStart); // Find end of the ID
String idStr = selected.substring(idStart, idEnd); // Extract the ID as a string
int playerId = Integer.parseInt(idStr); // Convert the ID to an integer
// Find the player by ID
Player selectedPlayer = app.getGameLogic().getPlayerHandler().getPlayerById(playerId);
if (selectedPlayer != null) {
tradeHandler.setReceiver(selectedPlayer); // Set the receiver in TradeHandler
System.out.println("Receiver set to: " + selectedPlayer.getName() + " (ID: " + selectedPlayer.getId() + ")");
} else {
System.err.println("Player with ID " + playerId + " not found.");
}
}
/**
* Functional interface for a selection action listener.
*/
@FunctionalInterface
private interface SelectionActionListener<T> {
void onSelectionChanged(T selection);
}
}

View File

@ -1,5 +1,7 @@
package pp.monopoly.client.gui;
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;
@ -10,11 +12,15 @@ 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.notification.Sound;
import java.util.Set;
public class TradeMenu extends Dialog {
private final MonopolyApp app;
private final Container mainContainer;
@ -23,6 +29,7 @@ public class TradeMenu extends Dialog {
private final Button cancelButton = new Button("Abbrechen");
private final Button tradeButton = new Button("Handeln");
private Container lowerLeftMenu, lowerRightMenu;
private TextField leftSelectionsField;
QuadBackgroundComponent translucentWhiteBackground =
new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White));
@ -43,12 +50,8 @@ public class TradeMenu extends Dialog {
mainContainer.setPreferredSize(new Vector3f(1200, 800, 0));
mainContainer.setBackground(translucentWhiteBackground);
// Add title with background
Label headerLabel = mainContainer.addChild(new Label("Handelsmenu", new ElementId("label-Bold")));
headerLabel.setFontSize(40);
headerLabel.setBackground(new QuadBackgroundComponent(new ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f)));
// Add header container
mainContainer.addChild(createHeaderContainer());
// Add main content (three columns: left, middle, right)
mainContainer.addChild(createMainContent());
@ -61,8 +64,25 @@ public class TradeMenu extends Dialog {
);
}
//TODO Logik
//TODO Ebenen prüfen und eine Ebene Hochsetzen
//TODO Farben der Label anpassen
/**
* 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).
@ -71,6 +91,7 @@ public class TradeMenu extends Dialog {
*/
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));
@ -79,18 +100,56 @@ public class TradeMenu extends Dialog {
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)));
TextField leftSelectionsField = middleSection.addChild(new TextField("Quellobjekte..."));
// 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
// Add listeners to update the TextField dynamically
addCustomSelectionListener(leftBuildingSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField));
addCustomSelectionListener(leftSpecialCardSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField));
Label arrows = middleSection.addChild(new Label(""));
arrows.setFontSize(40);
// 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
TextField rightSelectionsField = middleSection.addChild(new TextField("Zielobjekte..."));
// "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);
// Right Column
mainContent.addChild(createTradeColumn("Wähle Zielobjekt:", false));
Label spacer = middleSection.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 50, 0));
return mainContent;
}
@ -103,7 +162,7 @@ public class TradeMenu extends Dialog {
*/
private Container createTradeColumn(String label, boolean isLeft) {
Container column = new Container(new SpringGridLayout(Axis.Y, Axis.X));
column.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
column.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White)));
Label columnLabel = column.addChild(new Label(label));
columnLabel.setFontSize(24);
@ -112,22 +171,52 @@ public class TradeMenu extends Dialog {
// 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:"));
Selector<String> currencySelector = column.addChild(new Selector<>(getSampleItems(),"glass"));
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;
leftCurrencySelector = currencySelector;
leftSpecialCardSelector = specialCardSelector;
} else {
// Add listeners for left selectors to update the middle text field
addCustomSelectionListener(buildingSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField));
addCustomSelectionListener(specialCardSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField));
// "Abbrechen" button
lowerLeftMenu = new Container();
cancelButton.setPreferredSize(new Vector3f(200, 60, 0));
cancelButton.setFontSize(30);
cancelButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON);
close();
}));
lowerLeftMenu.addChild(cancelButton);
// Position the container near the bottom-left corner
lowerLeftMenu.setLocalTranslation(new Vector3f(70, 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;
rightCurrencySelector = currencySelector;
rightSpecialCardSelector = specialCardSelector;
Label spacer = column.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 130, 0));
}
return column;
@ -158,6 +247,26 @@ public class TradeMenu extends Dialog {
app.getGuiNode().attachChild(background);
}
private void updateLeftSelectionsField(TextField leftSelectionsField) {
// Get all selections from the leftBuildingSelector and leftSpecialCardSelector
String buildingSelections = leftBuildingSelector.getSelectedItem() != null
? leftBuildingSelector.getSelectedItem()
: "";
String specialCardSelections = leftSpecialCardSelector.getSelectedItem() != null
? leftSpecialCardSelector.getSelectedItem()
: "";
// Get the direct input from the middle part of the TextField
String manualInput = leftSelectionsField.getText();
// Combine all parts into one formatted string
String combinedText = String.format("%-30s %-30s %-30s", buildingSelections, manualInput, specialCardSelections);
// Update the content of the TextField
leftSelectionsField.setText(combinedText);
}
/**
* Handles the escape action for the dialog.
*/
@ -175,4 +284,41 @@ public class TradeMenu extends Dialog {
public void update(float delta) {
// Periodic updates (if needed) can be implemented here
}
/**
* 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() {}
});
}
}

View File

@ -12,13 +12,13 @@ import java.util.List;
public class TradeHandler {
private final Player sender;
private final Player receiver;
private final int offeredAmount;
private final List<PropertyField> offeredProperties;
private final int offeredJailCards;
private final int requestedAmount;
private final List<PropertyField> requestedProperties;
private final int requestedJailCards;
private Player receiver;
private int offeredAmount;
private List<PropertyField> offeredProperties;
private int offeredJailCards;
private int requestedAmount;
private List<PropertyField> requestedProperties;
private int requestedJailCards;
private Boolean status = null;
/**
@ -45,6 +45,22 @@ public class TradeHandler {
this.requestedJailCards = requestedJailCards;
}
/**
* Constructs a TradeHandler for a single trade instance.
*
* @param sender the Player initiating the trade
* @param receiver the Player receiving the trade offer
* @param offeredAmount the amount of money offered by the sender
* @param offeredProperties the properties offered by the sender
* @param offeredJailCards the jail cards offered by the sender
* @param requestedAmount the amount of money requested from the receiver
* @param requestedProperties the properties requested from the receiver
* @param requestedJailCards the jail cards requested from the receiver
*/
public TradeHandler(Player sender) {
this.sender = sender;
}
public int getOfferedAmount() {
return offeredAmount;
}
@ -209,4 +225,32 @@ public class TradeHandler {
}
System.out.println(numCards + " jail card(s) transferred from " + from.getName() + " to " + to.getName());
}
public void setOfferedAmount(int offeredAmount) {
this.offeredAmount = offeredAmount;
}
public void setOfferedJailCards(int offeredJailCards) {
this.offeredJailCards = offeredJailCards;
}
public void setOfferedProperties(List<PropertyField> offeredProperties) {
this.offeredProperties = offeredProperties;
}
public void setReceiver(Player receiver) {
this.receiver = receiver;
}
public void setRequestedAmount(int requestedAmount) {
this.requestedAmount = requestedAmount;
}
public void setRequestedJailCards(int requestedJailCards) {
this.requestedJailCards = requestedJailCards;
}
public void setRequestedProperties(List<PropertyField> requestedProperties) {
this.requestedProperties = requestedProperties;
}
}