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): text + render-order on UINode (REARCH_GUI unification S1)

esengine3d8c2641242b
diff --git a/desktop/public/scenes/ui-text-uinode.esscene b/desktop/public/scenes/ui-text-uinode.esscene
new file mode 100644
index 000000000..86196a834
--- /dev/null
+++ b/desktop/public/scenes/ui-text-uinode.esscene
@@ -0,0 +1,53 @@
+{
+ "version": "1.0",
+ "name": "Text laid out in a UINode box (REARCH_GUI S1)",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },
+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 300, "isActive": true, "priority": 0 } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Canvas",
+ "parent": null,
+ "children": [2],
+ "components": [
+ { "type": "Transform", "data": {} },
+ { "type": "Canvas", "data": { "designResolution": { "x": 800, "y": 600 }, "scaleMode": 2 } },
+ { "type": "UIRect", "data": { "anchorMin": { "x": 0.5, "y": 0.5 }, "anchorMax": { "x": 0.5, "y": 0.5 }, "size": { "x": 800, "y": 600 }, "pivot": { "x": 0.5, "y": 0.5 } } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "TextBox",
+ "parent": 1,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": {} },
+ { "type": "UINode", "data": { "position": 1, "insetLeft": { "value": 180, "unit": 0 }, "insetTop": { "value": 220, "unit": 0 }, "width": { "value": 440, "unit": 0 }, "height": { "value": 160, "unit": 0 } } },
+ { "type": "UIRenderer", "data": { "visualType": 1, "color": { "r": 0.18, "g": 0.2, "b": 0.28, "a": 1 }, "enabled": true } },
+ {
+ "type": "Text",
+ "data": {
+ "content": "UINode 文本框:自动换行 + 居中 wraps and centers inside its box",
+ "fontFamily": "sans-serif",
+ "fontSize": 34,
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "align": 1,
+ "verticalAlign": 1,
+ "wordWrap": true
+ }
+ }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/desktop/public/scenes/ui-text-uinode.textures.json b/desktop/public/scenes/ui-text-uinode.textures.json
new file mode 100644
index 000000000..9e26dfeeb
--- /dev/null
+++ b/desktop/public/scenes/ui-text-uinode.textures.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/sdk/src/ui/text/plugin.ts b/sdk/src/ui/text/plugin.ts
index 50214196b..b8edd180f 100644
--- a/sdk/src/ui/text/plugin.ts
+++ b/sdk/src/ui/text/plugin.ts
@@ -18,7 +18,8 @@ import { SdfTextRenderer } from './text-renderer';
import { composeTRS, rectTextBox, UI_TEXT_BOLD, UI_TEXT_ITALIC } from './text-transform';
import { Text, type TextData } from '../core/text';
import { UIRect, type UIRectData } from '../core/ui-rect';
-import { getEffectiveWidth, getEffectiveHeight, ensureUIRenderer } from '../uiHelpers';
+import { UINode } from '../core/ui-node';
+import { getEffectiveWidth, getEffectiveHeight, getUINodeWidth, getUINodeHeight, ensureUIRenderer } from '../uiHelpers';
// Matches C++ UIElementPlugin::UI_BASE_LAYER — UI quads use layer = base + uiOrder.
const UI_BASE_LAYER = 1000;
@@ -42,6 +43,9 @@ export class TextPlugin implements Plugin {
for (const e of world.getEntitiesWithComponents([Text, UIRect])) {
ensureUIRenderer(world, e as Entity);
}
+ for (const e of world.getEntitiesWithComponents([Text, UINode])) {
+ ensureUIRenderer(world, e as Entity);
+ }
}, { name: 'TextRenderNodeSystem' }));
const pipeline = app.pipeline;
@@ -66,19 +70,30 @@ export class TextPlugin implements Plugin {
// Text.lineHeight is a ratio of fontSize (legacy convention).
const lineHeightPx = t.lineHeight > 0 ? t.lineHeight * t.fontSize : undefined;
- // A UIRect (UI canvas) is the text box: place + align + wrap inside
- // it and sort by the UI render order. No UIRect ⇒ a world-space
- // label at the entity origin, layer 0.
+ // The layout box: a UINode (CSS box, pivot-centered) or legacy
+ // UIRect. Text is placed + aligned + wrapped inside it and sorted
+ // by the UI render order. No box ⇒ a world-space label at the
+ // entity origin, layer 0.
let originX: number | undefined;
let originY: number | undefined;
let maxWidth: number | undefined;
let boxHeight: number | undefined;
let layer = 0;
- if (world.has(entity, UIRect)) {
+ let w = 0, h = 0, pivotX = 0.5, pivotY = 0.5, hasBox = false;
+ if (world.has(entity, UINode)) {
+ w = getUINodeWidth(entity);
+ h = getUINodeHeight(entity);
+ hasBox = w > 0 || h > 0;
+ } else if (world.has(entity, UIRect)) {
const rect = world.get(entity, UIRect) as UIRectData;
- const w = getEffectiveWidth(rect, entity);
- const h = getEffectiveHeight(rect, entity);
- const box = rectTextBox(rect.pivot.x, rect.pivot.y, w, h, t.fontSize);
+ w = getEffectiveWidth(rect, entity);
+ h = getEffectiveHeight(rect, entity);
+ pivotX = rect.pivot.x;
+ pivotY = rect.pivot.y;
+ hasBox = true;
+ }
+ if (hasBox) {
+ const box = rectTextBox(pivotX, pivotY, w, h, t.fontSize);
originX = box.originX;
originY = box.originY;
boxHeight = box.boxHeight;
diff --git a/sdk/src/ui/uiHelpers.ts b/sdk/src/ui/uiHelpers.ts
index 476cf86de..6d15c2267 100644
--- a/sdk/src/ui/uiHelpers.ts
+++ b/sdk/src/ui/uiHelpers.ts
@@ -310,6 +310,22 @@ export function getEffectiveHeight(rect: UIRectData, entity: Entity): number {
return rect.size.y;
}
+/** Resolved px width of a UINode (its Yoga-computed size; REARCH_GUI F3). 0 if unresolved. */
+export function getUINodeWidth(entity: Entity): number {
+ if (module_ && nativeRegistry_ && module_.getUINodeComputedWidth) {
+ return module_.getUINodeComputedWidth(nativeRegistry_, entity);
+ }
+ return 0;
+}
+
+/** Resolved px height of a UINode. 0 if unresolved. */
+export function getUINodeHeight(entity: Entity): number {
+ if (module_ && nativeRegistry_ && module_.getUINodeComputedHeight) {
+ return module_.getUINodeComputedHeight(nativeRegistry_, entity);
+ }
+ return 0;
+}
+
export function setUIRectSizeNative(entity: Entity, w: number, h: number): void {
if (module_ && nativeRegistry_) {
module_.setUIRectSize(nativeRegistry_, entity, w, h);
diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts
index 12017d1f6..daa053f37 100644
--- a/sdk/src/wasm.ts
+++ b/sdk/src/wasm.ts
@@ -335,6 +335,8 @@ export interface ESEngineModule {
uiFlexLayout_update(registry: CppRegistry): void;
getUIRectComputedWidth(registry: CppRegistry, entity: number): number;
getUIRectComputedHeight(registry: CppRegistry, entity: number): number;
+ getUINodeComputedWidth?(registry: CppRegistry, entity: number): number;
+ getUINodeComputedHeight?(registry: CppRegistry, entity: number): number;
setUIRectSize(registry: CppRegistry, entity: number, w: number, h: number): void;
uiTree_markStructureDirty(): void;
uiTree_markDirty(entity: number): void;
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index 794d3618f..8d51739f0 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -44,6 +44,7 @@
#include "../ecs/components/Velocity.hpp"
#include "../ecs/components/Camera.hpp"
#include "../ecs/components/UIRect.hpp"
+#include "../ecs/components/UINode.hpp"
#include "../ecs/components/UIRenderer.hpp"
#include "../ecs/components/RigidBody.hpp"
#include "../ecs/components/Collider.hpp"
@@ -593,6 +594,20 @@ f32 getUIRectComputedHeight(ecs::Registry& registry, u32 entity) {
return rect->computed_size_.y;
}
+// UINode (CSS box, REARCH_GUI F3) computed size — its internal computed_size_ is
+// not embind-readable, so expose it the same way as UIRect for TS uiHelpers.
+f32 getUINodeComputedWidth(ecs::Registry& registry, u32 entity) {
+ auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
+ if (!node) return 0.0f;
+ return node->computed_size_.x;
+}
+
+f32 getUINodeComputedHeight(ecs::Registry& registry, u32 entity) {
+ auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
+ if (!node) return 0.0f;
+ return node->computed_size_.y;
+}
+
void setUIRectSize(ecs::Registry& registry, u32 entity, f32 w, f32 h) {
auto* rect = registry.tryGet<ecs::UIRect>(Entity::fromRaw(entity));
if (!rect) return;
@@ -651,6 +666,8 @@ EMSCRIPTEN_BINDINGS(esengine_ui_systems) {
emscripten::function("uiFlexLayout_update", &esengine::uiFlexLayout_update);
emscripten::function("getUIRectComputedWidth", &esengine::getUIRectComputedWidth);
emscripten::function("getUIRectComputedHeight", &esengine::getUIRectComputedHeight);
+ emscripten::function("getUINodeComputedWidth", &esengine::getUINodeComputedWidth);
+ emscripten::function("getUINodeComputedHeight", &esengine::getUINodeComputedHeight);
emscripten::function("uiTree_markStructureDirty", &esengine::uiTree_markStructureDirty);
emscripten::function("uiTree_markDirty", &esengine::uiTree_markDirty);
emscripten::function("uiTree_markAllDirty", &esengine::uiTree_markAllDirty);
diff --git a/src/esengine/ecs/UIRenderOrderSystem.hpp b/src/esengine/ecs/UIRenderOrderSystem.hpp
index 7bfdefbf6..ff55e909d 100644
--- a/src/esengine/ecs/UIRenderOrderSystem.hpp
+++ b/src/esengine/ecs/UIRenderOrderSystem.hpp
@@ -5,6 +5,7 @@
#include "Registry.hpp"
#include "components/Hierarchy.hpp"
#include "components/UIRect.hpp"
+#include "components/UINode.hpp"
#include "components/Sprite.hpp"
#include "components/UIRenderer.hpp"
#include "components/Canvas.hpp"
@@ -12,7 +13,8 @@
namespace esengine::ecs {
inline i32 assignRenderOrder(Registry& registry, Entity entity, i32 counter) {
- if (registry.has<UIRect>(entity)) {
+ // A UI render node is anything laid out: legacy UIRect or modern UINode.
+ if (registry.has<UIRect>(entity) || registry.has<UINode>(entity)) {
auto* uiRenderer = registry.tryGet<UIRenderer>(entity);
if (uiRenderer) {
uiRenderer->uiOrder = counter;