Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
gui

Custom Button Styling

Create custom styled buttons in native .ui files with different colors for default, hovered, and pressed states.

Example Code

java
// In your .ui file:
$C = "../Common.ui";

$C.@PageOverlay {
    $C.@DecoratedContainer {
        Anchor: (Width: 420, Height: 180);

        #Content {
            LayoutMode: Top;
            Padding: (Left: 20, Right: 20, Top: 10, Bottom: 10);

            Group #ButtonRow {
                LayoutMode: Left;

                // Green "Accept" button
                $C.@TextButton #AcceptButton {
                    Anchor: (Width: 100, Height: 36);
                    Text: "YES";
                    Style: TextButtonStyle(
                        Default: (Background: #2d8a2d, LabelStyle: (FontSize: 16, TextColor: #ffffff, RenderBold: true)),
                        Hovered: (Background: #3da83d, LabelStyle: (FontSize: 16, TextColor: #ffffff, RenderBold: true)),
                        Pressed: (Background: #1d6a1d, LabelStyle: (FontSize: 16, TextColor: #ffffff, RenderBold: true)),
                        Sounds: $C.@ButtonSounds,
                    );
                }

                // Red "Decline" button with spacing
                $C.@TextButton #DeclineButton {
                    Anchor: (Left: 30, Width: 100, Height: 36);
                    Text: "NO";
                    Style: TextButtonStyle(
                        Default: (Background: #8a2d2d, LabelStyle: (FontSize: 16, TextColor: #ffffff, RenderBold: true)),
                        Hovered: (Background: #a83d3d, LabelStyle: (FontSize: 16, TextColor: #ffffff, RenderBold: true)),
                        Pressed: (Background: #6a1d1d, LabelStyle: (FontSize: 16, TextColor: #ffffff, RenderBold: true)),
                        Sounds: $C.@ButtonSounds,
                    );
                }
            }
        }
    }
}

// In Java, bind button events:
uiEventBuilder.addEventBinding(
    CustomUIEventBindingType.Activating,
    "#AcceptButton",
    EventData.of("Button", "Accept"),
    false
);

Common Mistakes

Using Anchor: (Left: 30) for button spacing, not margin. Background colors use hex format #RRGGBB. Must include Sounds: $C.@ButtonSounds for click sounds.