Skip to content

Game.UI.Widgets.ColorFieldBuilders

Assembly:
Assembly-CSharp (typical for Cities: Skylines 2 mods; replace with actual assembly if different)

Namespace:
Game.UI.Widgets

Type:
public class

Base:
IFieldBuilderFactory

Summary:
Factory class that creates FieldBuilder delegates for UnityEngine.Color and UnityEngine.Color32 member types. It examines provided attributes to determine color usage (HDR and whether to show the alpha channel) and returns a FieldBuilder which constructs a ColorField configured with the appropriate settings and a CastAccessor to convert between the stored value and the Color value used by the widget. Local conversion functions handle mapping between Color, Color32 and object forms.


Fields

  • (none)
    No instance or static fields are declared in this class.

Properties

  • (none)
    This class exposes no properties.

Constructors

  • public ColorFieldBuilders()
    Default public parameterless constructor (implicit). Use to create an instance of the factory.

Methods

  • public FieldBuilder TryCreate(System.Type memberType, object[] attributes)
    Attempts to create a FieldBuilder for the given member type.
    Behavior:
  • If memberType is UnityEngine.Color, returns a FieldBuilder configured to build a ColorField that uses Color values.
  • If memberType is UnityEngine.Color32, returns a FieldBuilder configured to build a ColorField that uses Color32 values (via conversion).
  • Otherwise returns null. Implementation details:
  • For Color, it calls CreateColorFieldBuilder(attributes, ToColor, FromColor).
  • For Color32, it calls CreateColorFieldBuilder(attributes, ToColor32, FromColor32).
  • Defines local static conversion functions:

    • FromColor(Color) -> object (returns the Color)
    • FromColor32(Color) -> object (casts Color to Color32)
    • ToColor(object) -> Color (casts object to Color)
    • ToColor32(object) -> Color (casts object to Color32) These converters are used by CastAccessor so the created ColorField can read/write the underlying value wrapped by IValueAccessor.
  • private static FieldBuilder CreateColorFieldBuilder(object[] attributes, Converter<object, UnityEngine.Color> fromObject, Converter<UnityEngine.Color, object> toObject)
    Creates and returns a FieldBuilder delegate configured according to the provided attributes and conversion callbacks. Behavior:

  • Reads color usage flags (hdr, showAlpha) by calling WidgetAttributeUtils.GetColorUsage(attributes, ref hdr, ref showAlpha).
  • Returns a FieldBuilder delegate: given an IValueAccessor it constructs a new ColorField with:
    • hdr set according to attributes,
    • showAlpha set according to attributes,
    • accessor set to new CastAccessor(accessor, fromObject, toObject) to handle conversion between the widget Color and the underlying stored value type. Notes:
  • Uses System.Converter delegates for conversions between object and UnityEngine.Color.
  • ColorField, CastAccessor and WidgetAttributeUtils are expected to be present in the UI/widget framework used by the game.

Usage Example

// Example: create a FieldBuilder for a Color member and instantiate the ColorField.
// (FieldBuilder and IValueAccessor are types from the game's UI/widget framework.)

var factory = new Game.UI.Widgets.ColorFieldBuilders();

// Example attributes array (could include attributes that control HDR/showAlpha)
object[] attributes = new object[0];

FieldBuilder fb = factory.TryCreate(typeof(UnityEngine.Color), attributes);
if (fb != null)
{
    // Acquire or implement an IValueAccessor that reads/writes the underlying value.
    IValueAccessor accessor = /* obtain accessor for the target member */;
    UnityEngine.Object fieldObj = fb(accessor); // constructs the ColorField (actual type is ColorField)
    var colorField = fieldObj as Game.UI.Widgets.ColorField;
    // colorField.hdr and colorField.showAlpha are set according to attributes.
}

// Similarly for Color32:
FieldBuilder fb32 = factory.TryCreate(typeof(UnityEngine.Color32), attributes);
if (fb32 != null)
{
    IValueAccessor accessor32 = /* accessor for a Color32 member */;
    var colorField32 = fb32(accessor32) as Game.UI.Widgets.ColorField;
    // Underlying access is converted between Color and Color32 via CastAccessor<Color>.
}

Additional notes: - The factory returns null for non-color member types, so callers should handle that case. - If your mod or assembly uses a different assembly name than Assembly-CSharp, adjust the Assembly field above accordingly. - The implementation relies on WidgetAttributeUtils.GetColorUsage to extract HDR/showAlpha flags from attributes; ensure the relevant attribute types are passed in the attributes array when calling TryCreate.