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