Files
OpenEngine/editor/include/panels/scene_hierarchy.hpp

96 lines
3.5 KiB
C++

#ifndef SCENE_HIERARCHY_HPP
#define SCENE_HIERARCHY_HPP
#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 Init(const Ref<Scene>& scene);
void OnImGuiRender();
Entity GetSelectedEntity() const { return selected_context; };
void SetSelectedEntity(Entity entity) { selected_context = entity; };
private:
void DrawEntityNode(Entity& entity);
void DrawComponents(Entity& entity);
/*
template <typename T>
void DrawComponentDrawer(Entity& entity, const char* header)
{
bool component_marked_deletion = false;
ImVec2 region_available = ImGui::GetContentRegionAvail();
ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_DefaultOpen
| ImGuiTreeNodeFlags_Framed
| ImGuiTreeNodeFlags_AllowOverlap;
if (entity.HasComponent<T>()) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 4, 4 });
float line_height = ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f;
bool opened = ImGui::TreeNodeEx((void*)typeid(T).hash_code(), tree_node_flags, "%s", header);
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_marked_deletion = true;
ImGui::EndPopup();
}
if (opened) {
entity.GetComponents<T>().OnImGuiRender(entity);
ImGui::TreePop();
}
ImGui::Separator();
}
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;
};
}
#endif // SCENE_HIERARCHY_HPP