Skip to content

Commit

Permalink
feat(tesseratos): add/remove components in Entity Inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Feb 11, 2024
1 parent 54c99a0 commit 79ffd0e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Metrics panel plugin, which only shows FPS statistics for now (#275).
- Parent-child hierarchies to the transform plugin, at last (#334).
- New trait type for handling bit masks.
- Buttons to add/remove components through the entity inspector (#906).

### Changed

Expand Down
79 changes: 52 additions & 27 deletions tools/tesseratos/src/tesseratos/entity_inspector/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using cubos::core::ecs::Entity;
using cubos::core::ecs::World;
using cubos::core::memory::AnyValue;
using cubos::core::reflection::reflect;
using cubos::core::reflection::Type;

Expand All @@ -24,39 +25,63 @@ void tesseratos::entityInspectorPlugin(Cubos& cubos)
cubos.addPlugin(entitySelectorPlugin);
cubos.addPlugin(toolboxPlugin);

cubos.system("show Entity Inspector UI").tagged("cubos.imgui").call([](World& world) {
if (!world.write<Toolbox>().get().isOpen("Entity Inspector"))
{
return;
}

ImGui::Begin("Entity Inspector");
if (!ImGui::IsWindowCollapsed())
{
auto selection = world.read<EntitySelector>().get().selection;
cubos.system("show Entity Inspector UI")
.tagged("cubos.imgui")
.call([](World& world, Toolbox& toolbox, const EntitySelector& entitySelector, DataInspector& dataInspector) {
if (!toolbox.isOpen("Entity Inspector"))
{
return;
}

if (!selection.isNull() && world.isAlive(selection))
ImGui::Begin("Entity Inspector");
if (!ImGui::IsWindowCollapsed())
{
ImGui::Text("Entity %d selected", selection.index);
ImGui::BeginTable("showEntity", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable);
auto dataInspector = world.write<DataInspector>();
if (!entitySelector.selection.isNull() && world.isAlive(entitySelector.selection))
{
ImGui::Text("Entity %d selected", entitySelector.selection.index);
ImGui::Separator();

ImGui::TableNextRow();
if (ImGui::Button("Add Component"))
{
ImGui::OpenPopup("Select Component Type");
}

for (auto [type, value] : world.components(selection))
{
if (dataInspector.get().edit(type->name(), *type, value))
if (ImGui::BeginPopup("Select Component Type"))
{
for (auto [type, name] : world.types().components())
{
if (ImGui::Button(name.c_str()))
{
auto value = AnyValue::defaultConstruct(*type);
world.components(entitySelector.selection).add(value.type(), value.get());
ImGui::CloseCurrentPopup();
}
}

ImGui::EndPopup();
}

const Type* removed = nullptr;
for (auto [type, value] : world.components(entitySelector.selection))
{
ImGui::SeparatorText(type->name().c_str());
dataInspector.edit(*type, value);
if (ImGui::Button("Remove Component"))
{
removed = type;
}
}

if (removed != nullptr)
{
// ...
world.components(entitySelector.selection).remove(*removed);
}
}
ImGui::EndTable();
}
else
{
ImGui::Text("No entity selected");
else
{
ImGui::Text("No entity selected");
}
}
}
ImGui::End();
});
ImGui::End();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ void tesseratos::settingsInspectorPlugin(Cubos& cubos)
}
else
{
ImGui::BeginTable("split", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable);
inspector.edit("Settings", map);
ImGui::EndTable();
inspector.edit(map);
}
}
ImGui::End();
Expand Down

0 comments on commit 79ffd0e

Please sign in to comment.