Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
UIRenderOrderSystem.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"
13#include "CanvasRoots.hpp"
14
15#include <unordered_map>
16#include <vector>
17
18namespace esengine::ecs {
19
26 std::unordered_map<u32, i32> layers;
27 i32 base = 0;
28};
29
30inline i32 assignRenderOrder(Registry& registry, Entity entity, i32 counter, u32 cullBit, UITreeOrder* tree = nullptr) {
31 struct Task { Entity entity; u32 cullBit; };
32 std::vector<Task> pending{{entity, cullBit}};
33 std::vector<u8> visited;
34 while (!pending.empty()) {
35 const auto task = pending.back();
36 entity = task.entity;
37 pending.pop_back();
38 if (!registry.valid(entity)) continue;
39 const auto index = entity.index();
40 if (index >= visited.size()) visited.resize(index + 1, 0);
41 if (visited[index]) continue;
42 visited[index] = 1;
43 const auto* canvas = registry.tryGet<Canvas>(entity);
44 const u32 inheritedBit = canvas
45 ? ((canvas->layer < 0 || canvas->layer >= 32) ? 0u : (1u << canvas->layer))
46 : task.cullBit;
47 if (registry.has<UINode>(entity)) {
48 auto* uiVisual = registry.tryGet<UIVisual>(entity);
49 if (uiVisual) {
50 uiVisual->uiOrder = counter;
51 uiVisual->uiCullBit = inheritedBit;
52 counter++;
53 } else if (registry.has<Sprite>(entity)) {
54 auto& sprite = registry.get<Sprite>(entity);
55 if (sprite.layer != counter) {
56 sprite.layer = counter;
57 }
58 counter++;
59 }
60 }
61 if (tree && !registry.has<UIVisual>(entity)
62 && (registry.has<ParticleEmitter>(entity) || registry.has<SpineAnimation>(entity)
63 || (registry.has<Sprite>(entity) && !registry.has<UINode>(entity)))) {
64 tree->layers[entity.id()] = tree->base + counter;
65 counter++;
66 }
67 auto* children = registry.tryGet<Children>(entity);
68 if (!children) continue;
69 for (auto child = children->entities.rbegin(); child != children->entities.rend(); ++child)
70 pending.push_back({*child, inheritedBit});
71 }
72 return counter;
73}
74
75inline void uiRenderOrderUpdate(Registry& registry, UITreeOrder* tree = nullptr) {
76 if (tree) tree->layers.clear();
77 i32 counter = 0;
78 for (Entity entity : canvasRoots(registry, false)) {
79 const auto& canvas = registry.get<Canvas>(entity);
80 const u32 bit = (canvas.layer < 0 || canvas.layer >= 32) ? 0u : (1u << canvas.layer);
81 counter = assignRenderOrder(registry, entity, counter, bit, tree);
82 }
83}
84
85} // namespace esengine::ecs
Canvas component for 2D game resolution management.
Hierarchy components for parent-child relationships.
Central ECS registry for entity and component management.
Spine skeletal animation component.
2D sprite rendering component
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
void uiRenderOrderUpdate(Registry &registry, UITreeOrder *tree=nullptr)
std::vector< Entity > canvasRoots(Registry &registry, bool layoutOnly)
i32 assignRenderOrder(Registry &registry, Entity entity, i32 counter, u32 cullBit, UITreeOrder *tree=nullptr)
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
Entity identifier with packed index + generation.
Definition Types.hpp:135
constexpr u32 id() const
Returns the raw packed value for FFI serialization.
Definition Types.hpp:159
constexpr u32 index() const
Gets the entity index.
Definition Types.hpp:150
Canvas component for 2D game resolution management.
Definition Canvas.hpp:71
Children list component.
Definition Hierarchy.hpp:66
Spine skeletal animation component.
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).
Definition UINode.hpp:79
The draw layer of what a Canvas holds that has no uiOrder of its own: an emitter, a Spine skeleton,...
std::unordered_map< u32, i32 > layers
UIVisual — the single UI visual component, merging the former low-level UIRenderer (what the renderer...
Definition UIVisual.hpp:84