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.

fix: unify UI layout authority to C++ UILayoutSystem

esenginef1103fe8b831
diff --git a/sdk/src/ui/ProgressBarPlugin.ts b/sdk/src/ui/ProgressBarPlugin.ts
index f0644eac8..f12281acc 100644
--- a/sdk/src/ui/ProgressBarPlugin.ts
+++ b/sdk/src/ui/ProgressBarPlugin.ts
@@ -1,50 +1,11 @@
import type { App, Plugin } from '../app';
-import { registerComponent, Transform, Sprite } from '../component';
-import type { TransformData, SpriteData } from '../component';
+import { registerComponent } from '../component';
import { defineSystem, Schedule } from '../system';
-import type { Entity } from '../types';
-import type { World } from '../world';
import { UIRect } from './UIRect';
import type { UIRectData } from './UIRect';
-import { UIRenderer } from './UIRenderer';
-import type { UIRendererData } from './UIRenderer';
import { ProgressBar } from './ProgressBar';
import type { ProgressBarData } from './ProgressBar';
-import { applyDirectionalFill, getEffectiveWidth, getEffectiveHeight, computeUIRectLayout } from './uiHelpers';
-
-function syncFillLayout(
- world: World, fillEntity: Entity,
- parentW: number, parentH: number,
-): void {
- if (!world.has(fillEntity, UIRect)) return;
- const fillRect = world.get(fillEntity, UIRect) as UIRectData;
- const halfW = parentW / 2;
- const halfH = parentH / 2;
- const parentRect = { left: -halfW, bottom: -halfH, right: halfW, top: halfH };
- const result = computeUIRectLayout(
- fillRect.anchorMin, fillRect.anchorMax,
- fillRect.offsetMin, fillRect.offsetMax,
- fillRect.size, parentRect, fillRect.pivot,
- );
-
- if (!world.has(fillEntity, UIRenderer) && world.has(fillEntity, Sprite)) {
- const sprite = world.get(fillEntity, Sprite) as SpriteData;
- if (sprite.size.x !== result.width || sprite.size.y !== result.height) {
- sprite.size.x = result.width;
- sprite.size.y = result.height;
- world.insert(fillEntity, Sprite, sprite);
- }
- }
-
- if (world.has(fillEntity, Transform)) {
- const t = world.get(fillEntity, Transform) as TransformData;
- const curPos = t.position;
- if (curPos.x !== result.originX || curPos.y !== result.originY) {
- t.position = { x: result.originX, y: result.originY, z: curPos.z };
- world.insert(fillEntity, Transform, t);
- }
- }
-}
+import { applyDirectionalFill, withChildEntity } from './uiHelpers';
export class ProgressBarPlugin implements Plugin {
build(app: App): void {
@@ -58,14 +19,11 @@ export class ProgressBarPlugin implements Plugin {
const entities = world.getEntitiesWithComponents([ProgressBar, UIRect]);
for (const entity of entities) {
const bar = world.get(entity, ProgressBar) as ProgressBarData;
- if (!bar.fillEntity || !world.valid(bar.fillEntity)) continue;
-
- const rect = world.get(entity, UIRect) as UIRectData;
const value = Math.max(0, Math.min(1, bar.value));
- applyDirectionalFill(world, bar.fillEntity, bar.direction, value);
- const sw = getEffectiveWidth(rect, entity);
- const sh = getEffectiveHeight(rect, entity);
- syncFillLayout(world, bar.fillEntity, sw, sh);
+
+ withChildEntity(world, bar.fillEntity, (fill) => {
+ applyDirectionalFill(world, fill, bar.direction, value);
+ });
}
},
{ name: 'ProgressBarSystem' }
diff --git a/sdk/src/ui/SliderPlugin.ts b/sdk/src/ui/SliderPlugin.ts
index d00452ed1..b8b3043b4 100644
--- a/sdk/src/ui/SliderPlugin.ts
+++ b/sdk/src/ui/SliderPlugin.ts
@@ -17,7 +17,7 @@ import { UICameraInfo } from './UICameraInfo';
import type { UICameraData } from './UICameraInfo';
import { UIEvents, UIEventQueue } from './UIEvents';
import { isEditor, isPlayMode } from '../env';
-import { applyDirectionalFill, getEffectiveWidth, getEffectiveHeight, ensureComponent, layoutChildEntity, type LayoutRect } from './uiHelpers';
+import { applyDirectionalFill, getEffectiveWidth, getEffectiveHeight, ensureComponent, withChildEntity } from './uiHelpers';
import { quaternionToAngle2D } from './uiMath';
function computeSliderValue(
@@ -98,9 +98,9 @@ export class SliderPlugin implements Plugin {
const hasWT = world.has(entity, Transform);
if (hasWT) {
ensureComponent(world, entity, Interactable, { enabled: true, blockRaycast: true });
- if (slider.handleEntity !== 0 && world.valid(slider.handleEntity)) {
- ensureComponent(world, slider.handleEntity, Interactable, { enabled: true, blockRaycast: false });
- }
+ withChildEntity(world, slider.handleEntity, (handle) => {
+ ensureComponent(world, handle, Interactable, { enabled: true, blockRaycast: false });
+ });
const wt = world.get(entity, Transform) as TransformData;
@@ -108,9 +108,11 @@ export class SliderPlugin implements Plugin {
if (world.has(entity, UIInteraction)) {
isDragging = (world.get(entity, UIInteraction) as UIInteractionData).pressed;
}
- if (!isDragging && slider.handleEntity !== 0 && world.valid(slider.handleEntity) && world.has(slider.handleEntity, UIInteraction)) {
- isDragging = (world.get(slider.handleEntity, UIInteraction) as UIInteractionData).pressed;
- }
+ withChildEntity(world, slider.handleEntity, (handle) => {
+ if (!isDragging && world.has(handle, UIInteraction)) {
+ isDragging = (world.get(handle, UIInteraction) as UIInteractionData).pressed;
+ }
+ });
if (isDragging && input.isMouseButtonDown(0)) {
let t = computeSliderValue(
@@ -138,24 +140,16 @@ export class SliderPlugin implements Plugin {
const range = slider.maxValue - slider.minValue;
const normalizedValue = range > 0 ? (slider.value - slider.minValue) / range : 0;
- const sw = getEffectiveWidth(rect, entity);
- const sh = getEffectiveHeight(rect, entity);
- const parentRect: LayoutRect = {
- left: -sw / 2, bottom: -sh / 2,
- right: sw / 2, top: sh / 2,
- };
+ withChildEntity(world, slider.fillEntity, (fill) => {
+ applyDirectionalFill(world, fill, slider.direction, normalizedValue);
+ });
- if (slider.fillEntity !== 0 && world.valid(slider.fillEntity)) {
- applyDirectionalFill(world, slider.fillEntity, slider.direction, normalizedValue);
- layoutChildEntity(world, slider.fillEntity, parentRect, 0, 0);
- }
-
- if (slider.handleEntity !== 0 && world.valid(slider.handleEntity) && world.has(slider.handleEntity, UIRect)) {
- const handleRect = world.get(slider.handleEntity, UIRect) as UIRectData;
+ withChildEntity(world, slider.handleEntity, (handle) => {
+ if (!world.has(handle, UIRect)) return;
+ const handleRect = world.get(handle, UIRect) as UIRectData;
syncHandleRect(handleRect, slider.direction, normalizedValue);
- world.insert(slider.handleEntity, UIRect, handleRect);
- layoutChildEntity(world, slider.handleEntity, parentRect, 0, 0);
- }
+ world.insert(handle, UIRect, handleRect);
+ });
}
},
{ name: 'SliderSystem' }
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index 5ffa13c4c..fe21ce243 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -22,8 +22,13 @@ export {
ensureComponent,
makeInteractable,
syncFillSpriteSize,
- layoutChildEntity,
walkParentChain,
+ withChildEntity,
+ setEntityColor,
+ setEntityEnabled,
+ colorScale,
+ colorWithAlpha,
+ EntityStateMap,
} from './uiHelpers';
// Snapshot Utilities
@@ -358,3 +363,40 @@ export {
UIRenderOrderPlugin,
uiRenderOrderPlugin,
} from './UIRenderOrderPlugin';
+
+// UI Theme
+export {
+ UIThemeRes,
+ DARK_THEME,
+ type UITheme,
+} from './UITheme';
+
+// UI Builder (Factory Functions & Declarative Tree)
+export {
+ UI,
+ initUIBuilder,
+ type UIEntityDef,
+ type ButtonOptions,
+ type SliderOptions,
+ type ToggleOptions,
+ type ProgressBarOptions,
+ type ScrollViewOptions,
+ type TextInputOptions,
+ type DropdownOptions,
+ type LabelOptions,
+ type PanelOptions,
+ type FlexOptions,
+ type UINode,
+ type UIElementNode,
+ type UIButtonNode,
+ type UISliderNode,
+ type UIToggleNode,
+ type UITextInputNode,
+ type UIDropdownNode,
+ type UIProgressBarNode,
+ type UILabelNode,
+ type UIPanelNode,
+ type UIFlexRowNode,
+ type UIFlexColumnNode,
+ type UIScrollViewNode,
+} from './UIBuilder';
diff --git a/sdk/src/ui/uiHelpers.ts b/sdk/src/ui/uiHelpers.ts
index 8cf6b4b5b..0c31f7b3f 100644
--- a/sdk/src/ui/uiHelpers.ts
+++ b/sdk/src/ui/uiHelpers.ts
@@ -4,6 +4,8 @@ import type { Entity, Color } from '../types';
import type { World } from '../world';
import { UIRect } from './UIRect';
import type { UIRectData } from './UIRect';
+import { UIRenderer } from './UIRenderer';
+import type { UIRendererData } from './UIRenderer';
import { FillDirection } from './uiTypes';
import type { ColorTransition } from './uiTypes';
import type { ESEngineModule, CppRegistry } from '../wasm';
@@ -362,36 +364,78 @@ export function makeInteractable(world: World, entity: Entity): void {
});
}
-export function layoutChildEntity(
- world: World, entity: Entity,
- parentRect: LayoutRect,
- parentOriginX: number, parentOriginY: number,
+export function withChildEntity(
+ world: World,
+ childId: Entity,
+ callback: (entity: Entity) => void,
): void {
- if (!world.has(entity, UIRect)) return;
- const rect = world.get(entity, UIRect) as UIRectData;
-
- const result = computeUIRectLayout(
- rect.anchorMin, rect.anchorMax,
- rect.offsetMin, rect.offsetMax,
- rect.size, parentRect, rect.pivot,
- );
- const width = result.rect.right - result.rect.left;
- const height = result.rect.top - result.rect.bottom;
-
- if (world.has(entity, Sprite)) {
- const sprite = world.get(entity, Sprite) as SpriteData;
- if (sprite.size.x !== width || sprite.size.y !== height) {
- sprite.size.x = width;
- sprite.size.y = height;
- world.insert(entity, Sprite, sprite);
+ if (childId !== 0 && world.valid(childId)) {
+ callback(childId);
+ }
+}
+
+export function setEntityColor(world: World, entity: Entity, color: Color): void {
+ if (world.has(entity, UIRenderer)) {
+ const r = world.get(entity, UIRenderer) as UIRendererData;
+ r.color = color;
+ world.insert(entity, UIRenderer, r);
+ } else if (world.has(entity, Sprite)) {
+ const s = world.get(entity, Sprite) as SpriteData;
+ s.color = color;
+ world.insert(entity, Sprite, s);
+ }
+}
+
+export function setEntityEnabled(world: World, entity: Entity, enabled: boolean): void {
+ if (world.has(entity, UIRenderer)) {
+ const r = world.get(entity, UIRenderer) as UIRendererData;
+ r.enabled = enabled;
+ world.insert(entity, UIRenderer, r);
+ } else if (world.has(entity, Sprite)) {
+ const s = world.get(entity, Sprite) as SpriteData;
+ s.enabled = enabled;
+ world.insert(entity, Sprite, s);
+ }
+}
+
+export function colorScale(c: Color, factor: number): Color {
+ return {
+ r: Math.min(1, c.r * factor),
+ g: Math.min(1, c.g * factor),
+ b: Math.min(1, c.b * factor),
+ a: c.a,
+ };
+}
+
+export function colorWithAlpha(c: Color, alpha: number): Color {
+ return { r: c.r, g: c.g, b: c.b, a: alpha };
+}
+
+export class EntityStateMap<T> {
+ private map_ = new Map<Entity, T>();
+
+ get(entity: Entity): T | undefined { return this.map_.get(entity); }
+ set(entity: Entity, state: T): void { this.map_.set(entity, state); }
+ delete(entity: Entity): void { this.map_.delete(entity); }
+ has(entity: Entity): boolean { return this.map_.has(entity); }
+
+ cleanup(world: World): void {
+ for (const [e] of this.map_) {
+ if (!world.valid(e)) this.map_.delete(e);
}
}
- if (world.has(entity, Transform)) {
- const transform = world.get(entity, Transform) as TransformData;
- transform.position.x = result.originX - parentOriginX;
- transform.position.y = result.originY - parentOriginY;
- world.insert(entity, Transform, transform);
+ ensureInit(entity: Entity, init: () => T): T {
+ let state = this.map_.get(entity);
+ if (!state) {
+ state = init();
+ this.map_.set(entity, state);
+ }
+ return state;
}
+
+ clear(): void { this.map_.clear(); }
+
+ [Symbol.iterator]() { return this.map_[Symbol.iterator](); }
}
diff --git a/src/esengine/ecs/UILayoutSystem.hpp b/src/esengine/ecs/UILayoutSystem.hpp
index c3c0bb9f9..52ea2df77 100644
--- a/src/esengine/ecs/UILayoutSystem.hpp
+++ b/src/esengine/ecs/UILayoutSystem.hpp
@@ -3,7 +3,6 @@
#include "Registry.hpp"
#include "UITree.hpp"
#include "components/Transform.hpp"
-#include "components/Hierarchy.hpp"
#include "components/UIRect.hpp"
#include "components/Sprite.hpp"
#include "components/Canvas.hpp"
@@ -98,25 +97,8 @@ inline void writePosition(
auto* transform = registry.tryGet<Transform>(node.entity);
if (!transform) return;
- if (node.parent == INVALID_ENTITY) {
- transform->position.x = originX;
- transform->position.y = originY;
- } else {
- auto& parentTransform = registry.get<Transform>(node.parent);
- transform->position.x = originX - parentTransform.position.x;
- transform->position.y = originY - parentTransform.position.y;
-
- if (registry.has<Parent>(node.entity)) {
- Entity actualParent = registry.get<Parent>(node.entity).entity;
- if (actualParent != node.parent) {
- auto* actualTransform = registry.tryGet<Transform>(actualParent);
- if (actualTransform) {
- transform->position.x = originX - actualTransform->position.x;
- transform->position.y = originY - actualTransform->position.y;
- }
- }
- }
- }
+ transform->position.x = originX;
+ transform->position.y = originY;
}
inline void layoutNodeAnchor(