arranging children¶
put more than one thing in a container
this article shows how goo lays out a row of children. you will see a white rounded card with a counter and an orange "+" button side by side, and the button will already work.
the code¶
create Code/Demos/CounterUI.cs:
using Goo;
using Sandbox.UI;
namespace Sandbox;
public class CounterUI : GooPanel<Container>
{
private int _count;
protected override Container Build() => new Container
{
Padding = 16,
Width = 128,
Height = 128,
BackgroundColor = Color.White,
BorderRadius = 12,
FlexDirection = FlexDirection.Row,
Gap = 12,
AlignItems = Align.Center,
Children =
{
new Text(_count.ToString()),
new Container
{
Padding = 8,
BackgroundColor = Color.Orange,
HoverBackgroundColor = Color.Cyan,
BorderRadius = 6,
OnClick = e => { _count++; Rebuild(); },
Children = { new Text("+") },
},
},
};
}
drop it on a GameObject the same way you did in your first panel. you should see a white rounded card with a counter and an orange "+" button side by side.
what is new here¶
three properties do the layout work.
FlexDirection = FlexDirection.Row places children left-to-right. Row is already the engine default, so CounterUI would lay out the same way if you omitted this line; it is written out so the direction is explicit and easy to find later. set it to FlexDirection.Column instead and siblings stack top-to-bottom.
Gap = 12 puts 12 pixels of space between each sibling. you do not need margins on individual children.
AlignItems = Align.Center centers children on the cross axis. in a row, the cross axis runs top-to-bottom, so this keeps the number and the button vertically centered with each other.
the second child is a Container that wraps a Text, not just a Text on its own. nesting containers is how non-trivial UIs get built. each Container controls the look and layout of its own region.
HoverBackgroundColor changes the background when the cursor is over the element. it follows the same pattern as BackgroundColor, just for the hover state.
OnClick takes a lambda. the button increments _count and calls Rebuild(), which schedules a fresh Build() on the next tick. see your first counter for a full walkthrough of that pattern.
see also¶
- your first panel - create a single-child panel and add it to a scene.
- your first counter - deeper walkthrough of
OnClickandRebuild(). - build method - how
Build()andRebuild()work, and when re-renders happen. - styles -
FlexDirectionis the only non-nullable style property; defaults toRow. - overlay layout -
Hud.Overlay()flips the default toColumn, since a HUD stacks top-to-bottom. - container reference - full layout surface (
FlexDirection,JustifyContent,AlignItems,Gap, and more).