extracting ui render function from the ECS components. Adding dynamic component display

This commit is contained in:
Erris
2026-02-24 15:29:29 +01:00
parent 0bbfe41117
commit 38b92611be
7 changed files with 205 additions and 97 deletions

View File

@@ -1,5 +1,7 @@
#include "open_engine/scene/components.hpp"
#include "open_engine/scene/entity.hpp"
#include "editor_component.hpp"
#include <panels/scene_hierarchy.hpp>
#include <cstring>
@@ -7,13 +9,20 @@
#include <imgui.h>
namespace OpenEngine {
using ComponentDrawer = std::function<void(entt::registry&, entt::entity)>;
SceneHierarchy::SceneHierarchy(const Ref<Scene>& scene)
: scene(scene)
{
}
void SceneHierarchy::SetContext(const Ref<Scene>& context)
void SceneHierarchy::Init(const Ref<Scene>& context)
{
RegisterDrawer<TransformComponent>("Transform", &TransformOnImGuiRender);
RegisterDrawer<SpriteRendererComponent>("Sprite Renderer", &SpriteOnImGuiRender);
RegisterDrawer<CameraComponent>("Camera", &CameraOnImGuiRender);
scene = context;
selected_context = {};
}
@@ -184,11 +193,54 @@ namespace OpenEngine {
void SceneHierarchy::DrawComponents(Entity& entity)
{
if (entity.HasComponent<TagComponent>())
entity.GetComponents<TagComponent>().OnImGuiRender(entity);
auto& reg = scene->GetRegistry();
entt::entity handle = selected_context;
DrawComponentDrawer<TransformComponent>(entity, "Transform");
DrawComponentDrawer<SpriteRendererComponent>(entity, "Sprite");
DrawComponentDrawer<CameraComponent>(entity, "Camera");
if (!selected_context)
return;
entity.GetComponents<TagComponent>();
TagOnImGuiRender(reg, entity);
std::vector<entt::id_type> component_to_delete; // 0 is null/invalid in entt usually
// Iterate through every component type entt knows about
for (auto [id, storage] : reg.storage()) {
if (storage.contains(handle)) {
if (drawers.contains(id)) {
bool component_marked_deletion = false;
ImVec2 region_available = ImGui::GetContentRegionAvail();
ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_DefaultOpen
| ImGuiTreeNodeFlags_Framed
| ImGuiTreeNodeFlags_AllowOverlap;
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 4, 4 });
float line_height = ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f;
bool opened = ImGui::TreeNodeEx((void*)(intptr_t)id, tree_node_flags, "%s", drawers[id].name.c_str());
ImGui::SameLine(region_available.x - line_height * 0.5f);
ImGui::PushStyleColor(ImGuiCol_Button, { 0.290f, 0.301f, 0.388f, 1.0f });
if (ImGui::Button("...", ImVec2{ line_height, line_height }))
ImGui::OpenPopup("component_settings");
ImGui::PopStyleColor();
ImGui::PopStyleVar();
if (ImGui::BeginPopup("component_settings")) {
if (ImGui::MenuItem("Remove component"))
component_to_delete.emplace_back(id);
ImGui::EndPopup();
}
if (opened) {
drawers[id].draw_func(reg, handle);
ImGui::TreePop();
}
}
}
}
for (auto& id : component_to_delete)
drawers[id].remove_func(reg, entity);
}
}