#include "HierarchyPanel.h" #include "Managers/SceneManager.h" #include "Managers/SelectionManager.h" #include #include namespace UI { HierarchyPanel::HierarchyPanel() : Panel("Hierarchy") { SceneManager::Get().CreateDemoScene(); m_selectionHandlerId = SelectionManager::Get().OnSelectionChanged.Subscribe([this](uint64_t) { }); } HierarchyPanel::~HierarchyPanel() { SelectionManager::Get().OnSelectionChanged.Unsubscribe(m_selectionHandlerId); } void HierarchyPanel::Render() { ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None); RenderSearchBar(); ImGui::Separator(); HandleKeyboardShortcuts(); std::string filter = m_searchBuffer; ImGui::BeginChild("EntityList"); for (auto* entity : SceneManager::Get().GetRootEntities()) { RenderEntity(entity, filter); } if (ImGui::IsWindowHovered() && ImGui::IsMouseDown(0) && !ImGui::IsAnyItemHovered()) { if (!m_renaming) { SelectionManager::Get().ClearSelection(); } } if (ImGui::BeginPopupContextWindow("HierarchyContextMenu", ImGuiPopupFlags_MouseButtonRight)) { RenderCreateMenu(nullptr); ImGui::EndPopup(); } ImGui::InvisibleButton("##DragTarget", ImVec2(-1, -1)); if (ImGui::BeginDragDropTarget()) { if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_PTR")) { XCEngine::Components::GameObject* sourceEntity = *(XCEngine::Components::GameObject**)payload->Data; if (sourceEntity && sourceEntity->GetParent() != nullptr) { SceneManager::Get().MoveEntity(sourceEntity->GetID(), 0); } } ImGui::EndDragDropTarget(); } ImGui::EndChild(); ImGui::End(); } void HierarchyPanel::RenderSearchBar() { ImGui::SetNextItemWidth(-1); ImGui::InputTextWithHint("##Search", "Search...", m_searchBuffer, sizeof(m_searchBuffer)); } void HierarchyPanel::RenderEntity(XCEngine::Components::GameObject* entity, const std::string& filter) { if (!entity) return; if (!filter.empty() && !PassesFilter(entity, filter)) { return; } ImGui::PushID(static_cast(entity->GetID())); ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanAvailWidth; if (entity->GetChildCount() == 0) { flags |= ImGuiTreeNodeFlags_Leaf; } if (SelectionManager::Get().IsSelected(entity)) { flags |= ImGuiTreeNodeFlags_Selected; } if (m_renaming && m_renamingEntity == entity) { if (m_renameJustStarted) { ImGui::SetKeyboardFocusHere(); m_renameJustStarted = false; } ImGui::SetNextItemWidth(-1); if (ImGui::InputText("##Rename", m_renameBuffer, sizeof(m_renameBuffer), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll)) { if (strlen(m_renameBuffer) > 0) { SceneManager::Get().RenameEntity(entity->GetID(), m_renameBuffer); } m_renaming = false; m_renamingEntity = nullptr; } if (!ImGui::IsItemActive() && ImGui::IsMouseClicked(0)) { if (strlen(m_renameBuffer) > 0) { SceneManager::Get().RenameEntity(entity->GetID(), m_renameBuffer); } m_renaming = false; m_renamingEntity = nullptr; } } else { bool isOpen = ImGui::TreeNodeEx(entity->GetName().c_str(), flags); if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) { SelectionManager::Get().SetSelectedEntity(entity); } if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) { m_renaming = true; m_renamingEntity = entity; strcpy_s(m_renameBuffer, entity->GetName().c_str()); m_renameJustStarted = true; } HandleDragDrop(entity); if (ImGui::BeginPopupContextItem("EntityContextMenu")) { RenderContextMenu(entity); ImGui::EndPopup(); } if (isOpen) { for (size_t i = 0; i < entity->GetChildCount(); i++) { RenderEntity(entity->GetChild(i), filter); } ImGui::TreePop(); } } ImGui::PopID(); } void HierarchyPanel::RenderContextMenu(XCEngine::Components::GameObject* entity) { auto& sceneManager = SceneManager::Get(); auto& selectionManager = SelectionManager::Get(); if (ImGui::BeginMenu("Create")) { RenderCreateMenu(entity); ImGui::EndMenu(); } ImGui::Separator(); if (ImGui::MenuItem("Rename", "F2")) { if (entity) { m_renaming = true; m_renamingEntity = entity; strcpy_s(m_renameBuffer, entity->GetName().c_str()); m_renameJustStarted = true; } } if (ImGui::MenuItem("Delete", "Delete")) { sceneManager.DeleteEntity(entity->GetID()); } ImGui::Separator(); if (ImGui::MenuItem("Copy", "Ctrl+C")) { sceneManager.CopyEntity(entity->GetID()); } if (ImGui::MenuItem("Paste", "Ctrl+V", false, sceneManager.HasClipboardData())) { sceneManager.PasteEntity(entity->GetID()); } if (ImGui::MenuItem("Duplicate", "Ctrl+D")) { uint64_t newId = sceneManager.DuplicateEntity(entity->GetID()); if (newId != 0) { // Selection handled via callback } } } void HierarchyPanel::RenderCreateMenu(XCEngine::Components::GameObject* parent) { auto& sceneManager = SceneManager::Get(); auto& selectionManager = SelectionManager::Get(); if (ImGui::MenuItem("Empty Object")) { auto* newEntity = sceneManager.CreateEntity("GameObject", parent); selectionManager.SetSelectedEntity(newEntity); } ImGui::Separator(); if (ImGui::MenuItem("Camera")) { auto* newEntity = sceneManager.CreateEntity("Camera", parent); newEntity->AddComponent(); selectionManager.SetSelectedEntity(newEntity); } if (ImGui::MenuItem("Light")) { auto* newEntity = sceneManager.CreateEntity("Light", parent); selectionManager.SetSelectedEntity(newEntity); } ImGui::Separator(); if (ImGui::MenuItem("Cube")) { auto* newEntity = sceneManager.CreateEntity("Cube", parent); newEntity->AddComponent(); selectionManager.SetSelectedEntity(newEntity); } if (ImGui::MenuItem("Sphere")) { auto* newEntity = sceneManager.CreateEntity("Sphere", parent); newEntity->AddComponent(); selectionManager.SetSelectedEntity(newEntity); } if (ImGui::MenuItem("Plane")) { auto* newEntity = sceneManager.CreateEntity("Plane", parent); newEntity->AddComponent(); selectionManager.SetSelectedEntity(newEntity); } } void HierarchyPanel::HandleDragDrop(XCEngine::Components::GameObject* entity) { if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) { m_dragSource = entity; ImGui::SetDragDropPayload("ENTITY_PTR", &entity, sizeof(XCEngine::Components::GameObject*)); ImGui::Text("%s", entity->GetName().c_str()); ImGui::EndDragDropSource(); } if (ImGui::BeginDragDropTarget()) { if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_PTR")) { XCEngine::Components::GameObject* sourceEntity = *(XCEngine::Components::GameObject**)payload->Data; if (sourceEntity != entity && sourceEntity != nullptr) { bool isValidMove = true; XCEngine::Components::GameObject* checkParent = entity; while (checkParent != nullptr) { if (checkParent == sourceEntity) { isValidMove = false; break; } checkParent = checkParent->GetParent(); } if (isValidMove) { sceneManager.MoveEntity(sourceEntity->GetID(), entity->GetID()); } } } ImGui::EndDragDropTarget(); } } void HierarchyPanel::HandleKeyboardShortcuts() { auto& sceneManager = SceneManager::Get(); auto& selectionManager = SelectionManager::Get(); XCEngine::Components::GameObject* selectedEntity = selectionManager.GetSelectedEntity(); if (ImGui::IsWindowFocused()) { if (ImGui::IsKeyPressed(ImGuiKey_Delete)) { if (selectedEntity != nullptr) { sceneManager.DeleteEntity(selectedEntity->GetID()); } } if (ImGui::IsKeyPressed(ImGuiKey_F2)) { if (selectedEntity != nullptr) { m_renaming = true; m_renamingEntity = selectedEntity; strcpy_s(m_renameBuffer, selectedEntity->GetName().c_str()); m_renameJustStarted = true; } } ImGuiIO& io = ImGui::GetIO(); if (io.KeyCtrl) { if (ImGui::IsKeyPressed(ImGuiKey_C)) { if (selectedEntity != nullptr) { sceneManager.CopyEntity(selectedEntity->GetID()); } } if (ImGui::IsKeyPressed(ImGuiKey_V)) { if (sceneManager.HasClipboardData()) { sceneManager.PasteEntity(selectedEntity ? selectedEntity->GetID() : 0); } } if (ImGui::IsKeyPressed(ImGuiKey_D)) { if (selectedEntity != nullptr) { sceneManager.DuplicateEntity(selectedEntity->GetID()); } } } } } bool HierarchyPanel::PassesFilter(XCEngine::Components::GameObject* entity, const std::string& filter) { if (!entity) return false; if (entity->GetName().find(filter) != std::string::npos) { return true; } for (size_t i = 0; i < entity->GetChildCount(); i++) { if (PassesFilter(entity->GetChild(i), filter)) { return true; } } return false; } }