Game.UI.Widgets.EnumField
Assembly:
Assembly-CSharp (game assembly)
Namespace:
Game.UI.Widgets
Type:
public class EnumField : Field
Base:
Field
Summary:
A UI field widget that represents an enum-style selection using a backing ulong value. EnumField holds a list of EnumMember items (the selectable values), supports dynamic item updates via an ITypedValueAccessor, and exposes a warning state that can be driven either manually or by a callback (warningAction). It integrates with the widget update cycle and writes its relevant properties (enumMembers and warning) to an IJsonWriter when serializing.
Fields
-
private int m_ItemsVersion = -1
Internal version stamp used to detect changes in the items list when itemsAccessor/itemsVersion are used. When itemsVersion() returns a value different from m_ItemsVersion, enumMembers will be refreshed and widget properties marked dirty. -
private bool m_Warning
Backstore for the public warning property. Tracks current warning state of the field.
Properties
-
public Func<bool> warningAction { get; set; }
(Optional) Callback used each Update to determine the current warning state. If set, Update() will call this delegate and update m_Warning when the returned value changes. Can be null. -
public EnumMember[] enumMembers { get; set; }
The array of available enum members shown by the widget. Defaults to an empty array. Can be set directly or updated via itemsAccessor. -
public Func<int> itemsVersion { get; set; }
(Optional) Callback that returns a version number for the items; used to detect changes in the items source. If null, a default of 0 is used when checking for changes. -
public ITypedValueAccessor<EnumMember[]> itemsAccessor { get; set; }
(Optional) An accessor that provides the enumMembers array. When present, Update() will use itemsVersion (if provided) to detect changes and pull the latest EnumMember[] from this accessor. -
public bool warning { get; set; }
Exposes the warning state. The setter clears warningAction (sets it to null) and sets the internal m_Warning. Getting returns the current m_Warning. Setting this property is the manual way to control the warning flag (as opposed to using warningAction).
Constructors
public EnumField()
Initializes a new EnumField. The constructor configures the base Fieldto use ULongWriter and ULongReader for serializing/deserializing the backing ulong value: - base.valueWriter = new ULongWriter();
- base.valueReader = new ULongReader();
Methods
protected override WidgetChanges Update()
: System.ValueType (WidgetChanges)
Called during the widget update cycle. Behavior:- Calls base.Update() and accumulates its WidgetChanges.
- If itemsAccessor is not null:
- Queries itemsVersion via itemsVersion?.Invoke() ?? 0.
- If the returned version differs from m_ItemsVersion, sets widgetChanges |= WidgetChanges.Properties, updates m_ItemsVersion, and refreshes enumMembers from itemsAccessor.GetTypedValue() (falling back to Array.Empty
() if null).
- If warningAction is not null:
- Invokes warningAction(); if its boolean result differs from m_Warning, updates m_Warning and sets widgetChanges |= WidgetChanges.Properties.
- Returns the accumulated WidgetChanges flags indicating what changed.
-
Note: This method ensures dynamic lists and warning state cause the widget to mark its properties dirty when they change so UI can be updated/serialized as needed.
-
protected override void WriteProperties(IJsonWriter writer)
: void
Writes widget-specific properties to the provided IJsonWriter. Behavior: - Calls base.WriteProperties(writer).
- Writes a property named "enumMembers" and writes the enumMembers array (as IList
). - Writes a property named "warning" and writes the current warning state (bool).
- This is used for widget serialization (e.g., to save or transfer widget state).
Usage Example
// Create an EnumField and configure items and warning logic
var enumField = new EnumField();
// Directly set the available members:
enumField.enumMembers = new EnumMember[]
{
new EnumMember("None", 0),
new EnumMember("Low", 1),
new EnumMember("High", 2)
};
// Or bind to an external accessor so the list refreshes automatically:
enumField.itemsAccessor = someTypedValueAccessorProvidingEnumMembers;
enumField.itemsVersion = () => someSource.GetCurrentVersion(); // return an int version
// Use a callback to control warning state dynamically:
enumField.warningAction = () => {
// return true when a warning should be shown (e.g., invalid selection)
return enumField.value == 0; // example condition
};
// Manually set warning (this will clear warningAction):
enumField.warning = true;
Notes and related types:
- EnumMember: represents a single selectable enum entry (label, value, etc.). See its type for details.
- ITypedValueAccessor