Skip to content

Game.UI.Widgets.StringInputField

Assembly: Game
Namespace: Game.UI.Widgets

Type: class

Base: Field

Summary:
A UI input field for editing string values. Supports single-line or multiline modes, an optional maximum length, and a dynamic warning state. Implements IWarning and exposes a warningAction callback that can be used to evaluate the warning state each update. The control ensures GetValue() never returns null (it returns string.Empty instead).


Fields

  • public static readonly int kDefaultMultilines = 5
    Default number of lines used for multiline input when a default is needed.

  • public static readonly int kSingleLine = 0
    Constant indicating single-line mode (no multiline).

  • private bool m_Warning
    Backed field for the warning property — true when the field is in a warning state.

  • private int m_Multiline = kSingleLine
    Backed field for the multiline property. Holds the number of lines (0 for single-line).

  • private int m_MaxLength
    Backed field for the maxLength property. If >0, the widget should limit input length accordingly.

  • [CanBeNull] public Func<bool> warningAction { get; set; }
    A user-supplied callback that is evaluated during Update() to determine the dynamic warning state. If set, the return value is used to update m_Warning and trigger property changes when it flips.

Properties

  • public int multiline { get; set; }
    Gets or sets the number of lines to display/allow. Setting a different value updates the backing field and calls SetPropertiesChanged() to notify the UI system.

  • public int maxLength { get; set; }
    Gets or sets a maximum allowed length for the input. Changing the value calls SetPropertiesChanged().

  • public bool warning { get; set; }
    Gets or sets the warning state. Setting this property clears warningAction (warningAction = null) and sets the backing m_Warning value.

Constructors

  • public StringInputField()
    Implicit default constructor (no explicit constructor defined in the source). Initializes fields to their default values (m_Multiline = kSingleLine, m_MaxLength = 0, m_Warning = false). Typical usage expects you to configure properties after construction.

Methods

  • public override string GetValue()
    Returns the current string value of the field. Ensures a non-null result by returning string.Empty if the base value is null.

  • protected override WidgetChanges Update()
    Per-frame update override. Calls base.Update() then, if warningAction is set, invokes it to compute the current warning state. If the computed value differs from the stored m_Warning, updates m_Warning and marks WidgetChanges.Properties so the UI can refresh.

  • protected override void WriteProperties(IJsonWriter writer)
    Serializes important widget properties to the provided JSON writer. Writes:

  • "multiline" : m_Multiline
  • "maxLength" : m_MaxLength
  • "warning" : warning Also calls base.WriteProperties(writer) so base class properties are included.

Usage Example

// Create and configure a StringInputField
var input = new Game.UI.Widgets.StringInputField();
input.multiline = Game.UI.Widgets.StringInputField.kDefaultMultilines; // make it multiline
input.maxLength = 128; // limit input length

// Provide a dynamic warning check (e.g., show warning if empty)
input.warningAction = () =>
{
    string value = input.GetValue();
    return string.IsNullOrWhiteSpace(value);
};

// Alternatively, set a static warning state and clear the action
input.warning = true; // this sets warningAction = null internally

Notes: - The widget uses SetPropertiesChanged() internally when multiline or maxLength changes to notify the UI system. - Use warningAction when the warning state depends on external conditions; set warning directly to force a fixed state and clear any dynamic check.