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
refactor(ui): eliminate UI subsystem file-level singletons
esengine5157af6338b7
diff --git a/CMakeLists.txt b/CMakeLists.txtindex a1197e009..ac0a036cf 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -64,6 +64,7 @@ set(ESENGINE_SOURCESsrc/esengine/platform/PathResolver.cppsrc/esengine/animation/TweenSystem.cppsrc/esengine/ecs/UILayoutSystem.cpp+ src/esengine/ecs/UISystem.cpp)if(ES_ENABLE_POSTPROCESS)diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex c393a5c2b..55902edcb 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -28,6 +28,7 @@#include "../ecs/UILayoutSystem.hpp"#include "../ecs/UIHitTestSystem.hpp"#include "../ecs/UIRenderOrderSystem.hpp"+#include "../ecs/UISystem.hpp"#include "../renderer/OpenGLHeaders.hpp"#include "../renderer/GfxDevice.hpp"@@ -593,20 +594,20 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {namespace esengine {void uiLayout_update(ecs::Registry& registry, f32 camLeft, f32 camBottom, f32 camRight, f32 camTop) {- ecs::uiLayoutUpdate(registry, camLeft, camBottom, camRight, camTop);+ ctx().require<ecs::UISystem>().layoutUpdate(registry, camLeft, camBottom, camRight, camTop);}void uiHitTest_update(ecs::Registry& registry, f32 mouseWorldX, f32 mouseWorldY,bool mouseDown, bool mousePressed, bool mouseReleased) {- ecs::uiHitTestUpdate(registry, mouseWorldX, mouseWorldY, mouseDown, mousePressed, mouseReleased);+ ctx().require<ecs::UISystem>().hitTestUpdate(registry, mouseWorldX, mouseWorldY, mouseDown, mousePressed, mouseReleased);}u32 uiHitTest_getHitEntity() {- return ecs::uiHitTestGetHitEntity();+ return ctx().require<ecs::UISystem>().getHitEntity();}u32 uiHitTest_getHitEntityPrev() {- return ecs::uiHitTestGetHitEntityPrev();+ return ctx().require<ecs::UISystem>().getPrevHitEntity();}void uiRenderOrder_update(ecs::Registry& registry) {@@ -620,17 +621,17 @@ void uiFlexLayout_update(ecs::Registry& registry) {}void uiTree_markStructureDirty() {- ecs::uiTreeMarkStructureDirty();+ ctx().require<ecs::UISystem>().treeMarkStructureDirty();}void uiTree_markDirty(u32 entity) {auto e = Entity::fromRaw(entity);if (e == INVALID_ENTITY) return;- ecs::uiTreeMarkDirty(e);+ ctx().require<ecs::UISystem>().treeMarkDirty(e);}void uiTree_markAllDirty() {- ecs::getUITree().markAllDirty();+ ctx().require<ecs::UISystem>().tree.markAllDirty();}f32 getUIRectComputedWidth(ecs::Registry& registry, u32 entity) {@@ -650,7 +651,7 @@ void setUIRectSize(ecs::Registry& registry, u32 entity, f32 w, f32 h) {if (!rect) return;rect->size.x = w;rect->size.y = h;- ecs::uiTreeMarkDirty(Entity::fromRaw(entity));+ ctx().require<ecs::UISystem>().treeMarkDirty(Entity::fromRaw(entity));}void transform_update(ecs::Registry& registry) {diff --git a/src/esengine/core/EstellaContext.cpp b/src/esengine/core/EstellaContext.cppindex bd1c59f81..dda69dc62 100644--- a/src/esengine/core/EstellaContext.cpp+++ b/src/esengine/core/EstellaContext.cpp@@ -34,6 +34,7 @@#endif#include "../resource/ResourceManager.hpp"#include "../ecs/TransformSystem.hpp"+#include "../ecs/UISystem.hpp"#include "../animation/TweenSystem.hpp"#ifdef ES_ENABLE_TIMELINE#include "../animation/TimelineSystem.hpp"@@ -93,6 +94,7 @@ void EstellaContext::initSubsystems() {services_.registerOwned<RenderContext>(std::move(renderContext));services_.registerOwned<ecs::TransformSystem>(makeUnique<ecs::TransformSystem>());+ services_.registerOwned<ecs::UISystem>(makeUnique<ecs::UISystem>());services_.registerOwned<animation::TweenSystem>(makeUnique<animation::TweenSystem>());#ifdef ES_ENABLE_TIMELINEdiff --git a/src/esengine/ecs/UIHitTestSystem.hpp b/src/esengine/ecs/UIHitTestSystem.hppindex b49ee6201..2c2ccede5 100644--- a/src/esengine/ecs/UIHitTestSystem.hpp+++ b/src/esengine/ecs/UIHitTestSystem.hpp@@ -1,13 +1,16 @@+/**+ * @file UIHitTestSystem.hpp+ * @brief Pure geometric helpers used by UISystem hit-test+ * @details State and update entry point moved to UISystem (UISystem.hpp).+ * This header only exposes the reusable inline math helpers.+ */#pragma once#include "Registry.hpp"#include "components/Transform.hpp"#include "components/Hierarchy.hpp"#include "components/UIRect.hpp"-#include "components/Interactable.hpp"-#include "components/UIInteraction.hpp"#include "components/UIMask.hpp"-#include "UILayoutSystem.hpp"#include <cmath>@@ -76,66 +79,4 @@ inline bool isClippedByMask(return false;}-struct UIHitTestResult {- Entity hit_entity{INVALID_ENTITY};- Entity prev_hit_entity{INVALID_ENTITY};-};--static UIHitTestResult s_hit_test_result;--inline void uiHitTestUpdate(- Registry& registry,- f32 mouseWorldX, f32 mouseWorldY,- bool mouseDown, bool mousePressed, bool mouseReleased-) {- (void)mouseDown;- (void)mousePressed;- (void)mouseReleased;-- s_hit_test_result.prev_hit_entity = s_hit_test_result.hit_entity;- s_hit_test_result.hit_entity = INVALID_ENTITY;-- const auto& tree = getUITree();- const auto& nodes = tree.nodes_;-- for (i32 i = static_cast<i32>(nodes.size()) - 1; i >= 0; i--) {- Entity entity = nodes[i].entity;-- auto* interactable = registry.tryGet<Interactable>(entity);- if (!interactable || !interactable->enabled || !interactable->raycastTarget) continue;- if (!registry.has<Transform>(entity)) continue;-- registry.getOrEmplace<UIInteraction>(entity);-- auto& t = registry.get<Transform>(entity);- t.ensureDecomposed();- auto& rect = registry.get<UIRect>(entity);-- f32 worldW = (rect.computed_size_.x > 0.0f ? rect.computed_size_.x : rect.size.x) * t.worldScale.x;- f32 worldH = (rect.computed_size_.y > 0.0f ? rect.computed_size_.y : rect.size.y) * t.worldScale.y;-- if (pointInOBB(- mouseWorldX, mouseWorldY,- t.worldPosition.x, t.worldPosition.y,- worldW, worldH,- rect.pivot.x, rect.pivot.y,- t.worldRotation.z, t.worldRotation.w- )) {- if (isClippedByMask(registry, entity, mouseWorldX, mouseWorldY)) {- continue;- }- s_hit_test_result.hit_entity = entity;- return;- }- }-}--inline u32 uiHitTestGetHitEntity() {- return s_hit_test_result.hit_entity.id();-}--inline u32 uiHitTestGetHitEntityPrev() {- return s_hit_test_result.prev_hit_entity.id();-}-} // namespace esengine::ecsdiff --git a/src/esengine/ecs/UILayoutSystem.cpp b/src/esengine/ecs/UILayoutSystem.cppindex 0b1959da3..9cd82b662 100644--- a/src/esengine/ecs/UILayoutSystem.cpp+++ b/src/esengine/ecs/UILayoutSystem.cpp@@ -1,4 +1,5 @@#include "UILayoutSystem.hpp"+#include "UISystem.hpp"#include "components/Transform.hpp"#include "components/Sprite.hpp"@@ -576,27 +577,26 @@ void unifiedLayoutPass(Registry& registry, UITree& tree, const LayoutRect& camer} // anonymous namespace-static UITree s_ui_tree;+// =============================================================================+// UISystem method impls that need the anonymous-namespace helpers above+// (hitTestUpdate lives in UISystem.cpp since it only needs UIHitTestSystem.hpp)+// =============================================================================-void uiLayoutUpdate(+void UISystem::layoutUpdate(Registry& registry,f32 camLeft, f32 camBottom, f32 camRight, f32 camTop) {- s_ui_tree.rebuild(registry);+ tree.rebuild(registry);LayoutRect cameraRect{ camLeft, camBottom, camRight, camTop };- unifiedLayoutPass(registry, s_ui_tree, cameraRect);+ unifiedLayoutPass(registry, tree, cameraRect);}-UITree& getUITree() {- return s_ui_tree;+void UISystem::treeMarkStructureDirty() {+ tree.structure_dirty_ = true;}-void uiTreeMarkStructureDirty() {- s_ui_tree.structure_dirty_ = true;-}--void uiTreeMarkDirty(Entity entity) {- s_ui_tree.markDirty(entity);+void UISystem::treeMarkDirty(Entity entity) {+ tree.markDirty(entity);}} // namespace esengine::ecsdiff --git a/src/esengine/ecs/UILayoutSystem.hpp b/src/esengine/ecs/UILayoutSystem.hppindex d2d338ff6..8bbbadc7a 100644--- a/src/esengine/ecs/UILayoutSystem.hpp+++ b/src/esengine/ecs/UILayoutSystem.hpp@@ -28,13 +28,9 @@ LayoutResult computeAnchorLayout(const glm::vec2& pivot);-void uiLayoutUpdate(- Registry& registry,- f32 camLeft, f32 camBottom, f32 camRight, f32 camTop-);--UITree& getUITree();-void uiTreeMarkStructureDirty();-void uiTreeMarkDirty(Entity entity);+// Layout and tree operations are methods on UISystem (see UISystem.hpp).+// Previously free functions (uiLayoutUpdate, getUITree, uiTreeMarkStructureDirty,+// uiTreeMarkDirty) lived here but held state in a file-level static; that+// state now lives on UISystem registered via EstellaContext.} // namespace esengine::ecsdiff --git a/src/esengine/ecs/UISystem.cpp b/src/esengine/ecs/UISystem.cppnew file mode 100644index 000000000..52e6d3fe2--- /dev/null+++ b/src/esengine/ecs/UISystem.cpp@@ -0,0 +1,64 @@+/**+ * @file UISystem.cpp+ * @brief UISystem hit-test implementation+ * @details Layout methods are defined in UILayoutSystem.cpp to share the+ * anonymous-namespace helpers (resolveFlexChildren, etc.).+ */++#include "UISystem.hpp"+#include "UIHitTestSystem.hpp"++#include "components/Interactable.hpp"+#include "components/Transform.hpp"+#include "components/UIInteraction.hpp"+#include "components/UIRect.hpp"++namespace esengine::ecs {++void UISystem::hitTestUpdate(+ Registry& registry,+ f32 mouseWorldX, f32 mouseWorldY,+ bool mouseDown, bool mousePressed, bool mouseReleased+) {+ (void)mouseDown;+ (void)mousePressed;+ (void)mouseReleased;++ hitResult.prev_hit_entity = hitResult.hit_entity;+ hitResult.hit_entity = INVALID_ENTITY;++ const auto& nodes = tree.nodes_;++ for (i32 i = static_cast<i32>(nodes.size()) - 1; i >= 0; i--) {+ Entity entity = nodes[i].entity;++ auto* interactable = registry.tryGet<Interactable>(entity);+ if (!interactable || !interactable->enabled || !interactable->raycastTarget) continue;+ if (!registry.has<Transform>(entity)) continue;++ registry.getOrEmplace<UIInteraction>(entity);++ auto& t = registry.get<Transform>(entity);+ t.ensureDecomposed();+ auto& rect = registry.get<UIRect>(entity);++ f32 worldW = (rect.computed_size_.x > 0.0f ? rect.computed_size_.x : rect.size.x) * t.worldScale.x;+ f32 worldH = (rect.computed_size_.y > 0.0f ? rect.computed_size_.y : rect.size.y) * t.worldScale.y;++ if (pointInOBB(+ mouseWorldX, mouseWorldY,+ t.worldPosition.x, t.worldPosition.y,+ worldW, worldH,+ rect.pivot.x, rect.pivot.y,+ t.worldRotation.z, t.worldRotation.w+ )) {+ if (isClippedByMask(registry, entity, mouseWorldX, mouseWorldY)) {+ continue;+ }+ hitResult.hit_entity = entity;+ return;+ }+ }+}++} // namespace esengine::ecsdiff --git a/src/esengine/ecs/UISystem.hpp b/src/esengine/ecs/UISystem.hppnew file mode 100644index 000000000..4f156ae20--- /dev/null+++ b/src/esengine/ecs/UISystem.hpp@@ -0,0 +1,80 @@+/**+ * @file UISystem.hpp+ * @brief UI subsystem owning layout tree and hit-test state+ * @details Consolidates UITree and UIHitTestResult into a single service+ * registered on EstellaContext. Replaces the previous file-level+ * static globals (s_ui_tree, s_hit_test_result).+ *+ * @author ESEngine Team+ * @date 2026+ */+#pragma once++#include "../core/Types.hpp"+#include "Entity.hpp"+#include "Registry.hpp"+#include "UITree.hpp"++namespace esengine::ecs {++/**+ * @brief Result of the most recent hit-test pass+ */+struct UIHitTestResult {+ Entity hit_entity{INVALID_ENTITY};+ Entity prev_hit_entity{INVALID_ENTITY};+};++/**+ * @brief UI subsystem: owns layout tree + hit-test state+ *+ * @details Registered as a service on EstellaContext. All UI layout and+ * hit-test state lives on this instance; no file-level globals.+ *+ * @code+ * auto& ui = ctx.require<ecs::UISystem>();+ * ui.layoutUpdate(registry, -960, -540, 960, 540);+ * ui.hitTestUpdate(registry, mouseX, mouseY, false, true, false);+ * auto hitEntity = ui.getHitEntity();+ * @endcode+ */+class UISystem {+public:+ UISystem() = default;+ ~UISystem() = default;++ UISystem(const UISystem&) = delete;+ UISystem& operator=(const UISystem&) = delete;++ // ---- State (public by design: thin wrapper over data) ----++ UITree tree;+ UIHitTestResult hitResult;++ // ---- Layout pass (defined in UILayoutSystem.cpp) ----++ /** @brief Rebuild layout tree and apply layout to all dirty nodes */+ void layoutUpdate(Registry& registry,+ f32 camLeft, f32 camBottom, f32 camRight, f32 camTop);++ /** @brief Mark the tree structure as dirty (forces full rebuild next update) */+ void treeMarkStructureDirty();++ /** @brief Mark a single entity's layout as dirty */+ void treeMarkDirty(Entity entity);++ // ---- Hit test pass (defined in UISystem.cpp) ----++ /** @brief Run point-vs-UI hit-test, updating hitResult */+ void hitTestUpdate(Registry& registry,+ f32 mouseWorldX, f32 mouseWorldY,+ bool mouseDown, bool mousePressed, bool mouseReleased);++ /** @brief Entity hit by the most recent hitTestUpdate (or INVALID_ENTITY) */+ u32 getHitEntity() const { return hitResult.hit_entity.id(); }++ /** @brief Entity hit by the previous frame's hitTestUpdate */+ u32 getPrevHitEntity() const { return hitResult.prev_hit_entity.id(); }+};++} // namespace esengine::ecs