Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
UIBindings.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
12#include "UIBindings.hpp"
14
15#include "ActiveContext.hpp"
17#include "../core/World.hpp"
19#include "../ecs/Registry.hpp"
20#include "../ui/UISystem.hpp"
28#include <cstring>
29
30namespace esengine {
31
32static EstellaContext& ctx() { return activeCtx(); }
33
34
35bool uiLayout_update(ecs::Registry& registry, f32 boxLeft, f32 boxBottom, f32 boxRight, f32 boxTop,
36 bool propertyDirty) {
37 return ctx().require<ecs::UISystem>().layoutUpdate(registry, boxLeft, boxBottom, boxRight, boxTop, propertyDirty);
38}
39
41 return ctx().require<ecs::UISystem>().hitInputsChanged(registry, ctx().tryGet<resource::ResourceManager>());
42}
43
45 f32 originX, f32 originY, f32 originZ,
46 f32 dirX, f32 dirY, f32 dirZ) {
47 const ecs::PickRay ray{{originX, originY, originZ}, {dirX, dirY, dirZ}};
48 ctx().require<ecs::UISystem>().hitTestUpdate(registry, ray,
49 ctx().tryGet<resource::ResourceManager>());
50}
51
53 f32 worldOriginX, f32 worldOriginY, f32 worldOriginZ,
54 f32 worldDirX, f32 worldDirY, f32 worldDirZ,
55 f32 screenOriginX, f32 screenOriginY, f32 screenOriginZ,
56 f32 screenDirX, f32 screenDirY, f32 screenDirZ) {
57 const ecs::PickRay world{{worldOriginX, worldOriginY, worldOriginZ},
58 {worldDirX, worldDirY, worldDirZ}};
59 const ecs::PickRay screen{{screenOriginX, screenOriginY, screenOriginZ},
60 {screenDirX, screenDirY, screenDirZ}};
61 ctx().require<ecs::UISystem>().hitTestUpdate(registry, world, screen,
62 ctx().tryGet<resource::ResourceManager>());
63}
64
66 return ctx().require<ecs::UISystem>().getHitEntity();
67}
68
69u32 uiHitTest_pick(ecs::Registry& registry, f32 worldX, f32 worldY) {
70 return ctx().require<ecs::UISystem>().pick(registry, worldX, worldY);
71}
72
73u32 uiHitTest_pickAll(ecs::Registry& registry, f32 worldX, f32 worldY) {
74 return ctx().require<ecs::UISystem>().pickAll(registry, worldX, worldY);
75}
76
78 return ctx().require<ecs::UISystem>().pickResult(index);
79}
80
81// Resolved (Yoga-pass) pixel size of a UI node, for the editor's selection outline.
82// The node's world box is this size, pivot-centered on its Transform.
84 auto* n = r.tryGet<ecs::UINode>(Entity::fromRaw(e));
85 return n ? n->computed_size_.x : 0.0f;
86}
88 auto* n = r.tryGet<ecs::UINode>(Entity::fromRaw(e));
89 return n ? n->computed_size_.y : 0.0f;
90}
91
92// What the Canvases hold with no uiOrder of its own; the frame borrows it. One table: the order pass and every collect it serves run in turn.
94
95const std::unordered_map<u32, i32>* uiTreeLayers() {
96 return &g_uiTreeOrder.layers;
97}
98
103
104// The SDF text path (TS) reads an entity's UI
105// draw order so glyph quads interleave with UI quads. uiRenderOrderUpdate
106// assigns uiOrder to every UIVisual in the UI tree (text nodes carry a
107// visualType=None UIVisual purely to be ordered). -1 = not a UI node.
109 auto* ui = registry.tryGet<ecs::UIVisual>(Entity::fromRaw(entity));
110 return ui ? ui->uiOrder : -1;
111}
112
113// Same walk, same reason: the text path submits its own draws, so it has to carry
114// the owning Canvas' culling bit itself. 0 = not a UI node, drawn by every camera.
115u32 ui_getCullBit(ecs::Registry& registry, u32 entity) {
116 auto* ui = registry.tryGet<ecs::UIVisual>(Entity::fromRaw(entity));
117 return ui ? ui->uiCullBit : 0u;
118}
119
120bool ui_isScreenDomain(ecs::Registry& registry, u32 entity) {
121 auto* ui = ctx().tryGet<ecs::UISystem>();
122 return ui && ui->isScreenDomain(registry, entity);
123}
124
125// UINode (CSS box) computed size — its internal computed_size_ is not
126// embind-readable, so expose it for TS uiHelpers.
127bool getUINodeHiddenInTree(ecs::Registry& registry, u32 entity) {
128 auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
129 return node && node->hidden_in_tree_;
130}
131
132// Subtree opacity / pointer gate, resolved by the layout pass. Same reason as
133// the hidden bit: computed state is not embind-readable, and the TS text
134// renderer + input router need it to behave like the C++ visuals do.
136 auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
137 return (node ? node->alpha_in_tree_ : 1.0f) * ecs::GroupOpacityLookup(registry).of(Entity::fromRaw(entity));
138}
139
141 auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
142 if (!node) return 0.0f;
143 return node->computed_size_.x;
144}
145
147 auto* node = registry.tryGet<ecs::UINode>(Entity::fromRaw(entity));
148 if (!node) return 0.0f;
149 return node->computed_size_.y;
150}
151
152bool transform_copyFlatRenderMatrix(ecs::Registry& registry, u32 entity, uintptr_t outputPtr) {
153 glm::mat4 matrix;
154 if (!outputPtr || !ecs::copyFlatRenderMatrix(registry, Entity::fromRaw(entity), matrix)) return false;
155 std::memcpy(reinterpret_cast<void*>(outputPtr), glm::value_ptr(matrix), sizeof(glm::mat4));
156 return true;
157}
158
160 esengine::World world{registry, ctx().services(), 0.0f};
161 if (auto* ts = ctx().tryGet<ecs::TransformSystem>()) {
162 ts->update(world);
163 } else {
164 ecs::TransformSystem fallback;
165 fallback.update(world);
166 }
167}
168
169} // namespace esengine
Instance-based engine context for estella renderer.
Central ECS registry for entity and component management.
Central resource management system.
System for computing hierarchical world transforms.
Unified Transform component with local and world-space data.
The UI entry points the SDK drives: the Yoga layout pass, hit testing, draw order,...
UI subsystem owning layout tree and hit-test state.
Unified context passed to all ECS systems.
ServiceRegistry & services()
static constexpr i32 UI_BASE_LAYER
The opacity a draw inherits from the GroupOpacity above it.
f32 of(Entity entity) const
1 when no enabled group encloses entity (itself included).
Central container for all entities and components.
Definition Registry.hpp:73
T * tryGet(Entity entity)
Tries to get a component from an entity.
Definition Registry.hpp:670
void update(World &world) override
Called every frame to execute system logic.
UI subsystem: owns layout tree + hit-test state.
Definition UISystem.hpp:56
bool isScreenDomain(const Registry &registry, u32 entity) const
Definition UISystem.hpp:149
bool copyFlatRenderMatrix(Registry &registry, Entity entity, glm::mat4 &output)
void uiRenderOrderUpdate(Registry &registry, UITreeOrder *tree=nullptr)
void transform_update(ecs::Registry &registry)
f32 getUINodeAlphaInTree(ecs::Registry &registry, u32 entity)
f32 uiNode_computedWidth(ecs::Registry &r, u32 e)
void uiRenderOrder_update(ecs::Registry &registry)
u32 uiHitTest_pickAll(ecs::Registry &registry, f32 worldX, f32 worldY)
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
u32 uiHitTest_pick(ecs::Registry &registry, f32 worldX, f32 worldY)
bool ui_isScreenDomain(ecs::Registry &registry, u32 entity)
Whether entity belongs to the SCREEN rather than to the world.
f32 uiNode_computedHeight(ecs::Registry &r, u32 e)
bool transform_copyFlatRenderMatrix(ecs::Registry &registry, u32 entity, uintptr_t outputPtr)
f32 getUINodeComputedWidth(ecs::Registry &registry, u32 entity)
void uiHitTest_update(ecs::Registry &registry, f32 originX, f32 originY, f32 originZ, f32 dirX, f32 dirY, f32 dirZ)
void uiHitTest_updateDomains(ecs::Registry &registry, f32 worldOriginX, f32 worldOriginY, f32 worldOriginZ, f32 worldDirX, f32 worldDirY, f32 worldDirZ, f32 screenOriginX, f32 screenOriginY, f32 screenOriginZ, f32 screenDirX, f32 screenDirY, f32 screenDirZ)
The hit test with a ray per domain — world content and world-space UI answer the first,...
u32 uiHitTest_pickResult(u32 index)
bool uiLayout_update(ecs::Registry &registry, f32 boxLeft, f32 boxBottom, f32 boxRight, f32 boxTop, bool propertyDirty)
const std::unordered_map< u32, i32 > * uiTreeLayers()
u32 uiHitTest_getHitEntity()
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
f32 getUINodeComputedHeight(ecs::Registry &registry, u32 entity)
bool getUINodeHiddenInTree(ecs::Registry &registry, u32 entity)
bool uiHitTest_inputsChanged(ecs::Registry &registry)
i32 ui_getRenderOrder(ecs::Registry &registry, u32 entity)
static ecs::UITreeOrder g_uiTreeOrder
static EstellaContext & ctx()
EstellaContext & activeCtx()
The engine context every binding routes through — single source of truth.
u32 ui_getCullBit(ecs::Registry &registry, u32 entity)
float f32
32-bit floating point
Definition Types.hpp:59
static constexpr Entity fromRaw(u32 v)
Reconstructs an Entity from a raw packed value.
Definition Types.hpp:162
Context passed to every System::update call.
Definition World.hpp:40
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).
Definition UINode.hpp:79
glm::vec2 computed_size_
Definition UINode.hpp:147
The draw layer of what a Canvas holds that has no uiOrder of its own: an emitter, a Spine skeleton,...
UIVisual — the single UI visual component, merging the former low-level UIRenderer (what the renderer...
Definition UIVisual.hpp:84