Skip to content

Kiln API / kiln / UiState

Class: UiState ​

The HUD as retained data: res.mut(Ui).text('score', 'Coins 3'), .bar('hp', 0.5), .button('start', 'Start', { anchor: 'center', action: 'jump' }). Elements stay until removed (remove(id), or clear('menu.') for every id with that prefix). Calls with unchanged values cost nothing, so systems may set them every step. Headless the same data exists (tests read app.res(Ui).get('score')?.text); in the browser it is drawn as DOM over the canvas.

Constructors ​

Constructor ​

ts
new UiState(): UiState;

Returns ​

UiState

Properties ​

nodes ​

ts
nodes: Record<string, UiNode> = {};

version ​

ts
version: number = 0;

Bumped on every change; the DOM sync skips frames where it didn't move.


clicks ​

ts
clicks: string[] = [];

Clicks waiting to become UiClick events.

Methods ​

text() ​

ts
text(
   id, 
   text, 
   options?
): void;

Sets a text element.

Parameters ​

ParameterType
idstring
textstring | number
optionsUiOptions

Returns ​

void


bar() ​

ts
bar(
   id, 
   value, 
   options?
): void;

Sets a progress bar: value 0..1 (clamped).

Parameters ​

ParameterType
idstring
valuenumber
optionsBarOptions

Returns ​

void


button() ​

ts
button(
   id, 
   label, 
   options?
): void;

Sets a button. Clicking it emits UiClick { id }.

Parameters ​

ParameterType
idstring
labelstring
optionsButtonOptions

Returns ​

void


get() ​

ts
get(id): Readonly<UiNode> | undefined;

The element with this id, or undefined.

Parameters ​

ParameterType
idstring

Returns ​

Readonly<UiNode> | undefined


has() ​

ts
has(id): boolean;

Parameters ​

ParameterType
idstring

Returns ​

boolean


show() ​

ts
show(id, visible?): void;

Shows or hides an element without removing it.

Parameters ​

ParameterTypeDefault value
idstringundefined
visiblebooleantrue

Returns ​

void


remove() ​

ts
remove(id): void;

Removes an element. Removing a missing id is a no-op.

Parameters ​

ParameterType
idstring

Returns ​

void


clear() ​

ts
clear(prefix?): void;

Removes every element whose id starts with prefix (all of them with no prefix).

Parameters ​

ParameterTypeDefault value
prefixstring''

Returns ​

void


click() ​

ts
click(id): void;

Clicks a button as a player would; UiClick is emitted in the next Input phase.

Parameters ​

ParameterType
idstring

Returns ​

void


toJSON() ​

ts
toJSON(): object;

Returns ​

object

nodes ​
ts
nodes: Record<string, UiNode>;

restore() ​

ts
restore(json): void;

Parameters ​

ParameterType
json{ nodes: Record<string, UiNode>; }
json.nodesRecord<string, UiNode>

Returns ​

void