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 6, 2024
1 parent de6f2c7 commit d7c69d7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 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
28 changes: 18 additions & 10 deletions tools/tesseratos/samples/main/assets/scenes/main.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,68 @@
"z": 2
}
},
"child1": {
"child": {
"cubos::engine::Position": {
"x": 3,
"y": 3,
"z": 3
},
"cubos::engine::ChildOf@parent": {}
},
"chil2": {
"child1": {
"cubos::engine::Position": {
"x": 4,
"y": 4,
"z": 4
},
"cubos::engine::ChildOf@parent": {}
},
"chil3": {
"child2": {
"cubos::engine::Position": {
"x": 5,
"y": 5,
"z": 5
},
"cubos::engine::ChildOf@parent": {}
},
"parent2": {
"parent1": {
"cubos::engine::Position": {
"x": 6,
"y": 6,
"z": 6
}
},
"child4": {
"child3": {
"cubos::engine::Position": {
"x": 7,
"y": 7,
"z": 7
},
"cubos::engine::ChildOf@parent2": {}
"cubos::engine::ChildOf@parent1": {}
},
"chil5": {
"parent2": {
"cubos::engine::Position": {
"x": 8,
"y": 8,
"z": 8
},
"cubos::engine::ChildOf@parent2": {}
"cubos::engine::ChildOf@parent1": {}
},
"chil6": {
"child4": {
"cubos::engine::Position": {
"x": 9,
"y": 9,
"z": 9
},
"cubos::engine::ChildOf@parent1": {}
},
"child5": {
"cubos::engine::Position": {
"x": 10,
"y": 10,
"z": 10
},
"cubos::engine::ChildOf@parent2": {}
}
}
}
}
73 changes: 54 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,65 @@ 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);

selector.selection == entity ? textColor = (ImVec4)ImColor(149, 252, 75)
: textColor = (ImVec4)ImColor(255, 255, 255);

// check if entity has children
if (children.empty())
{
ImGui::PushStyleColor(ImGuiCol_Text, textColor);
ImGui::Selectable((" " + displayName).c_str(), selector.selection == entity);
ImGui::PopStyleColor();

if (ImGui::IsItemClicked(0))
{
selector.selection = entity;
}

return;
}

ImGui::PushStyleColor(ImGuiCol_Text, textColor);
if (ImGui::TreeNode(displayName.c_str()))
{
ImGui::PopStyleColor();

if (ImGui::IsItemClicked(0))
{
selector.selection = entity;
}
for (auto [child, nameChild, _, parent] : children)
{
show(child, childOf, nameChild, selector);
}
ImGui::TreePop();

return;
}
ImGui::PopStyleColor();
}

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 +84,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 d7c69d7

Please sign in to comment.