Finality Engine
Loading...
Searching...
No Matches
InspectorPanel.h
Go to the documentation of this file.
1
6#pragma once
7#include <Finality.h>
9#include <entt.hpp>
10#include <vector>
11#include <string>
12#include <algorithm>
13#include <filesystem>
14
15namespace FINALITY
16{
17 static glm::vec3 s_CopiedVector = glm::vec3(0.0f);
18
19 static void DrawVec3Control(FINALITY::ImGUIPanel& panel, std::string_view label, glm::vec3& values, float resetValue = 0.0f)
20 {
21 panel.PushID(label);
22
23 float totalWidth = panel.GetContentRegionAvail().x;
24 float labelWidth = totalWidth * 0.3f;
25 float itemWidth = (totalWidth * 0.7f) / 3.0f;
26
27 panel.Text(label);
28
29 if (panel.BeginPopup("VectorContextMenu"))
30 {
31 if (panel.MenuItem("Reset"))
32 {
33 values = glm::vec3(resetValue);
34 }
35 if (panel.MenuItem("Copy"))
36 {
37 s_CopiedVector = values;
38 }
39 if (panel.MenuItem("Paste"))
40 {
41 values = s_CopiedVector;
42 }
43 panel.EndPopup();
44 }
45
46 if (panel.IsItemClicked(1))
47 {
48 panel.OpenPopup("VectorContextMenu");
49 }
50
51 panel.SameLine(labelWidth);
52
53 panel.PushItemWidth(itemWidth - 4.0f);
54 panel.SliderFloat("X", &values.x, -50.0f, 50.0f);
55 panel.PopItemWidth();
56 panel.SameLine();
57
58 panel.PushItemWidth(itemWidth - 4.0f);
59 panel.SliderFloat("Y", &values.y, -50.0f, 50.0f);
60 panel.PopItemWidth();
61 panel.SameLine();
62
63 panel.PushItemWidth(itemWidth - 4.0f);
64 panel.SliderFloat("Z", &values.z, -50.0f, 50.0f);
65 panel.PopItemWidth();
66
67 panel.PopID();
68 }
69
70 static void DrawFloatControl(FINALITY::ImGUIPanel& panel, std::string_view label, float& value, float minVal, float maxVal)
71 {
72 panel.PushID(label);
73
74 float totalWidth = panel.GetContentRegionAvail().x;
75 float labelWidth = totalWidth * 0.3f;
76 float itemWidth = totalWidth * 0.7f;
77
78 panel.Text(label);
79 panel.SameLine(labelWidth);
80
81 panel.PushItemWidth(itemWidth - 4.0f);
82 panel.SliderFloat("##Value", &value, minVal, maxVal);
83 panel.PopItemWidth();
84
85 panel.PopID();
86 }
87
88 static void DrawComponentHeader(FINALITY::ImGUIPanel& panel, std::string_view title, bool canRemove, bool& shouldRemove, bool& shouldReset)
89 {
90 panel.PushID(title);
91
92 float availWidth = panel.GetContentRegionAvail().x;
93 panel.TextColored({ 0.85f, 0.85f, 0.9f, 1.0f }, title);
94
95 panel.SameLine(availWidth - 24.0f);
96 if (panel.Button("...", glm::vec2(24.0f, 20.0f)))
97 {
98 panel.OpenPopup("ComponentContextMenu");
99 }
100
101 if (panel.IsItemClicked(1))
102 {
103 panel.OpenPopup("ComponentContextMenu");
104 }
105
106 if (panel.BeginPopup("ComponentContextMenu"))
107 {
108 if (panel.MenuItem("Reset"))
109 {
110 shouldReset = true;
111 }
112 if (canRemove && panel.MenuItem("Remove Component"))
113 {
114 shouldRemove = true;
115 }
116 panel.EndPopup();
117 }
118
119 panel.PopID();
120 }
121
122 static void DrawInspectorPanel(const Scene* scene, const entt::entity& selectedEntityHandle)
123 {
124 FINALITY::ImGUIPanel panel("Inspector");
125
126 if (selectedEntityHandle == entt::null || !scene->GetRegistry().valid(selectedEntityHandle))
127 {
128 panel.Text("Select an entity in the Hierarchy to view properties.");
129 return;
130 }
131
132 entt::registry& registry = const_cast<entt::registry&>(scene->GetRegistry());
133
134 if (!registry.all_of<TagComponent>(selectedEntityHandle))
135 {
136 registry.emplace<TagComponent>(selectedEntityHandle, "Entity");
137 }
138 if (!registry.all_of<TransformComponent>(selectedEntityHandle))
139 {
140 registry.emplace<TransformComponent>(selectedEntityHandle);
141 }
142
143 {
144 auto& tagComp = registry.get<TagComponent>(selectedEntityHandle);
145
146 float totalWidth = panel.GetContentRegionAvail().x;
147 panel.Text("Name");
148 panel.SameLine(totalWidth * 0.2f);
149 panel.PushItemWidth(totalWidth * 0.8f - 4.0f);
150 panel.InputText("##TagInput", &tagComp.tag);
151 panel.PopItemWidth();
152
153 panel.TextColored({ 0.5f, 0.5f, 0.5f, 1.0f }, "UUID: " + std::to_string(static_cast<uint64_t>(tagComp.uuid)));
154 panel.Separator();
155 }
156
157 if (registry.all_of<TransformComponent>(selectedEntityHandle))
158 {
159 bool removeTransform = false;
160 bool resetTransform = false;
161 DrawComponentHeader(panel, "Transform", false, removeTransform, resetTransform);
162
163 auto& transform = registry.get<TransformComponent>(selectedEntityHandle);
164 if (resetTransform)
165 {
166 transform.Position = glm::vec3(0.0f);
167 transform.Rotation = glm::vec3(0.0f);
168 transform.Scale = glm::vec3(1.0f);
169 }
170
171 DrawVec3Control(panel, "Position", transform.Position, 0.0f);
172 DrawVec3Control(panel, "Rotation", transform.Rotation, 0.0f);
173 DrawVec3Control(panel, "Scale", transform.Scale, 1.0f);
174 transform.Matrix = transform.GetTransformMatrix();
175
176 panel.Separator();
177 }
178
179 if (registry.all_of<MeshComponent>(selectedEntityHandle))
180 {
181 bool removeMesh = false;
182 bool resetMesh = false;
183 DrawComponentHeader(panel, "Mesh Filter", true, removeMesh, resetMesh);
184
185 if (removeMesh)
186 {
187 registry.remove<MeshComponent>(selectedEntityHandle);
188 }
189 else
190 {
191 auto& mesh = registry.get<MeshComponent>(selectedEntityHandle);
192 if (resetMesh)
193 {
194 mesh.MeshData = nullptr;
195 mesh.MeshIdentifier.clear();
196 }
197
198 float totalWidth = panel.GetContentRegionAvail().x;
199 float labelWidth = totalWidth * 0.3f;
200 float fieldWidth = totalWidth * 0.7f;
201
202 panel.Text("Mesh");
203 panel.SameLine(labelWidth);
204
205 std::string displayLabel = "None (Mesh)";
206 if (mesh.MeshData && !mesh.MeshIdentifier.empty())
207 {
208 displayLabel = mesh.MeshIdentifier;
209 }
210 else if (mesh.MeshData)
211 {
212 displayLabel = "Runtime Mesh";
213 }
214
215 if (panel.Button(displayLabel, glm::vec2(fieldWidth - 4.0f, 22.0f)))
216 {
217 panel.OpenPopup("SelectMeshPopup");
218 }
219
220 if (panel.BeginPopup("SelectMeshPopup", PanelFlags_NoTitleBar | PanelFlags_AlwaysAutoResize))
221 {
222 static std::string meshSearchFilter;
223 if (meshSearchFilter.capacity() < 128)
224 meshSearchFilter.reserve(128);
225
226 panel.PushItemWidth(220.0f);
227 panel.InputText("##MeshSearch", &meshSearchFilter);
228 panel.PopItemWidth();
229 panel.Separator();
230
232 struct MeshOption
233 {
234 std::string Name;
235 std::string Identifier;
236 };
237
238 static const std::vector<MeshOption> builtinMeshes = {
239 { "None", "" },
240 { "Cube", "builtin://cube" }
241 };
242
243 std::string lowerFilter = meshSearchFilter;
244 std::transform(lowerFilter.begin(), lowerFilter.end(), lowerFilter.begin(), ::tolower);
245
246 for (const auto& option : builtinMeshes)
247 {
248 std::string lowerName = option.Name;
249 std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
250
251 if (!lowerFilter.empty() && lowerName.find(lowerFilter) == std::string::npos)
252 continue;
253
254 if (panel.Button(option.Name, glm::vec2(220.0f, 22.0f)))
255 {
256 mesh.MeshIdentifier = option.Identifier;
257 if (option.Identifier.empty())
258 {
259 mesh.MeshData = nullptr;
260 }
261 else
262 {
263 mesh.MeshData = Mesh::Create(option.Identifier);
264 }
265
266 meshSearchFilter.clear();
267 panel.CloseCurrentPopup();
268 }
269 }
270
271 panel.EndPopup();
272 }
273 }
274 panel.Separator();
275 }
276
277 if (registry.all_of<MaterialComponent>(selectedEntityHandle))
278 {
279 bool removeMaterial = false;
280 bool resetMaterial = false;
281 DrawComponentHeader(panel, "Mesh Renderer / Material", true, removeMaterial, resetMaterial);
282
283 if (removeMaterial)
284 {
285 registry.remove<MaterialComponent>(selectedEntityHandle);
286 }
287 else
288 {
289 auto& material = registry.get<MaterialComponent>(selectedEntityHandle);
290 if (resetMaterial)
291 {
292 material.MaterialInstance.reset();
293 material.MaterialIdentifier.clear();
294 }
295
296 auto toProjectPath = [](const std::string& selected) -> std::string
297 {
298 if (selected.empty()) return {};
299 try
300 {
301 namespace fs = std::filesystem;
302 const fs::path abs = fs::absolute(selected).lexically_normal();
303 const fs::path cwd = fs::current_path();
304 const fs::path relative = fs::relative(abs, cwd);
305 if (!relative.empty() && *relative.begin() != "..")
306 return relative.generic_string();
307 return abs.generic_string();
308 }
309 catch (const std::exception&)
310 {
311 return selected;
312 }
313 };
314
315 panel.Text("Material Asset");
316 panel.Text(material.MaterialIdentifier.empty()
317 ? (material.MaterialInstance ? "Runtime-only (not saved)" : "None")
318 : material.MaterialIdentifier);
319
320 if (panel.Button("Load .fmat"))
321 {
322 std::string selected = FileDialogs::OpenFile(
323 "Finality Material (*.fmat)\0*.fmat\0All Files (*.*)\0*.*\0");
324 if (!selected.empty())
325 {
326 const std::string path = toProjectPath(selected);
328 if (asset)
329 {
330 auto instance = asset->CreateInstance();
331 if (instance)
332 {
333 material.MaterialIdentifier = path;
334 material.MaterialInstance = std::move(instance);
335 }
336 }
337 }
338 }
339
340 if (!material.MaterialIdentifier.empty() && material.MaterialInstance)
341 {
343 material.MaterialIdentifier, Renderer::GetDevice());
344 if (asset)
345 {
346 panel.Text("Type: " + asset->GetDefinition().Type);
347 panel.Text("Shader: " + asset->GetDefinition().FragmentShader);
348
349 for (const auto& property : asset->GetDefinition().Properties)
350 {
351 if (property.Type == MaterialParameterType::Float)
352 {
353 float value = 0.0f;
354 if (material.MaterialInstance->TryGetUniform1f(property.Name, value))
355 {
356 if (panel.SliderFloat(property.Name, &value, -10.0f, 10.0f))
357 material.MaterialInstance->SetUniform1f(property.Name, value);
358 }
359 }
360 else if (property.Type == MaterialParameterType::Vec4)
361 {
362 glm::vec4 value;
363 if (material.MaterialInstance->TryGetUniformVec4(property.Name, value))
364 {
365 float rgba[4] = { value.r, value.g, value.b, value.a };
366 if (panel.ColorEdit4(property.Name, rgba))
367 material.MaterialInstance->SetUniformVec4(property.Name,
368 glm::vec4(rgba[0], rgba[1], rgba[2], rgba[3]));
369 }
370 }
371 }
372
373 if (panel.Button("Save As .fmat"))
374 {
375 const std::string defaultName = asset->GetDefinition().Name + ".fmat";
376 std::string selected = FileDialogs::SaveFile(
377 "Finality Material (*.fmat)\0*.fmat\0All Files (*.*)\0*.*\0",
378 defaultName.c_str());
379 if (!selected.empty())
380 {
381 std::filesystem::path saved(selected);
382 if (saved.extension() != ".fmat") saved.replace_extension(".fmat");
383 const auto previous = asset->GetDefinition();
384 asset->UpdateDefaultsFromInstance(*material.MaterialInstance);
385 const bool savedSuccessfully = MaterialSerializer::Serialize(asset->GetDefinition(), saved.string());
386 asset->GetDefinition() = previous;
387 if (savedSuccessfully)
388 {
389 material.MaterialIdentifier = toProjectPath(saved.string());
390 AssetManager::InvalidateAssetPath(material.MaterialIdentifier);
391 }
392 }
393 }
394 }
395 }
396 }
397 panel.Separator();
398 }
399
400 if (registry.all_of<CameraComponent>(selectedEntityHandle))
401 {
402 bool removeCamera = false;
403 bool resetCamera = false;
404 DrawComponentHeader(panel, "Camera", true, removeCamera, resetCamera);
405
406 if (removeCamera)
407 {
408 registry.remove<CameraComponent>(selectedEntityHandle);
409 }
410 else
411 {
412 auto& cameraComp = registry.get<CameraComponent>(selectedEntityHandle);
413 if (resetCamera)
414 {
415 cameraComp.FOV = 45.0f;
416 cameraComp.NearClip = 0.1f;
417 cameraComp.FarClip = 1000.0f;
418 cameraComp.RebuildCamera();
419 }
420
421 bool wasPrimary = cameraComp.IsPrimary;
422 panel.Checkbox("Main Camera (Primary)", &cameraComp.IsPrimary);
423
424 if (cameraComp.IsPrimary && !wasPrimary)
425 {
426 auto camView = registry.view<CameraComponent>();
427 for (auto entity : camView)
428 {
429 if (entity != selectedEntityHandle)
430 {
431 camView.get<CameraComponent>(entity).IsPrimary = false;
432 }
433 }
434 }
435
436 float prevFOV = cameraComp.FOV;
437 float prevNear = cameraComp.NearClip;
438 float prevFar = cameraComp.FarClip;
439
440 DrawFloatControl(panel, "Field of View", cameraComp.FOV, 10.0f, 120.0f);
441 DrawFloatControl(panel, "Clipping Near", cameraComp.NearClip, 0.01f, 10.0f);
442 DrawFloatControl(panel, "Clipping Far", cameraComp.FarClip, 10.0f, 5000.0f);
443
444 if (cameraComp.FOV != prevFOV || cameraComp.NearClip != prevNear || cameraComp.FarClip != prevFar)
445 {
446 cameraComp.RebuildCamera();
447 }
448 }
449 panel.Separator();
450 }
451
452 panel.Spacing();
453
454 float availWidth = panel.GetContentRegionAvail().x;
455 float buttonWidth = 180.0f;
456 panel.SetCursorPosX((availWidth - buttonWidth) * 0.5f);
457
458 if (panel.Button("Add Component", glm::vec2(buttonWidth, 26.0f)))
459 {
460 panel.OpenPopup("AddComponentPopup");
461 }
462
464 {
465 static std::string componentSearchFilter;
466 if (componentSearchFilter.capacity() < 128)
467 componentSearchFilter.reserve(128);
468
469 float popupItemWidth = 220.0f;
470
471 panel.PushItemWidth(popupItemWidth);
472 panel.InputText("##CompSearch", &componentSearchFilter);
473 panel.PopItemWidth();
474 panel.Separator();
475
477 struct ComponentEntry
478 {
479 std::string Name;
480 bool AlreadyAdded;
481 };
482
483 std::vector<ComponentEntry> availableComponents = {
484 { "Mesh Filter", registry.all_of<MeshComponent>(selectedEntityHandle) },
485 { "Material", registry.all_of<MaterialComponent>(selectedEntityHandle) },
486 { "Camera", registry.all_of<CameraComponent>(selectedEntityHandle) }
487 };
488
489 std::string lowerFilter = componentSearchFilter;
490 std::transform(lowerFilter.begin(), lowerFilter.end(), lowerFilter.begin(), ::tolower);
491
492 bool anyShown = false;
493
494 for (const auto& entry : availableComponents)
495 {
496 if (entry.AlreadyAdded)
497 continue;
498
499 std::string lowerName = entry.Name;
500 std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
501
502 if (!lowerFilter.empty() && lowerName.find(lowerFilter) == std::string::npos)
503 continue;
504
505 anyShown = true;
506
507 if (panel.Button(entry.Name, glm::vec2(popupItemWidth, 24.0f)))
508 {
509 if (entry.Name == "Mesh Filter")
510 {
511 registry.emplace<MeshComponent>(selectedEntityHandle);
512 }
513 else if (entry.Name == "Material")
514 {
515 registry.emplace<MaterialComponent>(selectedEntityHandle);
516 }
517 else if (entry.Name == "Camera")
518 {
519 auto camView = registry.view<CameraComponent>();
520 bool hasPrimaryInScene = false;
521 for (auto entity : camView)
522 {
523 if (camView.get<CameraComponent>(entity).IsPrimary)
524 {
525 hasPrimaryInScene = true;
526 break;
527 }
528 }
529
530 auto& newCam = registry.emplace<CameraComponent>(selectedEntityHandle, 45.0f, 1.778f, 0.1f, 1000.0f);
531 newCam.IsPrimary = !hasPrimaryInScene;
532 }
533
534 componentSearchFilter.clear();
535 panel.CloseCurrentPopup();
536 }
537 }
538
539 if (!anyShown)
540 {
541 panel.TextColored({ 0.5f, 0.5f, 0.5f, 1.0f }, "No matching components available.");
542 }
543
544 panel.EndPopup();
545 }
546 }
547}
static std::shared_ptr< T > CreateAsset(const std::string &filePath, Args &&... args)
static void InvalidateAssetPath(const std::string &filePath)
static std::string SaveFile(const char *filter, const char *defaultName)
static std::string OpenFile(const char *filter)
bool IsItemClicked(int mouseButton=0)
bool BeginPopup(std::string_view str_id, PanelFlags flags=0)
bool Checkbox(std::string_view label, bool *v)
bool InputText(std::string_view label, std::string *str, PanelFlags flags=0)
void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f)
bool ColorEdit4(std::string_view label, float col[4])
static bool MenuItem(std::string_view label, std::string_view shortcut="")
void Text(std::string_view text)
bool SliderFloat(std::string_view label, float *v, float v_min, float v_max, std::string_view format="%.3f")
bool Button(std::string_view label, const glm::vec2 &size=glm::vec2(0, 0))
void PushItemWidth(float item_width)
void PushID(int int_id)
void OpenPopup(std::string_view str_id, PanelPopupFlags popup_flags=0)
void TextColored(const glm::vec4 &col, std::string_view text)
glm::vec2 GetContentRegionAvail()
void SetCursorPosX(float x)
static bool Serialize(const MaterialDefinition &definition, const std::string &filePath)
static std::shared_ptr< Mesh > Create(const std::vector< Vertex > &vertices)
static RenderDevice * GetDevice()
Definition Renderer.h:57
Owns entities and dispatches script simulation.
Definition Scene.h:36
const entt::registry & GetRegistry() const
Definition Scene.h:82
@ PanelFlags_NoTitleBar
Definition ImGUIPanel.h:85
@ PanelFlags_AlwaysAutoResize
Definition ImGUIPanel.h:90
@ PanelFlags_NoResize
Definition ImGUIPanel.h:86
static void DrawInspectorPanel(const Scene *scene, const entt::entity &selectedEntityHandle)
static void DrawFloatControl(FINALITY::ImGUIPanel &panel, std::string_view label, float &value, float minVal, float maxVal)
static void DrawVec3Control(FINALITY::ImGUIPanel &panel, std::string_view label, glm::vec3 &values, float resetValue=0.0f)
static glm::vec3 s_CopiedVector
static void DrawComponentHeader(FINALITY::ImGUIPanel &panel, std::string_view title, bool canRemove, bool &shouldRemove, bool &shouldReset)