fixed trade display

This commit is contained in:
Yvonne Schmidt 2024-11-26 22:24:03 +01:00
parent 156e76fe0a
commit a6054e825e

View File

@ -30,6 +30,10 @@ public class TradeMenu extends Dialog {
private final Button tradeButton = new Button("Handeln"); private final Button tradeButton = new Button("Handeln");
private Container lowerLeftMenu, lowerRightMenu; private Container lowerLeftMenu, lowerRightMenu;
private TextField leftSelectionsField; private TextField leftSelectionsField;
private TextField rightSelectionsField;
private TextField leftCurrencyInput;
private TextField rightCurrencyInput;
QuadBackgroundComponent translucentWhiteBackground = QuadBackgroundComponent translucentWhiteBackground =
new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White)); new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.White));
@ -63,7 +67,7 @@ public class TradeMenu extends Dialog {
7 7
); );
} }
//TODO Logik
/** /**
* Creates a container for the header with a fixed size. * Creates a container for the header with a fixed size.
@ -109,24 +113,34 @@ public class TradeMenu extends Dialog {
leftSelectionsField = middleSection.addChild(new TextField("")); leftSelectionsField = middleSection.addChild(new TextField(""));
leftSelectionsField.setPreferredSize(new Vector3f(600, 50, 0)); // Larger width to fit the split sections leftSelectionsField.setPreferredSize(new Vector3f(600, 50, 0)); // Larger width to fit the split sections
// Add listeners to update the TextField dynamically addCustomSelectionListener(leftBuildingSelector, newSelection -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
addCustomSelectionListener(leftBuildingSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField)); addCustomSelectionListener(leftSpecialCardSelector, newSelection -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
addCustomSelectionListener(leftSpecialCardSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField)); // Add change listener for the currency input
monitorTextFieldChanges(leftCurrencyInput, () -> updateSelectionsField(leftSelectionsField, leftBuildingSelector, leftCurrencyInput, leftSpecialCardSelector));
Label arrows = middleSection.addChild(new Label("")); Label arrows = middleSection.addChild(new Label(""));
arrows.setFontSize(40); arrows.setFontSize(40);
// Right Column
mainContent.addChild(createTradeColumn("Wähle Zielobjekt:", false));
// Add combined label // Add combined label
middleLabel = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:")); middleLabel = middleSection.addChild(new Label("Gebäude: Währung: Sonderkarten:"));
middleLabel.setFontSize(24); // Adjust font size as needed middleLabel.setFontSize(24); // Adjust font size as needed
middleLabel.setInsets(new Insets3f(5, 5, 5, 5)); // Add padding around the label middleLabel.setInsets(new Insets3f(5, 5, 5, 5)); // Add padding around the label
TextField rightSelectionsField = middleSection.addChild(new TextField("Zielobjekte...")); // 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 // "Bestätigen" button
lowerRightMenu = new Container(); lowerRightMenu = new Container();
tradeButton.setPreferredSize(new Vector3f(200, 60, 0)); tradeButton.setPreferredSize(new Vector3f(200, 60, 0));
@ -144,9 +158,6 @@ public class TradeMenu extends Dialog {
// Right Column
mainContent.addChild(createTradeColumn("Wähle Zielobjekt:", false));
Label spacer = middleSection.addChild(new Label("")); // Spacer Label spacer = middleSection.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 50, 0)); spacer.setPreferredSize(new Vector3f(1, 50, 0));
@ -189,10 +200,7 @@ public class TradeMenu extends Dialog {
if (isLeft) { if (isLeft) {
leftBuildingSelector = buildingSelector; leftBuildingSelector = buildingSelector;
leftSpecialCardSelector = specialCardSelector; leftSpecialCardSelector = specialCardSelector;
leftCurrencyInput = currencyInput;
// Add listeners for left selectors to update the middle text field
addCustomSelectionListener(buildingSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField));
addCustomSelectionListener(specialCardSelector, newSelection -> updateLeftSelectionsField(leftSelectionsField));
// "Abbrechen" button // "Abbrechen" button
lowerLeftMenu = new Container(); lowerLeftMenu = new Container();
@ -214,6 +222,7 @@ public class TradeMenu extends Dialog {
} else { } else {
rightBuildingSelector = buildingSelector; rightBuildingSelector = buildingSelector;
rightSpecialCardSelector = specialCardSelector; rightSpecialCardSelector = specialCardSelector;
rightCurrencyInput = currencyInput;
Label spacer = column.addChild(new Label("")); // Spacer Label spacer = column.addChild(new Label("")); // Spacer
spacer.setPreferredSize(new Vector3f(1, 130, 0)); spacer.setPreferredSize(new Vector3f(1, 130, 0));
@ -247,26 +256,50 @@ public class TradeMenu extends Dialog {
app.getGuiNode().attachChild(background); app.getGuiNode().attachChild(background);
} }
private void updateLeftSelectionsField(TextField leftSelectionsField) { private String truncateText(String text, int maxLength) {
// Get all selections from the leftBuildingSelector and leftSpecialCardSelector return text.length() > maxLength ? text.substring(0, maxLength) + "..." : text;
String buildingSelections = leftBuildingSelector.getSelectedItem() != null }
? leftBuildingSelector.getSelectedItem()
: ""; private void updateSelectionsField(TextField selectionsField, Selector<String> buildingSelector, TextField currencyInput, Selector<String> specialCardSelector) {
String specialCardSelections = leftSpecialCardSelector.getSelectedItem() != null // Get selections from the building selector
? leftSpecialCardSelector.getSelectedItem() String buildingSelections = buildingSelector != null && buildingSelector.getSelectedItem() != null
? buildingSelector.getSelectedItem().trim()
: ""; : "";
// Get the direct input from the middle part of the TextField // Get text from the currency input
String manualInput = leftSelectionsField.getText(); String currencySelections = currencyInput != null
? currencyInput.getText().trim()
: "";
// Combine all parts into one formatted string // Get selections from the special card selector
String combinedText = String.format("%-30s %-30s %-30s", buildingSelections, manualInput, specialCardSelections); 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 // Update the content of the TextField
leftSelectionsField.setText(combinedText); selectionsField.setText(combinedText.toString());
} }
/** /**
* Handles the escape action for the dialog. * Handles the escape action for the dialog.
*/ */
@ -321,4 +354,29 @@ public class TradeMenu extends Dialog {
}); });
} }
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() {}
});
}
} }