Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
UISystem.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
13#pragma once
14
15#include "../core/Types.hpp"
16#include "../ecs/Entity.hpp"
17#include "../ecs/Registry.hpp"
18#include "./UITree.hpp"
19#include "./UIHitTestSystem.hpp"
20
21#include <memory>
22#include <unordered_set>
23#include <unordered_map>
24#include <vector>
25
26namespace esengine::resource { class ResourceManager; }
27
28namespace esengine::ecs {
29
30// Retained Yoga node cache — persists YGNodes across frames so a stable UI tree
31// is not reallocated every solve. Defined in UILayoutSystem.cpp (holds Yoga
32// types, kept out of this header).
33struct LayoutCache;
34
42
56class UISystem {
57public:
59 ~UISystem(); // out-of-line: layoutCache_ holds an incomplete LayoutCache here.
60
61 UISystem(const UISystem&) = delete;
62 UISystem& operator=(const UISystem&) = delete;
63
64 // ---- State (public by design: thin wrapper over data) ----
65
68 std::unique_ptr<LayoutCache> layoutCache_;
69
70 // ---- Layout pass (defined in UILayoutSystem.cpp) ----
71
79 bool layoutUpdate(Registry& registry,
80 f32 boxLeft, f32 boxBottom, f32 boxRight, f32 boxTop,
81 bool tsPropertyDirty);
82
83 // ---- Hit test pass (defined in UISystem.cpp) ----
84
92 void hitTestUpdate(Registry& registry, const PickRay& ray,
93 const resource::ResourceManager* resources = nullptr);
94
103 void hitTestUpdate(Registry& registry, const PickRay& worldRay, const PickRay& screenRay,
104 const resource::ResourceManager* resources);
105
106 bool hitInputsChanged(Registry& registry, const resource::ResourceManager* resources);
107
110 u32 pick(Registry& registry, f32 worldX, f32 worldY);
111
114 u32 pickAll(Registry& registry, f32 worldX, f32 worldY);
115
117 u32 pickResult(u32 index) const {
118 return index < pickResults_.size() ? pickResults_[index].id() : INVALID_ENTITY.id();
119 }
120
122 u32 getHitEntity() const { return hitResult.hit_entity.id(); }
123
125 u32 getPrevHitEntity() const { return hitResult.prev_hit_entity.id(); }
126
135 const std::unordered_set<u32>& screenDomain() const { return screen_domain_; }
136
139 const std::vector<u32>& screenRoots() const { return screen_roots_; }
141 const std::vector<u32>& screenRootByIndex() const { return screen_root_by_index_; }
142
144 u32 screenRootOf(u32 entity) const {
145 const u32 index = Entity::fromRaw(entity).index();
146 return index < screen_root_by_index_.size() ? screen_root_by_index_[index] : 0;
147 }
148
149 bool isScreenDomain(const Registry& registry, u32 entity) const {
150 // Resolved entries belong to the registry that last completed layout.
151 return lastRegistryId_ == registry.instanceId()
152 && registry.valid(Entity::fromRaw(entity))
153 && screen_domain_.count(entity) != 0;
154 }
155
156private:
157 struct InheritedState {
158 bool hidden = false;
159 f32 alpha = 1.0f;
160 bool blocked = false;
161 };
162 struct InheritedMemo {
163 u64 stamp = 0;
164 bool resolving = false;
165 InheritedState state;
166 };
167 std::unordered_map<Entity, InheritedMemo> inherited_cache_;
168 std::vector<Entity> inherited_path_;
169 u64 inherited_stamp_ = 0;
170 void propagateDetachedUIState(Registry& registry);
172 void hitWorldContent(Registry& registry, const PickRay& ray,
173 const resource::ResourceManager* resources);
174
175 std::vector<Entity> pickResults_;
176
177 u64 hit_registry_{0};
178 u32 hit_activity_{0};
179 u32 hit_tick_{0};
180 u64 hit_meshes_{0};
181
182 // ---- Layout skip-when-clean gate (see layoutUpdate) ----
183 // Snapshot of the last solved frame; a pass whose inputs match all of these
184 // (and no tween activity) reuses the retained YGNodes and computed output.
185 u64 lastSig_{0};
186 // Size and center are tracked apart: a resized box needs a new solve, a moved
187 // box needs only the screen roots replaced (see layoutUpdate).
188 f32 lastBoxW_{0}, lastBoxH_{0}, lastBoxCX_{0}, lastBoxCY_{0};
189 bool lastAnimActive_{false};
190 bool layoutPrimed_{false}; // force a solve on the very first pass
191 // Which registry the retained Yoga nodes belong to — entity ids restart with
192 // each one, so they mean nothing across registries (Registry::instanceId).
193 u64 lastRegistryId_{0};
194
195 std::unordered_set<u32> screen_domain_;
196 std::vector<u32> screen_roots_;
198 std::vector<u32> screen_root_by_index_;
199};
200
201} // namespace esengine::ecs
Entity utilities for the ECS system.
Central ECS registry for entity and component management.
Core type definitions and utilities for ESEngine.
Pure geometric helpers used by UISystem hit-test.
Central container for all entities and components.
Definition Registry.hpp:73
bool valid(Entity entity) const
Checks if an entity is currently valid.
Definition Registry.hpp:518
u64 instanceId() const
Which registry this is — unique for the process, never reused.
Definition Registry.hpp:96
bool hitInputsChanged(Registry &registry, const resource::ResourceManager *resources)
Definition UISystem.cpp:90
u32 pick(Registry &registry, f32 worldX, f32 worldY)
Editor pick: the most specific UI entity under the point. Unlike hitTestUpdate it ignores Interactabl...
Definition UISystem.cpp:311
UISystem & operator=(const UISystem &)=delete
const std::vector< u32 > & screenRoots() const
The screen's root Canvases, in the order the UI tree draws them (each Canvas in its pool's order,...
Definition UISystem.hpp:139
const std::vector< u32 > & screenRootByIndex() const
screenRootOf for every entity at once, by entity index, for a reader that asks of many.
Definition UISystem.hpp:141
bool layoutUpdate(Registry &registry, f32 boxLeft, f32 boxBottom, f32 boxRight, f32 boxTop, bool tsPropertyDirty)
Rebuild layout tree and apply layout to all dirty nodes. The four bounds are the world-space box UI i...
u32 screenRootOf(u32 entity) const
Which screen root entity (an id in screenDomain()) draws under; for one outside the domain,...
Definition UISystem.hpp:144
bool isScreenDomain(const Registry &registry, u32 entity) const
Definition UISystem.hpp:149
UIHitTestResult hitResult
Definition UISystem.hpp:67
u32 getHitEntity() const
Entity hit by the most recent hitTestUpdate (or INVALID_ENTITY).
Definition UISystem.hpp:122
UISystem(const UISystem &)=delete
std::unique_ptr< LayoutCache > layoutCache_
Definition UISystem.hpp:68
u32 pickAll(Registry &registry, f32 worldX, f32 worldY)
All UI entities under the point, most specific first. Returns the count; read entries via pickResult.
Definition UISystem.cpp:261
void hitTestUpdate(Registry &registry, const PickRay &ray, const resource::ResourceManager *resources=nullptr)
Run ray-vs-content hit-test, updating hitResult. A query: what a press or a hover MEANS is the intera...
Definition UISystem.cpp:31
const std::unordered_set< u32 > & screenDomain() const
Every entity that belongs to the SCREEN, as opposed to the world.
Definition UISystem.hpp:135
u32 getPrevHitEntity() const
Entity hit by the previous frame's hitTestUpdate.
Definition UISystem.hpp:125
u32 pickResult(u32 index) const
Entry index of the last pickAll (or INVALID_ENTITY).
Definition UISystem.hpp:117
Central manager for GPU resources.
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59
Entity identifier with packed index + generation.
Definition Types.hpp:135
static constexpr Entity fromRaw(u32 v)
Reconstructs an Entity from a raw packed value.
Definition Types.hpp:162
constexpr u32 index() const
Gets the entity index.
Definition Types.hpp:150
Result of the most recent hit-test pass.
Definition UISystem.hpp:38