Finality Engine
Loading...
Searching...
No Matches
SceneHierarchyPanel.h
Go to the documentation of this file.
1
6#pragma once
7#include <Finality.h>
9#include <entt.hpp>
10
11namespace FINALITY
12{
13 static void DrawSceneHierarchyPanel(Scene* scene, entt::entity& selectedEntityHandle)
14 {
15 FINALITY::ImGUIPanel panel("Hierarchy");
16
17 static std::string searchFilter;
18 if (searchFilter.capacity() < 256)
19 searchFilter.reserve(256);
20
21 float searchBarWidth = panel.GetContentRegionAvail().x - panel.GetWindowPadding().x;
22
23 panel.PushItemWidth(searchBarWidth);
24 panel.InputText("##HierarchySearch", &searchFilter);
25 panel.PopItemWidth();
26
27 panel.Spacing();
28
29 entt::registry& registry = scene->GetRegistry();
30
31 auto GetEntityName = [&](entt::entity entity) -> std::string
32 {
33 if (registry.all_of<TagComponent>(entity))
34 return registry.get<TagComponent>(entity).tag;
35
36 return "GameObject";
37 };
38
39 auto MatchesSearch = [&](entt::entity entity) -> bool
40 {
41 if (searchFilter.empty())
42 return true;
43
44 return GetEntityName(entity).find(searchFilter) != std::string::npos;
45 };
46
47 static entt::entity entityContextTriggered = entt::null;
48 static bool openEntityContext = false;
49 static bool openWorkspaceContext = false;
50 bool anyItemHoveredThisFrame = false;
51
52 auto DrawEntityNode = [&](auto& self, entt::entity entity) -> void
53 {
54 if (entity == entt::null || !registry.valid(entity))
55 return;
56
57 const auto* hierarchy = registry.try_get<RelationshipComponent>(entity);
58
59 entt::entity firstChild = hierarchy
60 ? hierarchy->FirstChild
61 : entt::null;
62
63 bool hasChildren = firstChild != entt::null;
64
65 if (!searchFilter.empty() && !MatchesSearch(entity))
66 {
67 bool childMatches = false;
68 entt::entity child = firstChild;
69
70 while (child != entt::null)
71 {
72 if (registry.valid(child) && MatchesSearch(child))
73 {
74 childMatches = true;
75 break;
76 }
77
78 const auto* childRel = registry.try_get<RelationshipComponent>(child);
79 if (!childRel)
80 break;
81
82 child = childRel->NextSibling;
83 }
84
85 if (!childMatches)
86 return;
87 }
88
89 panel.PushID(static_cast<int>(entity));
90
91 bool selected = selectedEntityHandle == entity;
92 bool opened = false;
93
94 if (hasChildren)
95 {
96 opened = panel.TreeNode(GetEntityName(entity), selected);
97
98 if (panel.IsItemClicked(0))
99 selectedEntityHandle = entity;
100 }
101 else
102 {
103 panel.Indent(14.0f);
104 if (panel.Selectable(GetEntityName(entity), selected))
105 selectedEntityHandle = entity;
106 panel.Unindent(14.0f);
107 }
108
109 if (panel.IsItemHovered())
110 anyItemHoveredThisFrame = true;
111
112 if (panel.IsItemClicked(1))
113 {
114 selectedEntityHandle = entity;
115 entityContextTriggered = entity;
116 openEntityContext = true;
117 }
118
119 panel.PopID();
120
121 if (!opened)
122 return;
123
124 entt::entity child = firstChild;
125 while (child != entt::null)
126 {
127 self(self, child);
128
129 const auto* childRel = registry.try_get<RelationshipComponent>(child);
130 if (!childRel)
131 break;
132
133 child = childRel->NextSibling;
134 }
135
136 panel.TreePop();
137 };
138
139 for (auto entity : registry.view<RelationshipComponent>())
140 {
141 const auto& hierarchy = registry.get<RelationshipComponent>(entity);
142 if (hierarchy.Parent == entt::null)
143 DrawEntityNode(DrawEntityNode, entity);
144 }
145
146 for (auto entity : registry.view<TagComponent>())
147 {
148 if (!registry.all_of<RelationshipComponent>(entity))
149 DrawEntityNode(DrawEntityNode, entity);
150 }
151
152 if (panel.IsHovered() && panel.IsMouseClicked(1) && !anyItemHoveredThisFrame)
153 {
154 openWorkspaceContext = true;
155 }
156
157 if (openEntityContext)
158 {
159 panel.OpenPopup("HierarchyEntityContext");
160 openEntityContext = false;
161 }
162
163 if (openWorkspaceContext)
164 {
165 panel.OpenPopup("HierarchyWorkspaceContext");
166 openWorkspaceContext = false;
167 }
168
169 bool entityDestroyed = false;
170 entt::entity entityToDestroy = entt::null;
171
172 if (panel.BeginPopup("HierarchyEntityContext"))
173 {
174 if (panel.MenuItem("Create Child Entity"))
175 {
176 FINALITY::Entity childEntity = scene->CreateEntity("Child GameObject");
177 childEntity.SetParent(scene->GetEntity(entityContextTriggered));
178 }
179 if (panel.MenuItem("Delete Entity"))
180 {
181 entityToDestroy = entityContextTriggered;
182 entityDestroyed = true;
183 }
184 panel.EndPopup();
185 }
186
187 if (panel.BeginPopup("HierarchyWorkspaceContext"))
188 {
189 if (panel.MenuItem("Create Empty Entity"))
190 {
191 scene->CreateEntity("Empty");
192 }
193 panel.EndPopup();
194 }
195
196 if (entityDestroyed && entityToDestroy != entt::null)
197 {
198 scene->DestroyEntity(entityToDestroy);
199 selectedEntityHandle = entt::null;
200 entityContextTriggered = entt::null;
201 }
202
203 if (panel.IsHovered() && panel.IsItemClicked(0) && !anyItemHoveredThisFrame)
204 {
205 selectedEntityHandle = entt::null;
206 }
207 }
208}
Provides a scene-bound handle for component and hierarchy operations.
Definition Entity.h:34
void SetParent(Entity parent)
bool TreeNode(std::string_view label)
bool IsItemClicked(int mouseButton=0)
void Indent(float width=0.0f)
bool BeginPopup(std::string_view str_id, PanelFlags flags=0)
bool InputText(std::string_view label, std::string *str, PanelFlags flags=0)
void Unindent(float width=0.0f)
static bool MenuItem(std::string_view label, std::string_view shortcut="")
void PushItemWidth(float item_width)
void PushID(int int_id)
glm::vec2 GetWindowPadding()
bool IsMouseClicked(int a)
void OpenPopup(std::string_view str_id, PanelPopupFlags popup_flags=0)
glm::vec2 GetContentRegionAvail()
bool Selectable(std::string_view label, bool selected=false)
Owns entities and dispatches script simulation.
Definition Scene.h:36
void DestroyEntity(entt::entity entity)
const entt::registry & GetRegistry() const
Definition Scene.h:82
Entity CreateEntity(const std::string &name=std::string())
Entity GetEntity(entt::entity handle)
static void DrawSceneHierarchyPanel(Scene *scene, entt::entity &selectedEntityHandle)
bool MatchesSearch(const std::string &text, const std::string &query)
Intrusive hierarchy links using EnTT handles; entt::null denotes a missing link.
Definition Entity.h:84