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): button + toggle widgets author UINode (REARCH_GUI S2b)

esengine66c067586c96
diff --git a/sdk/src/ui/widgets/button.ts b/sdk/src/ui/widgets/button.ts
index e0b0e52ec..c158c7b89 100644
--- a/sdk/src/ui/widgets/button.ts
+++ b/sdk/src/ui/widgets/button.ts
@@ -14,7 +14,7 @@ import { UIEventType, type UIEventQueue } from '../core/events';
import {
spawnUIEntity,
- type UIRectInit,
+ type UINodeInit,
type UIRendererInit,
type TextInit,
} from './helpers';
@@ -33,7 +33,8 @@ export interface ButtonOptions {
world: World;
events: UIEventQueue;
parent?: Entity;
- rect?: UIRectInit;
+ /** CSS-box layout. Default: fill the parent. */
+ node?: UINodeInit;
/** Background renderer config. Default: solid white quad. */
background?: UIRendererInit;
/** Label text. Omit to skip spawning a child label entity. */
@@ -90,7 +91,7 @@ export function createButton(opts: ButtonOptions): Entity {
const entity = spawnUIEntity({
world,
parent: opts.parent,
- rect: opts.rect,
+ node: opts.node ?? { fill: true },
renderer: opts.background ?? {},
});
@@ -128,7 +129,7 @@ export function createButton(opts: ButtonOptions): Entity {
spawnUIEntity({
world,
parent: entity,
- rect: { anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 } },
+ node: { fill: true },
text: textInit,
});
}
diff --git a/sdk/src/ui/widgets/toggle.ts b/sdk/src/ui/widgets/toggle.ts
index 837b3a8fd..a237584aa 100644
--- a/sdk/src/ui/widgets/toggle.ts
+++ b/sdk/src/ui/widgets/toggle.ts
@@ -6,13 +6,13 @@ import type { World } from '../../world';
import { UIEventType, type UIEventQueue } from '../core/events';
import { createButton, type ButtonStateVisual } from './button';
-import { spawnUIEntity, setUIVisible, type UIRectInit, type UIRendererInit } from './helpers';
+import { spawnUIEntity, setUIVisible, type UINodeInit, type UIRendererInit } from './helpers';
export interface ToggleOptions {
world: World;
events: UIEventQueue;
parent?: Entity;
- rect?: UIRectInit;
+ node?: UINodeInit;
/** Background renderer for the frame. */
background?: UIRendererInit;
@@ -22,11 +22,11 @@ export interface ToggleOptions {
/**
* Optional rendering for the on-state indicator ("check mark").
- * A child entity hidden when off, shown when on. Size defaults to
- * the parent rect; override via `checkRect`.
+ * A child entity hidden when off, shown when on. Fills the parent by
+ * default; override via `check.node`.
*/
check?: {
- rect?: UIRectInit;
+ node?: UINodeInit;
color?: Color;
sprite?: number;
};
@@ -59,7 +59,7 @@ export function createToggle(opts: ToggleOptions): ToggleHandle {
world,
events,
parent: opts.parent,
- rect: opts.rect,
+ node: opts.node,
background: opts.background,
states: opts.interactionStates,
disabled: opts.disabled,
@@ -68,7 +68,7 @@ export function createToggle(opts: ToggleOptions): ToggleHandle {
const check = spawnUIEntity({
world,
parent: button,
- rect: opts.check?.rect ?? { anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 } },
+ node: opts.check?.node ?? { fill: true },
renderer: opts.check
? {
color: opts.check.color,
diff --git a/sdk/tests/ui-widgets.test.ts b/sdk/tests/ui-widgets.test.ts
index 1dcc65719..18e257cda 100644
--- a/sdk/tests/ui-widgets.test.ts
+++ b/sdk/tests/ui-widgets.test.ts
@@ -11,6 +11,7 @@ import {
StateMachine,
StateVisuals,
UIRect,
+ UINode,
UIRenderer,
UIEventQueue,
UIEventType,
@@ -107,7 +108,7 @@ describe('createButton', () => {
},
});
- expect(world.has(btn, UIRect)).toBe(true);
+ expect(world.has(btn, UINode)).toBe(true);
expect(world.has(btn, UIRenderer)).toBe(true);
expect(world.has(btn, Interactable)).toBe(true);
expect(world.has(btn, StateMachine)).toBe(true);