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"
|
2026-03-20 17:08:06 +08:00
|
|
|
#include "HierarchyPanel.h"
|
2026-03-25 15:51:27 +08:00
|
|
|
#include "Core/IEditorContext.h"
|
2026-03-25 16:39:15 +08:00
|
|
|
#include "Core/ISceneManager.h"
|
2026-03-25 16:41:21 +08:00
|
|
|
#include "Core/ISelectionManager.h"
|
|
|
|
|
#include "Core/EditorEvents.h"
|
|
|
|
|
#include "Core/EventBus.h"
|
2026-03-26 21:18:33 +08:00
|
|
|
#include "UI/UI.h"
|
2026-03-20 17:08:06 +08:00
|
|
|
#include <imgui.h>
|
|
|
|
|
|
2026-03-24 20:02:38 +08:00
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Editor {
|
2026-03-20 17:08:06 +08:00
|
|
|
|
|
|
|
|
HierarchyPanel::HierarchyPanel() : Panel("Hierarchy") {
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 00:08:46 +08:00
|
|
|
void HierarchyPanel::OnAttach() {
|
|
|
|
|
if (!m_context || m_selectionHandlerId || m_renameRequestHandlerId) {
|
|
|
|
|
return;
|
2026-03-25 16:41:21 +08:00
|
|
|
}
|
2026-03-25 15:51:27 +08:00
|
|
|
|
2026-03-25 16:41:21 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-03-25 16:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 00:08:46 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 16:41:21 +08:00
|
|
|
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-20 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 17:08:06 +08:00
|
|
|
void HierarchyPanel::Render() {
|
2026-03-27 22:05:05 +08:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_WindowBg, UI::HierarchyInspectorPanelBackgroundColor());
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_ChildBg, UI::HierarchyInspectorPanelBackgroundColor());
|
|
|
|
|
{
|
|
|
|
|
UI::PanelWindowScope panel(m_name.c_str());
|
|
|
|
|
if (!panel.IsOpen()) {
|
|
|
|
|
ImGui::PopStyleColor(2);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-26 16:43:06 +08:00
|
|
|
|
2026-03-27 22:05:05 +08:00
|
|
|
Actions::ObserveFocusedActionRoute(*m_context, EditorActionRoute::Hierarchy);
|
2026-03-26 16:43:06 +08:00
|
|
|
|
2026-03-27 22:05:05 +08:00
|
|
|
UI::PanelContentScope content("EntityList");
|
|
|
|
|
if (!content.IsOpen()) {
|
|
|
|
|
ImGui::PopStyleColor(2);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-27 12:06:24 +08:00
|
|
|
|
2026-03-27 22:05:05 +08:00
|
|
|
auto& sceneManager = m_context->GetSceneManager();
|
|
|
|
|
auto rootEntities = sceneManager.GetRootEntities();
|
2026-03-27 12:06:24 +08:00
|
|
|
|
2026-03-27 22:05:05 +08:00
|
|
|
for (auto* gameObject : rootEntities) {
|
|
|
|
|
RenderEntity(gameObject);
|
|
|
|
|
}
|
2026-03-20 17:08:06 +08:00
|
|
|
|
2026-03-27 22:05:05 +08:00
|
|
|
Actions::HandleHierarchyBackgroundPrimaryClick(*m_context, m_renameState);
|
2026-03-26 16:43:06 +08:00
|
|
|
|
2026-03-27 22:05:05 +08:00
|
|
|
Actions::DrawHierarchyBackgroundContextPopup(*m_context);
|
|
|
|
|
Actions::DrawHierarchyRootDropTarget(*m_context);
|
2026-03-26 16:43:06 +08:00
|
|
|
}
|
2026-03-27 22:05:05 +08:00
|
|
|
ImGui::PopStyleColor(2);
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 22:05:05 +08:00
|
|
|
void HierarchyPanel::RenderEntity(::XCEngine::Components::GameObject* gameObject) {
|
2026-03-25 01:23:08 +08:00
|
|
|
if (!gameObject) return;
|
2026-03-27 22:05:05 +08:00
|
|
|
|
2026-03-25 01:23:08 +08:00
|
|
|
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()) {
|
2026-03-20 17:08:06 +08:00
|
|
|
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-20 17:08:06 +08:00
|
|
|
}
|
2026-03-26 21:18:33 +08:00
|
|
|
|
|
|
|
|
if (ImGui::IsItemActive() && ImGui::IsKeyPressed(ImGuiKey_Escape)) {
|
|
|
|
|
CancelRename();
|
|
|
|
|
} else if (!ImGui::IsItemActive() && ImGui::IsMouseClicked(0)) {
|
|
|
|
|
CommitRename();
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-03-27 22:05:05 +08:00
|
|
|
UI::TreeNodeOptions nodeOptions;
|
|
|
|
|
nodeOptions.selected = m_context->GetSelectionManager().IsSelected(gameObject->GetID());
|
|
|
|
|
nodeOptions.leaf = gameObject->GetChildCount() == 0;
|
|
|
|
|
|
|
|
|
|
const UI::TreeNodeResult node = UI::DrawTreeNode(
|
2026-03-26 21:18:33 +08:00
|
|
|
(void*)gameObject->GetUUID(),
|
|
|
|
|
gameObject->GetName().c_str(),
|
2026-03-27 22:05:05 +08:00
|
|
|
nodeOptions);
|
2026-03-20 17:08:06 +08:00
|
|
|
|
2026-03-26 21:18:33 +08:00
|
|
|
if (node.clicked) {
|
2026-03-27 00:30:11 +08:00
|
|
|
Actions::HandleHierarchySelectionClick(*m_context, gameObject->GetID(), ImGui::GetIO().KeyCtrl);
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 21:18:33 +08:00
|
|
|
if (node.doubleClicked) {
|
|
|
|
|
BeginRename(gameObject);
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 00:30:11 +08:00
|
|
|
Actions::BeginHierarchyEntityDrag(gameObject);
|
|
|
|
|
Actions::AcceptHierarchyEntityDrop(*m_context, gameObject);
|
2026-03-27 12:06:24 +08:00
|
|
|
|
|
|
|
|
Actions::DrawHierarchyEntityContextPopup(*m_context, gameObject);
|
2026-03-20 17:08:06 +08:00
|
|
|
|
2026-03-26 21:18:33 +08:00
|
|
|
if (node.open) {
|
2026-03-25 01:23:08 +08:00
|
|
|
for (size_t i = 0; i < gameObject->GetChildCount(); i++) {
|
2026-03-27 22:05:05 +08:00
|
|
|
RenderEntity(gameObject->GetChild(i));
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
2026-03-27 22:05:05 +08:00
|
|
|
UI::EndTreeNode();
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 21:18:33 +08:00
|
|
|
void HierarchyPanel::BeginRename(::XCEngine::Components::GameObject* gameObject) {
|
|
|
|
|
if (!gameObject) {
|
|
|
|
|
CancelRename();
|
|
|
|
|
return;
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
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();
|
2026-03-27 12:06:24 +08:00
|
|
|
Actions::CommitEntityRename(*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();
|
2026-03-20 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-24 18:38:01 +08:00
|
|
|
}
|
2026-03-24 20:02:38 +08:00
|
|
|
}
|