ECS with entt

This commit is contained in:
Erris
2026-02-17 00:50:43 +01:00
parent 86392e0790
commit d4c420d5b4
30 changed files with 1314 additions and 222 deletions

View File

@@ -0,0 +1,150 @@
#include "open_engine/scene/components.hpp"
#include <cstdint>
#include <cstring>
#include <panels/scene_hierarchy.hpp>
#include <imgui.h>
namespace OpenEngine {
SceneHierarchy::SceneHierarchy(const Ref<Scene>& scene)
: scene(scene)
{
}
void SceneHierarchy::SetContext(const Ref<Scene>& context)
{
scene = context;
}
void SceneHierarchy::OnImGuiRender()
{
ImGui::Begin("Scene");
for (auto entity_entt : scene->GetRegistry().view<TagComponent>()) {
Entity entity{ entity_entt, scene.get() };
DrawEntityNode(entity);
}
if ((ImGui::IsMouseDown(0) && ImGui::IsWindowHovered())
|| (ImGui::IsKeyDown(ImGuiKey_Escape) && ImGui::IsWindowHovered()))
{
selected_context = {};
renamed_entity = {};
}
if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems))
{
if (ImGui::MenuItem("Create Empty Entity"))
{
scene->CreateEntity("Empty entity");
}
ImGui::EndPopup();
}
ImGui::End();
ImGui::Begin("Properties");
if (selected_context) {
DrawComponents(selected_context);
if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems))
{
if (ImGui::MenuItem("Add transform"))
{
selected_context.AddComponents<TransformComponent>();
}
if (ImGui::MenuItem("Add Sprite"))
{
selected_context.AddComponents<SpriteRendererComponent>();
}
if (ImGui::MenuItem("Add camera"))
{
selected_context.AddComponents<CameraComponent>();
}
ImGui::EndPopup();
}
}
ImGui::End();
}
void SceneHierarchy::DrawEntityNode(Entity& entity)
{
auto& tag = entity.GetComponents<TagComponent>().tag;
if (renamed_entity == entity && selected_context == entity) {
char buffer[255];
std::memset(buffer, 0, sizeof(buffer));
std::strncpy(buffer, tag.c_str(), sizeof(buffer));
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGui::SetKeyboardFocusHere();
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
if (ImGui::InputText("##tagEditor", buffer, sizeof(buffer), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll)
|| (!ImGui::IsItemActive() && ImGui::IsMouseClicked(0))) {
tag = buffer;
renamed_entity = {};
}
if (ImGui::IsKeyPressed(ImGuiKey_Escape))
renamed_entity = {};
ImGui::PopStyleVar();
} else {
ImGuiTreeNodeFlags flags = ((selected_context == entity) ? ImGuiTreeNodeFlags_Selected : 0) | ImGuiTreeNodeFlags_OpenOnArrow;
flags |= ImGuiTreeNodeFlags_SpanAvailWidth;
bool opened = ImGui::TreeNodeEx((void*)(uint64_t)(uint32_t)entity, flags, "%s", tag.c_str());
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
// FocusViewportOnEntity(entity); // future implementation
renamed_entity = {};
} else if (ImGui::IsItemClicked()) {
double current_time = ImGui::GetTime();
double double_click_time = ImGui::GetIO().MouseDoubleClickTime; // Usually ~0.3s
if (selected_context == entity) {
renamed_entity = entity;
if (current_time - last_selected_time > double_click_time)
{
renamed_entity = entity;
}
} else {
// First time selecting
selected_context = entity;
last_selected_time = current_time;
}
}
if (opened)
ImGui::TreePop();
}
}
void SceneHierarchy::DrawComponents(Entity& entity)
{
if (entity.HasComponent<TagComponent>())
entity.GetComponents<TagComponent>().OnImGuiRender(entity);
DrawComponentDrawer<TransformComponent>(entity, "Transform");
DrawComponentDrawer<SpriteRendererComponent>(entity, "Sprite");
DrawComponentDrawer<CameraComponent>(entity, "Camera");
}
}
/*
if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems))
{
if (ImGui::MenuItem("Create Empty Entity"))
{
// m_Context->CreateEntity("Empty Entity");
}
ImGui::EndPopup();
}
*/