Rebuild UI as a scriptable element tree, archive the old system

The old UI system (X-macro static element list, hand-authored C
screens/widgets/focus stack) is archived under archive/ rather than
deleted, since it's a useful reference during the rewrite.

New native UI element pool (src/dusk/ui): a flat UI_ELEMENTS[128] pool
of tagged-union elements (Label, Rectangle, Scripted), a real
persistent parent/child tree (children[8] per element, cycle- and
capacity-checked uiElementSetParent), always-fresh worldX/worldY
(cheap enough to recompute on every read, no dirty-flag cache needed),
and cascading dispose. Rendering stays manual/immediate: a scripted
element with no render() override auto-renders its children by
default, but overriding render() takes full control (an override must
call renderChildren() itself to opt back in) -- this is deliberately
preserved end to end via a render()-shadow trampoline so overriding
render() always keeps working the same way regardless of how a node
is reached.

New scripting layer (src/dusk/script/module/ui): UIElement/Label/
Rectangle JS classes (Label/Rectangle share UIElement's prototype via
manual chaining, not JS `extends`), exposing x/y/worldX/worldY/parent/
add()/remove()/render()/renderChildren()/dispose(). UI.add()/
UI.remove() manage top-level render roots, mutually exclusive with
being someone's child.

Also: Scene gains a lateUpdate() hook (called once per frame after
every other update, for things like camera-follow that need to react
to where everything else ended up); several duskrpg call sites
(cutscene items, entityinteractable, entityplayer) that depended on
the now-archived RPG textbox are stubbed to console output instead of
a dialogue box, pending the new UI reaching that far.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-13 11:13:23 -05:00
parent 7b58addf7e
commit 7ee04c78cd
118 changed files with 2502 additions and 391 deletions
+3
View File
@@ -15,4 +15,7 @@
/// <reference path="console/console.d.ts" />
/// <reference path="platform/platform.d.ts" />
/// <reference path="input/input.d.ts" />
/// <reference path="ui/uielement.d.ts" />
/// <reference path="ui/label.d.ts" />
/// <reference path="ui/rectangle.d.ts" />
/// <reference path="ui/ui.d.ts" />
+12 -2
View File
@@ -31,8 +31,10 @@ declare class Scene {
* Scene.set() call installed (calling its dispose(), then destroying
* the scene it owned), then creates and activates a fresh Scene and
* calls the new module's init() with it active -- so init() can use
* `new Entity()`/etc. as usual. The module's update() (if defined) is
* then called once per engine frame until Scene.set() is called again.
* `new Entity()`/etc. as usual. The module's update() and lateUpdate()
* (if defined) are then each called once per engine frame -- update()
* first, lateUpdate() after everything else has updated -- until
* Scene.set() is called again.
*
* Typical usage (see require.d.ts):
* ```js
@@ -55,6 +57,14 @@ interface SceneModule {
/** Called once per engine frame while this module is the active scene. */
update?(): void;
/**
* Called once per engine frame, after every other update (entities,
* physics, UI, etc.) has already run for that frame -- useful for
* things like camera follow that need to react to where everything
* else ended up this frame, rather than where it was at the start.
*/
lateUpdate?(): void;
/** Called once when this module is replaced by another Scene.set() call. */
dispose?(): void;
}
+17
View File
@@ -0,0 +1,17 @@
// Copyright (c) 2026 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/// <reference path="uielement.d.ts" />
/**
* A single-line text element. Extends UIElement -- x/y/worldX/worldY/
* parent/render()/dispose() all work the same way.
*/
declare class Label extends UIElement {
constructor();
/** Display text. Rebuilds the label's glyph cache on assignment. */
text: string;
}
+21
View File
@@ -0,0 +1,21 @@
// Copyright (c) 2026 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/// <reference path="uielement.d.ts" />
/**
* A flat-colored rectangle element. Extends UIElement -- x/y/worldX/
* worldY/parent/render()/dispose() all work the same way.
*/
declare class Rectangle extends UIElement {
constructor();
/** Width, in screen space. No-op to render at <= 0. */
width: number;
/** Height, in screen space. No-op to render at <= 0. */
height: number;
/** Fill color, 0-255 per channel. */
color: { r: number; g: number; b: number; a: number };
}
+9 -6
View File
@@ -3,13 +3,16 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/// <reference path="uielement.d.ts" />
/**
* Stub for now: both methods just stringify and space-join their
* arguments (like console.log) and forward the result to the engine's
* console, prefixed with the called method's name. No real UI element
* system exists behind this yet.
* Adds/removes a UIElement (or Label/Rectangle/etc.) from the engine's
* render roots. Root elements get their render() called once per frame;
* everything else only renders as a child of a root (see
* UIElement.add()). A root and a child are mutually exclusive --
* UI.add() detaches the element from any parent first.
*/
declare const UI: {
add(...args: any[]): void;
remove(...args: any[]): void;
add(element: UIElement): void;
remove(element: UIElement): void;
};
+107
View File
@@ -0,0 +1,107 @@
// Copyright (c) 2026 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/**
* Base class for every UI element (Label, Rectangle, and any composite
* class you define). `new UIElement()` allocates a pool element and, if
* the (sub)class defines init(), calls it immediately.
*
* add()/remove() maintain a persistent parent/child link (up to 8
* children per element) -- worldX/worldY/parent are always current, not
* just "as of last render()". Added children render automatically, in
* add-order, right after this element's own visuals -- unless render()
* is overridden, in which case the override takes full manual control
* and must call renderChildren() itself if it still wants them drawn.
*
* ```js
* // No render() needed -- children added via add() draw automatically.
* class HealthBar extends UIElement {
* init() {
* this.label = new Label();
* this.label.x = 32;
* this.label.y = 32;
* this.add(this.label);
* this.x = 64;
* this.y = 64;
* // this.label.worldX is now 96, immediately -- no render() call needed
* }
* }
*
* // Overriding render() takes full manual control: added children are
* // NOT drawn automatically here -- renderChildren() opts back in.
* class PlayerHealth extends UIElement {
* init() {
* this.icon = new Rectangle();
* this.add(this.icon);
* }
* render() {
* if(this.player) this.renderChildren();
* }
* }
* ```
*/
declare class UIElement {
constructor();
/** The engine-assigned numeric element ID. */
readonly id: number;
/** Local x position, relative to this element's parent (if any). */
x: number;
/** Local y position, relative to this element's parent (if any). */
y: number;
/** Screen-space x. Always current -- recomputed on every read. */
readonly worldX: number;
/** Screen-space y. Always current -- recomputed on every read. */
readonly worldY: number;
/** The UIElement this one was added to via add(), or undefined. */
readonly parent: UIElement | undefined;
/** Number of elements currently added to this one. */
readonly childCount: number;
/**
* Adds child as a child of this element, detaching it from any
* previous parent (or from UI's render roots) first. Returns child,
* so calls can be chained.
*
* @throws if child is this element itself, if it's already an
* ancestor of this element (which would create a cycle), or if this
* element already has 8 children.
*/
add(child: UIElement): UIElement;
/** Removes child from this element's children, if it's currently one.
* No-op otherwise. */
remove(child: UIElement): void;
/**
* Called once, right after construction, if defined -- the usual
* place to build children and set initial properties.
*/
init?(): void;
/**
* Draws this element. The base implementation draws whatever native
* visuals this element type has (Label/Rectangle), then calls
* renderChildren(). Override it to draw your own content instead --
* an override does NOT get its children auto-rendered; call
* renderChildren() yourself if you still want them drawn.
*/
render(): void;
/** Renders every added child, in add-order. Called automatically by
* the base render() -- only needed explicitly from inside an
* overridden render(). */
renderChildren(): void;
/** Releases this element's pool slot and cascades to every descendant
* (each disposed the same way, recursively). Also removes it from
* UI's render roots if it was added there. Safe to call more than
* once, or on an element a cascade already disposed. */
dispose(): void;
}