Skip to content

Game.UI.Widgets.LayoutContainer

Assembly:
Namespace: Game.UI.Widgets

Type: abstract class

Base: Widget, IContainerWidget

Summary:
LayoutContainer is an abstract base widget that implements IContainerWidget and provides basic child-management and layout-related properties for container widgets. It holds an internal list of child widgets, exposes a flex layout configuration, and participates in the widget JSON serialization by writing the flex property. When the children list is changed, defaults are applied to new children and the container is notified so that layout/visual updates can occur.


Fields

  • private IList<IWidget> m_Children = Array.Empty<IWidget>()
    Internal storage for the container's children. Initialized to an empty, shared array to avoid null references. Assignments to the public children property replace this field and trigger container update behavior (ContainerExtensions.SetDefaults and SetChildrenChanged).

Properties

  • public FlexLayout flex { get; set; } = FlexLayout.Default
    Controls flex-based layout settings for this container. Defaults to FlexLayout.Default. This value is written out in WriteProperties for serialization.

  • public IList<IWidget> children
    Getter returns the internal children list (m_Children). Setter applies default settings to the provided list via ContainerExtensions.SetDefaults, assigns it to the internal field, and calls SetChildrenChanged() when the provided list is different from the current one. This ensures added children receive default configuration and the container updates appropriately.

  • public override IList<IWidget> visibleChildren => children
    Exposes the list of children that should be considered for visibility/layout. In this implementation it simply returns the same list as children; derived containers could override this to filter visible children.

Constructors

  • public LayoutContainer()
    No explicit constructor is declared in the source; the default parameterless constructor is available. Initialization performed in-field: m_Children defaults to Array.Empty() and flex defaults to FlexLayout.Default.

Methods

  • protected override void WriteProperties(IJsonWriter writer)
    Overrides the base widget JSON writer to include the container's flex property. Implementation details:
  • Calls base.WriteProperties(writer) to allow base class properties to be written first.
  • Writes a property named "flex" and writes the flex value using the provided IJsonWriter. This method integrates the container's layout settings into any serialization pipeline used by the UI system.

Usage Example

// Example custom container that sets up children and flex layout.
public class MyHorizontalContainer : LayoutContainer
{
    public MyHorizontalContainer()
    {
        // configure flex for a horizontal layout (hypothetical API)
        flex = new FlexLayout
        {
            direction = FlexDirection.Row,
            alignItems = FlexAlign.Center,
            justifyContent = FlexJustify.Start
        };

        // create children (IWidget implementations)
        children = new List<IWidget>
        {
            new LabelWidget("Item 1"),
            new ButtonWidget("Click"),
            new LabelWidget("Item 3")
        };

        // Setting children triggers ContainerExtensions.SetDefaults on each child
        // and calls SetChildrenChanged() so the UI system updates layout/state.
    }

    // Optionally override visibleChildren to filter children at runtime:
    public override IList<IWidget> visibleChildren
    {
        get
        {
            // Return only children that are currently active/visible (example)
            return children.Where(w => w.IsVisible).ToList();
        }
    }
}

Notes: - When assigning a new children list, ContainerExtensions.SetDefaults is invoked automatically to ensure consistent defaults on child widgets. - SetChildrenChanged() is called automatically after changing the children list; this is the hook the underlying widget system uses to recompute layout, reparent, or refresh visuals. - WriteProperties allows the flex configuration to be persisted or transmitted by the UI's JSON writer.