Layout

This guide will show you the containers that arrange components.

A container is pushed, then the components you put become its children until you pop it:

Vertical.Push();
Label.Put("Name");
Textbox.Put(ref _name);
Vertical.Pop();

Each component asks for a preferred size, then its parent gives it a spot. Positions and sizes are in UI units, which are pixels at a scale of 1.

Vertical and Horizontal

A Vertical stacks its children from top to bottom and a Horizontal from left to right. Both are a StackPanel, so they have the same settings:

Vertical v = Vertical.Push();
v.Spacing = 10f;
v.Padding = 20f;
v.Align = CrossAlign.Center;

Spacing goes between children and Padding around all of them. Align places the children across the stack: Start, Center, End, or Stretch to give them the full width of a vertical or the full height of a horizontal.

Four verticals with the same three buttons, aligned to the start, the center, the end, and stretched

A row of components with different heights usually wants CrossAlign.Center so they line up:

Horizontal.Push().Align = CrossAlign.Center;
Icon.Put(_coin);
Label.Put($"{_coins}");
Horizontal.Pop();

A coin icon and the number 250, lined up on their centers

Scrolling

A stack scrolls when its parent gives it less room than its children need. The mouse wheel, dragging the scrollbar and dragging with a finger all scroll it. A component that gets focus inside it scrolls into view.

A Block is a simple way to limit its size:

Block.Put(400f, 300f);
Vertical.Push();
foreach (Item item in _inventory) {
    // ...
}
Vertical.Pop();

The stack gets at most 400 by 300. With enough items, it’s 300 tall and scrolls, while its width stays the width of its widest item.

A list of items cut off at the top and the bottom, with a scrollbar on its right

The scrollbar sits inside the stack’s padding. While the stack scrolls, it keeps room for it beside the children, with ScrollbarGap between them.

ScrollIncrement sets how far a wheel notch goes, ShowScrollbar hides the scrollbar, and OffsetX and OffsetY let you scroll from your own code.

A vertical stack that fills the whole UI, centers its children, and scrolls when they don’t fit. It’s the usual root for a menu screen.

MenuPanel.Push();
// ...
MenuPanel.Pop();

Block

Takes a fixed size and centers its one child in it. The child keeps its own size, up to the block’s. A side you leave out is the child’s size plus padding. It pops itself after that child.

Block.Put(width: 200f, height: 50f);
Label.Put("Centered in 200 by 50");

With only a padding, it puts room around its child, like an icon in a button:

Button.Put();
Block.Put(padding: 10f);
Icon.Put(_playIcon);

Dock

Places its one child at a fixed rectangle, given as its left, top, right and bottom edges.

Dock.Put(20f, 20f, 300f, 100f);
Label.Put("Top left corner");

FloatingWindow

A window with a title bar you drag it by. It opens in the middle of the UI unless you pick another spot, stays inside it, and comes to the front when it’s pressed or gets focus.

FloatingWindow.Push("Inventory");
Label.Put("Sword");
// ...
FloatingWindow.Pop();

With ref to your open state, it gets a close button, and back closes it while the focus is in it:

if (_inventoryOpen) {
    FloatingWindow.Push(ref _inventoryOpen, "Inventory");
    // ...
    FloatingWindow.Pop();
}

A window titled "Inventory" with a close button and three items in it

Its components go in Content, a vertical stack, so you can change its spacing and padding there. It scrolls when the window is taller than the UI.

StartAlign picks where it opens, from 0 to 1 on each axis, and StartOffset moves it from there. This one opens in the top right corner, 20 units from the edges:

FloatingWindow w = FloatingWindow.Push("Map");
w.StartAlign = new Vector2(1f, 0f);
w.StartOffset = new Vector2(-20f, 20f);

They’re only read when the window opens. After that it stays where the player drags it.

The arrow keys and the d-pad stay inside a floating window once the focus is in it. Focus explains why.

Clipping

Every component has a Clip, the part of it that its parents leave visible. Stacks cut what their children draw to that rectangle. Labels, icons and paragraphs also cut themselves when their text or texture is bigger than they are. A custom component that draws past its bounds can do the same with GuiHelper.PushClip(Clip) and GuiHelper.PopClip(). A clip you push stays inside the clips that are already there, so a component in a stack that scrolls can’t draw outside of it. Floating windows and popups keep their content inside their border.

ClipRounding on a stack rounds the corners of that cut. Each clip keeps its own rounding, so a rounded stack inside another rounded stack that scrolls is cut by both. Up to four clips that cut across each other apply at once. A clip that sits whole inside another doesn’t count toward that, which covers the usual stacks inside stacks.

Edit this page on GitHub