ECS with entt
This commit is contained in:
@@ -9,18 +9,14 @@
|
||||
#include <overlay.hpp>
|
||||
#include <modding.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
EditorApp::EditorApp()
|
||||
: Application("OpenEngine Editor")
|
||||
{
|
||||
OpenEngine::Ref<OpenEngine::Layer> modding_layer = std::make_shared<Modding>();
|
||||
OpenEngine::Ref<ViewLayer> view = OpenEngine::CreateRef<LevelEditor>();
|
||||
OpenEngine::Ref<OpenEngine::Layer> initial_layer = std::make_shared<EditorLayer>(view);
|
||||
OpenEngine::Ref<OpenEngine::Layer> control_layer = std::make_shared<ControlLayer>(initial_layer);
|
||||
OpenEngine::Ref<OpenEngine::Layer> modding_layer = OpenEngine::CreateRef<Modding>();
|
||||
OpenEngine::Ref<OpenEngine::Layer> editor_layer = OpenEngine::CreateRef<OpenEngine::EditorLayer>();
|
||||
OpenEngine::Ref<OpenEngine::Layer> control_layer = OpenEngine::CreateRef<ControlLayer>(editor_layer);
|
||||
|
||||
QueueLayerPush(view);
|
||||
QueueLayerPush(initial_layer);
|
||||
QueueLayerPush(editor_layer);
|
||||
QueueLayerPush(control_layer);
|
||||
QueueLayerPush(modding_layer);
|
||||
}
|
||||
|
||||
150
editor/src/panels/scene_hierarchy.cpp
Normal file
150
editor/src/panels/scene_hierarchy.cpp
Normal 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();
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user