ImageButtons in Toolbar

This commit is contained in:
Johannes Schmelz 2024-12-08 20:59:08 +01:00
parent e14f6bbef7
commit b3fe289fbe
3 changed files with 145 additions and 87 deletions

View File

@ -218,7 +218,6 @@ selector("button", "pp") {
insets = new Insets3f(3, 3, 3, 3) // Adjust the border thickness
textHAlignment = HAlignment.Center
textVAlignment = VAlignment.Center
buttonCommands = stdButtonCommands
}
selector("slider", "pp") {
@ -387,7 +386,7 @@ selector("button-toolbar", "pp") {
}
selector("button-toolbar2", "pp") { playerColor ->
selector("button-clear", "pp") { playerColor ->
def validColor = playerColor ?: new ColorRGBA(0, 0, 0, 0) // Vollständig transparent
def playerGradientBackground = new QuadBackgroundComponent(validColor)
@ -395,12 +394,7 @@ selector("button-toolbar2", "pp") { playerColor ->
playerGradientBackground.setColor(new ColorRGBA(0, 0, 0, 0)) // RGBA (Rot, Grün, Blau, Alpha)
background = playerGradientBackground.clone() // Setze den Hintergrund
insets = new Insets3f(3, 3, 3, 3) // Optional: Ränder
insets = new Insets3f(-3, -3, -3, -3) // Optional: Ränder
textHAlignment = HAlignment.Center // Text-Zentrierung
textVAlignment = VAlignment.Center // Text-Zentrierung
}

View File

@ -2,38 +2,92 @@ package pp.monopoly.client.gui;
import com.jme3.texture.Texture;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Command;
import com.simsilica.lemur.component.QuadBackgroundComponent;
import com.simsilica.lemur.style.ElementId;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player;
import pp.monopoly.game.server.PlayerColor;
import pp.monopoly.notification.Sound;
public class ImageButton extends Button {
private final String file;
private static MonopolyApp app;
public ImageButton( String s, String file, MonopolyApp app ) {
this(s, true, new ElementId(ELEMENT_ID), null, file, app);
}
public ImageButton( String s, String style, String file, MonopolyApp app ) {
this(s, true, new ElementId(ELEMENT_ID), style, file, app);
}
public ImageButton( String s, ElementId elementId, String file, MonopolyApp app ) {
this(s, true, elementId, null, file, app);
}
public ImageButton( String s, ElementId elementId, String style, String file, MonopolyApp app ) {
this(s, true, elementId, style, file, app);
}
protected ImageButton( String s, boolean applyStyles, ElementId elementId, String style, String file, MonopolyApp app ) {
super(s, false, elementId, style);
this.file = file;
ImageButton.app = app;
Texture backgroundImage = app.getAssetManager().loadTexture("Pictures/Buttons/"+file+".png");
setBackground(new QuadBackgroundComponent(backgroundImage));
private final MonopolyApp app;
private final String functionality;
private final PlayerColor playerColor;
public ImageButton(String functionality, MonopolyApp app) {
super("", "button-clear");
this.app = app;
this.functionality = functionality;
this.playerColor = Player.getColor(app.getId());
updateButtonAppearance(ButtonState.ENABLED);
addButtonCommands();
}
/**
* Updates the button's appearance based on its state.
*
* @param state the current button state
*/
private void updateButtonAppearance(ButtonState state) {
setBackgroundTexture(state.name().toLowerCase());
}
/**
* Adds button commands for state-specific actions like hover, press, enable, and disable.
*/
private void addButtonCommands() {
addCommands(ButtonAction.Enabled, source -> updateButtonAppearance(ButtonState.ENABLED));
addCommands(ButtonAction.Disabled, source -> updateButtonAppearance(ButtonState.DISABLED));
addCommands(ButtonAction.Hover, source -> {
if (isEnabled()) {
updateButtonAppearance(ButtonState.HOVER);
}
});
addCommands(ButtonAction.HighlightOff, source -> updateButtonAppearance(isEnabled() ? ButtonState.ENABLED : ButtonState.DISABLED));
addCommands(ButtonAction.Up, source -> updateButtonAppearance(isEnabled() ? ButtonState.ENABLED : ButtonState.DISABLED));
addCommands(ButtonAction.Down, source -> {
if (isEnabled()) {
app.getGameLogic().playSound(Sound.BUTTON);
}
});
}
/**
* Sets the background texture for the button based on the given state.
*
* @param state the button state (e.g., "enabled", "disabled", "hover")
*/
private void setBackgroundTexture(String state) {
String texturePath = buildTexturePath(state);
Texture texture = app.getAssetManager().loadTexture(texturePath);
setBackground(new QuadBackgroundComponent(texture));
}
/**
* Builds the file path for the button texture.
*
* @param state the button state (e.g., "enabled", "disabled", "hover")
* @return the full file path to the texture
*/
private String buildTexturePath(String state) {
return String.format("Pictures/Buttons/Button_%s_%s_%s.png", functionality, playerColor.getColorName(), state);
}
/**
* Button states for handling appearance transitions.
*/
private enum ButtonState {
ENABLED, DISABLED, HOVER
}
public void addClickCommands( Command<? super Button> command ) {
super.addCommands(ButtonAction.Down, command);
}
@SuppressWarnings("unchecked") // because Java doesn't like var-arg generics
public void addClickCommands( Command<? super Button>... commands ) {
super.addCommands(ButtonAction.Down, commands);
}
}

View File

@ -79,6 +79,8 @@ public class Toolbar extends Dialog implements GameEventListener {
toolbarContainer = setupToolbar();
app.getGuiNode().attachChild(toolbarContainer);
endTurnButton.setEnabled(false);
}
/**
@ -175,9 +177,9 @@ public class Toolbar extends Dialog implements GameEventListener {
*/
private void setupActionMenu(Container container) {
Container menuContainer = container.addChild(new Container());
menuContainer.addChild(createTradeButton(getCurrentPlayerColor()));
menuContainer.addChild(createPropertyMenuButton(getCurrentPlayerColor()));
menuContainer.addChild(createEndTurnButton(getCurrentPlayerColor()));
menuContainer.addChild(createTradeButton());
menuContainer.addChild(createPropertyMenuButton());
menuContainer.addChild(createEndTurnButton());
menuContainer.setBackground(null);
}
@ -278,58 +280,65 @@ public class Toolbar extends Dialog implements GameEventListener {
});
}
/**
* Creates a trade button with the specified player color.
*
* @param playerColor The color of the player.
* @return The button representing the trade action.
*/
private Button createTradeButton(ColorRGBA playerColor) {
return createActionButton(playerColor, "icons/icon-handeln.png", 100, () -> new ChoosePartner(app).open());
}
/**
* Creates a property menu button with the specified player color.
*
* @param playerColor The color of the player.
* @return The button representing the property menu action.
*/
private Button createPropertyMenuButton(ColorRGBA playerColor) {
return createActionButton(playerColor, "icons/icon-gebaude.png", 75, () -> new BuildingAdminMenu(app).open());
}
/**
* Creates an end turn button with the specified player color.
*
* @param playerColor The color of the player.
* @return The button representing the end turn action.
*/
private Button createEndTurnButton(ColorRGBA playerColor) {
return createActionButton(playerColor, "icons/icon-zugbeenden.png", 75, () -> handleEndTurn());
}
/**
* Creates an action button with the specified color, icon path, icon size, and action.
*
* @param color The color of the button.
* @param iconPath The path to the icon image.
* @param iconSize The size of the icon.
* @param action The action to perform when the button is clicked.
* @return The button representing the action.
*/
private Button createActionButton(ColorRGBA color, String iconPath, int iconSize, Runnable action) {
Button button = new Button("", new ElementId("button-toolbar2"));
button.setPreferredSize(new Vector3f(150, 50, 0));
button.setBackground(createButtonBackground(color));
private Button createTradeButton() {
String iconPath = "icons/icon-handeln.png";
// createActionButton(playerColor, "icons/icon-handeln.png", 100, () -> new ChoosePartner(app).open());
tradeButton = new ImageButton("generic", app);
tradeButton.setPreferredSize(new Vector3f(150, 50, 0));
IconComponent icon = new IconComponent(iconPath);
icon.setHAlignment(HAlignment.Center);
icon.setVAlignment(VAlignment.Center);
icon.setIconSize(new Vector2f(iconSize, iconSize));
button.setIcon(icon);
icon.setIconSize(new Vector2f(75 , 75));
tradeButton.setIcon(icon);
button.addClickCommands(source -> ifTopDialog(action));
return button;
tradeButton.addClickCommands(s -> ifTopDialog(() -> {
new ChoosePartner(app).open();
}));
return tradeButton;
}
private Button createPropertyMenuButton() {
String iconPath = "icons/icon-gebaude.png";
propertyMenuButton = new ImageButton("generic", app);
propertyMenuButton.setPreferredSize(new Vector3f(150, 50, 0));
IconComponent icon = new IconComponent(iconPath);
icon.setHAlignment(HAlignment.Center);
icon.setVAlignment(VAlignment.Center);
icon.setIconSize(new Vector2f(50 , 50));
propertyMenuButton.setIcon(icon);
propertyMenuButton.addClickCommands(s -> ifTopDialog(() -> {
new BuildingAdminMenu(app).open();
}));
return propertyMenuButton;
}
private Button createEndTurnButton() {
// return createActionButton(playerColor, "icons/icon-zugbeenden.png", 75, () -> handleEndTurn());
String iconPath = "icons/icon-zugbeenden.png";
endTurnButton = new ImageButton("generic", app);
endTurnButton.setPreferredSize(new Vector3f(150, 50, 0));
IconComponent icon = new IconComponent(iconPath);
icon.setHAlignment(HAlignment.Center);
icon.setVAlignment(VAlignment.Center);
icon.setIconSize(new Vector2f(50 , 50));
endTurnButton.setIcon(icon);
endTurnButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().send(new EndTurn());
receivedEvent(new ButtonStatusEvent(false));
}));
return endTurnButton;
}
/**
@ -501,11 +510,12 @@ public class Toolbar extends Dialog implements GameEventListener {
@Override
public void receivedEvent(ButtonStatusEvent event) {
System.out.println("Button status event received: " + event.buttonsEnabled()+ "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG");
boolean enabled = event.buttonsEnabled();
canRollDice = enabled;
if (tradeButton != null) tradeButton.setEnabled(enabled);
if (propertyMenuButton != null) propertyMenuButton.setEnabled(enabled);
if (endTurnButton != null) endTurnButton.setEnabled(false);
tradeButton.setEnabled(enabled);
propertyMenuButton.setEnabled(enabled);
endTurnButton.setEnabled(false);
}
@Override