14#include <unordered_map>
15#include <unordered_set>
31 std::unordered_map<Entity, YGNodeRef>
nodes;
34 for (
auto& [entity, node] :
nodes) YGNodeFree(node);
38 auto it =
nodes.find(entity);
39 if (it !=
nodes.end())
return it->second;
40 YGNodeRef node = YGNodeNew();
41 nodes.emplace(entity, node);
48 void reap(
const std::unordered_set<Entity>& live) {
49 for (
auto it =
nodes.begin(); it !=
nodes.end();) {
50 if (live.find(it->first) == live.end()) {
51 YGNodeFree(it->second);
73 return YGFlexDirectionRow;
93 return YGJustifyFlexStart;
103 return YGAlignStretch;
115 return YGAlignFlexStart;
140constexpr u8 DIM_PX = 0, DIM_PERCENT = 1, DIM_AUTO = 2;
142void applyWidth(YGNodeRef n,
const Dimension& d) {
143 if (d.unit == DIM_PERCENT) YGNodeStyleSetWidthPercent(n, d.value);
144 else if (d.unit == DIM_AUTO) YGNodeStyleSetWidthAuto(n);
145 else YGNodeStyleSetWidth(n, d.value);
147void applyHeight(YGNodeRef n,
const Dimension& d) {
148 if (d.unit == DIM_PERCENT) YGNodeStyleSetHeightPercent(n, d.value);
149 else if (d.unit == DIM_AUTO) YGNodeStyleSetHeightAuto(n);
150 else YGNodeStyleSetHeight(n, d.value);
153void applyMinWidth(YGNodeRef n,
const Dimension& d) {
154 if (d.unit == DIM_PERCENT) YGNodeStyleSetMinWidthPercent(n, d.value);
155 else if (d.unit == DIM_PX) YGNodeStyleSetMinWidth(n, d.value);
157void applyMinHeight(YGNodeRef n,
const Dimension& d) {
158 if (d.unit == DIM_PERCENT) YGNodeStyleSetMinHeightPercent(n, d.value);
159 else if (d.unit == DIM_PX) YGNodeStyleSetMinHeight(n, d.value);
161void applyMaxWidth(YGNodeRef n,
const Dimension& d) {
162 if (d.unit == DIM_PERCENT) YGNodeStyleSetMaxWidthPercent(n, d.value);
163 else if (d.unit == DIM_PX) YGNodeStyleSetMaxWidth(n, d.value);
165void applyMaxHeight(YGNodeRef n,
const Dimension& d) {
166 if (d.unit == DIM_PERCENT) YGNodeStyleSetMaxHeightPercent(n, d.value);
167 else if (d.unit == DIM_PX) YGNodeStyleSetMaxHeight(n, d.value);
169void applyFlexBasis(YGNodeRef n,
const Dimension& d) {
170 if (d.unit == DIM_PERCENT) YGNodeStyleSetFlexBasisPercent(n, d.value);
171 else if (d.unit == DIM_AUTO) YGNodeStyleSetFlexBasisAuto(n);
172 else YGNodeStyleSetFlexBasis(n, d.value);
174void applyMargin(YGNodeRef n, YGEdge edge,
const Dimension& d) {
175 if (d.unit == DIM_PERCENT) YGNodeStyleSetMarginPercent(n, edge, d.value);
176 else if (d.unit == DIM_AUTO) YGNodeStyleSetMarginAuto(n, edge);
177 else YGNodeStyleSetMargin(n, edge, d.value);
179void applyInset(YGNodeRef n, YGEdge edge,
const Dimension& d) {
181 if (d.unit == DIM_PERCENT) YGNodeStyleSetPositionPercent(n, edge, d.value);
182 else if (d.unit == DIM_PX) YGNodeStyleSetPosition(n, edge, d.value);
185void applyUINodeStyle(
Registry& registry, Entity entity, YGNodeRef yg) {
186 auto& n = registry.get<
UINode>(entity);
188 ? YGDisplayNone : YGDisplayFlex);
190 ? YGPositionTypeAbsolute : YGPositionTypeRelative);
191 applyInset(yg, YGEdgeLeft, n.insetLeft);
192 applyInset(yg, YGEdgeTop, n.insetTop);
193 applyInset(yg, YGEdgeRight, n.insetRight);
194 applyInset(yg, YGEdgeBottom, n.insetBottom);
195 applyWidth(yg, n.width);
196 applyHeight(yg, n.height);
197 applyMinWidth(yg, n.minWidth);
198 applyMinHeight(yg, n.minHeight);
199 applyMaxWidth(yg, n.maxWidth);
200 applyMaxHeight(yg, n.maxHeight);
201 YGNodeStyleSetFlexGrow(yg, n.flexGrow);
202 YGNodeStyleSetFlexShrink(yg, n.flexShrink);
203 applyFlexBasis(yg, n.flexBasis);
204 YGNodeStyleSetAlignSelf(yg, toYGAlignSelf(n.alignSelf));
205 applyMargin(yg, YGEdgeLeft, n.marginLeft);
206 applyMargin(yg, YGEdgeTop, n.marginTop);
207 applyMargin(yg, YGEdgeRight, n.marginRight);
208 applyMargin(yg, YGEdgeBottom, n.marginBottom);
218 YGNodeStyleSetFlexDirection(yg, toYGFlexDirection(fc->direction));
219 YGNodeStyleSetFlexWrap(yg, toYGWrap(fc->wrap));
220 YGNodeStyleSetJustifyContent(yg, toYGJustify(fc->justifyContent));
221 YGNodeStyleSetAlignItems(yg, toYGAlign(fc->alignItems));
222 YGNodeStyleSetAlignContent(yg, toYGAlignContent(fc->alignContent));
223 YGNodeStyleSetGap(yg, YGGutterColumn, fc->gap.x);
224 YGNodeStyleSetGap(yg, YGGutterRow, fc->gap.y);
225 YGNodeStyleSetPadding(yg, YGEdgeLeft, fc->padding.left);
226 YGNodeStyleSetPadding(yg, YGEdgeTop, fc->padding.top);
227 YGNodeStyleSetPadding(yg, YGEdgeRight, fc->padding.right);
228 YGNodeStyleSetPadding(yg, YGEdgeBottom, fc->padding.bottom);
230 YGNodeStyleSetFlexDirection(yg, YGFlexDirectionColumn);
231 YGNodeStyleSetFlexWrap(yg, YGWrapNoWrap);
232 YGNodeStyleSetJustifyContent(yg, YGJustifyFlexStart);
233 YGNodeStyleSetAlignItems(yg, YGAlignStretch);
234 YGNodeStyleSetAlignContent(yg, YGAlignFlexStart);
235 YGNodeStyleSetGap(yg, YGGutterColumn, 0.0f);
236 YGNodeStyleSetGap(yg, YGGutterRow, 0.0f);
237 YGNodeStyleSetPadding(yg, YGEdgeLeft, 0.0f);
238 YGNodeStyleSetPadding(yg, YGEdgeTop, 0.0f);
239 YGNodeStyleSetPadding(yg, YGEdgeRight, 0.0f);
240 YGNodeStyleSetPadding(yg, YGEdgeBottom, 0.0f);
246void layoutUINodeSubtree(
248 f32 availW,
f32 availH,
f32 parentPivotX,
f32 parentPivotY,
249 bool structureChanged,
bool forceWriteback
252 i32 end = rootIdx + tree.nodes_[rootIdx].subtree_size;
254 std::vector<YGNodeRef> yg(
static_cast<usize>(end - begin),
nullptr);
255 std::unordered_map<Entity, i32> slotOf;
262 for (
i32 k = begin; k < end; ++k) {
263 Entity e = tree.nodes_[k].entity;
264 if (!registry.has<
UINode>(e))
continue;
265 YGNodeRef node = cache.getOrCreate(e);
266 applyUINodeStyle(registry, e, node);
267 yg[
static_cast<usize>(k - begin)] = node;
268 slotOf[e] = k - begin;
270 YGNodeRef rootYG = yg[0];
276 if (structureChanged) {
277 for (
i32 k = begin; k < end; ++k) {
278 YGNodeRef node = yg[
static_cast<usize>(k - begin)];
279 if (node) YGNodeRemoveAllChildren(node);
281 for (
i32 k = begin + 1; k < end; ++k) {
282 YGNodeRef child = yg[
static_cast<usize>(k - begin)];
283 if (!child)
continue;
284 auto it = slotOf.find(tree.nodes_[k].parent);
285 if (it == slotOf.end())
continue;
286 YGNodeRef parentYG = yg[
static_cast<usize>(it->second)];
287 YGNodeInsertChild(parentYG, child, YGNodeGetChildCount(parentYG));
292 auto& rootNode = registry.get<
UINode>(tree.nodes_[begin].entity);
293 if (rootNode.width.unit == DIM_AUTO) YGNodeStyleSetWidth(rootYG, availW);
294 if (rootNode.height.unit == DIM_AUTO) YGNodeStyleSetHeight(rootYG, availH);
296 YGNodeCalculateLayout(rootYG, availW, availH, YGDirectionLTR);
303 std::vector<u8> resized(
static_cast<usize>(end - begin), 0);
305 for (
i32 k = begin; k < end; ++k) {
306 YGNodeRef node = yg[
static_cast<usize>(k - begin)];
308 Entity e = tree.nodes_[k].entity;
310 bool parentResized =
false;
312 auto it = slotOf.find(tree.nodes_[k].parent);
313 if (it != slotOf.end()) parentResized = resized[
static_cast<usize>(it->second)] != 0;
317 if (!forceWriteback && !YGNodeGetHasNewLayout(node) && !parentResized)
continue;
318 YGNodeSetHasNewLayout(node,
false);
320 auto& un = registry.get<
UINode>(e);
321 f32 fw = YGNodeLayoutGetWidth(node);
322 f32 fh = YGNodeLayoutGetHeight(node);
323 resized[
static_cast<usize>(k - begin)] =
324 (un.computed_size_.x != fw || un.computed_size_.y != fh) ? 1 : 0;
325 un.computed_size_.x = fw;
326 un.computed_size_.y = fh;
330 f32 pw, ph, ppx, ppy;
332 pw = availW; ph = availH; ppx = parentPivotX; ppy = parentPivotY;
334 auto& pn = registry.get<
UINode>(tree.nodes_[k].parent);
335 pw = pn.
computed_size_.x; ph = pn.computed_size_.y; ppx = 0.5f; ppy = 0.5f;
337 f32 yl = YGNodeLayoutGetLeft(node);
338 f32 yt = YGNodeLayoutGetTop(node);
347 auto axisCentred = [](
const Dimension& a,
const Dimension& b,
348 const Dimension& m0,
const Dimension& m1) {
349 return a.unit == DIM_AUTO && b.unit == DIM_AUTO &&
350 m0.unit == DIM_AUTO && m1.unit == DIM_AUTO;
352 if (axisCentred(un.insetLeft, un.insetRight, un.marginLeft, un.marginRight))
353 yl = 0.5f * (pw - fw);
354 if (axisCentred(un.insetTop, un.insetBottom, un.marginTop, un.marginBottom))
355 yt = 0.5f * (ph - fh);
359 f32 localX = -ppx * pw + yl + 0.5f * fw;
360 f32 localY = (1.0f - ppy) * ph - yt - 0.5f * fh;
362 if (
auto* t = registry.tryGet<
Transform>(e)) {
365 const glm::vec3 before = t->position;
368 if (t->position != before) registry.markChanged<
Transform>(e);
380 auto& nodes = tree.nodes_;
381 for (
i32 i = 0; i < static_cast<i32>(nodes.size()); ) {
382 auto* n = registry.tryGet<
UINode>(nodes[i].entity);
384 i32 end = i + nodes[i].subtree_size;
385 for (
i32 k = i; k < end; ++k) {
386 if (
auto* c = registry.tryGet<
UINode>(nodes[k].entity)) c->hidden_in_tree_ =
true;
390 if (n) n->hidden_in_tree_ =
false;
405 auto& nodes = tree.nodes_;
406 if (nodes.empty())
return;
411 for (
usize i = 0; i < nodes.size(); ++i) {
412 const u32 depth = nodes[i].depth;
416 auto* n = registry.tryGet<
UINode>(nodes[i].entity);
422 const f32 alpha = parentAlpha * std::clamp(n->opacity, 0.0f, 1.0f);
425 n->alpha_in_tree_ = alpha;
426 n->pointer_blocked_in_tree_ = blocked;
437bool anyUIAnimActive(
Registry& registry) {
440 if (n.anim_override_ != 0) active =
true;
447bool isScreenRoot(
Registry& registry, Entity e) {
448 auto* p = registry.tryGet<
Parent>(e);
456 const f32 availW = box.width();
457 const f32 availH = box.height();
458 const f32 cx = box.centerX();
459 const f32 cy = box.centerY();
460 for (
i32 i = 0; i < static_cast<i32>(tree.nodes_.size()); ) {
461 auto& node = tree.nodes_[i];
463 if (!parentIsUINode && isScreenRoot(registry, node.entity)) {
464 auto it = cache.nodes.find(node.entity);
465 auto* t = registry.tryGet<
Transform>(node.entity);
466 auto* un = registry.tryGet<
UINode>(node.entity);
467 if (it != cache.nodes.end() && t && un) {
468 const f32 fw = YGNodeLayoutGetWidth(it->second);
469 const f32 fh = YGNodeLayoutGetHeight(it->second);
470 const f32 localX = -0.5f * availW + YGNodeLayoutGetLeft(it->second) + 0.5f * fw;
471 const f32 localY = 0.5f * availH - YGNodeLayoutGetTop(it->second) - 0.5f * fh;
472 const glm::vec3 before = t->position;
475 if (t->position != before) registry.markChanged<
Transform>(node.entity);
478 i += node.subtree_size;
483void addSubtree(
Registry& registry, Entity entity, std::unordered_set<u32>& out, std::vector<u32>& rootByIndex) {
484 std::vector<Entity> pending{entity};
485 while (!pending.empty()) {
486 const Entity current = pending.back();
488 if (!registry.valid(current) || !out.insert(current.id()).second)
continue;
489 if (current.index() >= rootByIndex.size()) rootByIndex.resize(current.index() + 1, 0);
490 rootByIndex[current.index()] = entity.id();
491 auto* children = registry.tryGet<
Children>(current);
492 if (!children)
continue;
493 for (Entity child : children->entities) pending.push_back(child);
505void collectScreenDomain(
Registry& registry, std::unordered_set<u32>& out, std::vector<u32>& roots, std::vector<u32>& rootByIndex) {
509 if (!registry.has<
UINode>(entity) || !registry.has<
Transform>(entity))
return;
510 if (!isScreenRoot(registry, entity))
return;
511 roots.push_back(entity.id());
512 addSubtree(registry, entity, out, rootByIndex);
518 bool forceWriteback) {
519 for (
i32 i = 0; i < static_cast<i32>(tree.nodes_.size()); ) {
520 auto& node = tree.nodes_[i];
527 if (!parentIsUINode) {
528 layoutUINodeSubtree(registry, tree, cache, i, box.width(), box.height(), 0.5f, 0.5f,
529 structureChanged, forceWriteback);
531 i += node.subtree_size;
541void UISystem::propagateDetachedUIState(
Registry& registry) {
542 if (++inherited_stamp_ == 0) {
543 inherited_cache_.clear();
546 registry.each<UINode>([&](Entity entity, UINode&) {
547 inherited_path_.clear();
548 InheritedState state;
549 Entity current = entity;
550 while (registry.valid(current)) {
551 auto& memo = inherited_cache_[current];
552 if (memo.stamp == inherited_stamp_) {
553 state = memo.resolving ? InheritedState{
true, 0.0f,
true} : memo.state;
556 memo.stamp = inherited_stamp_;
557 memo.resolving =
true;
558 inherited_path_.push_back(current);
559 const auto* parent = registry.tryGet<Parent>(current);
562 for (
auto it = inherited_path_.rbegin(); it != inherited_path_.rend(); ++it) {
563 if (
auto* node = registry.tryGet<UINode>(*it)) {
565 state.alpha *= std::clamp(node->opacity, 0.0f, 1.0f);
568 node->hidden_in_tree_ = state.hidden;
569 node->alpha_in_tree_ = state.alpha;
570 node->pointer_blocked_in_tree_ = state.blocked;
572 auto& memo = inherited_cache_.at(*it);
574 memo.resolving =
false;
578 for (
auto it = inherited_cache_.begin(); it != inherited_cache_.end();) {
579 if (it->second.stamp != inherited_stamp_) it = inherited_cache_.erase(it);
594 if (lastRegistryId_ != registry.
instanceId()) {
596 inherited_cache_.clear();
601 layoutPrimed_ =
false;
608 tree.rebuild(registry);
612 collectScreenDomain(registry, screen_domain_, screen_roots_, screen_root_by_index_);
616 const bool detached = registry.
view<
UINode>().size() !=
tree.nodes_.size();
617 if (detached) propagateDetachedUIState(registry);
618 else if (!inherited_cache_.empty()) inherited_cache_.clear();
620 LayoutRect box{ boxLeft, boxBottom, boxRight, boxTop };
622 bool animNow = anyUIAnimActive(registry);
623 bool sigChanged =
tree.structure_sig_ != lastSig_;
628 bool sizeChanged = box.
width() != lastBoxW_ || box.
height() != lastBoxH_;
629 bool centerChanged = box.
centerX() != lastBoxCX_ || box.
centerY() != lastBoxCY_;
630 bool wasAnimActive = lastAnimActive_;
632 lastSig_ =
tree.structure_sig_;
635 lastAnimActive_ = animNow;
641 bool first = !layoutPrimed_;
642 bool dirty = first || sigChanged || sizeChanged || tsPropertyDirty
643 || animNow || wasAnimActive;
644 layoutPrimed_ =
true;
650 return centerChanged;
661 bool structureChanged = first || sigChanged;
662 bool forceWriteback = structureChanged || sizeChanged;
669 propagateHiddenInTree(registry,
tree);
670 propagateInheritedUIState(registry,
tree);
672 unifiedLayoutPass(registry,
tree, *
layoutCache_, box, structureChanged, forceWriteback);
678 if (structureChanged) {
679 std::unordered_set<Entity> live;
680 live.reserve(
tree.nodes_.size());
681 for (
auto& node :
tree.nodes_) live.insert(node.entity);
Canvas component for 2D game resolution management.
Hierarchy components for parent-child relationships.
UI subsystem owning layout tree and hit-test state.
Central container for all entities and components.
u64 instanceId() const
Which registry this is — unique for the process, never reused.
View< Components... > view()
Creates a view for iterating entities with specific components.
bool layoutUpdate(Registry ®istry, 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...
std::unique_ptr< LayoutCache > layoutCache_
static std::vector< f32 > inherited_alpha_stack_
AlignSelf
Per-item cross-axis alignment override (CSS align-self). Lives on UINode since UINode carries the fle...
static std::vector< u8 > inherited_block_stack_
void invalidateTransformComposition()
Say that composed world transforms are stale.
std::int32_t i32
Signed 32-bit integer.
std::size_t usize
Size type for indexing and sizes.
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
std::uint32_t u32
Unsigned 32-bit integer.
std::uint8_t u8
Unsigned 8-bit integer.
float f32
32-bit floating point
Entity identifier with packed index + generation.
Canvas component for 2D game resolution management.
void reap(const std::unordered_set< Entity > &live)
std::unordered_map< Entity, YGNodeRef > nodes
YGNodeRef getOrCreate(Entity entity)
Parent reference component.
Entity entity
Parent entity ID.
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).
static constexpr u8 ANIM_POS_X
static constexpr u8 ANIM_POS_Y