41 const auto& nodes =
tree.nodes_;
43 for (
i32 i =
static_cast<i32>(nodes.size()) - 1; i >= 0; i--) {
44 Entity entity = nodes[i].entity;
48 if (!interactable || !interactable->enabled || !interactable->raycastTarget)
continue;
54 const PickRay& ray = screen_domain_.count(entity.
id()) != 0 ? screenRay : worldRay;
61 f32 mouseWorldX = 0.0f, mouseWorldY = 0.0f, hitT = 0.0f;
62 if (!
rayHitsPlaneZ(ray, t.worldPosition.z, mouseWorldX, mouseWorldY, hitT))
continue;
66 if (!node || node->hidden_in_tree_)
continue;
69 if (node->pointer_blocked_in_tree_)
continue;
70 f32 baseW = node->computed_size_.x;
71 f32 baseH = node->computed_size_.y;
72 f32 pivotX = 0.5f, pivotY = 0.5f;
75 registry, entity, t, mouseWorldX, mouseWorldY,
76 baseW, baseH, pivotX, pivotY
87 hitWorldContent(registry, worldRay, resources);
93 const u32 since = hit_tick_;
95 || meshes != hit_meshes_
106 if (!changed && moved) {
112 hit_registry_ = registry.
identity();
114 hit_meshes_ = meshes;
123std::vector<u32> hierarchyPath(
Registry& registry,
Entity entity) {
124 std::vector<u32> reversed;
125 for (
Entity current = entity; ; ) {
128 reversed.push_back(current.id());
132 if (
const auto* children = registry.
tryGet<Children>(parent->entity)) {
133 const auto& list = children->entities;
134 index =
static_cast<u32>(std::find(list.begin(), list.end(), current) - list.begin());
136 reversed.push_back(index);
137 current = parent->entity;
139 return std::vector<u32>(reversed.rbegin(), reversed.rend());
143bool drawsBefore(
Registry& registry, Entity a, Entity b) {
144 return hierarchyPath(registry, a) < hierarchyPath(registry, b);
159 const resource::ResourceManager* resources) {
163 bool haveBest =
false;
167 const auto consider = [&](Entity entity,
f32 t,
i32 layer) {
169 const bool sameDepth = std::abs(t - bestT) <= 1e-4f;
170 if (!sameDepth && t > bestT)
return;
171 if (sameDepth && layer < bestLayer)
return;
174 if (sameDepth && layer == bestLayer && drawsBefore(registry, entity, best))
return;
182 registry.eachLive<
Sprite, Interactable>(
183 [&](Entity entity,
Sprite& sprite, Interactable& interactable) {
184 if (!registry.isEntityActive(entity))
return;
185 if (!interactable.enabled || !interactable.raycastTarget)
return;
187 if (registry.has<UINode>(entity))
return;
188 if (!registry.has<Transform>(entity))
return;
190 auto& t = registry.get<
Transform>(entity);
191 t.ensureDecomposed();
193 f32 px = 0.0f, py = 0.0f, hitT = 0.0f;
194 if (!
rayHitsPlaneZ(ray, t.worldPosition.z, px, py, hitT))
return;
197 registry, entity, t, px, py, sprite.size.x, sprite.size.y,
198 sprite.pivot.x, sprite.pivot.y
201 consider(entity, hitT,
static_cast<i32>(sprite.layer));
206 registry.eachLive<ContentRect, Interactable>(
207 [&](Entity entity, ContentRect& rect, Interactable& interactable) {
208 if (!registry.isEntityActive(entity))
return;
209 if (!interactable.enabled || !interactable.raycastTarget)
return;
210 if (registry.has<UINode>(entity) || registry.has<
Sprite>(entity))
return;
211 if (!registry.has<Transform>(entity))
return;
213 auto& t = registry.get<
Transform>(entity);
214 t.ensureDecomposed();
216 f32 px = 0.0f, py = 0.0f, hitT = 0.0f;
217 if (!
rayHitsPlaneZ(ray, t.worldPosition.z, px, py, hitT))
return;
220 registry, entity, t, px, py, rect.size.x, rect.size.y,
221 rect.pivot.x, rect.pivot.y
225 consider(entity, hitT, 0);
228 registry.eachLive<MeshRenderer, Interactable>(
229 [&](Entity entity, MeshRenderer& mesh, Interactable& interactable) {
230 if (!registry.isEntityActive(entity))
return;
231 if (!interactable.enabled || !interactable.raycastTarget)
return;
232 if (!mesh.enabled)
return;
233 if (registry.has<UINode>(entity))
return;
234 if (!registry.has<Transform>(entity))
return;
239 const Mesh* resident =
240 (resources && mesh.mesh.isValid()) ? resources->getMesh(mesh.mesh) :
nullptr;
241 if (!resident && mesh.indices.empty())
return;
242 const glm::vec3 localMin = resident ? resident->localMin
243 : glm::vec3(mesh.localMin, 0.0f);
244 const glm::vec3 localMax = resident ? resident->localMax
245 : glm::vec3(mesh.localMax, 0.0f);
247 auto& t = registry.get<
Transform>(entity);
248 t.ensureDecomposed();
251 if (!
rayHitsOBB(ray, t.worldPosition, t.worldRotation, t.worldScale,
252 localMin, localMax, hitT))
return;
254 consider(entity, hitT,
static_cast<i32>(mesh.layer));
257 if (!haveBest)
return;
262 pickResults_.clear();
263 const auto& nodes =
tree.nodes_;
271 std::vector<Hit> hits;
273 for (
i32 i =
static_cast<i32>(nodes.size()) - 1; i >= 0; i--) {
274 Entity entity = nodes[i].entity;
279 if (!node || node->computed_size_.x <= 0.0f || node->computed_size_.y <= 0.0f)
continue;
285 registry, entity, t, worldX, worldY,
286 node->computed_size_.x, node->computed_size_.y, 0.5f, 0.5f
294 if (!registry.
valid(parent))
break;
299 hits.push_back({entity, depth, vis ? vis->uiOrder : INT32_MIN});
302 std::sort(hits.begin(), hits.end(), [](
const Hit& a,
const Hit& b) {
303 if (a.depth != b.depth) return a.depth > b.depth;
304 return a.order > b.order;
306 pickResults_.reserve(hits.size());
307 for (
const Hit& h : hits) pickResults_.push_back(h.entity);
308 return static_cast<u32>(pickResults_.size());
Canvas component for 2D game resolution management.
The rectangle a piece of world content occupies: a size, and the point of it the entity's transform s...
Hierarchy components for parent-child relationships.
Arbitrary-geometry 2D mesh renderable (deformed sprites, polygons, water surfaces,...
Central resource management system.
2D sprite rendering component
Pure geometric helpers used by UISystem hit-test.
UI subsystem owning layout tree and hit-test state.
Central container for all entities and components.
bool isEntityActive(Entity e) const
bool anyChangedSince(u32 tick) const
Whether any T was written, added or removed after tick: the question a frame that may skip its walk a...
void eachLive(Func &&func)
each() without the per-call snapshot copy, for read-only passes.
u32 activityEpoch() const
T & get(Entity entity)
Gets a component from an entity (must exist).
bool has(Entity entity) const
Checks if an entity has a specific component.
u32 beginChangeRead()
For a reader about to look: the tick it is looking at, after which the clock moves on.
bool changedSince(Entity entity, u32 tick) const
Whether entity's T was written, added or replaced after tick (an answer of beginChangeRead).
bool valid(Entity entity) const
Checks if an entity is currently valid.
u64 identity() const
Which registry this is, never shared with another made in this process (a registry assigned over this...
T * tryGet(Entity entity)
Tries to get a component from an entity.
bool hitInputsChanged(Registry ®istry, const resource::ResourceManager *resources)
u32 pick(Registry ®istry, f32 worldX, f32 worldY)
Editor pick: the most specific UI entity under the point. Unlike hitTestUpdate it ignores Interactabl...
UIHitTestResult hitResult
u32 pickAll(Registry ®istry, f32 worldX, f32 worldY)
All UI entities under the point, most specific first. Returns the count; read entries via pickResult.
void hitTestUpdate(Registry ®istry, 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...
Central manager for GPU resources.
u64 meshesRevision() const
Changes whenever any mesh is realized (each takes a new Mesh::revision) or released: while it holds,...
bool rayHitsOBB(const PickRay &ray, const glm::vec3 ¢re, const glm::quat &rotation, const glm::vec3 &scale, const glm::vec3 &localMin, const glm::vec3 &localMax, f32 &outT)
bool pointInTransformedBox(Registry ®istry, Entity entity, Transform &transform, f32 px, f32 py, f32 width, f32 height, f32 pivotX, f32 pivotY)
bool isClippedByMask(Registry ®istry, Entity entity, f32 worldMouseX, f32 worldMouseY)
bool rayHitsPlaneZ(const PickRay &ray, f32 planeZ, f32 &outX, f32 &outY, f32 &outT)
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
std::uint32_t u32
Unsigned 32-bit integer.
float f32
32-bit floating point
Entity identifier with packed index + generation.
constexpr u32 id() const
Returns the raw packed value for FFI serialization.
Canvas component for 2D game resolution management.
The rectangle of this entity's content, in its own units.
Parent reference component.
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).
UIVisual — the single UI visual component, merging the former low-level UIRenderer (what the renderer...