Skip to content

UI Components

The widget factories build common controls out of the UI component model (UINode + Interactable + a $interaction controller with gears), so you don’t wire each one by hand. Each factory takes an options object and returns a handle — always with entity and dispose(), plus imperative methods to read and drive it.

Interactive widgets need the world and the UI event queue — world from GetWorld() and events from Res(UIEvents), exactly as in the UI guide. They all accept parent?, a node? (UINode layout — default: fill the parent), and visual overrides; visual defaults come from the active theme’s roles and re-resolve on a live theme swap. Interactive widgets are keyboard-reachable by default: they carry a Focusable, so Tab reaches them, Enter/Space clicks them, and arrow keys drive sliders — opt out with focusable: false.

The built-in widgets running — a Button, Toggle, Slider, Progress bar, a Dialog trigger, and more, in one panel

The built-in widgets (from the ui-controls example): a Button, a Toggle, a Slider, a Progress bar, and buttons that open a Dialog and cycle an accent color — each a plain entity tree with the UI components on it.

createButton(opts) spawns a clickable entity (with an optional child label) that transitions between visual states. Returns a ButtonHandle. A click is a press-then-release over the button — or Enter/Space while it has keyboard focus.

import { createButton } from 'esengine';
const btn = createButton({
world, events, parent,
text: 'Play',
node: { width: px(160), height: px(44) },
states: {
normal: { color: { r: 0.20, g: 0.55, b: 1.0, a: 1 } },
hover: { color: { r: 0.30, g: 0.65, b: 1.0, a: 1 } },
pressed: { color: { r: 0.12, g: 0.45, b: 0.9, a: 1 } },
disabled:{ color: { r: 0.3, g: 0.3, b: 0.3, a: 1 } },
},
onClick: (entity) => { /* … */ },
});
Option Default Description
node fill the parent The button’s UINode layout.
text none Label — a string or a TextInit. Omit for no label. The label color/size default to the theme’s text role and label type size.
states theme control roles Map of state name (normal / hover / pressed / disabled / focused) → color / sprite / scale override. Custom names (e.g. 'loading') are allowed.
background solid white Background visual (a UIVisualInit).
fadeDuration 0 (snap) Tween time for color/scale state changes; sprite swaps always snap.
disabled false Start disabled.
focusable / tabIndex true / 0 Keyboard reachability and Tab order.
onClick(entity) — Click callback (gated on the enabled state).
userGesture false Run onClick inside the host’s own release event instead of on the next frame — for a call a host accepts only from a player’s gesture (sharing a recording on WeChat).

Handle: entity, setDisabled(disabled), dispose().

The theme-default states are also exported as themeButtonStates() — spread and override them instead of restating all five. Flip a custom state later with setButtonState(world, handle.entity, 'loading'); the pointer driver leaves any non-canonical page alone until you switch back.

createToggle(opts) composes a button with an on/off check indicator. Returns a ToggleHandle.

import { createToggle } from 'esengine';
const toggle = createToggle({
world, events, parent,
interactionStates: { normal: { color: /* … */ }, hover: { /* … */ }, pressed: { /* … */ } },
check: { color: { r: 0.2, g: 0.8, b: 0.4, a: 1 } }, // the "on" indicator
isOn: true,
onChange: (isOn, entity) => { /* … */ },
});
toggle.setValue(false); // programmatic change (fires onChange)
Option Default Description
node / background fill the parent / solid white Frame layout + background, as in createButton.
interactionStates theme control roles Frame visual states (like the button’s states).
check theme primary fill The on-state indicator: { node?, color?, sprite? }, a child shown when on. Fills the frame by default — inset it via check.node.
isOn false Initial state.
disabled false Start disabled.
focusable / tabIndex true / 0 Keyboard reachability.
onChange(isOn, entity) — Fires on toggle.

Handle: getValue(), setValue(value), setDisabled(disabled), entity, dispose().

createSlider(opts) builds a track + fill + handle. Returns a SliderHandle. Input is built in: drag anywhere on the track (press captures until release), or focus it and use arrow keys (step, or 1% of the range when continuous), Home/End.

import { createSlider } from 'esengine';
const slider = createSlider({
world, events, parent,
min: 0, max: 100, value: 50, step: 5,
onChange: (value) => { /* … */ },
});
slider.setValue(75);
Option Default Description
node fill the parent The track’s UINode layout.
min / max 0 / 1 The range.
value min Initial value (clamped and step-quantized).
step 0 (continuous) Quantization step. Also the keyboard nudge.
handleWidth 12 Thumb width in pixels.
disabled / focusable / tabIndex false / true / 0 Interactivity + keyboard reachability.
trackVisual / fillVisual / handleVisual theme track / primary / onPrimary Per-part visuals.
onChange(value, entity) — Fires on value change — from input, setValue, or any other writer.

Handle: getValue(), setValue(v), dispose(), plus entity / trackEntity (the same entity) / fillEntity / handleEntity.

createProgress(opts) is a non-interactive fill bar. Returns a ProgressHandle. It takes no events — nothing to interact with.

import { createProgress } from 'esengine';
const bar = createProgress({
world, parent,
direction: 'right',
fill: { color: { r: 0.2, g: 0.8, b: 0.4, a: 1 } },
value: 0,
});
bar.setValue(0.6); // 0..1
// Radial gauge (cooldown / ring meter) — a clockwise wedge from 12 o'clock:
const ring = createProgress({ world, parent, radial: true, value: 0.75 });
Option Default Description
node fill the parent The track’s UINode layout.
value 0 Initial progress, 0..1 (clamped).
direction 'right' Linear fill growth: 'right', 'left', 'up', 'down'.
radial false Radial gauge instead of a bar — a clockwise wedge sweeping the full circle. Ignores direction.
background theme track Track visual (a UIVisualInit).
fill theme primary The filled bar: { color?, sprite? }.

Handle: getValue(), setValue(v), dispose(), plus entity / fillEntity.

createDropdown(opts) shows the current selection and opens a popup of option rows. It’s generic over the option type. Returns a DropdownHandle<T>.

import { createDropdown } from 'esengine';
const dd = createDropdown({
world, events, parent,
options: ['Low', 'Medium', 'High'],
selectedIndex: 1,
onSelect: (index, option) => { /* … */ },
});
// Objects need a label mapper:
createDropdown({
world, events, parent,
options: resolutions, // e.g. { w, h }[]
optionToLabel: (r) => `${r.w}×${r.h}`,
onSelect: (i, r) => applyResolution(r),
});
Option Default Description
node fill the parent The main button’s UINode layout.
options — The list of selectable values. Required.
selectedIndex 0 Initial selection.
optionToLabel(option, i) String(option) Map a non-string option to its display text.
buttonStates theme control roles { normal?, hover?, pressed? } colors for the main button (option rows use theme roles).
optionHeight 32 Row height in pixels.
disabled / focusable / tabIndex false / true / 0 Interactivity + keyboard reachability.
onSelect(index, option, entity) — Fires on selection.

Handle: getSelected(), getSelectedIndex(), setSelectedIndex(i), open(), close(), isOpen(), dispose(), plus entity / labelEntity (the child Text showing the current selection).

createDialog(opts) is a modal: a click-blocking backdrop with a centered panel, hidden until opened. Returns a DialogHandle. Add your content as children of panelEntity — closing sets display: none on the backdrop root, so the whole subtree (your content included) leaves layout, rendering, and input in one write.

import { createDialog, spawnUIEntity } from 'esengine';
const dialog = createDialog({
world, events, parent,
panelNode: { width: px(420), height: px(260) },
});
spawnUIEntity({ world, parent: dialog.panelEntity, text: { content: 'Are you sure?' } });
dialog.open();
// …later
dialog.close();
Option Default Description
backdropNode / backdropVisual fill the parent / theme backdrop scrim The full-viewport scrim behind the panel. It blocks pointer hits but is not focusable.
panelNode / panelVisual 400×300 centered / theme surface The modal panel. The panel swallows clicks so only true scrim clicks dismiss.
startHidden true Start closed.
closeOnEscape / closeOnBackdrop true / true The standard dismissals — Escape, and a click on the scrim (not the panel).
onOpenChange(open) — Fires on every open/close, whoever initiated it.

While a dialog is open it also traps the Tab ring: only focusables inside the open dialog’s subtree participate (the scrim already blocks pointer focus outside).

Handle: open(), close(), isOpen(), dispose(), plus entity (the backdrop root) / panelEntity.

createTextInput(opts) builds an editable field — background, caret, placeholder, and IME plumbing included — with theme-role colors that re-resolve on a theme swap. Returns a TextInputHandle: getValue() / setValue(v) / entity / dispose().

import { createTextInput } from 'esengine';
const name = createTextInput({
world, events, parent,
placeholder: 'Your name', maxLength: 20,
onChange: (value) => { /* value edited */ },
onSubmit: (value) => save(value), // Enter
});
Option Default Description
node 220×32 The field’s UINode layout (the one factory that doesn’t default to fill-parent).
value / placeholder '' Content / hint shown while empty.
placeholderI18nKey '' Catalog key for the empty-field hint; needs a Localization resource. The authored placeholder stays the fallback.
maxLength 0 Character cap (0 = unlimited).
multiline false Enter inserts a newline instead of submitting.
password false Mask the characters.
readOnly false Focusable but not editable.
fontSize / fontFamily theme label size / '' (the project’s default font) Text style.
color / backgroundColor / placeholderColor theme text / control / faded text Color overrides — theme roles when omitted.
padding 6 Inner horizontal padding in px.
renderMode Auto Glyph pipeline for the field text — Auto (crisp bitmap when unscaled, SDF once scaled), always Bitmap, or always Sdf.
disabled / tabIndex false / 0 Interactivity + Tab order. Text fields are always focusable — that is what they are for.
onChange(value, entity) — Fires on every edit.
onSubmit(value, entity) — Fires on Enter (single-line).

The factory is sugar over the TextInput component — insert that directly on any UI entity for full manual control (every field above, plus the live focused / cursorPos state):

import { spawnUIEntity, TextInput, px } from 'esengine';
const field = spawnUIEntity({ world, parent, node: { width: px(240), height: px(32) } });
world.insert(field, TextInput, { placeholder: 'Your name', maxLength: 20 });
events.on(field, 'change', () => { /* value edited */ });
events.on(field, 'submit', () => save(world.get(field, TextInput).value)); // Enter

The component carries four fields the factory doesn’t take — the ones a field authored in a scene needs to stop looking generic:

Field Default Description
font 0 A font asset this project ships (.ttf / .otf), the same slot Text.font uses. Set, it wins over fontFamily — so a field on a panel drawn in the game’s typeface is drawn in it too, rather than in Arial.
textAlign Left Where the value sits in the field: Left / Center / Right. A single line holds the alignment while the value fits; past that the field scrolls to follow the caret, exactly as a left-aligned one always has. Multiline aligns per line.
renderMode Auto Glyph pipeline for the field text — mirrors Text.renderMode.
inputFilter None Which characters the field accepts, whether typed or pasted — the rest are taken out of the value itself, so what is on screen and what a script reads are the same. TextInputFilter.Digits keeps 0-9; Decimal keeps 0-9, a point and a minus sign; Phone keeps 0-9 and + * # -.

A skinned field keeps its skin. The field draws over the entity’s own UIVisual, and it only manages that visual when it is a plain fill — backgroundColor is the fallback for a field nobody skinned. Give the entity a 9-sliced UIVisual (the frame every UI pack draws its input boxes with) and it is left alone, so the art survives; backgroundColor then does nothing.

// A skinned field: the frame is the entity's own visual, and TextInput draws into it.
const field = spawnUIEntity({
world, parent,
node: { width: px(240), height: px(40) },
visual: { visualType: UIVisualType.NineSlice, texture: frameTex, sliceBorder: { x: 12, y: 12, z: 12, w: 12 } },
});
world.insert(field, TextInput, { placeholder: 'Name', font: gameFont, textAlign: TextAlign.Center });

A field does not need a UINode: TextInput also works on an entity with a ContentRect, so a text field can stand in the world.

Click (or Tab to) the field to focus it; Escape blurs, Enter fires submit on single-line fields. A value longer than the box clips to it and scrolls horizontally so the caret stays in view as you type.

Editing is backed by a hidden native <textarea>, so caret movement and selection come for free: click to place the caret at that character, arrows / Home / End move it, Shift + arrows (or Ctrl/⌘ + A) select a range — drawn as a highlight behind the text — and the platform clipboard (cut / copy / paste) works. The field mirrors that native selection each frame, so it always matches what a native input would do.

IME / CJK input works: the field drives a hidden native <textarea>, so a Chinese/Japanese/Korean composition renders live in the field (the preedit string shows at the caret as you type) and only fires change once the composition commits — the same behavior as a native input. The textarea tracks the caret’s on-screen position, so the OS candidate window pops up at the caret, not in a screen corner.

A multiline field (Enter inserts a newline) stacks its lines from the top; the caret position, click-to-place and within-line selection track the \n-broken lines (soft word-wrap is not modeled — lines break on explicit newlines).

Virtualized data-driven lists and grids (createListView), free-form scrollable panels (createScrollView), data sources, layout providers, auto-height rows, and the kinetic-scroll / scrollbar machinery have their own chapter: Lists & Scrolling.

Widget values are plain component fields (UISlider.value, UIToggle.isOn, …), so the signal layer binds to them directly — and bindWidgetValue makes it two-way. See UI Binding.

Add Draggable to any interactable UI entity and pointer drags move it, with drag_start / drag_move / drag_end events and a live DragState component. Axis locks and range clamps included. See UI Interaction.

Entities with a Focusable participate in keyboard focus: Tab / Shift+Tab cycle by tabIndex, Enter/Space activate, sliders take the arrow keys, and a control focused from the keyboard shows its focused interaction page. Widget factories add Focusable automatically (focusable: false opts out); the FocusManager resource gives programmatic control. Details in UI Interaction.

Add SafeArea to a full-screen, absolutely-positioned UINode container and its insets track the platform safe area — notches, rounded corners, the WeChat capsule — with per-edge opt-outs. See Screen & Resolution.

Native iOS and Android hosts supply live content insets in surface pixels, including landscape rotation and layout changes. Put the content container under the Canvas; the world and backgrounds can still fill the whole screen. The ui-localization-debug example authors this container around its text and controls.

Widget colors come from a shared token set with roles like surface, control, primary, and track; switchTheme(world, tokens) live-restyles everything on screen, and a project declares its palette once in Settings → Project → Game → UI. See UI Theme.

The same widgets ship as editor prefabs, so Create… → UI drops a ready-made Button, Toggle, Slider, Dialog, TextInput, and so on into the scene, which you then edit in Details. Behavior that lives in components — a slider’s drag/keys, a dialog’s Escape/scrim dismissal — works on placed prefabs without code.

Dropping from the palette nests: the drop parents into the layout container under the pointer (a dashed outline previews the would-be parent while you drag), and an empty-canvas drop lands at the root as before. The anchor picker edits per axis and keeps the widget exactly where it was — changing the horizontal anchor never disturbs the vertical layout, and vice versa. See The Editor.

  • Prefer the state components over handle juggling. UIToggle.isOn, UISlider.value, UIDropdown.selectedIndex are the single source of truth — write them (or bind them) and the visuals follow; the handle methods are just typed sugar over the same writes.
  • Let the theme own colors. Omit states / visuals to get theme roles that re-resolve on a live swap; hard-code colors only for deliberately off-theme art.
  • Put dialog content under panelEntity, never beside it — that’s what makes open/close a single display write for the whole subtree.
  • Dispose handles you created at runtime — dispose() despawns the widget’s entities and detaches its event subscriptions.
  • Keep keyboard reachability on. Only pass focusable: false for decorative or duplicate controls; the Tab ring is your accessibility story.
  • UI — the component model: UINode, Text, UIVisual, interaction.
  • Lists & Scrolling — createListView, createScrollView, virtualization.
  • UI Controllers — shared “page” state + declarative per-page field bindings.
  • UI Binding — signals and two-way widget binding.
  • UI Interaction — focus, drag & drop, pointer events.
  • UI Theme — tokens, roles, live theme swaps, the project theme.

A virtual joystick captures one pointer until it lifts or is cancelled. inputId names its InputMap Virtual binding. Fixed mode measures from the node centre; Follow mode starts at the captured finger. Radius, radial dead zone and return speed are editable in the inspector. The handle returns visually to rest while its input becomes zero immediately on release.

const stick = createVirtualJoystick({ world, parent: canvas, inputId: 'move',
mode: VirtualJoystickMode.Fixed, radius: 50, deadZone: 0.15 });
const actions = defineInputMap({ Move: Axis2D(Virtual('move')) });

Defaults: inputId 'move', mode Fixed, radius 50 px, deadZone 0.15 (0 to 0.99) and returnSpeed 18. A Follow joystick defaults to a 240×160 touch area.

The editor’s Create entity picker’s UI category includes the same generated prefab. Mouse drag in Play uses the same capture as touch. Disable, hide, remove, cancel or background a control to release its own contribution; other controls keep their outputs. Custom base/handle visuals are supported.

createVirtualButton({ world, events, parent: canvas, inputId: 'fire', text: 'Fire' }) uses the normal button appearance and publishes a held scalar to Virtual('fire') (inputId defaults to 'fire'; value is the published scalar, default 1; the button is never keyboard-focusable). It supports independent fingers, multiple fingers on the same button, and taps completed between frames. Multiple controls can share a binding without one control’s removal clearing the others. Use an explicit stable owner with Input.setVirtualSource for custom controls, then remove that owner at teardown.

The Mobile Controls example authors both controls in its scene and combines them with keyboard bindings, optional tilt, and hit vibration.

Drop TabView from UI Widgets, then edit its ordinary Toggle children and content pages. The Inspector pairs each tab with a page, keeps their order when moving a binding, and exposes the initial page, all-off policy, keyboard direction, looping and selection-on-focus. Editor Preview changes only the temporary projection; saving preserves the initial page. Bind distinct descendants of the view. A nested ToggleGroup owns its own toggles.

At runtime UITabView.selectedIndex is the current selection (-1 means none when allowSwitchOff is enabled). It uses UIToggleGroup, shows one content subtree via UINode.display, and emits change with { index, page }. Arrows follow the authored direction, Home/End reach the first/last eligible tab, and disabled/hidden tabs are skipped. With select-on-focus off, Enter/Space activates the focused tab.

import { createTabView, px } from 'esengine';
const settings = createTabView({ world, events, parent: canvas,
node: { width: px(420), height: px(240) },
labels: ['Graphics', 'Audio', 'Controls'],
});
// Put ordinary UI entities under settings.pages[1] to author the Audio panel.
settings.select(1);

createTabView also takes selectedIndex (initial tab, default 0), tabHeight (default 36 px) and onChange(index, page). UITabView additionally carries vertical (arrows follow the vertical axis, default false), loopNavigation (default true), selectOnFocus (default true) and allowSwitchOff (default false).

The tabs and pages entity lists are remapped when a scene/prefab is instantiated or copied. labels provides captions for the tab’s direct Text child. Deleting a selected target chooses another valid binding; removing a binding restores that page’s original display value.

Attach UITooltip to a control, or drop the Tooltip example target from UI Widgets. Pointer triggers need an Interactable target; keyboard hints use Focusable. The popup is engine UI and never takes focus or blocks pointer input. It disappears on leave, release, cancellation, target removal or a hidden page. Moving a touch more than eight screen pixels cancels its long press without consuming the drag.

import { attachTooltip } from 'esengine';
attachTooltip(world, musicVolume, {
content: 'Adjust background music volume.',
hoverDelay: 0.5, longPressDelay: 0.6,
side: 0, gap: 8, maxWidth: 240, avoidEdges: true,
});

The defaults are hover: true, focus: true, longPress: true, hoverDelay: 0.5 s, longPressDelay: 0.6 s, side: 0, gap: 8 px, avoidEdges: true and maxWidth: 240 px; hover / focus / longPress switch each trigger on or off. side is Bottom (0), Top (1), Left (2) or Right (3). Placement prefers that side, tries the opposite, then clamps inside the screen safe area. maxWidth is text width in layout pixels. The empty stylePrefab uses the project’s live UI theme; a custom .esprefab needs a UINode root and a Text child. Its colors and font style are retained while the behavior owns popup placement, text and padding. Custom styles use the normal prefab asset loader and require PrefabsPlugin. A pending load cannot reopen a hint after its target has disappeared. Editor Preview bypasses the delay without changing the saved trigger settings.

Open assets/scenes/tabs-tooltips.esscene in the UI Controls example for authored tabs and a volume slider with a tooltip.

Drop Tree View from UI Widgets. The Inspector configures selection (None, Single, Multiple), row height, indentation, drag grips, row prefab and empty-state prefab. Its sample-data preview is temporary: save and undo still use project content. A row prefab supplies a UINode subtree with a Text label; it is a display view, while the wrapper owns selection and disclosure input. Runtime styles use the regular Prefabs service. For interactive custom rows, provide a factory item template and optionally dragHandle instead.

A tree does not require scene entities. Adapt arbitrary application records with getRoots, getChildren, getId, getLabel, optional isSelectable and optional subscribe. IDs must be unique strings or finite numbers, stable through reordering. Cycles and duplicate IDs are rejected. Traversal is iterative; only visible rows plus the ListView recycle buffer become UI entities.

import { setTreeViewData } from 'esengine';
const detach = setTreeViewData(world, treeEntity, {
getRoots: () => project.folders,
getChildren: folder => folder.entries,
getId: folder => folder.key,
getLabel: folder => folder.title,
subscribe: changed => project.onFoldersChanged(changed),
}, {
canDrop: request => project.canMove(request.source.id, request.target.id),
onDrop: request => project.move(request.source.id, request.target.id, request.position),
});
// detach() restores the previous adapter. The project emits its normal data
// notification after a move; the widget does not mutate project records.

createTreeView also accepts that adapter, or an optional authored nodes tree of { id, label, children?, selectable? }. Its handle exposes the model after runtime adoption, refresh(), reveal(id) and dispose(). Scene-placed trees use setTreeViewData to receive runtime data. Mutable sources notify via subscribe; the factory handle’s refresh() is available for sources without notifications.

Disclosure controls expand/collapse; row clicks select. Ctrl/Command-click supports multiple selection. Arrows navigate and expand/collapse, Home/End reach the first/last selectable visible row, and Enter/Space activates the current row. The tree occupies one Tab stop. change carries { ids }. Collapse retains selection by ID. A dedicated grip handles mouse/touch reordering while the row body retains normal drag scrolling. Drop placement is before, inside or after; self/descendant drops are rejected, and drop_request only describes an intent. Your host validates and applies it. Appearance uses live project theme roles or your own row views.

Drop Color Picker from UI Widgets. Its ordinary child controls can be edited and saved as a project prefab. The Inspector configures the initial color, alpha, HSV/RGB numeric format and recent-color capacity. Preview visibility does not change scene opacity in the saved model.

import { createColorPicker } from 'esengine';
const picker = createColorPicker({ world, events, parent: canvas,
color: { r: 0.2, g: 0.6, b: 0.9, a: 1 },
onChange: color => project.setAccent(color),
onHistoryChange: colors => project.savePalette(colors),
});
picker.setColor(project.accent);

Other options: alphaEnabled (default true), format (0 HSV + HEX, 1 RGB + HEX), history (initial recent colors), historyLimit (default 8, at most 32) and rememberColors (default true). The default size is 280×412.

UIColorPicker.color is the canonical { r, g, b, a } shape, with 0..1 channels. change carries { color }; history_change carries { colors }. The picker never selects a Sprite or Material target itself and does not perform an implicit sRGB/linear conversion. Bind the value through the target’s normal color API. col.fromHsv / col.toHsv share color math with editor fields; hue uses degrees, saturation/value use 0..1. Hue is retained while editing gray or black.

The saturation/value plane supports pointer capture and arrow keys (Shift makes larger steps). Hue and alpha reuse UISlider keyboard/input behavior; numeric and HEX fields reuse TextInput. HEX accepts the engine’s short or full RGB/RGBA notation. Invalid partial text remains editable and normalizes on blur. Disabling alpha forces an opaque value. Recent colors are local to each picker; persistence is explicitly supplied by the host. The history area is an ordinary editable UI container: size it to accommodate the desired palette capacity.

The composed uiPlugin installs both widgets for scene/prefab instances and factory-created controls. Their views use the existing UI renderer. The picker shares three tiny retained gradient/checker textures per app. Each picker owns a 64×64 SV image following the same color contract as sprites. Only hue changes update its pixels; saturation/value/alpha edits reuse it. The existing content provider restores the latest image after device loss without changing its handle.

Open UI Controls → assets/scenes/trees-colors.esscene: one tree switches between scene, asset and quest records, and one picker drives an actual 2D Sprite and 3D MeshRenderer color through project code. Target-device acceptance remains separate from local WASM checks and CI screenshots.