Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
UIHitTestSystem.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
9#pragma once
10
11#include "../ecs/Registry.hpp"
17
18#include <algorithm>
19#include <cmath>
20#include <limits>
21
22namespace esengine::ecs {
23
25 return 2.0f * std::atan2(rz, rw);
26}
27
28inline bool pointInOBB(
29 f32 px, f32 py,
30 f32 worldX, f32 worldY,
31 f32 worldW, f32 worldH,
32 f32 pivotX, f32 pivotY,
33 f32 rotationZ, f32 rotationW
34) {
35 if (worldW == 0.0f || worldH == 0.0f) return false;
36 f32 angle = quaternionToAngle2D(rotationZ, rotationW);
37 f32 sinA = std::sin(-angle);
38 f32 cosA = std::cos(-angle);
39
40 f32 dx = px - worldX;
41 f32 dy = py - worldY;
42 f32 localX = dx * cosA - dy * sinA + worldX;
43 f32 localY = dx * sinA + dy * cosA + worldY;
44
45 f32 left = worldX - worldW * pivotX;
46 f32 right = worldX + worldW * (1.0f - pivotX);
47 f32 bottom = worldY - worldH * pivotY;
48 f32 top = worldY + worldH * (1.0f - pivotY);
49
50 return localX >= std::min(left, right) && localX <= std::max(left, right)
51 && localY >= std::min(bottom, top) && localY <= std::max(bottom, top);
52}
53
59struct PickRay {
60 glm::vec3 origin{0.0f, 0.0f, 0.0f};
61 glm::vec3 dir{0.0f, 0.0f, -1.0f};
62};
63
69inline bool rayHitsPlaneZ(const PickRay& ray, f32 planeZ, f32& outX, f32& outY, f32& outT) {
70 if (std::abs(ray.dir.z) <= 1e-9f) return false;
71 const f32 t = (planeZ - ray.origin.z) / ray.dir.z;
72 if (t < 0.0f) return false; // behind the viewer
73 outX = ray.origin.x + ray.dir.x * t;
74 outY = ray.origin.y + ray.dir.y * t;
75 outT = t;
76 return true;
77}
78
86inline bool rayHitsOBB(const PickRay& ray,
87 const glm::vec3& centre, const glm::quat& rotation,
88 const glm::vec3& scale,
89 const glm::vec3& localMin, const glm::vec3& localMax,
90 f32& outT) {
91 const glm::quat inverse = glm::conjugate(glm::normalize(rotation));
92 const glm::vec3 origin = inverse * (ray.origin - centre);
93 const glm::vec3 dir = inverse * ray.dir;
94
95 const glm::vec3 a = localMin * scale;
96 const glm::vec3 b = localMax * scale;
97 const glm::vec3 lo = glm::min(a, b);
98 const glm::vec3 hi = glm::max(a, b);
99
100 // Not `near`/`far`: both are macros in the Windows headers the native host
101 // pulls in, and a slab test that stops compiling on one platform is worse
102 // than two duller names.
103 f32 enter = 0.0f;
104 f32 exit = std::numeric_limits<f32>::max();
105 for (int axis = 0; axis < 3; ++axis) {
106 if (std::abs(dir[axis]) <= 1e-9f) {
107 // Parallel to this pair of slabs: it can only ever be inside them.
108 if (origin[axis] < lo[axis] || origin[axis] > hi[axis]) return false;
109 continue;
110 }
111 f32 t0 = (lo[axis] - origin[axis]) / dir[axis];
112 f32 t1 = (hi[axis] - origin[axis]) / dir[axis];
113 if (t0 > t1) std::swap(t0, t1);
114 enter = std::max(enter, t0);
115 exit = std::min(exit, t1);
116 if (enter > exit) return false;
117 }
118 outT = enter;
119 return true;
120}
121
122inline bool pointInTransformedBox(Registry& registry, Entity entity, Transform& transform,
123 f32 px, f32 py, f32 width, f32 height,
124 f32 pivotX, f32 pivotY) {
125 if (!hasFlatGeometryArea(registry, entity, transform)) return false;
126 if (!registry.has<Parent>(entity)) {
127 return pointInOBB(px, py, transform.worldPosition.x, transform.worldPosition.y,
128 width * transform.worldScale.x, height * transform.worldScale.y,
129 pivotX, pivotY, transform.worldRotation.z, transform.worldRotation.w);
130 }
131 if (width == 0.0f || height == 0.0f) return false;
132 const auto& matrix = transform.cachedMatrix_;
133 const f64 determinant = static_cast<f64>(matrix[0].x) * matrix[1].y
134 - static_cast<f64>(matrix[1].x) * matrix[0].y;
135 if (determinant == 0.0 || !std::isfinite(determinant)) return false;
136 const f64 dx = static_cast<f64>(px) - transform.worldPosition.x;
137 const f64 dy = static_cast<f64>(py) - transform.worldPosition.y;
138 const f64 localX = (dx * matrix[1].y - dy * matrix[1].x) / determinant;
139 const f64 localY = (dy * matrix[0].x - dx * matrix[0].y) / determinant;
140 const f32 x0 = -width * pivotX, x1 = width * (1.0f - pivotX);
141 const f32 y0 = -height * pivotY, y1 = height * (1.0f - pivotY);
142 return localX >= std::min(x0, x1) && localX <= std::max(x0, x1)
143 && localY >= std::min(y0, y1) && localY <= std::max(y0, y1);
144}
145
146inline bool isClippedByMask(
147 Registry& registry,
148 Entity entity,
149 f32 worldMouseX,
150 f32 worldMouseY
151) {
152 Entity current = entity;
153 while (registry.has<Parent>(current)) {
154 Entity ancestor = registry.get<Parent>(current).entity;
155 if (!registry.valid(ancestor)) break;
156
157 auto* mask = registry.tryGet<UIMask>(ancestor);
158 if (mask && mask->enabled) {
159 auto* t = registry.tryGet<Transform>(ancestor);
160 auto* node = registry.tryGet<UINode>(ancestor);
161 if (t && node) {
162 t->ensureDecomposed();
163 // UINode mask box is pivot-centered.
165 registry, ancestor, *t, worldMouseX, worldMouseY,
166 node->computed_size_.x, node->computed_size_.y, 0.5f, 0.5f
167 )) {
168 return true;
169 }
170 }
171 }
172 current = ancestor;
173 }
174 return false;
175}
176
177} // namespace esengine::ecs
Hierarchy components for parent-child relationships.
Central ECS registry for entity and component management.
Unified Transform component with local and world-space data.
Central container for all entities and components.
Definition Registry.hpp:73
T & get(Entity entity)
Gets a component from an entity (must exist).
Definition Registry.hpp:649
bool has(Entity entity) const
Checks if an entity has a specific component.
Definition Registry.hpp:711
bool valid(Entity entity) const
Checks if an entity is currently valid.
Definition Registry.hpp:518
T * tryGet(Entity entity)
Tries to get a component from an entity.
Definition Registry.hpp:670
bool rayHitsOBB(const PickRay &ray, const glm::vec3 &centre, const glm::quat &rotation, const glm::vec3 &scale, const glm::vec3 &localMin, const glm::vec3 &localMax, f32 &outT)
bool hasFlatGeometryArea(Transform &transform, bool parented)
bool pointInOBB(f32 px, f32 py, f32 worldX, f32 worldY, f32 worldW, f32 worldH, f32 pivotX, f32 pivotY, f32 rotationZ, f32 rotationW)
bool pointInTransformedBox(Registry &registry, Entity entity, Transform &transform, f32 px, f32 py, f32 width, f32 height, f32 pivotX, f32 pivotY)
f32 quaternionToAngle2D(f32 rz, f32 rw)
bool isClippedByMask(Registry &registry, Entity entity, f32 worldMouseX, f32 worldMouseY)
bool rayHitsPlaneZ(const PickRay &ray, f32 planeZ, f32 &outX, f32 &outY, f32 &outT)
double f64
64-bit floating point
Definition Types.hpp:61
float f32
32-bit floating point
Definition Types.hpp:59
Entity identifier with packed index + generation.
Definition Types.hpp:135
Parent reference component.
Definition Hierarchy.hpp:46
Unified transform component.
Definition Transform.hpp:45
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).
Definition UINode.hpp:79