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

@@ -3,12 +3,6 @@
#include <open_engine.hpp>
#include "open_engine/events/mouse_event.hpp"
#include "open_engine/input/input_system.hpp"
#include "open_engine/input/mouse_codes.hpp"
#include "open_engine/renderer/render_command.hpp"
#include "open_engine/renderer/renderer2d.hpp"
#include "open_engine/scene/components.hpp"
#include "panels/scene_hierarchy.hpp"
#include <glm/ext/matrix_transform.hpp>
@@ -94,7 +88,7 @@ namespace OpenEngine {
}
*/
scene_hierarchy.SetContext(scene);
scene_hierarchy.Init(scene);
}
void OnDetach() override
@@ -364,7 +358,7 @@ namespace OpenEngine {
if (!file.empty()) {
scene = CreateRef<Scene>();
scene->OnViewportResize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
scene_hierarchy.SetContext(scene);
scene_hierarchy.Init(scene);
SceneSerializer serializer(scene);
serializer.Deserialize(file);
@@ -373,7 +367,7 @@ namespace OpenEngine {
if (ImGui::MenuItem("New Scene", "Ctrl+N")) {
scene = CreateRef<Scene>();
scene->OnViewportResize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
scene_hierarchy.SetContext(scene);
scene_hierarchy.Init(scene);
}
ImGui::Separator();
if (ImGui::MenuItem("Exit"))

View File

@@ -0,0 +1,27 @@
#ifndef EDITOR_COMPONENT_HPP
#define EDITOR_COMPONENT_HPP
#include "open_engine/scene/components.hpp"
#include <entt/entity/fwd.hpp>
#include <imgui.h>
namespace OpenEngine {
/*
void DrawVec3Control(const char* label, glm::vec3& values,
float reset_value = 0.0f, float column_width = 100.0f,
const std::array<const char*, 3> labels = {"x", "y", "z"});
*/
void TagOnImGuiRender(entt::registry& registry, entt::entity entity);
void TransformOnImGuiRender(entt::registry& registry, entt::entity entity);
void SpriteOnImGuiRender(entt::registry& registry, entt::entity entity);
void CameraOnImGuiRender(entt::registry& registry, entt::entity entity);
}
#endif // EDITOR_COMPONENT_HPP

View File

@@ -1,17 +1,27 @@
#ifndef SCENE_HIERARCHY_HPP
#define SCENE_HIERARCHY_HPP
#include "imgui.h"
#include <entt/core/fwd.hpp>
#include <entt/entity/fwd.hpp>
#include <open_engine.hpp>
namespace OpenEngine {
using ComponentDrawer = std::function<void(entt::registry&, entt::entity)>;
struct ComponentUI {
std::string name;
std::function<void(entt::registry&, entt::entity)> draw_func;
std::function<void(entt::registry&, entt::entity)> remove_func;
};
class SceneHierarchy
{
public:
SceneHierarchy() = default;
SceneHierarchy(const Ref<Scene>& scene);
void SetContext(const Ref<Scene>& scene);
void Init(const Ref<Scene>& scene);
void OnImGuiRender();
@@ -22,6 +32,7 @@ namespace OpenEngine {
void DrawEntityNode(Entity& entity);
void DrawComponents(Entity& entity);
/*
template <typename T>
void DrawComponentDrawer(Entity& entity, const char* header)
{
@@ -62,8 +73,19 @@ namespace OpenEngine {
if (component_marked_deletion)
entity.RemoveComponents<T>();
}
*/
private:
template<typename T>
static void RegisterDrawer(const std::string& name, std::function<void(entt::registry&, entt::entity)> func) {
drawers[entt::type_id<T>().hash()] = {
name,
func,
[](entt::registry& reg, entt::entity ent) { reg.remove<T>(ent); }
};
}
inline static std::map<entt::id_type, ComponentUI> drawers;
Ref<Scene> scene;
Entity selected_context, renamed_entity;
double last_selected_time = 10.0;