UI
In-game UI is built from three kinds of file that sit side by side in ui/:
| File | Holds | Like |
|---|---|---|
hud.ui |
The widget tree, in JSX-style markup | HTML / JSX |
hud.style |
Styles: flexbox and grid layout, colors, fonts | CSS (a subset) |
hud.cast (any name) |
Event handlers and code that publishes values | your script |
UI belongs to screens: a .screen lists the UI files it shows.
// ui/hud.ui (shortened from samples/farmhouse)
import { selectLantern, noteChanged } from "./hud";
export default ui(
<panel class="hud">
<text class="heading">Player health</text>
<progress id="health" class="health" value={player.health.current} max={player.health.max} />
<button id="lantern" class="slot" onClick={selectLantern}>Lantern</button>
<textInput id="inventoryNote" placeholder="Inventory note" onChange={noteChanged} />
</panel>
);
Widgets
panel, text, image, button, slider, toggle, textInput, list, scrollView
and progress. Panels, lists and scroll views contain other widgets; text and buttons
contain text. The .ui reference lists each widget's
attributes.
Data binding: Cast publishes, the UI reads
A .ui file never runs gameplay code. Inside braces it can only use literals and dotted
binding names. Cast publishes values under those names:
// behaviors/health.cast (from samples/farmhouse)
uiSetNumber("player.health.current", obj.health.current);
uiSetNumber("player.health.max", obj.health.max);
uiSetNumber(name, float),uiSetBool(name, bool)anduiSetText(name, string)copy the value into engine memory. It stays there until you set it again, for the whole game session, so you only need to publish when it changes (publishing every tick is fine too).- One name holds one value; setting it with another kind replaces it.
- Text can mix literal text with several bindings:
<text>{inventory.filter} {inventory.volume}</text>.
Events: the UI calls Cast
Event attributes name functions imported from a Cast module (with a relative path):
| Attribute | On | Handler signature |
|---|---|---|
onClick |
button |
(): void |
onHover |
button |
(): void, when the pointer enters an enabled button |
onChange |
slider |
(value: float): void or (): void |
onChange |
toggle |
(value: bool): void or (): void |
onChange |
textInput |
(value: string): void or (): void |
// ui/hud.cast (from samples/farmhouse)
import { uiSetText } from "molten";
export function noteChanged(value: string): void {
uiSetText("inventory.note", value);
}
A widget's own value (the slider position, the typed text) stays local to the widget until
Cast publishes a new value for its value binding.
Styles
/* ui/hud.style (from samples/farmhouse, shortened) */
.hud {
position: absolute;
top: 24px;
left: 24px;
width: 280px;
padding: 14px;
flex-direction: column;
gap: 8px;
background-color: #141e2bef;
border-radius: 12px;
font-family: Inter;
}
.inventory { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.slot:hover { background: #4f8497; transition: background 120ms; }
Selectors are .class, #id and the states :hover, :focus, :disabled. Rules cascade
by specificity and source order. Everything supported is listed in the
.style reference.
Input
Tab moves focus between focusable widgets; the mouse wheel scrolls a scrollView. A
mouse press that the UI uses is not reported to gameplay's mousePressed.
Live reload keeps widget state
Saving a .ui or .style file rebuilds the tree in about a millisecond and keeps each
widget's focus, scroll position, typed text and caret, matched by widget id and type.
Give widgets that have state a stable id. A broken file keeps the previous tree and logs
an error with its position. Reloading Cast replaces the event handlers without touching
widget state.
Fonts and drawing
Text is drawn with signed-distance-field fonts. font-family: Inter is the built-in face;
font-family: "fonts/MyFont.ttf" uses a TrueType file from assets/. Atlases cover
Latin-1 plus a replacement glyph. UI is drawn after the 3D scene.