Skip to content

Game.UI.Widgets.NamedWidget

Assembly:
Assembly-CSharp

Namespace:
Game.UI.Widgets

Type:
public abstract class

Base:
Widget, INamed

Summary:
NamedWidget is a reusable base class for UI widgets that exposes a localized display name and description. It stores both values as LocalizedString instances and also supports deferred/dynamic evaluation via Func delegates (displayNameAction and descriptionAction). The class detects changes to the evaluated or assigned strings and marks widget properties as changed when necessary. It also serializes the two values when WriteProperties is called.


Fields

  • private LocalizedString m_displayName
    Stores the current display name for the widget. This is the authoritative value returned by the displayName property unless a displayNameAction is in use and updates it during UpdateNameAndDescription.

  • private LocalizedString m_description
    Stores the current description for the widget. This is the authoritative value returned by the description property unless a descriptionAction is in use and updates it during UpdateNameAndDescription.

Properties

  • public Func<LocalizedString> displayNameAction { get; set; }
    Optional delegate that, when set, is invoked by UpdateNameAndDescription to compute the current display name. If a value is assigned to the displayName property, this delegate is cleared (set to null) to favor the explicit value.

  • public Func<LocalizedString> descriptionAction { get; set; }
    Optional delegate that, when set, is invoked by UpdateNameAndDescription to compute the current description. If a value is assigned to the description property, this delegate is cleared (set to null) to favor the explicit value.

  • public LocalizedString displayName { get; set; }
    Gets or sets the concrete display name. Setting this property clears displayNameAction to ensure the explicit value is used.

  • public LocalizedString description { get; set; }
    Gets or sets the concrete description. Setting this property clears descriptionAction to ensure the explicit value is used.

Constructors

  • public NamedWidget()
    Default constructor (implicit if not declared). As an abstract class, it is intended to be inherited; there is no special construction logic in the class itself.

Methods

  • protected override WidgetChanges Update() : WidgetChanges
    Overrides Widget.Update() to include name/description updates. It calls the base Update and combines its result with UpdateNameAndDescription(setChanged: false). Using setChanged: false prevents UpdateNameAndDescription from calling SetPropertiesChanged every frame; callers can choose when to mark the widget changed.

  • public WidgetChanges UpdateNameAndDescription(bool setChanged = true) : WidgetChanges
    If displayNameAction is assigned, invokes it and, if the returned LocalizedString differs from the stored m_displayName, updates m_displayName and accumulates WidgetChanges.Properties. Similarly handles descriptionAction and m_description. If setChanged is true, SetPropertiesChanged() is called when a property actually changes. Returns the combined WidgetChanges flag indicating which aspects changed.

  • protected override void WriteProperties(IJsonWriter writer) : void
    Serializes the displayName and description properties to the provided IJsonWriter. Calls base.WriteProperties(writer) and then writes "displayName" and "description" properties with their current values.

Usage Example

// Example subclass showing typical usage of NamedWidget:
public class MyLocalizedButton : NamedWidget
{
    public MyLocalizedButton()
    {
        // set a static localized name and description:
        displayName = new LocalizedString("menu_mybutton_name");
        description = new LocalizedString("menu_mybutton_desc");

        // OR set dynamic evaluation (e.g. reacts to game state / language changes)
        displayNameAction = () => GameLocalization.Get("menu_mybutton_name_key");
        descriptionAction = () => GameLocalization.Get("menu_mybutton_desc_key");
    }

    protected override void OnClick()
    {
        // handle click...
    }

    // If you need to force a refresh of the name/description and mark properties changed:
    public void RefreshNames()
    {
        // This will update internal values and mark properties as changed when they differ.
        UpdateNameAndDescription(setChanged: true);
    }
}

Note: Assigning displayName or description directly clears the corresponding action delegate to prefer the explicit value. Use UpdateNameAndDescription periodically (or rely on the overridden Update) to pick up changes from the action delegates and to update widget state accordingly.