added groundwork for selection listeners

This commit is contained in:
Yvonne Schmidt 2024-11-26 19:54:02 +01:00
parent 3ce27023c2
commit 0242587a5f

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));
@ -57,9 +64,6 @@ 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.
@ -73,7 +77,7 @@ public class TradeMenu extends Dialog {
// Add the header label
Label headerLabel = headerContainer.addChild(new Label("Handelsmenü", new ElementId("label-Bold")));
headerLabel.setFontSize(32); // Adjust font size as needed
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
@ -96,12 +100,32 @@ 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();
@ -147,22 +171,29 @@ 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"));
currencySelector.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
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;
// 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));
@ -182,7 +213,6 @@ public class TradeMenu extends Dialog {
} else {
rightBuildingSelector = buildingSelector;
rightCurrencySelector = currencySelector;
rightSpecialCardSelector = specialCardSelector;
Label spacer = column.addChild(new Label("")); // Spacer
@ -217,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.
*/
@ -234,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() {}
});
}
}