Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
CanvasRoots.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"
10
11#include <unordered_map>
12#include <vector>
13
14namespace esengine::ecs {
15
17inline std::vector<Entity> canvasRoots(Registry& registry, bool layoutOnly) {
18 std::vector<Entity> candidates;
19 registry.each<Canvas>([&](Entity entity, Canvas&) {
20 if (!layoutOnly || (registry.has<UINode>(entity) && registry.has<Transform>(entity)))
21 candidates.push_back(entity);
22 });
23 if (candidates.size() <= 1) return candidates;
24 // 1 = current ancestor chain, 2 = no eligible canvas, 3 = eligible canvas reachable.
25 std::unordered_map<Entity, u8> ancestry;
26 ancestry.reserve(candidates.size());
27 for (Entity entity : candidates) ancestry.emplace(entity, 3);
28 std::vector<Entity> roots;
29 std::vector<Entity> trail;
30 for (Entity entity : candidates) {
31 trail.clear();
32 const auto* parent = registry.tryGet<Parent>(entity);
33 Entity ancestor = parent ? parent->entity : INVALID_ENTITY;
34 bool nested = false;
35 while (registry.valid(ancestor)) {
36 auto [entry, inserted] = ancestry.emplace(ancestor, 1);
37 if (!inserted) {
38 nested = entry->second == 3;
39 break;
40 }
41 trail.push_back(ancestor);
42 parent = registry.tryGet<Parent>(ancestor);
43 ancestor = parent ? parent->entity : INVALID_ENTITY;
44 }
45 for (Entity visited : trail) ancestry[visited] = nested ? 3 : 2;
46 if (!nested) roots.push_back(entity);
47 }
48 return roots;
49}
50
51} // 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
void each(Func &&func)
Executes a function for each entity with specified components.
Definition Registry.hpp:881
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
std::vector< Entity > canvasRoots(Registry &registry, bool layoutOnly)
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
Entity identifier with packed index + generation.
Definition Types.hpp:135
Canvas component for 2D game resolution management.
Definition Canvas.hpp:71
Parent reference component.
Definition Hierarchy.hpp:46
Entity entity
Parent entity ID.
Definition Hierarchy.hpp:49
Unified transform component.
Definition Transform.hpp:45
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).
Definition UINode.hpp:79