Skip to content

Commit

Permalink
feat(tesseratos): show childOf hierarchy on world inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomsmiranda committed Mar 3, 2024
1 parent de6f2c7 commit 7f2d738
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
69 changes: 50 additions & 19 deletions tools/tesseratos/src/tesseratos/world_inspector/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cubos/core/ecs/query/opt.hpp>

#include <cubos/engine/imgui/plugin.hpp>
#include <cubos/engine/transform/child_of.hpp>

#include <tesseratos/entity_selector/plugin.hpp>
#include <tesseratos/toolbox/plugin.hpp>
Expand All @@ -14,17 +15,61 @@ 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<Entity, Opt<const Name&>, const ChildOf&, Entity> childOf, Opt<const Name&> name,
tesseratos::EntitySelector& selector)
{
auto displayName = name.contains() ? name->value : std::to_string(entity.index);
auto children = childOf.pin(1, entity);
auto textColor = (ImVec4)ImColor(255, 255, 255);

// check if entity has children
if (children.empty())
{
selector.selection == entity ? textColor = (ImVec4)ImColor(149, 252, 75)
: textColor = (ImVec4)ImColor(255, 255, 255);

ImGui::PushStyleColor(ImGuiCol_Text, textColor);

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);
cubos.addPlugin(toolboxPlugin);
cubos.addPlugin(entitySelectorPlugin);
cubos.system("show World Inspector UI")
.tagged("cubos.imgui")
.call([](World& world, Query<Entity, Opt<const Name&>> query) {
.call([](World& world, Query<Entity, Opt<const Name&>> all,
Query<Entity, Opt<const Name&>, const ChildOf&, Entity> query) {
if (!(world.write<Toolbox>().get().isOpen("World Inspector")))
{
return;
Expand All @@ -35,28 +80,14 @@ 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"))
if (!query.pin(0, entity).empty())
{
selector.get().selection = entity;
continue; // Skip all entities with parents
}

n++;
ImGui::PopID();
show(entity, query, name, selector.get());
}
}
ImGui::End();
Expand Down

0 comments on commit 7f2d738

Please sign in to comment.