Game.UI.Widgets.ToggleField
Assembly:
Assembly-CSharp (typical for Cities: Skylines 2 mods; class defined in the game's managed assembly)
Namespace: Game.UI.Widgets
Type:
class
Base:
Field
Summary:
ToggleField is a UI Field widget specialized for boolean values that also supports a "warning" state. The warning state can be driven either by a static boolean (the warning property) or dynamically via a delegate (warningAction). The Update override evaluates the delegate (if present) and marks the widget's properties as changed when the warning state toggles. The WriteProperties method serializes the current warning state to an IJsonWriter as the "warning" property.
Fields
private bool m_Warning
Backing storage for the current warning state exposed by the warning property. This value is updated either directly via the warning setter or by invoking warningAction inside Update().
Properties
-
public Func<bool> warningAction { get; set; }
A nullable delegate that, when assigned, is invoked on Update() to determine the current warning state. If this delegate is non-null, its returned value will be compared to m_Warning and, if different, m_Warning will be updated and widget property changes will be flagged. The property is annotated with [CanBeNull], meaning it may be left null to disable dynamic warning evaluation. -
public bool warning { get; set; }
Gets or sets the current (static) warning state. The getter returns m_Warning. The setter clears warningAction (disabling dynamic evaluation) and assigns the given value to m_Warning.
Constructors
- No explicit constructors are defined in the source file. The class uses the compiler-generated default constructor.
Methods
-
protected override WidgetChanges Update()
Calls base.Update() and then, if warningAction is non-null, invokes it to get the current dynamic warning state. If the returned value differs from m_Warning, m_Warning is updated and WidgetChanges.Properties is OR'ed into the returned WidgetChanges to indicate that widget properties changed and UI should be refreshed accordingly. Returns the combined WidgetChanges. -
protected override void WriteProperties(IJsonWriter writer)
Calls base.WriteProperties(writer), then writes a JSON property named "warning" with the current value of the warning property. This is used to serialize the widget's warning state for the UI layer.
Usage Example
// Example: attach a dynamic warning based on some external condition
var toggle = new ToggleField();
// dynamic warning: will be evaluated in Update()
toggle.warningAction = () => someManager.HasProblemFor(toggleId);
// OR: set a static warning and disable dynamic evaluation
toggle.warning = true; // this sets m_Warning and clears warningAction
// When serialized, the widget will include: "warning": true/false