Files
YourWishes b639bc6c4f Reimplement the archived UI widget stack on the new element-tree system
The old widget system (archive/dusk/ui/) depended on infrastructure this
rewrite deleted -- a static X-macro element list, a 9-slice UI_FRAME, and
a global gamepad/keyboard focus stack -- so everything below is rebuilt
on top of the new native uielement_t pool + parent/child tree instead,
gamepad/keyboard-only (no mouse support exists in the input system).

New JS widgets (assets/scripts/), all pure composites of Label/Rectangle
following the Button.js pattern: Checkbox, Tab, Slider, Dropdown. Slider/
Dropdown expose a directionInput(dx, dy) hook so a menu can hand them
LEFT/RIGHT before falling back to cursor movement.

New Menu.js replaces the old global uifocus_t stack: a small push/pop
stack of Menu instances where only the topmost consumes CANCEL/ACCEPT/
direction input each tick (needed for nested modals -- a Settings
sub-page menu plus a "discard changes?" Confirm can be open at once).
Held-direction repeat timing matches the old 0.5s delay / 0.1s repeat.

New overlay composites (added via a new UI.addOverlay()/UI.removeOverlay(),
always drawn/updated after normal roots): FpsCounter, ConsoleOverlay
(one Label per console history line, driven directly rather than via
add() since History (16) exceeds the 8-children-per-element cap), Crop
(letterbox/pillarbox bars), and Fullbox (a reusable tweened full-screen
fade covering both the old fullbox and transition effects -- reuses
shared instances rather than allocate-and-dispose, since disposing an
element from inside its own render() callback risks the same JerryScript
refcount corruption hit earlier in this rewrite).

New Confirm.js (Yes/No dialog) and Settings.js (+ SettingsGeneral/
SettingsInput sub-pages) built on Menu. Display/Audio settings pages are
intentionally not ported -- neither had a real backing engine system
even in the old code. Frame is a flat Rectangle for now, not real 9-slice
(a genuinely bigger native lift, deliberately deferred).

Small native binding additions needed by the above, each with its own
unit test: Time.renderDelta, Console.lineCount/getLine/visible/
consumeDirty, a new Screen module, a new Locale module, Input.deadzone,
a new Save module, and Label.color (was missing entirely). Also fixes
a real gap from the UI.addOverlay() work: uiElementDispose() wasn't
removing disposed elements from the new overlay root array, which would
have left dangling ids behind.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-14 09:05:11 -05:00

123 lines
4.2 KiB
TypeScript

// 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;
/**
* Called once per tick. The base implementation calls updateChildren()
* and nothing else. Override it for per-tick logic (e.g. polling
* input) -- an override does NOT get its children auto-updated; call
* updateChildren() yourself if you still want them updated. Not called
* automatically by the engine unless this element is a render root
* (added via UI.add()) or a descendant of one.
*/
update(): void;
/** Updates every added child, in add-order. Called automatically by
* the base update() -- only needed explicitly from inside an
* overridden update(). */
updateChildren(): 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;
}