Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
UITree.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include "../ecs/Registry.hpp"
6#include "../core/Hash.hpp"
11#include "CanvasRoots.hpp"
12
13#include <vector>
14#include <algorithm>
15
16namespace esengine::ecs {
17
29struct UITree {
36
37 std::vector<Node> nodes_;
38 // FNV-1a hash of the (entity, parent) pairs in DFS order — a compact
39 // fingerprint of the tree's shape. It changes on any spawn/despawn/reparent,
40 // sibling reorder, or UINode/Canvas add-remove, which is how a pass knows to
41 // rebuild the retained Yoga hierarchy rather than reuse it.
43
44 void rebuild(Registry& reg) {
45 nodes_.clear();
46 for (Entity entity : canvasRoots(reg, true)) {
47 buildDFS(reg, entity, INVALID_ENTITY, 0);
48 }
49
51 for (const auto& n : nodes_) {
52 h = hash::fnv1aWord(h, n.entity.id());
53 h = hash::fnv1aWord(h, n.parent.id());
54 }
56 }
57
58private:
59 struct Frame {
60 Entity entity;
61 Entity layoutParent;
62 u32 depth;
63 i32 nodeIndex;
64 usize nextChild;
65 };
66 std::vector<Frame> pending_;
67 std::vector<u32> visit_stamps_;
68 u32 visit_stamp_{0};
69
70 void buildDFS(Registry& reg, Entity entity, Entity layoutParent, u32 depth) {
71 if (++visit_stamp_ == 0) {
72 std::fill(visit_stamps_.begin(), visit_stamps_.end(), 0);
73 ++visit_stamp_;
74 }
75 pending_.clear();
76 const auto enter = [&](Entity current, Entity parent, u32 currentDepth) {
77 const auto index = current.index();
78 if (index >= visit_stamps_.size()) visit_stamps_.resize(index + 1, 0);
79 if (visit_stamps_[index] == visit_stamp_) return;
80 visit_stamps_[index] = visit_stamp_;
81 i32 nodeIndex = -1;
82 if (reg.has<UINode>(current)) {
83 nodeIndex = static_cast<i32>(nodes_.size());
84 nodes_.push_back({current, parent, currentDepth, 1});
85 parent = current;
86 ++currentDepth;
87 }
88 pending_.push_back({current, parent, currentDepth, nodeIndex, 0});
89 };
90 enter(entity, layoutParent, depth);
91 while (!pending_.empty()) {
92 auto& frame = pending_.back();
93 auto* children = reg.tryGet<Children>(frame.entity);
94 if (children && frame.nextChild < children->entities.size()) {
95 Entity child = children->entities[frame.nextChild++];
96 if (reg.valid(child)) enter(child, frame.layoutParent, frame.depth);
97 continue;
98 }
99 if (frame.nodeIndex >= 0) {
100 nodes_[frame.nodeIndex].subtree_size = static_cast<u32>(nodes_.size() - frame.nodeIndex);
101 }
102 pending_.pop_back();
103 }
104 }
105};
106
107} // namespace esengine::ecs
Canvas component for 2D game resolution management.
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
std::vector< Entity > canvasRoots(Registry &registry, bool layoutOnly)
constexpr u64 fnv1aWord(u64 h, u64 word)
Definition Hash.hpp:17
constexpr u64 kFnv64Offset
Definition Hash.hpp:12
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
Entity identifier with packed index + generation.
Definition Types.hpp:135
void rebuild(Registry &reg)
Definition UITree.hpp:44
std::vector< Node > nodes_
Definition UITree.hpp:37