Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(ui): progress/dialog/dropdown widgets author UINode (REARCH_GUI S2b complete)

esenginebdbe23e6c220
diff --git a/sdk/src/ui/widgets/dialog.ts b/sdk/src/ui/widgets/dialog.ts
index 6fa467b9d..2580fef83 100644
--- a/sdk/src/ui/widgets/dialog.ts
+++ b/sdk/src/ui/widgets/dialog.ts
@@ -5,17 +5,18 @@ import type { World } from '../../world';
import { Interactable, UIInteraction } from '../behavior/interactable';
-import { spawnUIEntity, setUIVisible, type UIRectInit, type UIRendererInit } from './helpers';
+import { spawnUIEntity, setUIVisible, type UINodeInit, type UIRendererInit } from './helpers';
+import { px, percent } from '../core/dimension';
export interface DialogOptions {
world: World;
parent?: Entity;
- /** Full-viewport backdrop rect. Default: stretched to parent. */
- backdropRect?: UIRectInit;
+ /** Full-viewport backdrop. Default: fill the parent. */
+ backdropNode?: UINodeInit;
/** Backdrop visuals. Default: 50% black overlay. */
backdropRenderer?: UIRendererInit;
- /** Panel rect (the modal box). Default: 400x300 centered. */
- panelRect?: UIRectInit;
+ /** Panel box (the modal). Default: 400x300 centered. */
+ panelNode?: UINodeInit;
panelRenderer?: UIRendererInit;
/** Start hidden. Default true. */
startHidden?: boolean;
@@ -46,7 +47,7 @@ export function createDialog(opts: DialogOptions): DialogHandle {
const backdrop = spawnUIEntity({
world,
parent: opts.parent,
- rect: opts.backdropRect ?? { anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 } },
+ node: opts.backdropNode ?? { fill: true },
renderer: opts.backdropRenderer ?? { color: DEFAULT_BACKDROP_COLOR },
});
@@ -63,11 +64,15 @@ export function createDialog(opts: DialogOptions): DialogHandle {
const panel = spawnUIEntity({
world,
parent: backdrop,
- rect: opts.panelRect ?? {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- size: { x: 400, y: 300 },
- pivot: { x: 0.5, y: 0.5 },
+ // Centered modal: absolute, 50% inset shifted back by half its size.
+ node: opts.panelNode ?? {
+ position: 1,
+ width: px(400),
+ height: px(300),
+ insetLeft: percent(50),
+ insetTop: percent(50),
+ marginLeft: px(-200),
+ marginTop: px(-150),
},
renderer: opts.panelRenderer ?? { color: DEFAULT_PANEL_COLOR },
});
diff --git a/sdk/src/ui/widgets/dropdown.ts b/sdk/src/ui/widgets/dropdown.ts
index f51924b19..12775e127 100644
--- a/sdk/src/ui/widgets/dropdown.ts
+++ b/sdk/src/ui/widgets/dropdown.ts
@@ -9,13 +9,14 @@ import { StateVisuals, TransitionFlag, type StateVisualsData } from '../behavior
import { UIEventType, type UIEventQueue } from '../core/events';
import { Text, type TextData } from '../core/text';
-import { spawnUIEntity, type UIRectInit, type UIRendererInit } from './helpers';
+import { spawnUIEntity, type UINodeInit, type UIRendererInit } from './helpers';
+import { px, percent } from '../core/dimension';
export interface DropdownOptions<T> {
world: World;
events: UIEventQueue;
parent?: Entity;
- rect?: UIRectInit;
+ node?: UINodeInit;
options: readonly T[];
selectedIndex?: number;
@@ -112,7 +113,7 @@ export function createDropdown<T>(opts: DropdownOptions<T>): DropdownHandle<T> {
const button = spawnUIEntity({
world,
parent: opts.parent,
- rect: opts.rect,
+ node: opts.node ?? { fill: true },
renderer: { color: btnColors.normal },
});
world.insert(button, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
@@ -123,7 +124,7 @@ export function createDropdown<T>(opts: DropdownOptions<T>): DropdownHandle<T> {
const label = spawnUIEntity({
world,
parent: button,
- rect: { anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 } },
+ node: { fill: true },
text: { content: labelOf(opts.options[selectedIndex]!, selectedIndex) },
});
@@ -141,18 +142,20 @@ export function createDropdown<T>(opts: DropdownOptions<T>): DropdownHandle<T> {
popupPanel = spawnUIEntity({
world,
parent: button,
- rect: {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 0 },
- offsetMin: { x: 0, y: -totalHeight },
- offsetMax: { x: 0, y: 0 },
+ // Below the button: absolute, full width, top at the button's bottom edge.
+ node: {
+ position: 1,
+ insetLeft: px(0),
+ insetRight: px(0),
+ insetTop: percent(100),
+ height: px(totalHeight),
},
renderer: opts.popupRenderer ?? { color: DEFAULT_POPUP_BG },
});
for (let i = 0; i < opts.options.length; i++) {
const index = i;
- const row = spawnOptionRow(index, totalHeight);
+ const row = spawnOptionRow(index);
const off = events.on(row, UIEventType.StateChanged, (e) => {
const d = e.data as { from: string; to: string };
if (d.from === 'pressed' && d.to === 'hover') {
@@ -163,15 +166,17 @@ export function createDropdown<T>(opts: DropdownOptions<T>): DropdownHandle<T> {
}
}
- function spawnOptionRow(index: number, totalHeight: number): Entity {
- const yTop = 1 - (index * optionHeight) / totalHeight;
- const yBottom = 1 - ((index + 1) * optionHeight) / totalHeight;
+ function spawnOptionRow(index: number): Entity {
const row = spawnUIEntity({
world,
parent: popupPanel!,
- rect: {
- anchorMin: { x: 0, y: yBottom },
- anchorMax: { x: 1, y: yTop },
+ // Stacked top-down: absolute, full width, row i at i*optionHeight.
+ node: {
+ position: 1,
+ insetLeft: px(0),
+ insetRight: px(0),
+ insetTop: px(index * optionHeight),
+ height: px(optionHeight),
},
renderer: { color: optColors.normal },
});
@@ -183,7 +188,7 @@ export function createDropdown<T>(opts: DropdownOptions<T>): DropdownHandle<T> {
spawnUIEntity({
world,
parent: row,
- rect: { anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 } },
+ node: { fill: true },
text: { content: labelOf(opts.options[index]!, index) },
});
diff --git a/sdk/src/ui/widgets/progress.ts b/sdk/src/ui/widgets/progress.ts
index 12c1e2fb8..945b3c35a 100644
--- a/sdk/src/ui/widgets/progress.ts
+++ b/sdk/src/ui/widgets/progress.ts
@@ -3,14 +3,15 @@
import type { Color, Entity } from '../../types';
import type { World } from '../../world';
-import { UIRect, type UIRectData } from '../core/ui-rect';
+import { UINode, type UINodeData } from '../core/ui-node';
+import { px, percent } from '../core/dimension';
-import { spawnUIEntity, type UIRectInit, type UIRendererInit } from './helpers';
+import { spawnUIEntity, type UINodeInit, type UIRendererInit } from './helpers';
export interface ProgressOptions {
world: World;
parent?: Entity;
- rect?: UIRectInit;
+ node?: UINodeInit;
/** Background (track) renderer. */
background?: UIRendererInit;
/** Fill renderer config (the filled bar). */
@@ -42,14 +43,14 @@ export function createProgress(opts: ProgressOptions): ProgressHandle {
const track = spawnUIEntity({
world,
parent: opts.parent,
- rect: opts.rect,
+ node: opts.node ?? { fill: true },
renderer: opts.background ?? { color: { r: 0.15, g: 0.15, b: 0.15, a: 1 } },
});
const fill = spawnUIEntity({
world,
parent: track,
- rect: anchorsForProgress(direction, value),
+ node: nodeForProgress(direction, value),
renderer: {
color: opts.fill?.color ?? { r: 0.25, g: 0.56, b: 0.96, a: 1 },
texture: opts.fill?.sprite ?? 0,
@@ -57,15 +58,16 @@ export function createProgress(opts: ProgressOptions): ProgressHandle {
},
});
+ const horizontal = direction === 'right' || direction === 'left';
+
function setValue(v: number): void {
const next = clamp01(v);
if (next === value) return;
value = next;
- const rect = world.get(fill, UIRect) as UIRectData;
- const updated = anchorsForProgress(direction, value);
- rect.anchorMin = updated.anchorMin!;
- rect.anchorMax = updated.anchorMax!;
- world.insert(fill, UIRect, rect);
+ const node = world.get(fill, UINode) as UINodeData;
+ if (horizontal) node.width = percent(value * 100);
+ else node.height = percent(value * 100);
+ world.insert(fill, UINode, node);
}
return {
@@ -83,18 +85,19 @@ function clamp01(v: number): number {
return v < 0 ? 0 : v > 1 ? 1 : v;
}
-function anchorsForProgress(
+// Fill grows along `dir`: pinned to 3 edges, with width/height = value fraction.
+function nodeForProgress(
dir: 'right' | 'left' | 'up' | 'down',
t: number,
-): UIRectInit {
+): UINodeInit {
switch (dir) {
case 'right':
- return { anchorMin: { x: 0, y: 0 }, anchorMax: { x: t, y: 1 } };
+ return { position: 1, insetLeft: px(0), insetTop: px(0), insetBottom: px(0), width: percent(t * 100) };
case 'left':
- return { anchorMin: { x: 1 - t, y: 0 }, anchorMax: { x: 1, y: 1 } };
+ return { position: 1, insetRight: px(0), insetTop: px(0), insetBottom: px(0), width: percent(t * 100) };
case 'up':
- return { anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: t } };
+ return { position: 1, insetLeft: px(0), insetRight: px(0), insetBottom: px(0), height: percent(t * 100) };
case 'down':
- return { anchorMin: { x: 0, y: 1 - t }, anchorMax: { x: 1, y: 1 } };
+ return { position: 1, insetLeft: px(0), insetRight: px(0), insetTop: px(0), height: percent(t * 100) };
}
}
diff --git a/sdk/tests/ui-widgets.test.ts b/sdk/tests/ui-widgets.test.ts
index 18e257cda..e4347ae6c 100644
--- a/sdk/tests/ui-widgets.test.ts
+++ b/sdk/tests/ui-widgets.test.ts
@@ -12,6 +12,7 @@ import {
StateVisuals,
UIRect,
UINode,
+ DimensionUnit,
UIRenderer,
UIEventQueue,
UIEventType,
@@ -285,32 +286,32 @@ describe('createProgress', () => {
expect(q.value()).toBe(0);
});
- it('setValue clamps and updates fill anchorMax.x (default "right")', () => {
+ it('setValue updates fill width % (default "right", pinned left)', () => {
const p = createProgress({
world: world as unknown as World,
value: 0,
});
p.setValue(0.4);
- const rect = world.get(p.fillEntity, UIRect) as {
- anchorMin: { x: number; y: number };
- anchorMax: { x: number; y: number };
+ const node = world.get(p.fillEntity, UINode) as {
+ width: { value: number; unit: number };
+ insetLeft: { value: number; unit: number };
};
- expect(rect.anchorMin).toEqual({ x: 0, y: 0 });
- expect(rect.anchorMax).toEqual({ x: 0.4, y: 1 });
+ expect(node.width).toEqual({ value: 40, unit: DimensionUnit.Percent });
+ expect(node.insetLeft).toEqual({ value: 0, unit: DimensionUnit.Px });
});
- it('uses reversed anchors when direction = "left"', () => {
+ it('pins the fill to the right when direction = "left"', () => {
const p = createProgress({
world: world as unknown as World,
direction: 'left',
value: 0.25,
});
- const rect = world.get(p.fillEntity, UIRect) as {
- anchorMin: { x: number; y: number };
- anchorMax: { x: number; y: number };
+ const node = world.get(p.fillEntity, UINode) as {
+ width: { value: number; unit: number };
+ insetRight: { value: number; unit: number };
};
- expect(rect.anchorMin.x).toBe(0.75);
- expect(rect.anchorMax.x).toBe(1);
+ expect(node.width).toEqual({ value: 25, unit: DimensionUnit.Percent });
+ expect(node.insetRight).toEqual({ value: 0, unit: DimensionUnit.Px });
});
it('dispose despawns the track', () => {