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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(ui): ThemeTokens design-token palette + widgets consume it (REARCH_GUI F7)
esengine4030b8bcc8c9
diff --git a/sdk/src/core-ui.ts b/sdk/src/core-ui.tsindex 3674c8389..3bb66f4b5 100644--- a/sdk/src/core-ui.ts+++ b/sdk/src/core-ui.ts@@ -89,4 +89,11 @@ export {colorWithAlpha,/** @internal per-entity state map used by interaction behaviour */EntityStateMap,+ // Theme design tokens (REARCH_GUI F7)+ DARK_TOKENS,+ getTheme,+ setTheme,+ themeColors,+ type ThemeTokens,+ type ThemeColors,} from './ui';diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.tsindex 89447f86c..5e03c6dc3 100644--- a/sdk/src/ui/index.ts+++ b/sdk/src/ui/index.ts@@ -295,6 +295,17 @@ export { SafeAreaPlugin, safeAreaPlugin } from './layout/safe-area';export { UIRenderOrderPlugin, uiRenderOrderPlugin } from './render/render-order';+// ─── Theme (design tokens) ──────────────────────────────────────────────────++export {+ DARK_TOKENS,+ getTheme,+ setTheme,+ themeColors,+ type ThemeTokens,+ type ThemeColors,+} from './theme/tokens';+// Property Path Utilitiesexport {getNestedProperty,diff --git a/sdk/src/ui/theme/tokens.ts b/sdk/src/ui/theme/tokens.tsnew file mode 100644index 000000000..88d15d7c1--- /dev/null+++ b/sdk/src/ui/theme/tokens.ts@@ -0,0 +1,74 @@+// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file ui/theme/tokens.ts+ * @brief ThemeTokens — semantic design tokens for the widget layer (REARCH_GUI+ * F7), replacing the deleted per-widget UITheme god-object.+ *+ * Widgets read their default colors from the active theme via {@link getTheme}+ * instead of hard-coding RGBA ("去裸色"): one place defines the palette, and an+ * app can re-theme every widget by calling {@link setTheme} before constructing+ * them. Semantic names (surface/control/primary/…) decouple widgets from literal+ * colors. `DARK_TOKENS` is the built-in default.+ */+import type { Color } from '../../types';++/** Semantic color roles consumed by widgets. */+export interface ThemeColors {+ /** Dialog / panel background. */+ surface: Color;+ /** Raised surface — popup / dropdown list background. */+ surfaceElevated: Color;+ /** Interactive control resting fill (button/option). */+ control: Color;+ controlHover: Color;+ controlActive: Color;+ /** Slider / progress track. */+ track: Color;+ /** Accent — slider fill, progress fill, selected option. */+ primary: Color;+ primaryHover: Color;+ primaryActive: Color;+ /** Content/handle drawn on top of primary or controls. */+ onPrimary: Color;+ /** Modal scrim. */+ backdrop: Color;+}++export interface ThemeTokens {+ colors: ThemeColors;+}++/** Built-in dark palette (mirrors the values the widgets used to hard-code). */+export const DARK_TOKENS: ThemeTokens = {+ colors: {+ surface: { r: 0.16, g: 0.16, b: 0.18, a: 1 },+ surfaceElevated:{ r: 0.14, g: 0.14, b: 0.16, a: 1 },+ control: { r: 0.22, g: 0.22, b: 0.26, a: 1 },+ controlHover: { r: 0.28, g: 0.28, b: 0.32, a: 1 },+ controlActive: { r: 0.18, g: 0.18, b: 0.22, a: 1 },+ track: { r: 0.15, g: 0.15, b: 0.15, a: 1 },+ primary: { r: 0.25, g: 0.56, b: 0.96, a: 1 },+ primaryHover: { r: 0.30, g: 0.50, b: 0.90, a: 1 },+ primaryActive: { r: 0.20, g: 0.40, b: 0.75, a: 1 },+ onPrimary: { r: 1, g: 1, b: 1, a: 1 },+ backdrop: { r: 0, g: 0, b: 0, a: 0.5 },+ },+};++let activeTheme: ThemeTokens = DARK_TOKENS;++/** The active design tokens widgets resolve their defaults from. */+export function getTheme(): ThemeTokens {+ return activeTheme;+}++/** Replace the active design tokens (affects widgets constructed afterwards). */+export function setTheme(tokens: ThemeTokens): void {+ activeTheme = tokens;+}++/** Convenience: the active color palette. */+export function themeColors(): ThemeColors {+ return activeTheme.colors;+}diff --git a/sdk/src/ui/widgets/dialog.ts b/sdk/src/ui/widgets/dialog.tsindex bf47d2c45..c870a69b1 100644--- a/sdk/src/ui/widgets/dialog.ts+++ b/sdk/src/ui/widgets/dialog.ts@@ -1,23 +1,24 @@// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team-import type { Color, Entity } from '../../types';+import type { Entity } from '../../types';import type { World } from '../../world';import { Interactable, UIInteraction } from '../behavior/interactable';import { spawnUIEntity, setUIVisible, type UINodeInit, type UIVisualInit } from './helpers';import { px, percent } from '../core/dimension';+import { themeColors } from '../theme/tokens';export interface DialogOptions {world: World;parent?: Entity;/** Full-viewport backdrop. Default: fill the parent. */backdropNode?: UINodeInit;- /** Backdrop visuals. Default: 50% black overlay. */- backdropRenderer?: UIVisualInit;+ /** Backdrop visuals. Default: themed scrim. */+ backdropVisual?: UIVisualInit;/** Panel box (the modal). Default: 400x300 centered. */panelNode?: UINodeInit;- panelRenderer?: UIVisualInit;+ panelVisual?: UIVisualInit;/** Start hidden. Default true. */startHidden?: boolean;}@@ -31,9 +32,6 @@ export interface DialogHandle {dispose(): void;}-const DEFAULT_BACKDROP_COLOR: Color = { r: 0, g: 0, b: 0, a: 0.5 };-const DEFAULT_PANEL_COLOR: Color = { r: 0.16, g: 0.16, b: 0.18, a: 1 };-/*** Modal dialog: backdrop entity (blocks clicks behind it) with a* centered panel child. Hidden by default; `open()` / `close()`@@ -43,12 +41,13 @@ const DEFAULT_PANEL_COLOR: Color = { r: 0.16, g: 0.16, b: 0.18, a: 1 };*/export function createDialog(opts: DialogOptions): DialogHandle {const { world } = opts;+ const c = themeColors();const backdrop = spawnUIEntity({world,parent: opts.parent,node: opts.backdropNode ?? { fill: true },- visual: opts.backdropRenderer ?? { color: DEFAULT_BACKDROP_COLOR },+ visual: opts.backdropVisual ?? { color: c.backdrop },});// Blocks hit-test on the scene behind the dialog.@@ -74,7 +73,7 @@ export function createDialog(opts: DialogOptions): DialogHandle {marginLeft: px(-200),marginTop: px(-150),},- visual: opts.panelRenderer ?? { color: DEFAULT_PANEL_COLOR },+ visual: opts.panelVisual ?? { color: c.surface },});let open = !(opts.startHidden ?? true);diff --git a/sdk/src/ui/widgets/dropdown.ts b/sdk/src/ui/widgets/dropdown.tsindex faec47833..74159d429 100644--- a/sdk/src/ui/widgets/dropdown.ts+++ b/sdk/src/ui/widgets/dropdown.ts@@ -11,6 +11,7 @@ import { Text, type TextData } from '../core/text';import { spawnUIEntity, type UINodeInit, type UIVisualInit } from './helpers';import { px, percent } from '../core/dimension';+import { themeColors } from '../theme/tokens';export interface DropdownOptions<T> {world: World;@@ -35,7 +36,7 @@ export interface DropdownOptions<T> {pressed?: Color;};/** Popup panel background. */- popupRenderer?: UIVisualInit;+ popupVisual?: UIVisualInit;/** Height of each option row in pixels. Default 32. */optionHeight?: number;@@ -54,14 +55,6 @@ export interface DropdownHandle<T> {dispose(): void;}-const DEFAULT_BTN_NORMAL: Color = { r: 0.22, g: 0.22, b: 0.26, a: 1 };-const DEFAULT_BTN_HOVER: Color = { r: 0.28, g: 0.28, b: 0.32, a: 1 };-const DEFAULT_BTN_PRESS: Color = { r: 0.18, g: 0.18, b: 0.22, a: 1 };-const DEFAULT_OPT_NORMAL: Color = { r: 0.20, g: 0.20, b: 0.24, a: 1 };-const DEFAULT_OPT_HOVER: Color = { r: 0.30, g: 0.50, b: 0.90, a: 1 };-const DEFAULT_OPT_PRESS: Color = { r: 0.20, g: 0.40, b: 0.75, a: 1 };-const DEFAULT_POPUP_BG: Color = { r: 0.14, g: 0.14, b: 0.16, a: 1 };-const INTERACTION_DEFAULT = {hovered: false, pressed: false, justPressed: false, justReleased: false,};@@ -94,15 +87,16 @@ export function createDropdown<T>(opts: DropdownOptions<T>): DropdownHandle<T> {const optionHeight = opts.optionHeight ?? 32;let selectedIndex = opts.selectedIndex ?? 0;+ const c = themeColors();const btnColors = {- normal: opts.buttonStates?.normal ?? DEFAULT_BTN_NORMAL,- hover: opts.buttonStates?.hover ?? DEFAULT_BTN_HOVER,- pressed: opts.buttonStates?.pressed ?? DEFAULT_BTN_PRESS,+ normal: opts.buttonStates?.normal ?? c.control,+ hover: opts.buttonStates?.hover ?? c.controlHover,+ pressed: opts.buttonStates?.pressed ?? c.controlActive,};const optColors = {- normal: opts.optionStates?.normal ?? DEFAULT_OPT_NORMAL,- hover: opts.optionStates?.hover ?? DEFAULT_OPT_HOVER,- pressed: opts.optionStates?.pressed ?? DEFAULT_OPT_PRESS,+ normal: opts.optionStates?.normal ?? c.control,+ hover: opts.optionStates?.hover ?? c.primaryHover,+ pressed: opts.optionStates?.pressed ?? c.primaryActive,};// Button root.@@ -146,7 +140,7 @@ export function createDropdown<T>(opts: DropdownOptions<T>): DropdownHandle<T> {insetTop: percent(100),height: px(totalHeight),},- visual: opts.popupRenderer ?? { color: DEFAULT_POPUP_BG },+ visual: opts.popupVisual ?? { color: c.surfaceElevated },});for (let i = 0; i < opts.options.length; i++) {diff --git a/sdk/src/ui/widgets/progress.ts b/sdk/src/ui/widgets/progress.tsindex f2ff3e259..c74d0b086 100644--- a/sdk/src/ui/widgets/progress.ts+++ b/sdk/src/ui/widgets/progress.ts@@ -7,6 +7,7 @@ import { UINode, type UINodeData } from '../core/ui-node';import { px, percent } from '../core/dimension';import { spawnUIEntity, type UINodeInit, type UIVisualInit } from './helpers';+import { themeColors } from '../theme/tokens';export interface ProgressOptions {world: World;@@ -39,12 +40,13 @@ export function createProgress(opts: ProgressOptions): ProgressHandle {const { world } = opts;const direction = opts.direction ?? 'right';let value = clamp01(opts.value ?? 0);+ const c = themeColors();const track = spawnUIEntity({world,parent: opts.parent,node: opts.node ?? { fill: true },- visual: opts.background ?? { color: { r: 0.15, g: 0.15, b: 0.15, a: 1 } },+ visual: opts.background ?? { color: c.track },});const fill = spawnUIEntity({@@ -52,7 +54,7 @@ export function createProgress(opts: ProgressOptions): ProgressHandle {parent: track,node: nodeForProgress(direction, value),visual: {- color: opts.fill?.color ?? { r: 0.25, g: 0.56, b: 0.96, a: 1 },+ color: opts.fill?.color ?? c.primary,texture: opts.fill?.sprite ?? 0,visualType: opts.fill?.sprite ? 2 /* Image */ : 1 /* SolidColor */,},diff --git a/sdk/src/ui/widgets/slider.ts b/sdk/src/ui/widgets/slider.tsindex a9652be31..7e4b742fb 100644--- a/sdk/src/ui/widgets/slider.ts+++ b/sdk/src/ui/widgets/slider.ts@@ -7,6 +7,7 @@ import { UINode, type UINodeData } from '../core/ui-node';import { px, percent } from '../core/dimension';import { spawnUIEntity, type UINodeInit, type UIVisualInit } from './helpers';+import { themeColors } from '../theme/tokens';export interface SliderOptions {world: World;@@ -22,9 +23,9 @@ export interface SliderOptions {/** Handle width in pixels. Default 12. */handleWidth?: number;- trackRenderer?: UIVisualInit;- fillRenderer?: UIVisualInit;- handleRenderer?: UIVisualInit;+ trackVisual?: UIVisualInit;+ fillVisual?: UIVisualInit;+ handleVisual?: UIVisualInit;onChange?: (value: number, entity: Entity) => void;}@@ -45,10 +46,6 @@ export interface SliderHandle {dispose(): void;}-const DEFAULT_TRACK: UIVisualInit = { color: { r: 0.15, g: 0.15, b: 0.15, a: 1 } };-const DEFAULT_FILL: UIVisualInit = { color: { r: 0.25, g: 0.56, b: 0.96, a: 1 } };-const DEFAULT_HANDLE: UIVisualInit = { color: { r: 1, g: 1, b: 1, a: 1 } };-/*** Horizontal slider composed of a track, a fill bar, and a handle thumb.* Interaction (drag, click-to-snap) is not wired here — use@@ -62,25 +59,27 @@ export function createSlider(opts: SliderOptions): SliderHandle {const handleWidth = opts.handleWidth ?? 12;let value = clampAndSnap(opts.value ?? min, min, max, step);+ const c = themeColors();+const track = spawnUIEntity({world: opts.world,parent: opts.parent,node: opts.node ?? { fill: true },- visual: opts.trackRenderer ?? DEFAULT_TRACK,+ visual: opts.trackVisual ?? { color: c.track },});const fill = spawnUIEntity({world: opts.world,parent: track,node: fillNodeAt(fraction(value, min, max)),- visual: opts.fillRenderer ?? DEFAULT_FILL,+ visual: opts.fillVisual ?? { color: c.primary },});const handle = spawnUIEntity({world: opts.world,parent: track,node: handleNodeAt(fraction(value, min, max), handleWidth),- visual: opts.handleRenderer ?? DEFAULT_HANDLE,+ visual: opts.handleVisual ?? { color: c.onPrimary },});// Value -> geometry: the fill's width is the value fraction; the handle'sdiff --git a/sdk/tests/ui-theme.test.ts b/sdk/tests/ui-theme.test.tsnew file mode 100644index 000000000..fce872949--- /dev/null+++ b/sdk/tests/ui-theme.test.ts@@ -0,0 +1,33 @@+// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file ThemeTokens — the design-token palette widgets resolve defaults from.+ */+import { describe, it, expect, afterEach } from 'vitest';+import {+ DARK_TOKENS,+ getTheme,+ setTheme,+ themeColors,+ type ThemeTokens,+} from '../src/ui/theme/tokens';++describe('ThemeTokens', () => {+ afterEach(() => setTheme(DARK_TOKENS));++ it('defaults to the built-in dark palette', () => {+ expect(getTheme()).toBe(DARK_TOKENS);+ expect(themeColors().primary).toEqual({ r: 0.25, g: 0.56, b: 0.96, a: 1 });+ expect(themeColors().backdrop).toEqual({ r: 0, g: 0, b: 0, a: 0.5 });+ });++ it('setTheme swaps the active palette for later widget construction', () => {+ const custom: ThemeTokens = {+ colors: { ...DARK_TOKENS.colors, primary: { r: 1, g: 0, b: 0, a: 1 } },+ };+ setTheme(custom);+ expect(themeColors().primary).toEqual({ r: 1, g: 0, b: 0, a: 1 });+ // unrelated roles are preserved+ expect(themeColors().surface).toEqual(DARK_TOKENS.colors.surface);+ });+});