# ui

Part of the [scripting reference](index.md), written by the engine so it matches your `frost`. Start with [your first game](../start/first-game.md) if you are new.

Use when building menus, buttons, sliders or HUD layout, changing focus, displaying text or loading fonts.

| In a script | Does |
| --- | --- |
| `ui.measure("text", "style"?)` | Width and height in design units at Text.size 1, using the declared font style; omit style for the built-in font. Unknown styles are errors |

| Component | Field | Type | Default | Meaning |
| --- | --- | --- | --- | --- |
| `Text` | `text` | text | `""` | What to write. `\n` starts a new line |
| `Text` | `size` | whole number | 1 | World units to one font pixel: a glyph is 5 by 8 font pixels, the next starts 6 further on and the next line 10 down, so a line of n letters is `(6n - 1) * size` wide. Less than 1 is 1 |
| `Text` | `color` | whole number | `0xffffff` | The colour as `0xRRGGBB` |
| `Text` | `align` | number | 0.5 | Where `Transform.x` is along each line: 0 its left end, 0.5 its middle, 1 its right end |
| `Text` | `layer` | whole number | 0 | Drawing order, shared with `Rect` and `Sprite` |
| `Shadow` | `color` | whole number | `0x000000` | The colour of the copy of the entity's text drawn under it |
| `Shadow` | `x` | number | 2 | How far right of the text the copy is drawn, in the text's units |
| `Shadow` | `y` | number | -2 | How far up; below the text is negative |
| `Font` | `style` | text | `""` | The text style the entity's `Text` is written in: a name under `[fonts]` in the project file. `""` is the built-in font |
| `Wrap` | `w` | number | 0 | The entity's `Text` starts a new line before a word that would pass this width, in units. 0: it does not |
| `Wrap` | `ellipsis` | true or false | false | Truncate each explicit line to `w` with `...` instead of wrapping; dots shorten when the width cannot fit them |

### Text

One font is built in: 5 by 8 pixels a glyph, the printable ASCII characters, drawn
for this engine (760 bytes). A character it does not have is drawn as `?`. It has one
width for every glyph, 6 font pixels from one to the next, and lines are 10 apart.
`size` is a whole number, and each line starts on a whole unit, so in a frame with one
unit to a pixel every font pixel is a sharp square. This is enough for a score and a
title.

**A game's own fonts.** The project file names *text styles* under `[fonts]`: each is a
size in pixels and one or more TrueType or OpenType files (`.ttf`, `.otf`). A `Text`
on an entity that also has `Font = { style = "body" }` is written in that style:

```toml
# game.toml
[fonts]
body = { files = ["fonts/Inter.ttf", "fonts/NotoSansSC.otf"], size = 24 }

# a scene
[greeting]
Transform = { x = 160, y = 90 }
Text = { text = "Hello, 你好" }
Font = { style = "body" }
```

- Each character is drawn from the first file of the style that has it, so a second
  file adds an alphabet the first lacks. One that none has is the first file's sign
  for a missing glyph (often a box).

- The style's `size` is the height of the font's em in pixels, 4 to 200, as in a
  drawing program. `Text.size` multiplies it and stays a whole number: 1 is the style
  as it is, sharp; 2 is the same glyphs drawn twice as large and blended. For large
  sharp letters make a second style at the larger size.

- Letters have their own widths and pairs like `AV` close up (kerning). Lines are the
  font's own distance apart. `align`, `color`, `layer`, `Shadow`, `Opacity` and `Scale`
  work as for the built-in font, and each glyph stands on whole pixels.

- Not here: joining letters (Arabic, Devanagari), right-to-left
  lines, emoji in colour, and marks that combine with the letter before them.

- A glyph is drawn once, the first time a text shows it, and kept: a page of 512 by
  512 pixels holds a few hundred letters at 24 pixels, and more pages are made as
  needed (1 MB each). A game that shows thousands of different large characters
  uses memory for each.

- Fonts are the game's own files and ship with it. Never let a player or a server
  supply one: the reader (stb_truetype) trusts what a font file says about itself.

- A game with no `[fonts]` is exported without any of this.

## Interface

Menus and controls are entities in the scene file. An entity with a `Node` is a box;
boxes sit in boxes; a `Button`, a `Slider`, a `Choice` or a `Toggle` on a node is used
by a person, and a `Bar` shows a number. The engine lays the boxes out and drives them
from input actions each step, before the scripts, which read what was pressed in that
step.

Set `[ui] fit = 720` to author the interface at a design height of 720. Layout,
Anchor offsets, Node sizes, Stack/Grid spacing, and `ui.tree` use design units.
Everything with `Screen` (including text, shapes and Slice borders without a Node)
is drawn at frame height / 720 in both 2D and 3D. The pointer uses the same layout.
Omit `fit` to keep pixel units. `frame.size()` still reports frame pixels.

`local width, height = ui.measure("Hello", "body")` measures text in a declared font
style, in design units at Text.size 1. Omit the style for the built-in pixel font.
Multiply both results by Text.size for a larger text. Unknown styles are errors.
`Wrap = { w = 240 }` wraps text; `Wrap = { w = 240, ellipsis = true }` truncates each
explicit line with `...`, shortened further if even the dots cannot fit.

| Component | Field | Type | Default | Meaning |
| --- | --- | --- | --- | --- |
| `Node` | `parent` | text | `""` | The node this one is in, by name. Empty: the frame |
| `Node` | `w` | number | 0 | Its box's width. 0: that of its `Text`, or of what its `Stack` or `Grid` holds |
| `Node` | `h` | number | 0 | Its box's height, the same way |
| `Node` | `order` | whole number | 0 | Its turn in its stack or grid, lower first; equal ones go by name |
| `Node` | `shown` | true or false | true | False hides it and every node in it |
| `Node` | `focus` | true or false | true | False: keys and a gamepad cannot reach it, only the pointer |
| `Node` | `min_w` | number | 0 | The least width of its box |
| `Node` | `min_h` | number | 0 | The least height |
| `Node` | `back` | text | `""` | The event sent, about this node, on back with the focus on or in it |
| `Node` | `modal` | true or false | false | True: while shown, only nodes in it can be used |
| `Node` | `locked` | true or false | false | True: it takes the focus and refuses to be used |
| `Node` | `up` | text | `""` | The node, by name, the focus goes to going up. Empty: the rules say |
| `Node` | `down` | text | `""` | Going down |
| `Node` | `left` | text | `""` | Going left |
| `Node` | `right` | text | `""` | Going right |
| `Node` | `pointer` | true or false | true | False: the pointer goes through it, and a press on it is the game's |
| `Anchor` | `x` | number | 0.5 | Which point of the node sits on the same point of what it is in: 0 left edges, 0.5 middles, 1 right edges |
| `Anchor` | `y` | number | 0.5 | The same from the bottom: 0 bottom edges, 1 top edges |
| `Anchor` | `dx` | number | 0 | Units right of there |
| `Anchor` | `dy` | number | 0 | Units up from there |
| `Anchor` | `stretch_x` | true or false | false | True: from the left edge of what it is in to the right, less `dx` at each end |
| `Anchor` | `stretch_y` | true or false | false | The same from bottom to top, less `dy` at each end |
| `Stack` | `dir` | text | `"down"` | Which way its nodes follow one another: `"down"`, `"up"`, `"right"` or `"left"` |
| `Stack` | `gap` | number | 0 | The space between one and the next |
| `Stack` | `pad` | number | 0 | The space kept clear inside its edges |
| `Stack` | `align` | number | 0.5 | Where each sits across the stack: 0 left or bottom, 0.5 middle, 1 right or top |
| `Grid` | `columns` | whole number | 2 | Cells in a row. They share the width equally |
| `Grid` | `row` | number | 0 | A row's height |
| `Grid` | `gap` | number | 0 | The space between cells, both ways |
| `Grid` | `pad` | number | 0 | The space kept clear inside its edges |
| `Button` | `event` | text | `""` | The event sent, about the button, when it is used |
| `Button` | `off` | true or false | false | True: it cannot be used or take the focus |
| `Slider` | `value` | number | 0 | Where it stands, `min` to `max` |
| `Slider` | `min` | number | 0 | Its least value |
| `Slider` | `max` | number | 1 | Its greatest |
| `Slider` | `step` | number | 0.1 | How far apart its values are. 0: any value, and a key moves it a tenth of the way |
| `Slider` | `event` | text | `""` | The event sent, with its new value, when a person moves it |
| `Slider` | `fill` | text | `""` | A node, by name, kept as wide as the slider is full |
| `Slider` | `label` | text | `""` | The entity, by name, whose `Text` reads the value, with the decimals of `step` |

| Component | Field | Type | Default | Meaning |
| --- | --- | --- | --- | --- |
| `Choice` | `options` | text | `""` | What can be chosen, `\|` between: `"Low\|Medium\|High"` |

| Component | Field | Type | Default | Meaning |
| --- | --- | --- | --- | --- |
| `Choice` | `index` | whole number | 0 | Which is chosen, from 0 |
| `Choice` | `event` | text | `""` | The event sent, with the new `index`, when a person changes it |
| `Choice` | `label` | text | `""` | The entity, by name, whose `Text` reads the chosen option. Empty: the choice's own `Text` |
| `Toggle` | `on` | true or false | false | Whether it is on |
| `Toggle` | `event` | text | `""` | The event sent, with the new `on`, when a person switches it |
| `Toggle` | `mark` | text | `""` | A node, by name, shown only while it is on: a tick |
| `Bar` | `value` | number | 0 | What it shows, `min` to `max` |
| `Bar` | `min` | number | 0 | The value at which it is empty |
| `Bar` | `max` | number | 1 | The value at which it is full |
| `Bar` | `fill` | text | `""` | A node, by name, kept as wide as the bar is full |
| `Inherit` | `alpha` | number | 1 | The interface's to write: what a node takes from those it is in. Multiplies its `Opacity`; 0 while hidden |
| `Inherit` | `x` | number | 1 | Multiplies its `Scale.x` |
| `Inherit` | `y` | number | 1 | Multiplies its `Scale.y` |

- **Boxes.** A node's box has its middle on the node's `Transform` and is `w` by `h`.
  Its `Rect` or `Sprite` is drawn that size (with a `Slice`, a panel of any size):
  write the size once, in `Node`. A button's words are its own `Text`. An icon inside a box is a node of its own with the box as its `parent`.
  Give interface entities `Screen`, so the camera does not move them.

- **Sizes nobody writes.** A node with a `Text` and no `w` or `h` is as big as its
  words are drawn (`Wrap = { w = 120 }` breaks them into lines), so an `Anchor` places
  it by its edges. A `Stack` or `Grid` with none is as big as what is shown in it.

- **Where a node goes.** Where the `Stack` or `Grid` of what it is in puts it; 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 = -8, dy = -8 }` is eight units in from
  the top right corner.

- **Stretching.** `Anchor = { stretch_x = true, dx = 8 }` makes a node as wide as what
  it is in, less eight units at each side; with `stretch_y` too it fills the box: a
  backdrop is a node in nothing with both, and `pointer = false` unless it should
  swallow presses. A node stretched across its stack fills it from pad to pad, those
  stretched along it share what the rest leave, and one in a grid fills its cell.

- **Stacks and grids.** A stack puts its nodes one after another by their `order`,
  from the edge its direction leaves. Give each its `order` (1, 2, 3): a scene file's
  entities are kept by name, not as written. A grid fills rows from the top, left to
  right, each node in the middle of its cell. A node that is not shown takes no room.

- **Showing and hiding.** `set(find("pause"), "Node", "shown", true)` shows a menu and
  everything in it. A node's `Opacity` and `Scale` are its children's too. What a
  script changes is laid out, and the
  focus given, at the end of that step.

- **Focus.** One node that can be used has the focus. A direction moves it to the
  nearest node that way: one level with it first, else one no further to the side
  than ahead. With none, a node of a stack that runs that way goes round to the
  stack's other end, and any other stays. In a grid it goes along the row or the
  column, past cells that cannot be used, and leaves by an edge only for a node beyond
  it; coming back, it returns to the cell it left. Where these guess wrong, a node
  names its neighbour: `up`, `down`, `left`, `right`. A held direction counts again
  after 24 steps and then every 7. An axis counts past 0.5; a stick pushed both ways
  goes the way it is pushed further.

- **Where it starts.** When nothing has the focus, the node
  that last had it among those usable now takes it: a screen shown again is where it
  was left. Else the `[ui]` `start` node, else the highest usable node, the leftmost
  of those level. `ui.focus(entity)` gives it to a node that can be used.

- **Modal nodes.** While a node with `modal = true` is shown, only the nodes in it can
  be used. Of several, the one with the highest `order` has the interface. It opens as new each time; when it is
  hidden the focus is back where it was.

- **Using a node.** The accept action uses the node with the focus: a button sends its
  event, a choice goes to its next option, a toggle switches. Left
  and right move a slider by its step and turn a choice; up and down still move the
  focus. The pointer gives the focus to the node it moves onto; a press takes hold of
  that node and letting go on it uses it, so sliding off first cancels. A pressed
  slider follows the pointer until it lets go. The pointer finds only nodes with
  `Screen`. A node with `locked = true` takes the focus and is not used: `ui_locked`
  is sent about it instead. An `off` button is not there at all.

- **A press on the interface is the interface's.** When the pointer's press begins on
  a shown node's box (any with a `w` and an `h`), or anywhere while a
  modal node is shown, scripts read the `[ui]` `press` action as 0 until it is let go.

- **Bars.** A `Bar` is a slider nobody moves: a script sets its `value` and its `fill`
  node follows, hidden when the bar is empty. A fill with an
  `Anchor.dx` keeps that much clear at both ends.

- **Events.** A use sends the event the node names, about the node, and scripts read
  it in the same step with `events`. A slider's event
  carries its value, a choice's its index and a toggle's whether it is on. `ui_moved`
  is the engine's: about the node a person just moved the focus to. A
  script that sets a `value`, an `index` or an `on` sends nothing.

- **Going back.** The back action sends the event named by the `back` of the node
  with the focus, else of the nearest node it is in that names one, about that node:
  give a menu `back = "closed"` and every control in it goes back the same way. With
  none, `ui_back` is sent about the node with the focus.

- **Colours.** With a `[ui.theme]`, a usable node takes the colour of its state each
  step: in its `Rect.color`, else its `Sprite.tint`, else its `Text.color`.

- **Errors** are `E1301` to `E1306`. `E1306`: a tween cannot move the `Transform` of
  a node the interface places; tween its `Anchor.dx`, `Scale` or `Opacity`.

A menu in the middle of the frame, in a scene file:

```toml
[menu]
Transform = {}
Screen = {}
Node = {}
Anchor = {}
Stack = { gap = 4 }

[start]
Transform = {}
Screen = {}
Node = { parent = "menu", w = 120, h = 28 }
Rect = { color = 0x334455, layer = 10 }
Text = { text = "START", layer = 11 }
Button = { event = "start" }
```

```toml
# Optional: text styles, which a text is written in with `Font = { style = "body" }`
# beside its `Text`. A style is a size in pixels (4 to 200) and one to eight TrueType or
# OpenType files; each character is drawn from the first that has it. The files ship
# with the game. Left out: every text is in the built-in pixel font. See Text.
[fonts]
body = { files = ["fonts/Inter.ttf", "fonts/NotoSansSC.otf"], size = 24 }
title = { files = ["fonts/Inter.ttf"], size = 64 }
```

```toml
[ui.theme]
focus = 0xf2a53b
```
