Skip to content

Game.UI.Widgets.IconButton

Assembly: Game
Namespace: Game.UI.Widgets

Type: class

Base: Widget, ITooltipTarget, IInvokable, IWidget, IJsonWritable

Summary:
IconButton is a small clickable widget that displays an icon, optional tooltip and maintains a selected state via a user-provided selector delegate. The class exposes an Action to invoke when activated and implements JSON writing for its visible properties. It updates its internal selected flag each frame based on the provided selected Func.


Fields

  • private bool m_Selected
    Tracks the current evaluated selected state. Updated in Update() when the selected delegate changes its return value.

  • private string m_Icon
    Backing field for the public icon property. When the public icon is changed, the setter calls SetPropertiesChanged() so the UI system knows to re-render or sync the property.

Properties

  • public string icon { get; set; }
    Exposes the icon identifier (string). Getter returns m_Icon; setter updates m_Icon and calls SetPropertiesChanged() only if the value actually changed.

  • public Action action { get; set; }
    Action invoked by Invoke(). Note: Invoke() calls this without a null check, so assigning a valid Action is required to avoid a NullReferenceException.

  • [CanBeNull] public Func<bool> selected { get; set; }
    Optional selector delegate used each Update to determine whether the button is currently selected. If null, the button will not change its selected state from this mechanism.

  • [CanBeNull] public LocalizedString? tooltip { get; set; }
    Optional localized tooltip text to display for the button. Written out via WriteProperties for serialization/JSON output.

Constructors

  • public IconButton()
    Implicit default constructor is available. No custom constructor defined in source — initialize properties (icon, action, selected, tooltip) after construction.

Methods

  • public void Invoke()
    Calls the assigned action: action(). No null-check is performed before invocation — ensure action is non-null before calling Invoke().

  • protected override WidgetChanges Update()
    Per-frame update override. Calls base.Update() and evaluates the selected delegate (if provided). If the evaluated selected value differs from m_Selected, sets WidgetChanges.Properties in the returned flag and updates m_Selected accordingly. This allows the UI system to react to selected state changes.

  • protected override void WriteProperties(IJsonWriter writer)
    Writes widget properties to the provided IJsonWriter for serialization/syncing. The implementation writes the following properties in order:

  • "icon" : icon
  • "selected" : m_Selected (current boolean)
  • "tooltip" : tooltip

It calls base.WriteProperties(writer) before writing the IconButton-specific properties.

Usage Example

var button = new IconButton
{
    icon = "MyMod/Icons/hammer",
    action = () => Debug.Log("Icon button pressed"),
    selected = () => someGameState == DesiredState,
    tooltip = new LocalizedString("myMod.button.tooltip")
};

// Somewhere in UI setup, add the widget to its parent
parent.AddChild(button);

// Invocation example (e.g., from input handling)
button.Invoke(); // will call the assigned action (must not be null)

Notes and tips: - Always assign action before calling Invoke() to avoid NullReferenceException. - Use selected to provide dynamic selection behavior; Update() will detect changes and mark properties changed so the UI can update visuals. - Changing the icon property will call SetPropertiesChanged(), which indicates the widget's properties need to be resent/rendered.