Game.UI.Editor.ListField
Assembly: Assembly-CSharp.dll
Namespace: Game.UI.Editor
Type: public class
Base: Widget
Summary:
ListField is a UI Widget representing a simple list control used in editors. It holds a list of Item entries (a nested struct) and exposes a binding factory (nested Bindings class) for UI triggers (for example, removing an item). The control supports JSON serialization of its items via the implemented WriteProperties override. Note that RemoveItem only invokes the onItemRemoved callback — it does not automatically remove an element from m_Items; the owner is expected to update the underlying collection if desired.
Fields
-
public List<Item> m_Items
A list containing the current items displayed by the ListField. Each entry is an instance of the nested Item struct. This collection is written to JSON by WriteProperties. -
public Action<int> onItemRemoved
Callback invoked when RemoveItem(index) is called (for example, as a result of a binding trigger). The integer parameter is the index of the item that was requested to be removed. Consumers should use this callback to actually remove the item from m_Items and perform any other cleanup. -
public struct Item
Nested struct representing a single list entry. Implements IJsonWritable so it can be serialized. -
public string m_Label
The display label for the list entry. -
public bool m_Removable
When true, the UI should present an option to remove this item (usually a remove button). The removal action triggers the "removeListItem" trigger binding. -
public object m_Data
Arbitrary attached data for the item. Can be any object the caller needs to associate with the list entry (not directly serialized by ListField unless Item.Write handles it). -
public string[] m_SubItems
Optional array of sub-item strings (e.g., columns or additional textual fields). -
public void Write(IJsonWriter writer)
Writes the item into the provided IJsonWriter. The implementation writes a JSON object with properties "label", "removable", and "subItems". Useful for saving editor state. -
public class Bindings
Nested class implementing IWidgetBindingFactory. Produces widget bindings used by UI markup to hook up actions to widget functions. -
public IEnumerable<IBinding> CreateBindings(string group, IReader<IWidget> pathResolver, ValueChangedCallback onValueChanged)
Creates a TriggerBinding named "removeListItem". When triggered (with an index), it checks that the widget is a ListField, calls RemoveItem(index) on it, and then calls onValueChanged(widget) so the binding system knows the widget changed. This binding enables UI code to invoke list item removal from markup or scripts.
Properties
- (No public properties are defined on ListField itself in the provided source.)
Constructors
public ListField()
The default constructor is used (no explicit constructor defined in source). Typical initialization is handled by the base Widget constructor and by the owner code after instantiation.
Methods
-
protected override void WriteProperties(IJsonWriter writer) : System.Void
Overrides the Widget base method to include the list contents when serializing this widget. After calling base.WriteProperties(writer), it writes a property named "items" and serializes m_Items (the List- ) through the provided IJsonWriter. This enables the ListField's items to be saved/restored.
-
protected void RemoveItem(int index) : System.Void
Invokes onItemRemoved with the provided index if a handler is installed. This method does not itself remove the entry from m_Items — it simply signals the host that the user requested removal (for example via a remove button). Consumers should handle updating m_Items (and the UI) in their onItemRemoved handler. -
public struct Item.Write(IJsonWriter writer) : System.Void
Serializes the item into JSON. Writes the label, removable flag, and subItems array. Used by WriteProperties when serializing the ListField items. -
public IEnumerable<IBinding> Bindings.CreateBindings(string group, IReader<IWidget> pathResolver, ValueChangedCallback onValueChanged)
Creates and returns the trigger binding(s) for ListField. The produced TriggerBinding listens for the "removeListItem" action, calls RemoveItem on the target ListField, and notifies the binding system about the value change.
Usage Example
// Create the ListField and populate items
var listField = new ListField();
listField.m_Items = new List<ListField.Item>
{
new ListField.Item { m_Label = "First", m_Removable = true, m_SubItems = new[] { "A", "1" } },
new ListField.Item { m_Label = "Second", m_Removable = false, m_SubItems = new[] { "B", "2" } }
};
// Handle removal requests: actually remove the item from m_Items and refresh UI
listField.onItemRemoved = index =>
{
if (index >= 0 && index < listField.m_Items.Count)
{
listField.m_Items.RemoveAt(index);
// Notify/refresh the widget or parent UI as needed
}
};
// If using UI bindings, the Bindings factory exposes the "removeListItem" trigger,
// which should be invoked with the index to remove. The binding will call RemoveItem,
// which will in turn call the onItemRemoved handler above.
Additional notes: - Serialization: Item.Write writes only label, removable and subItems. If you store custom objects in m_Data, ensure you handle their serialization separately if needed. - Removal semantics: RemoveItem intentionally delegates removal to the owner via onItemRemoved to allow synchronized state changes and custom logic (undo, confirmation dialogs, linked data cleanup, etc.). - Bindings integration: Use the Bindings factory when wiring ListField into the UI binding system so UI elements can trigger removals by name ("removeListItem").