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
fix(ui): bound hierarchy walks and preserve sibling order
yhh2a8870981657
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 37c8da530..14fab8b28 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **UI hierarchy walks tolerate deep scene groups and broken child links.** Tree construction, screen-domain collection and draw-order assignment use iterative traversal. Repeated or cyclic child links stop revisiting entities while preserving the authored sibling order and nearest UI parent.+- **Large UI subtrees keep their full traversal span.** Subtree lengths no longer wrap at 65536 nodes, preventing layout and visibility walks from freezing or skipping content at the 16-bit boundary.- **Static UI layout preserves composed world transforms.** Clean early and late layout passes keep the transform cache valid, and the SDK composes through the shared change-aware path. Moving a screen box, changing layout properties, tweening, and structural edits still invalidate the affected composition.diff --git a/src/esengine/ui/UILayoutSystem.cpp b/src/esengine/ui/UILayoutSystem.cppindex 513b542a7..d7f710a7a 100644--- a/src/esengine/ui/UILayoutSystem.cpp+++ b/src/esengine/ui/UILayoutSystem.cpp@@ -406,7 +406,7 @@ void propagateInheritedUIState(Registry& registry, UITree& tree) {inherited_block_stack_.assign(nodes.size() + 1, static_cast<u8>(0));for (usize i = 0; i < nodes.size(); ++i) {- const u16 depth = nodes[i].depth;+ const u32 depth = nodes[i].depth;const f32 parentAlpha = depth == 0 ? 1.0f : inherited_alpha_stack_[depth - 1];const bool parentBlocked = depth == 0 ? false : inherited_block_stack_[depth - 1] != 0;@@ -475,11 +475,14 @@ void placeScreenRoots(Registry& registry, UITree& tree, LayoutCache& cache,/** @brief Adds @p entity and its whole Transform subtree to @p out. */void addSubtree(Registry& registry, Entity entity, std::unordered_set<u32>& out) {- out.insert(entity.id());- auto* children = registry.tryGet<Children>(entity);- if (!children) return;- for (Entity child : children->entities) {- if (registry.valid(child)) addSubtree(registry, child, out);+ std::vector<Entity> pending{entity};+ while (!pending.empty()) {+ const Entity current = pending.back();+ pending.pop_back();+ if (!registry.valid(current) || !out.insert(current.id()).second) continue;+ auto* children = registry.tryGet<Children>(current);+ if (!children) continue;+ for (Entity child : children->entities) pending.push_back(child);}}diff --git a/src/esengine/ui/UIRenderOrderSystem.hpp b/src/esengine/ui/UIRenderOrderSystem.hppindex 4974ebbb3..aa5bdf0e5 100644--- a/src/esengine/ui/UIRenderOrderSystem.hpp+++ b/src/esengine/ui/UIRenderOrderSystem.hpp@@ -10,40 +10,45 @@#include "../ecs/components/Canvas.hpp"#include "../ecs/components/ParticleEmitter.hpp"+#include <vector>+namespace esengine::ecs {inline i32 assignRenderOrder(Registry& registry, Entity entity, i32 counter, u32 cullBit) {- if (registry.has<UINode>(entity)) {- auto* uiVisual = registry.tryGet<UIVisual>(entity);- if (uiVisual) {- uiVisual->uiOrder = counter;- uiVisual->uiCullBit = cullBit;- counter++;- } else if (registry.has<Sprite>(entity)) {- auto& sprite = registry.get<Sprite>(entity);- if (sprite.layer != counter) {- sprite.layer = counter;- }- counter++;- } else if (auto* emitter = registry.tryGet<ParticleEmitter>(entity)) {- // An emitter in the UI tree draws where the tree says it does, like the- // Sprite above: without this it keeps its authored layer and a UI effect- // lands under (or over) the whole panel instead of between two of its- // elements — the reason Unity needs a UIParticle extension at all.- if (emitter->layer != counter) {- emitter->layer = counter;+ std::vector<Entity> pending{entity};+ std::vector<u8> visited;+ while (!pending.empty()) {+ entity = pending.back();+ pending.pop_back();+ if (!registry.valid(entity)) continue;+ const auto index = entity.index();+ if (index >= visited.size()) visited.resize(index + 1, 0);+ if (visited[index]) continue;+ visited[index] = 1;+ if (registry.has<UINode>(entity)) {+ auto* uiVisual = registry.tryGet<UIVisual>(entity);+ if (uiVisual) {+ uiVisual->uiOrder = counter;+ uiVisual->uiCullBit = cullBit;+ counter++;+ } else if (registry.has<Sprite>(entity)) {+ auto& sprite = registry.get<Sprite>(entity);+ if (sprite.layer != counter) {+ sprite.layer = counter;+ }+ counter++;+ } else if (auto* emitter = registry.tryGet<ParticleEmitter>(entity)) {+ // UI emitters inherit tree order so their particles interleave with controls.+ if (emitter->layer != counter) {+ emitter->layer = counter;+ }+ counter++;}- counter++;- }- }-- auto* children = registry.tryGet<Children>(entity);- if (!children) return counter;-- for (Entity child : children->entities) {- if (registry.valid(child)) {- counter = assignRenderOrder(registry, child, counter, cullBit);}+ auto* children = registry.tryGet<Children>(entity);+ if (!children) continue;+ for (auto child = children->entities.rbegin(); child != children->entities.rend(); ++child)+ pending.push_back(*child);}return counter;}diff --git a/src/esengine/ui/UITree.hpp b/src/esengine/ui/UITree.hppindex f95503d0f..6c73217dd 100644--- a/src/esengine/ui/UITree.hpp+++ b/src/esengine/ui/UITree.hpp@@ -9,6 +9,7 @@#include "../ecs/components/Transform.hpp"#include <vector>+#include <algorithm>namespace esengine::ecs {@@ -27,7 +28,7 @@ struct UITree {struct Node {Entity entity;Entity parent;- u16 depth;+ u32 depth;u32 subtree_size;};@@ -54,29 +55,50 @@ struct UITree {}private:- void buildDFS(Registry& reg, Entity entity, Entity layoutParent, u16 depth) {- // A layout node is any UINode (the single CSS-box layout model).- bool isLayoutNode = reg.has<UINode>(entity);- i32 nodeIndex = -1;+ struct Frame {+ Entity entity;+ Entity layoutParent;+ u32 depth;+ i32 nodeIndex;+ usize nextChild;+ };+ std::vector<Frame> pending_;+ std::vector<u32> visit_stamps_;+ u32 visit_stamp_{0};- if (isLayoutNode) {- nodeIndex = static_cast<i32>(nodes_.size());- nodes_.push_back({entity, layoutParent, depth, 1});- layoutParent = entity;- depth++;+ void buildDFS(Registry& reg, Entity entity, Entity layoutParent, u32 depth) {+ if (++visit_stamp_ == 0) {+ std::fill(visit_stamps_.begin(), visit_stamps_.end(), 0);+ ++visit_stamp_;}-- auto* children = reg.tryGet<Children>(entity);- if (children) {- for (Entity child : children->entities) {- if (reg.valid(child)) {- buildDFS(reg, child, layoutParent, depth);- }+ pending_.clear();+ const auto enter = [&](Entity current, Entity parent, u32 currentDepth) {+ const auto index = current.index();+ if (index >= visit_stamps_.size()) visit_stamps_.resize(index + 1, 0);+ if (visit_stamps_[index] == visit_stamp_) return;+ visit_stamps_[index] = visit_stamp_;+ i32 nodeIndex = -1;+ if (reg.has<UINode>(current)) {+ nodeIndex = static_cast<i32>(nodes_.size());+ nodes_.push_back({current, parent, currentDepth, 1});+ parent = current;+ ++currentDepth;}- }-- if (isLayoutNode && nodeIndex >= 0) {- nodes_[nodeIndex].subtree_size = static_cast<u32>(nodes_.size() - nodeIndex);+ pending_.push_back({current, parent, currentDepth, nodeIndex, 0});+ };+ enter(entity, layoutParent, depth);+ while (!pending_.empty()) {+ auto& frame = pending_.back();+ auto* children = reg.tryGet<Children>(frame.entity);+ if (children && frame.nextChild < children->entities.size()) {+ Entity child = children->entities[frame.nextChild++];+ if (reg.valid(child)) enter(child, frame.layoutParent, frame.depth);+ continue;+ }+ if (frame.nodeIndex >= 0) {+ nodes_[frame.nodeIndex].subtree_size = static_cast<u32>(nodes_.size() - frame.nodeIndex);+ }+ pending_.pop_back();}}};diff --git a/tests/ecs/test_ui_tree.cpp b/tests/ecs/test_ui_tree.cppindex 4d52974cc..76d4bbad7 100644--- a/tests/ecs/test_ui_tree.cpp+++ b/tests/ecs/test_ui_tree.cpp@@ -1,10 +1,62 @@#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN#include <doctest.h>#include "esengine/ui/UITree.hpp"+#include "esengine/ui/UIRenderOrderSystem.hpp"using namespace esengine;using namespace esengine::ecs;+TEST_CASE("deep scene groups keep UI traversal and draw order intact") {+ Registry registry;+ auto root = registry.create();+ registry.emplace<Canvas>(root);+ registry.emplace<Transform>(root);+ registry.emplace<UINode>(root);+ auto parent = root;+ for (int i = 0; i < 50000; ++i) {+ auto child = registry.create();+ registry.emplace<Children>(parent).entities.push_back(child);+ registry.emplace<Parent>(child).entity = parent;+ parent = child;+ }+ registry.emplace<UINode>(parent);+ registry.emplace<UIVisual>(parent);+ UITree tree;+ tree.rebuild(registry);+ REQUIRE(tree.nodes_.size() == 2);+ CHECK(tree.nodes_[0].subtree_size == 2);+ CHECK(tree.nodes_[1].parent == root);+ CHECK(tree.nodes_[1].depth == 1);+ uiRenderOrderUpdate(registry);+ CHECK(registry.get<UIVisual>(parent).uiOrder == 0);+}++TEST_CASE("repeated and cyclic child links cannot duplicate UI traversal") {+ Registry registry;+ auto root = registry.create(), first = registry.create(), last = registry.create();+ registry.emplace<Canvas>(root);+ registry.emplace<Transform>(root);+ registry.emplace<UINode>(root);+ registry.emplace<UINode>(first);+ registry.emplace<UINode>(last);+ registry.emplace<UIVisual>(first);+ registry.emplace<UIVisual>(last);+ registry.emplace<Children>(root).entities = {first, first, last};+ registry.emplace<Children>(first).entities = {root};+ UITree tree;+ tree.rebuild(registry);+ REQUIRE(tree.nodes_.size() == 3);+ CHECK(tree.nodes_[1].entity == first);+ CHECK(tree.nodes_[2].entity == last);+ CHECK(tree.nodes_[0].subtree_size == 3);+ uiRenderOrderUpdate(registry);+ CHECK(registry.get<UIVisual>(first).uiOrder == 0);+ CHECK(registry.get<UIVisual>(last).uiOrder == 1);+ const auto signature = tree.structure_sig_;+ tree.rebuild(registry);+ CHECK(tree.structure_sig_ == signature);+}+TEST_CASE("wide UI subtrees preserve their full span across the 16-bit boundary") {Registry registry;auto root = registry.create();diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 7ed6f1139..b7389ad17 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(ui): bound hierarchy walks and preserve sibling order':+ { note: 'UI hierarchy walks tolerate deep scene groups and broken child links.' },'fix(ui): preserve traversal spans for large subtrees':{ note: 'Large UI subtrees keep their full traversal span.' },'perf(ui): preserve transform composition across clean layout passes':