Skip to content

Button

namespace Griff.Scenes;
public sealed class Button : Component, IDrawEffect

Makes the entity clickable: it lightens under the mouse, darkens while held, and clicks when the mouse is released over it. Behaviors on the entity get OnClick. Menus need no code at all: set Action to load a scene, restart or quit. Put a Sprite on the entity for a background and a Text for its label. HoverGrow, PressShrink and PopTime make it pop: grow under the mouse and shrink as it’s pressed, easing into each (only as it’s drawn: its clickable area stays its size).

This is a component: its saved properties are on its component page.

public ButtonAction Action { get; set; }

What clicking it does with no code: load a scene, restart, quit, open or close a panel, rebind a key and so on.

public int BindingSlot { get; set; }

For Rebind: which of the action’s bindings on the device pressed it replaces, from 0: 0 its first key or mouse button (or gamepad button), 1 its second, so a controls screen can have a primary and a secondary key. One past the last adds one. Label it “{PanUp:1}” to show that slot’s binding.

public string? Do { get; set; }

For Do: what to do, e.g. “Attack”, “Cast Spell” or “Open”: an action of the Targets’ components (or this entity’s). “Battler.Attack” names one component’s.

public float Fade { get; set; }

For loading and restarting: seconds to fade the screen out (and back in) around the switch. 0 cuts straight there.

public float Highlight { get; set; }

How much lighter the button’s sprite and text get under the mouse, from 0 (not at all) to 1 (white).

public float HoverGrow { get; set; }

How much bigger it’s drawn under the mouse or picked with keys, with what’s inside it: 0.08 is 8% bigger. 0 keeps its size.

public string? InputAction { get; set; }

For Rebind: the input action it changes, e.g. “Jump”.

public bool Interactable { get; set; }

Whether it can be clicked. A button that can’t is drawn faded, ignores the mouse and its shortcut, and is skipped by the arrow keys. Scripts can set it, e.g. a Buy button while the player can’t afford it.

public bool IsFocused { get; }

True while the keyboard or a gamepad has picked this button: the arrow keys, D-pad or stick move between buttons, and Enter, Space or the A button click the picked one. It’s lit up like the mouse is over it.

public bool IsHovered { get; }

True while the mouse is over the button.

public bool IsListening { get; }

True while a Rebind button waits for the player to press something.

public bool IsMissingSave { get; }

True while the game has nothing saved under NeedsSaved, or, for Load Game, Delete Save and Quick Load, no game saved in the slot (and for saving and loading, during a network game). Kept up to date by the game.

public bool IsPressed { get; }

True while the button is held down after being pressed on.

public bool IsUsable { get; }

True while it can be clicked: it’s Interactable, and what it NeedsSaved is saved.

public bool Navigable { get; set; }

Whether the arrow keys, D-pad and stick can move to this button (see IsFocused). Turn it off for a button on a HUD during play, so moving the player doesn’t pick it.

public string? NeedsSaved { get; set; }

A name in Game.SaveData that has to be saved for the button to work, e.g. “level” for a Continue button that does nothing until there’s a game to continue. Empty always works.

public bool OverPanels { get; set; }

Whether it still takes clicks and its shortcuts while a Panel it isn’t on is open, even one that pauses the game: a menu bar’s Game button or a pause button that opens the game menu over a letter or a shop, on top of it (closing that menu leaves the letter open). The arrow keys stay on the panel. Off, it waits until panels close.

public string? Panel { get; set; }

For OpenPanel: the name of the panel’s entity, e.g. “Options”.

public float PopTime { get; set; }

Seconds it takes to grow, shrink and shade into being under the mouse, held down or left, eased, rather than changing at once: 0.1 for a soft pop. 0 changes at once.

public float PressShrink { get; set; }

How much smaller than its size it’s drawn while it’s held down: 0.06 is 6% smaller. 0 keeps its size.

public string? SavedScene { get; set; }

For LoadScene: a name in Game.SaveData holding a scene to go to instead of Scene, e.g. “Room” (where an adventure’s walker keeps the room it’s in): a Play button that carries on where the player left off, and goes to Scene while nothing’s saved there.

public string? Scene { get; set; }

For LoadScene and New Game: the scene’s name, e.g. “Level1”. For Host Game and Leave Game, the scene to go to afterwards (empty stays).

public Key Shortcut { get; set; }

A key that clicks the button too, e.g. Enter for a Play button. None for mouse only.

public string? ShortcutAction { get; set; }

An input action that clicks the button too, e.g. “OpenMap” bound to M in Game > Controls: like Shortcut, but its keys are the action’s, so the Controls window and the player’s Rebind change them. Empty for none.

public Vector2 Size { get; set; }

The clickable area, centered on the entity, in world units before the transform’s scale. Rotation is ignored.

public string? Slot { get; set; }

For Save Game, Load Game and Delete Save: the slot’s name, e.g. “1”. Empty uses the slot of the SaveSlot it’s on or inside.

public string? Sound { get; set; }

Plays when it’s clicked, e.g. “Sounds/select.wav”.

public string? Targets { get; set; }

For Do: the entities to do it on, by name, separated by commas. Empty does it on this entity.

public string? With { get; set; }

For Do: what to do it with, for an action that takes a value (Damage 2). Empty uses the action’s own default.

public Color Shade(Color color)

The color to draw color in for the button’s current state: with a PopTime, part way into it.

public event Action<Button>? Clicked

Raised when the button is clicked, after its Action is requested and behaviors’ OnClick ran.