Skip to content

1681-engine / ui / UiRoot

Class: UiRoot ​

Defined in: src/ui/UiRoot.ts:49

The canvas UI's root, on the stage's screen layer above the world: it pins HUD pieces to screen corners and keeps them there on resize, owns keyboard/gamepad focus, ticks only the widgets that are moving, opens modals with a focus trap, and keeps presses on widgets from reaching the world (UiPointerGate).

ts
const ui = new UiRoot(engine.stage, { parent: scene.hud }); // goes when the scene does
ui.pin(healthBar, 'top-left', 8);
// in a fixed step, between actions.poll() and endStep():
if (!ui.handleInput(actions) && !ui.capturesInput) movePlayer();

Extends ​

  • Container

Implements ​

Constructors ​

Constructor ​

ts
new UiRoot(stage, options?): UiRoot;

Defined in: src/ui/UiRoot.ts:60

Parameters ​

ParameterType
stageStage
optionsUiRootOptions

Returns ​

UiRoot

Overrides ​

ts
Container.constructor

Properties ​

focus ​

ts
readonly focus: FocusManager;

Defined in: src/ui/UiRoot.ts:52


isUiHost ​

ts
readonly isUiHost: true = true;

Defined in: src/ui/UiRoot.ts:50

Implementation of ​

UiHost.isUiHost


pixelPerfect ​

ts
readonly pixelPerfect: boolean;

Defined in: src/ui/UiRoot.ts:51

Lay out and draw on whole pixels.

Implementation of ​

UiHost.pixelPerfect

Accessors ​

capturesInput ​

Get Signature ​

ts
get capturesInput(): boolean;

Defined in: src/ui/UiRoot.ts:80

A modal is open or a widget has focus: the game should leave its own actions alone this step.

Returns ​

boolean


viewport ​

Get Signature ​

ts
get viewport(): Size;

Defined in: src/ui/UiRoot.ts:75

The screen's size in screen pixels.

Returns ​

Size

Methods ​

animate() ​

ts
animate(t): void;

Defined in: src/ui/UiRoot.ts:138

Ticks t every frame until it reports it has settled: only what moves costs a frame.

Parameters ​

ParameterType
tUiTicking

Returns ​

void

Implementation of ​

UiHost.animate


closeModal() ​

ts
closeModal(modal): void;

Defined in: src/ui/UiRoot.ts:121

Parameters ​

ParameterType
modalUiModal

Returns ​

void


destroy() ​

ts
destroy(): void;

Defined in: src/ui/UiRoot.ts:148

Removes all internal references and listeners as well as removes children from the display list. Do not use a Container after calling destroy.

Returns ​

void

Example ​

ts
container.destroy();
container.destroy(true);
container.destroy({ children: true });
container.destroy({ children: true, texture: true, textureSource: true });

Overrides ​

ts
Container.destroy

handleInput() ​

ts
handleInput(input): boolean;

Defined in: src/ui/UiRoot.ts:130

Reads one step of navigation input (call between actions.poll() and endStep()); true when the UI used it.

Parameters ​

ParameterType
inputNavInput

Returns ​

boolean


invalidateFocus() ​

ts
invalidateFocus(): void;

Defined in: src/ui/UiRoot.ts:134

A widget became focusable or stopped being (added, removed, enabled, hidden): the focus list is stale.

Returns ​

void

Implementation of ​

UiHost.invalidateFocus


layout() ​

ts
layout(): void;

Defined in: src/ui/UiRoot.ts:102

Places every pinned piece and open modal again (after one changed size; resizes do it by themselves).

Returns ​

void


openModal() ​

ts
openModal(modal): void;

Defined in: src/ui/UiRoot.ts:113

Opens a modal over everything: focus is trapped inside it until it closes.

Parameters ​

ParameterType
modalUiModal

Returns ​

void


pin() ​

ts
pin(
   child, 
   anchor, 
   margin?
): this;

Defined in: src/ui/UiRoot.ts:85

Adds child and keeps it pinned to a screen anchor, margin in from the edges, through every resize.

Parameters ​

ParameterTypeDefault value
childContainerundefined
anchorScreenAnchorundefined
marginnumber | Partial<Insets>0

Returns ​

this


unpin() ​

ts
unpin(child): this;

Defined in: src/ui/UiRoot.ts:95

Parameters ​

ParameterType
childContainer

Returns ​

this


update() ​

ts
update(dtMs): void;

Defined in: src/ui/UiRoot.ts:143

Ticks what is moving; called by the ticker, or by a game driving the UI itself.

Parameters ​

ParameterType
dtMsnumber

Returns ​

void

Released under the MIT License.