Files
XCEngine/editor/src/panels/HierarchyPanel.cpp

314 lines
10 KiB
C++
Raw Normal View History

2026-03-26 23:52:05 +08:00
#include "Actions/HierarchyActionRouter.h"
2026-03-26 22:10:43 +08:00
#include "Actions/ActionRouting.h"
2026-03-26 21:18:33 +08:00
#include "Commands/EntityCommands.h"
#include "HierarchyPanel.h"
#include "Core/IEditorContext.h"
#include "Core/ISceneManager.h"
#include "Core/ISelectionManager.h"
#include "Core/EditorEvents.h"
#include "Core/EventBus.h"
2026-03-26 21:18:33 +08:00
#include "UI/UI.h"
#include <imgui.h>
namespace XCEngine {
namespace Editor {
HierarchyPanel::HierarchyPanel() : Panel("Hierarchy") {
}
void HierarchyPanel::OnAttach() {
if (!m_context || m_selectionHandlerId || m_renameRequestHandlerId) {
return;
}
m_selectionHandlerId = m_context->GetEventBus().Subscribe<SelectionChangedEvent>(
[this](const SelectionChangedEvent& event) {
OnSelectionChanged(event);
}
);
2026-03-26 22:10:43 +08:00
m_renameRequestHandlerId = m_context->GetEventBus().Subscribe<EntityRenameRequestedEvent>(
[this](const EntityRenameRequestedEvent& event) {
OnRenameRequested(event);
}
);
}
void HierarchyPanel::OnDetach() {
if (!m_context) {
return;
}
if (m_selectionHandlerId) {
m_context->GetEventBus().Unsubscribe<SelectionChangedEvent>(m_selectionHandlerId);
m_selectionHandlerId = 0;
}
if (m_renameRequestHandlerId) {
m_context->GetEventBus().Unsubscribe<EntityRenameRequestedEvent>(m_renameRequestHandlerId);
m_renameRequestHandlerId = 0;
}
}
void HierarchyPanel::OnSelectionChanged(const SelectionChangedEvent& event) {
2026-03-26 21:30:46 +08:00
if (m_renameState.IsActive() && event.primarySelection != m_renameState.Item()) {
CancelRename();
}
}
2026-03-26 22:10:43 +08:00
void HierarchyPanel::OnRenameRequested(const EntityRenameRequestedEvent& event) {
if (!m_context || event.entityId == 0) {
return;
}
if (auto* gameObject = m_context->GetSceneManager().GetEntity(event.entityId)) {
BeginRename(gameObject);
}
}
void HierarchyPanel::Render() {
UI::PanelWindowScope panel(m_name.c_str());
if (!panel.IsOpen()) {
return;
}
2026-03-26 22:10:43 +08:00
Actions::ObserveFocusedActionRoute(*m_context, EditorActionRoute::Hierarchy);
RenderSearchBar();
std::string filter = m_searchBuffer;
UI::PanelContentScope content("EntityList");
if (!content.IsOpen()) {
return;
}
auto& sceneManager = m_context->GetSceneManager();
auto rootEntities = sceneManager.GetRootEntities();
SortEntities(const_cast<std::vector<::XCEngine::Components::GameObject*>&>(rootEntities));
for (auto* gameObject : rootEntities) {
RenderEntity(gameObject, filter);
}
if (ImGui::IsWindowHovered() && ImGui::IsMouseDown(0) && !ImGui::IsAnyItemHovered()) {
2026-03-26 21:30:46 +08:00
if (!m_renameState.IsActive()) {
m_context->GetSelectionManager().ClearSelection();
}
}
2026-03-26 21:18:33 +08:00
if (UI::BeginPopupContextWindow("HierarchyContextMenu", ImGuiPopupFlags_MouseButtonRight)) {
2026-03-26 23:52:05 +08:00
Actions::DrawHierarchyCreateActions(*m_context, nullptr);
2026-03-26 21:18:33 +08:00
UI::EndPopup();
}
ImGui::InvisibleButton("##DragTarget", ImVec2(-1, -1));
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_PTR")) {
::XCEngine::Components::GameObject* sourceGameObject = *(::XCEngine::Components::GameObject**)payload->Data;
if (sourceGameObject && sourceGameObject->GetParent() != nullptr) {
2026-03-26 21:18:33 +08:00
Commands::ReparentEntityPreserveWorldTransform(*m_context, sourceGameObject, 0);
}
}
ImGui::EndDragDropTarget();
}
}
void HierarchyPanel::RenderSearchBar() {
2026-03-26 21:18:33 +08:00
UI::PanelToolbarScope toolbar("HierarchyToolbar", UI::StandardPanelToolbarHeight());
if (!toolbar.IsOpen()) {
return;
}
2026-03-26 21:18:33 +08:00
const float buttonWidth = UI::HierarchyOverflowButtonWidth();
UI::ToolbarSearchField(
"##Search",
"Search hierarchy",
m_searchBuffer,
sizeof(m_searchBuffer),
buttonWidth + UI::ToolbarSearchTrailingSpacing());
ImGui::SameLine();
if (UI::ToolbarButton("...", false, ImVec2(buttonWidth, 0.0f))) {
ImGui::OpenPopup("HierarchyOptions");
}
2026-03-26 21:18:33 +08:00
if (UI::BeginPopup("HierarchyOptions")) {
const UI::MenuCommand commands[] = {
UI::MenuCommand::Action("Sort By Name", nullptr, m_sortMode == SortMode::Name),
UI::MenuCommand::Action("Sort By Component Count", nullptr, m_sortMode == SortMode::ComponentCount),
UI::MenuCommand::Action("Transform First", nullptr, m_sortMode == SortMode::TransformFirst)
};
2026-03-26 21:18:33 +08:00
UI::DrawMenuCommands(commands, [&](size_t index) {
switch (index) {
case 0:
m_sortMode = SortMode::Name;
break;
case 1:
m_sortMode = SortMode::ComponentCount;
break;
case 2:
m_sortMode = SortMode::TransformFirst;
break;
default:
break;
}
});
UI::EndPopup();
}
}
void HierarchyPanel::RenderEntity(::XCEngine::Components::GameObject* gameObject, const std::string& filter) {
if (!gameObject) return;
if (!filter.empty() && !PassesFilter(gameObject, filter)) {
return;
}
ImGui::PushID(static_cast<int>(gameObject->GetID()));
2026-03-26 21:18:33 +08:00
2026-03-26 21:30:46 +08:00
if (m_renameState.IsEditing(gameObject->GetID())) {
if (m_renameState.ConsumeFocusRequest()) {
ImGui::SetKeyboardFocusHere();
}
ImGui::SetNextItemWidth(-1);
2026-03-26 21:30:46 +08:00
if (ImGui::InputText(
"##Rename",
m_renameState.Buffer(),
m_renameState.BufferSize(),
ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll)) {
2026-03-26 21:18:33 +08:00
CommitRename();
}
2026-03-26 21:18:33 +08:00
if (ImGui::IsItemActive() && ImGui::IsKeyPressed(ImGuiKey_Escape)) {
CancelRename();
} else if (!ImGui::IsItemActive() && ImGui::IsMouseClicked(0)) {
CommitRename();
}
} else {
2026-03-26 21:18:33 +08:00
const UI::HierarchyNodeResult node = UI::DrawHierarchyNode(
(void*)gameObject->GetUUID(),
gameObject->GetName().c_str(),
m_context->GetSelectionManager().IsSelected(gameObject->GetID()),
gameObject->GetChildCount() == 0);
2026-03-26 21:18:33 +08:00
if (node.clicked) {
ImGuiIO& io = ImGui::GetIO();
if (io.KeyCtrl) {
if (!m_context->GetSelectionManager().IsSelected(gameObject->GetID())) {
m_context->GetSelectionManager().AddToSelection(gameObject->GetID());
}
} else {
m_context->GetSelectionManager().SetSelectedEntity(gameObject->GetID());
}
}
2026-03-26 21:18:33 +08:00
if (node.doubleClicked) {
BeginRename(gameObject);
}
HandleDragDrop(gameObject);
2026-03-26 21:18:33 +08:00
if (UI::BeginPopupContextItem("EntityContextMenu")) {
2026-03-26 23:52:05 +08:00
Actions::DrawHierarchyContextActions(*m_context, gameObject);
2026-03-26 21:18:33 +08:00
UI::EndPopup();
}
2026-03-26 21:18:33 +08:00
if (node.open) {
for (size_t i = 0; i < gameObject->GetChildCount(); i++) {
RenderEntity(gameObject->GetChild(i), filter);
}
2026-03-26 21:18:33 +08:00
UI::EndHierarchyNode();
}
}
ImGui::PopID();
}
2026-03-26 21:18:33 +08:00
void HierarchyPanel::BeginRename(::XCEngine::Components::GameObject* gameObject) {
if (!gameObject) {
CancelRename();
return;
}
2026-03-26 21:18:33 +08:00
2026-03-26 21:30:46 +08:00
m_renameState.Begin(gameObject->GetID(), gameObject->GetName().c_str());
2026-03-26 21:18:33 +08:00
}
void HierarchyPanel::CommitRename() {
2026-03-26 21:30:46 +08:00
if (!m_renameState.IsActive()) {
return;
}
const uint64_t entityId = m_renameState.Item();
if (!m_renameState.Empty() && m_context->GetSceneManager().GetEntity(entityId)) {
Commands::RenameEntity(*m_context, entityId, m_renameState.Buffer());
}
2026-03-26 21:18:33 +08:00
CancelRename();
}
void HierarchyPanel::CancelRename() {
2026-03-26 21:30:46 +08:00
m_renameState.Cancel();
}
void HierarchyPanel::HandleDragDrop(::XCEngine::Components::GameObject* gameObject) {
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
ImGui::SetDragDropPayload("ENTITY_PTR", &gameObject, sizeof(::XCEngine::Components::GameObject*));
ImGui::Text("%s", gameObject->GetName().c_str());
ImGui::EndDragDropSource();
}
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("ENTITY_PTR")) {
::XCEngine::Components::GameObject* sourceGameObject = *(::XCEngine::Components::GameObject**)payload->Data;
2026-03-26 21:18:33 +08:00
if (sourceGameObject != gameObject && Commands::CanReparentEntity(sourceGameObject, gameObject)) {
Commands::ReparentEntityPreserveWorldTransform(*m_context, sourceGameObject, gameObject->GetID());
}
}
ImGui::EndDragDropTarget();
}
}
bool HierarchyPanel::PassesFilter(::XCEngine::Components::GameObject* gameObject, const std::string& filter) {
if (!gameObject) return false;
if (gameObject->GetName().find(filter) != std::string::npos) {
return true;
}
for (size_t i = 0; i < gameObject->GetChildCount(); i++) {
if (PassesFilter(gameObject->GetChild(i), filter)) {
return true;
}
}
return false;
}
void HierarchyPanel::SortEntities(std::vector<::XCEngine::Components::GameObject*>& entities) {
switch (m_sortMode) {
case SortMode::Name:
std::sort(entities.begin(), entities.end(), [](::XCEngine::Components::GameObject* a, ::XCEngine::Components::GameObject* b) {
return a->GetName() < b->GetName();
});
break;
case SortMode::ComponentCount:
std::sort(entities.begin(), entities.end(), [](::XCEngine::Components::GameObject* a, ::XCEngine::Components::GameObject* b) {
return a->GetComponents<::XCEngine::Components::Component>().size() > b->GetComponents<::XCEngine::Components::Component>().size();
});
break;
case SortMode::TransformFirst:
std::sort(entities.begin(), entities.end(), [](::XCEngine::Components::GameObject* a, ::XCEngine::Components::GameObject* b) {
bool aHasTransform = a->GetComponent<::XCEngine::Components::TransformComponent>() != nullptr;
bool bHasTransform = b->GetComponent<::XCEngine::Components::TransformComponent>() != nullptr;
if (aHasTransform != bHasTransform) {
return aHasTransform;
}
return a->GetName() < b->GetName();
});
break;
}
}
}
}