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): refresh inherited state outside Canvas layout trees
yhh4005a25b5ab0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0af308074..0aa98de72 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,6 +30,8 @@ published separately; it ships inside the editor.### Fixed+- **UI inheritance stays current after leaving a Canvas.** Hidden state, opacity and pointer policy refresh for UI nodes outside the layout tree, including detached Canvas subtrees and ancestors of nested canvases. Cached layout sizes and transforms remain unchanged until layout owns them again. Shared ancestry is resolved iteratively with reusable memo entries; fully covered Canvas trees keep their existing static layout path.+- **Bitmap text follows device pixel ratio changes during an App.** World and screen text refresh bitmap atlas density from the current frame, rebuilding cached geometry for changed pixel sizes without restarting the App. Stable frames keep cached glyphs, SDF retains its fixed source size, and existing frame-boundary page retirement keeps referenced textures alive during transitions.- **Nested UI stencil masks intersect their parents and restore clipping.** Child masks enter only matching parent stencil pixels, and leaving a subtree replays its original geometry to restore the parent before siblings draw. Rotated and alpha-cutout shapes keep their intersections; mask replays preserve sampler bindings. Depth references are reused across siblings, with up to 255 nested levels when no SpriteMask references are reserved. Shared reference overflow clips affected subtrees instead of aliasing; restoration adds draws without allocating mask textures or duplicate vertices.diff --git a/src/esengine/ui/UILayoutSystem.cpp b/src/esengine/ui/UILayoutSystem.cppindex d7f710a7a..b22e67214 100644--- a/src/esengine/ui/UILayoutSystem.cpp+++ b/src/esengine/ui/UILayoutSystem.cpp@@ -528,6 +528,48 @@ void unifiedLayoutPass(Registry& registry, UITree& tree, LayoutCache& cache,// UISystem layout entry (defined here to share the anon-namespace helpers)// =============================================================================+void UISystem::propagateDetachedUIState(Registry& registry) {+ if (++inherited_stamp_ == 0) {+ inherited_cache_.clear();+ ++inherited_stamp_;+ }+ registry.each<UINode>([&](Entity entity, UINode&) {+ inherited_path_.clear();+ InheritedState state;+ Entity current = entity;+ while (registry.valid(current)) {+ auto& memo = inherited_cache_[current];+ if (memo.stamp == inherited_stamp_) {+ state = memo.resolving ? InheritedState{true, 0.0f, true} : memo.state;+ break;+ }+ memo.stamp = inherited_stamp_;+ memo.resolving = true;+ inherited_path_.push_back(current);+ const auto* parent = registry.tryGet<Parent>(current);+ current = parent ? parent->entity : INVALID_ENTITY;+ }+ for (auto it = inherited_path_.rbegin(); it != inherited_path_.rend(); ++it) {+ if (auto* node = registry.tryGet<UINode>(*it)) {+ state.hidden = state.hidden || node->display == UIDisplay::None;+ state.alpha *= std::clamp(node->opacity, 0.0f, 1.0f);+ state.blocked = state.blocked || node->pointerEvents == UIPointerEvents::None;+ node->hidden_in_tree_ = state.hidden;+ node->alpha_in_tree_ = state.alpha;+ node->pointer_blocked_in_tree_ = state.blocked;+ }+ auto& memo = inherited_cache_.at(*it);+ memo.state = state;+ memo.resolving = false;+ }+ });+ // Keep live ancestry entries for reuse, but do not retain removed trees.+ for (auto it = inherited_cache_.begin(); it != inherited_cache_.end();) {+ if (it->second.stamp != inherited_stamp_) it = inherited_cache_.erase(it);+ else ++it;+ }+}+void UISystem::layoutUpdate(Registry& registry,f32 boxLeft, f32 boxBottom, f32 boxRight, f32 boxTop,@@ -538,7 +580,10 @@ void UISystem::layoutUpdate(// the new world the previous world's layout. This is only load-bearing now// that styles persist between frames: the old pass reset every node it// touched, which hid the aliasing by throwing the stale state away.- if (layoutCache_ && lastRegistryId_ != registry.instanceId()) layoutCache_.reset();+ if (lastRegistryId_ != registry.instanceId()) {+ layoutCache_.reset();+ inherited_cache_.clear();+ }lastRegistryId_ = registry.instanceId();if (!layoutCache_) {layoutCache_ = std::make_unique<LayoutCache>();@@ -555,6 +600,12 @@ void UISystem::layoutUpdate(// stale set would draw it in the wrong domain rather than in the wrong place.collectScreenDomain(registry, screen_domain_);+ // Layout belongs to Canvas; visibility and input inheritance belong to the+ // whole UI hierarchy, including nodes that just left their Canvas.+ const bool detached = registry.view<UINode>().size() != tree.nodes_.size();+ if (detached) propagateDetachedUIState(registry);+ else if (!inherited_cache_.empty()) inherited_cache_.clear();+LayoutRect box{ boxLeft, boxBottom, boxRight, boxTop };bool animNow = anyUIAnimActive(registry);@@ -603,8 +654,10 @@ void UISystem::layoutUpdate(// which is exactly when an entity may still be owned by the parent it had// last frame. Orphaning unconditionally (as this used to) dirtied every// Yoga node on every pass, so no solve could ever be partial.- propagateHiddenInTree(registry, tree);- propagateInheritedUIState(registry, tree);+ if (!detached) {+ propagateHiddenInTree(registry, tree);+ propagateInheritedUIState(registry, tree);+ }unifiedLayoutPass(registry, tree, *layoutCache_, box, structureChanged, forceWriteback);placeScreenRoots(registry, tree, *layoutCache_, box);diff --git a/src/esengine/ui/UISystem.hpp b/src/esengine/ui/UISystem.hppindex 35c4df6a2..6d30357d4 100644--- a/src/esengine/ui/UISystem.hpp+++ b/src/esengine/ui/UISystem.hpp@@ -20,6 +20,7 @@#include <memory>#include <unordered_set>+#include <unordered_map>#include <vector>namespace esengine::resource { class ResourceManager; }@@ -132,6 +133,20 @@ public:const std::unordered_set<u32>& screenDomain() const { return screen_domain_; }private:+ struct InheritedState {+ bool hidden = false;+ f32 alpha = 1.0f;+ bool blocked = false;+ };+ struct InheritedMemo {+ u64 stamp = 0;+ bool resolving = false;+ InheritedState state;+ };+ std::unordered_map<Entity, InheritedMemo> inherited_cache_;+ std::vector<Entity> inherited_path_;+ u64 inherited_stamp_ = 0;+ void propagateDetachedUIState(Registry& registry);/** @brief World-space pick for entities outside the layout tree (see .cpp) */void hitWorldContent(Registry& registry, const PickRay& ray,const resource::ResourceManager* resources);diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cppindex f8f77ef59..bc0dd5334 100644--- a/tests/renderer/test_batch_builder.cpp+++ b/tests/renderer/test_batch_builder.cpp@@ -1100,6 +1100,134 @@ TEST_CASE("UI stencil reference budget follows depth rather than sibling count")frame.endFrame();}+TEST_CASE("detached UI refreshes inherited state without relocating its geometry") {+ ecs::Registry registry;+ ecs::UISystem ui;+ const auto child = [&](Entity parent, bool node) {+ const Entity entity = registry.create();+ registry.emplace<ecs::Transform>(entity);+ if (node) registry.emplace<ecs::UINode>(entity);+ if (parent != INVALID_ENTITY) {+ registry.emplace<ecs::Parent>(entity).entity = parent;+ if (!registry.has<ecs::Children>(parent)) registry.emplace<ecs::Children>(parent);+ registry.get<ecs::Children>(parent).entities.push_back(entity);+ }+ return entity;+ };+ const Entity root = child(INVALID_ENTITY, true);+ registry.emplace<ecs::Canvas>(root);+ const Entity group = child(root, false), leaf = child(group, true);+ registry.get<ecs::UINode>(root).opacity = .5f;+ registry.get<ecs::UINode>(leaf).opacity = .5f;+ ui.layoutUpdate(registry, -100, -100, 100, 100, true);+ CHECK(registry.get<ecs::UINode>(leaf).alpha_in_tree_ == doctest::Approx(.25f));+ const auto position = registry.get<ecs::Transform>(leaf).position;+ const auto size = registry.get<ecs::UINode>(leaf).computed_size_;+ registry.remove<ecs::Canvas>(root);+ auto& parent = registry.get<ecs::UINode>(root);+ parent.display = ecs::UIDisplay::None;+ parent.pointerEvents = ecs::UIPointerEvents::None;+ ui.layoutUpdate(registry, -200, -200, 200, 200, true);+ CHECK(ui.tree.nodes_.empty());+ CHECK(parent.hidden_in_tree_);+ CHECK(registry.get<ecs::UINode>(leaf).hidden_in_tree_);+ CHECK(registry.get<ecs::UINode>(leaf).pointer_blocked_in_tree_);+ CHECK(registry.get<ecs::Transform>(leaf).position == position);+ CHECK(registry.get<ecs::UINode>(leaf).computed_size_ == size);+ parent.display = ecs::UIDisplay::Flex;+ parent.pointerEvents = ecs::UIPointerEvents::Auto;+ parent.opacity = .8f;+ ui.layoutUpdate(registry, -200, -200, 200, 200, false);+ CHECK_FALSE(registry.get<ecs::UINode>(leaf).hidden_in_tree_);+ CHECK_FALSE(registry.get<ecs::UINode>(leaf).pointer_blocked_in_tree_);+ CHECK(registry.get<ecs::UINode>(leaf).alpha_in_tree_ == doctest::Approx(.4f));+ registry.emplace<ecs::Canvas>(root);+ ui.layoutUpdate(registry, -100, -100, 100, 100, false);+ CHECK(ui.tree.nodes_.size() == 2);+ CHECK(registry.get<ecs::UINode>(leaf).alpha_in_tree_ == doctest::Approx(.4f));+}++TEST_CASE("detached UI state walks shared deep ancestry iteratively and recovers from reparenting") {+ ecs::Registry registry;+ ecs::UISystem ui;+ const Entity root = registry.create();+ auto& parent = registry.emplace<ecs::UINode>(root);+ parent.display = ecs::UIDisplay::None;+ parent.opacity = .5f;+ parent.pointerEvents = ecs::UIPointerEvents::None;+ Entity group = root;+ for (u32 i = 0; i < 50000; ++i) {+ const Entity next = registry.create();+ registry.emplace<ecs::Parent>(next).entity = group;+ group = next;+ }+ std::vector<Entity> leaves;+ for (u32 i = 0; i < 100; ++i) {+ const Entity leaf = registry.create();+ registry.emplace<ecs::Parent>(leaf).entity = group;+ registry.emplace<ecs::UINode>(leaf).opacity = .25f;+ leaves.push_back(leaf);+ }+ ui.layoutUpdate(registry, -100, -100, 100, 100, false);+ for (Entity leaf : leaves) {+ const auto& node = registry.get<ecs::UINode>(leaf);+ CHECK(node.hidden_in_tree_);+ CHECK(node.pointer_blocked_in_tree_);+ CHECK(node.alpha_in_tree_ == doctest::Approx(.125f));+ }+ const Entity visible = registry.create();+ registry.emplace<ecs::UINode>(visible).opacity = .8f;+ registry.get<ecs::Parent>(group).entity = visible;+ ui.layoutUpdate(registry, -100, -100, 100, 100, false);+ for (Entity leaf : leaves) {+ const auto& node = registry.get<ecs::UINode>(leaf);+ CHECK_FALSE(node.hidden_in_tree_);+ CHECK_FALSE(node.pointer_blocked_in_tree_);+ CHECK(node.alpha_in_tree_ == doctest::Approx(.2f));+ }+}++TEST_CASE("a Canvas inherits visibility and pointer policy from an ancestor outside its layout tree") {+ ecs::Registry registry;+ ecs::UISystem ui;+ ecs::TransformSystem transforms;+ const Entity parent = registry.create(), canvas = registry.create(), leaf = registry.create();+ registry.emplace<ecs::Transform>(parent).position = {13, 17, 0};+ registry.emplace<ecs::UINode>(parent).opacity = .5f;+ registry.get<ecs::UINode>(parent).pointerEvents = ecs::UIPointerEvents::None;+ registry.emplace<ecs::Children>(parent).entities.push_back(canvas);+ registry.emplace<ecs::Parent>(canvas).entity = parent;+ registry.emplace<ecs::Transform>(canvas);+ registry.emplace<ecs::UINode>(canvas);+ registry.emplace<ecs::Canvas>(canvas);+ registry.emplace<ecs::Children>(canvas).entities.push_back(leaf);+ registry.emplace<ecs::Parent>(leaf).entity = canvas;+ registry.emplace<ecs::Transform>(leaf);+ auto& node = registry.emplace<ecs::UINode>(leaf);+ node.width = {40, 0};+ node.height = {30, 0};+ node.opacity = .5f;+ registry.emplace<ecs::Interactable>(leaf);+ ui.layoutUpdate(registry, -100, -100, 100, 100, true);+ transforms.ensureComposed(registry);+ REQUIRE(ui.tree.nodes_.size() == 2);+ CHECK(node.alpha_in_tree_ == doctest::Approx(.25f));+ auto& transform = registry.get<ecs::Transform>(leaf);+ transform.ensureDecomposed();+ const auto position = transform.worldPosition;+ const ecs::PickRay ray{{position.x, position.y, 100}, {0, 0, -1}};+ ui.hitTestUpdate(registry, ray, nullptr);+ CHECK(ui.hitResult.hit_entity == INVALID_ENTITY);+ registry.get<ecs::UINode>(parent).pointerEvents = ecs::UIPointerEvents::Auto;+ ui.layoutUpdate(registry, -100, -100, 100, 100, false);+ ui.hitTestUpdate(registry, ray, nullptr);+ CHECK(ui.hitResult.hit_entity == leaf);+ registry.get<ecs::UINode>(parent).display = ecs::UIDisplay::None;+ ui.layoutUpdate(registry, -100, -100, 100, 100, false);+ ui.hitTestUpdate(registry, ray, nullptr);+ CHECK(ui.hitResult.hit_entity == INVALID_ENTITY);+}+TEST_CASE("nine-slice: small boxes contain their borders and preserve source UVs") {for (const glm::vec2 size : {glm::vec2{40, 30}, {10, 6}, {10, 30}, {40, 6}, {0, 6}, {10, 0}}) {Harness h;diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex ee9ec63e4..867afeeb8 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -32,6 +32,8 @@ export const NOTED = {{ internal: 'Example validation reports filesystem and process failures instead of dropping all non-TS diagnostics.' },'perf(templates): minify the WeChat publishing profile':{ note: "The WeChat starter's publishing profile minifies its game bundle." },+ 'fix(ui): refresh inherited state outside Canvas layout trees':+ { note: 'UI inheritance stays current after leaving a Canvas.' },'feat(templates): add WeChat gameplay and host-service starter':{ note: 'The WeChat Mini Game starter combines gameplay and host-service examples.' },'feat(templates): ship a complete playable ad starter':