# Interface

Build menus, buttons, sliders and a HUD without writing layout code. They are things in
the scene, like everything else. A thing
with a `Node` is a box; boxes sit in boxes; the engine lays them out and drives them from
input actions each step, before the scripts, which read what was used in that same step.
The focus is world state, so a recording replays a menu. Every field is in the
[interface reference](../reference/ui.md).

![The menu-ui sample at step 120](../files/examples/menu-ui/shots/menu.png)

That frame is the menu-ui sample: a stack of a
button, a slider, a choice and a toggle, a health bar that drains, and a line cut short
with `...`. Its project file says which actions drive the interface and its design height:

```toml
# example: menu-ui/game.toml
# The interface guide's game: a menu laid out by the engine (a stack of a button, a
# slider, a choice and a toggle), a health bar, and a line cut short with an ellipsis,
# all at a design height of 180 that is drawn at the frame's height.
#
#   frost dev game.toml
#   frost run --headless --steps 120 --shot shot.png game.toml
scene = "menu.toml"
scripts = ["menu.luau"]
seed = 1
shots = [{ name = "menu", step = 120 }]
replay = { steps = 120, checksum = "2a6eedea15152a6d", engine = "0.0.1 r280" }
# The run's checksum on the way there, written by `frost test --write`: a test that
# fails says between which two of these steps the game first went another way.
checkpoints = []

[window]
size = [640, 360]
background = 0x15171c

[input]
actions = ["move_x", "move_y", "accept", "back"]
# Down to the slider, right twice, down to the choice, accept, up to the top, accept.
recording = [
    { steps = 20 },
    { steps = 2, move_y = -1 }, { steps = 10 },
    { steps = 2, move_x = 1 }, { steps = 10 },
    { steps = 2, move_x = 1 }, { steps = 10 },
    { steps = 2, move_y = -1 }, { steps = 10 },
    { steps = 2, accept = 1 }, { steps = 10 },
    { steps = 2, move_y = 1 }, { steps = 4 }, { steps = 2, move_y = 1 }, { steps = 10 },
    { steps = 2, accept = 1 }, { steps = 20 },
]

[input.keys]
move_x = [{ negative = "Left", positive = "Right" }, "PadLeftX"]
move_y = [{ negative = "Down", positive = "Up" }, "PadLeftY"]
accept = ["Enter", "Space", "PadA"]
back = ["Escape", "PadB"]

# The actions that drive the interface, the node a menu opens on, and the design height:
# every node and text with Screen is laid out in units of a 180-high frame.
[ui]
fit = 180
x = "move_x"
y = "move_y"
accept = "accept"
back = "back"
start = "play"

[ui.theme]
normal = 0x8a8f98
focus = 0xf2a53b
pressed = 0xffffff
```

## Boxes and where they go

The menu, with no `Transform` saying where anything is:

```toml
# example: menu-ui/menu.toml
# Every thing here is a node: the engine lays them out, so no Transform says where.
[menu]
Transform = {}
Screen = {}
Node = {}
Anchor = { dy = 6 }
Stack = { gap = 4, pad = 4 }
Rect = { color = 0x1c1f27, layer = 9 }

[play]
Transform = {}
Screen = {}
Node = { parent = "menu", order = 1, w = 110, h = 20 }
Rect = { layer = 10 }
Text = { text = "PLAY", color = 0x15171c, layer = 11 }
Button = { event = "play" }

[volume]
Transform = {}
Screen = {}
Node = { parent = "menu", order = 2, w = 110, h = 20 }
Rect = { layer = 10 }
Text = { text = "VOLUME", color = 0x15171c, layer = 11 }
Slider = { value = 0.5, step = 0.1, event = "volume", fill = "volume_fill" }

[volume_fill]
Transform = {}
Screen = {}
Node = { parent = "volume", h = 3 }
Anchor = { x = 0, y = 0, dx = 2, dy = 2 }
Rect = { color = 0x15171c, layer = 11 }

[quality]
Transform = {}
Screen = {}
Node = { parent = "menu", order = 3, w = 110, h = 20 }
Rect = { layer = 10 }
Text = { text = "", color = 0x15171c, layer = 11 }
Choice = { options = "LOW|MEDIUM|HIGH", index = 1, event = "quality" }

[sound]
Transform = {}
Screen = {}
Node = { parent = "menu", order = 4, w = 110, h = 20 }
Rect = { layer = 10 }
Text = { text = "SOUND", color = 0x15171c, layer = 11 }
Toggle = { on = true, event = "sound" }

# A bar nobody moves: the script sets its value and the fill follows.
[health]
Transform = {}
Screen = {}
Node = { w = 100, h = 8 }
Anchor = { x = 0, y = 1, dx = 6, dy = -6 }
Rect = { color = 0x2b3140, layer = 9 }
Bar = { value = 1, fill = "health_fill" }

[health_fill]
Transform = {}
Screen = {}
Node = { parent = "health", h = 4 }
Anchor = { x = 0, dx = 2 }
Rect = { color = 0x6fd16f, layer = 10 }

# One line cut to the width with "..." rather than wrapped.
[news]
Transform = {}
Screen = {}
Node = {}
Anchor = { y = 0, dy = 6 }
Text = { text = "THE CARAVAN LEAVES AT DAWN FROM THE EASTERN GATE", color = 0xeceae6 }
Wrap = { w = 150, ellipsis = true }

[said]
Transform = {}
Screen = {}
Node = {}
Anchor = { x = 1, y = 1, dx = -6, dy = -6 }
Text = { text = "", color = 0x8a8f98 }
```

- A node's box has its middle on its `Transform` and is `w` by `h`; its `Rect` or
  `Sprite` is drawn that size. Write the size once, in `Node`.
- A node with a `Text` and no size is as big as its words; a `Stack` or `Grid` with none
  is as big as what is shown in it.
- A node goes where the `Stack` or `Grid` it is in puts it (by `order`, lower first: give
  each one), else where its `Anchor` says, else where its `Transform` is. A node in
  nothing is anchored to the frame: `Anchor = { x = 1, y = 1, dx = -6, dy = -6 }` is six
  units in from the top-right corner.
- `stretch_x` and `stretch_y` make a node as wide or as tall as what it is in.
- Give interface things `Screen`, so the camera does not move them.
- `set(find("pause"), "Node", "shown", true)` shows a menu and everything in it.

## Controls

| On a node | Is used by | Sends |
| --- | --- | --- |
| `Button = { event = "play" }` | accept, or a click | its event |
| `Slider = { value, min, max, step, fill }` | left and right, or a drag | its event with the new value |
| `Choice = { options = "LOW\|MEDIUM\|HIGH" }` | accept, left and right | its event with the new index; writes the option into its text |
| `Toggle = { on, mark }` | accept, or a click | its event with whether it is on |
| `Bar = { value, fill }` | nobody: a script sets `value` | nothing |

A script that sets a `value`, an `index` or an `on` itself sends nothing. `locked = true`
takes the focus and refuses to be used (`ui_locked` is sent instead); an `off` button is
not there at all. `modal = true` makes a shown node the only one that can be used, and
`back = "closed"` on a menu sends that event when the back action is used anywhere in it.

The sample's script reads what was used:

```lua
-- example: menu-ui/menu.luau
-- The interface sends events as it is used; the script reads them in the same step.
state.said = state.said or "PICK ONE"
state.volume = state.volume or 0.5

local function say(text)
    state.said = text
    set(find("said"), "Text", "text", text)
end

function on_scene(name)
    say(state.said)
    -- ui.measure: how wide a text is drawn, in design units at size 1.
    local width = ui.measure("PLAY")
    print("PLAY is " .. width .. " units wide")
end

function step(dt)
    for _, e in ipairs(events("play")) do say("PLAY") end
    for _, e in ipairs(events("volume")) do
        state.volume = e.value
        say("VOLUME " .. math.floor(e.value * 10 + 0.5))
    end
    for _, e in ipairs(events("quality")) do say("QUALITY " .. e.value) end
    for _, e in ipairs(events("sound")) do say(if e.value ~= 0 then "SOUND ON" else "SOUND OFF") end
    -- The bar drains with time, to show it follows its value.
    local health = find("health")
    set(health, "Bar", "value", math.max(0, get(health, "Bar", "value") - 0.2 * dt))
end
```

## Focus

`[ui]` names the actions: `x` and `y` move the focus (and a slider), `accept` uses the
node with the focus, `back` goes back, and `press` is the pointer's button. One node has
the focus; a direction moves it to the nearest one that way, round a stack's ends and
along a grid's rows. Where the rules guess wrong, a node names its neighbour with `up`,
`down`, `left` or `right`. When a menu opens, the focus goes to the node that last had
it, else the `[ui]` `start` node. `ui.focus(entity)` gives it to a node. `[ui.theme]`
colours a usable node by its state: `normal`, `focus`, `pressed`, `off`, `locked`.

A pointer press that began on a shown node is the interface's: scripts read the `press`
action as 0 until it is let go, so a click on a button never fires the game's gun too.

## A design height

`[ui] fit = 180` lays the interface out in units of a frame 180 high and draws it at the
frame's real height: the sample's frame is 360 high, so every node and text is drawn at
twice its size, and a frame of another height draws the same layout larger or smaller. Everything
with `Screen` follows it, in 2D and in 3D, and the pointer finds nodes in the same units.
Left out, the interface is laid out in pixels. `frame.size()` still says the frame's pixels.

## Text

The built-in font is 5 by 8 pixels a glyph, printable ASCII, `size` a whole number, so
every font pixel is a sharp square. `align` says where `Transform.x` is along a line,
`Shadow` draws a copy under it, and `Wrap = { w = 150 }` starts new lines before a word
that would pass the width. `Wrap = { w = 150, ellipsis = true }` cuts each line to the
width with `...` instead, as the sample's news line does.

`ui.measure("PLAY")` is how wide and tall a text is drawn, in design units at size 1
(the sample prints 23: four letters of six font pixels, less the last gap). Give it a
style name to measure a text in that style.

A game's own fonts are text styles under `[fonts]`: a size in pixels and one or more
TrueType or OpenType files, each character drawn from the first file that has it, so a
second file adds an alphabet the first lacks. A text is written in a style with `Font = {
style = "body" }` beside it. Letters keep their own widths and kerning, and each glyph
stands on whole pixels. Not yet: joining scripts (Arabic, Devanagari), right-to-left
lines, colour emoji, combining marks, and text outlines. Fonts are the game's own files:
never load one from a player or a server.

## Checks

```sh
frost check game.toml
frost test game.toml
```

Over the protocol, `ui.tree` reads the laid-out tree (each node's box, in design units)
and `ui.focus` moves the focus: what an agent uses to see a menu without a picture.
