From 8d458f2633ba6d0de5ffb2816c2ea828d644c53c Mon Sep 17 00:00:00 2001 From: Diogo Miranda Date: Sun, 3 Mar 2024 16:49:42 +0000 Subject: [PATCH] feat(tesseratos): show childOf hierarchy on world inspector --- CHANGELOG.md | 1 + .../src/tesseratos/world_inspector/plugin.cpp | 74 +++++++++++++------ 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de27345376..c7676ef2a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New feature guide focused on queries (#995, **@RiscadoA**). - ECS Statistics tesseratos plugin (#1024, **@RiscadoA**). - Global position, rotation and scale getters (#1002, **@DiogoMendonc-a**). +- Show childOf hierarchy on the world inspector (#991, **@diogomsmiranda**). ### Fixed diff --git a/tools/tesseratos/src/tesseratos/world_inspector/plugin.cpp b/tools/tesseratos/src/tesseratos/world_inspector/plugin.cpp index c8c76e9070..d5a2971082 100644 --- a/tools/tesseratos/src/tesseratos/world_inspector/plugin.cpp +++ b/tools/tesseratos/src/tesseratos/world_inspector/plugin.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -14,9 +15,54 @@ using cubos::core::ecs::Name; using cubos::core::ecs::Opt; using cubos::core::ecs::Query; using cubos::core::ecs::World; +using cubos::core::ecs::WriteResource; +using cubos::engine::ChildOf; using cubos::engine::Cubos; +static void show(Entity entity, Query, const ChildOf&, Entity> childOf, Opt name, + tesseratos::EntitySelector& selector) +{ + auto displayName = name.contains() ? name->value : std::to_string(entity.index); + auto children = childOf.pin(1, entity); + auto bg_color = (ImVec4)ImColor(0, 0, 0); + auto text_color = (ImVec4)ImColor(255, 255, 255); + + // check if entity has children + if (children.empty()) + { + selector.selection == entity ? text_color = (ImVec4)ImColor(149, 252, 75) + : text_color = (ImVec4)ImColor(255, 255, 255); + + ImGui::PushStyleColor(ImGuiCol_Text, text_color); + ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, bg_color); + + ImGui::BulletText("%s", displayName.c_str()); + ImGui::PopStyleColor(2); + + if (ImGui::IsItemClicked(0)) + { + selector.selection = entity; + } + + return; + } + + if (ImGui::TreeNode(displayName.c_str())) + { + if (ImGui::IsItemClicked(0)) + { + selector.selection = entity; + } + + for (auto [child, nameChild, _, parent] : children) + { + show(child, childOf, nameChild, selector); + } + ImGui::TreePop(); + } +} + void tesseratos::worldInspectorPlugin(Cubos& cubos) { cubos.addPlugin(cubos::engine::imguiPlugin); @@ -24,7 +70,8 @@ void tesseratos::worldInspectorPlugin(Cubos& cubos) cubos.addPlugin(entitySelectorPlugin); cubos.system("show World Inspector UI") .tagged("cubos.imgui") - .call([](World& world, Query> query) { + .call([](World& world, Query> all, + Query, const ChildOf&, Entity> query) { if (!(world.write().get().isOpen("World Inspector"))) { return; @@ -35,28 +82,11 @@ void tesseratos::worldInspectorPlugin(Cubos& cubos) ImGui::Begin("World Inspector"); if (!ImGui::IsWindowCollapsed()) { - int n = 0; - for (auto [entity, name] : query) + for (auto [entity, name] : all) { - ImGui::PushID(n); - - if (name.contains()) - { - ImGui::BulletText("%s", name->value.c_str()); - } - else - { - ImGui::BulletText("%s", std::to_string(entity.index).c_str()); - } - - ImGui::SameLine(); - if (ImGui::Button("Select")) - { - selector.get().selection = entity; - } - - n++; - ImGui::PopID(); + if (query.pin(0, entity).first().contains()) + continue; // Skip all entities with parents + show(entity, query, name, selector.get()); } } ImGui::End();