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-27 23:21:43 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
void DrawHierarchyTreePrefix(const XCEngine::Editor::UI::TreeNodePrefixContext& context) {
|
|
|
|
|
if (!context.drawList) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 15:07:19 +08:00
|
|
|
const float width = context.max.x - context.min.x;
|
|
|
|
|
const float height = context.max.y - context.min.y;
|
|
|
|
|
const float iconExtent = XCEngine::Editor::UI::NavigationTreeIconSize();
|
|
|
|
|
const float minX = context.min.x + (width - iconExtent) * 0.5f;
|
|
|
|
|
const float minY = context.min.y + (height - iconExtent) * 0.5f;
|
|
|
|
|
XCEngine::Editor::UI::DrawAssetIcon(
|
|
|
|
|
context.drawList,
|
|
|
|
|
ImVec2(minX, minY),
|
|
|
|
|
ImVec2(minX + iconExtent, minY + iconExtent),
|
|
|
|
|
XCEngine::Editor::UI::AssetIconKind::GameObject);
|
2026-03-27 23:21:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
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-28 15:07:19 +08:00
|
|
|
UI::PanelContentScope content("EntityList", UI::HierarchyPanelContentPadding());
|
2026-03-27 22:05:05 +08:00
|
|
|
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-28 15:07:19 +08:00
|
|
|
UI::ResetTreeLayout();
|
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-28 00:03:20 +08:00
|
|
|
Actions::RequestHierarchyBackgroundContextPopup(m_backgroundContextMenu);
|
|
|
|
|
Actions::DrawHierarchyEntityContextPopup(*m_context, m_itemContextMenu);
|
|
|
|
|
Actions::DrawHierarchyBackgroundContextPopup(*m_context, m_backgroundContextMenu);
|
2026-03-27 22:05:05 +08:00
|
|
|
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 23:21:43 +08:00
|
|
|
UI::TreeNodeDefinition nodeDefinition;
|
|
|
|
|
nodeDefinition.options.selected = m_context->GetSelectionManager().IsSelected(gameObject->GetID());
|
|
|
|
|
nodeDefinition.options.leaf = gameObject->GetChildCount() == 0;
|
|
|
|
|
const std::string persistenceKey = std::to_string(gameObject->GetUUID());
|
|
|
|
|
nodeDefinition.persistenceKey = persistenceKey;
|
2026-03-28 15:07:19 +08:00
|
|
|
nodeDefinition.style = UI::HierarchyTreeStyle();
|
2026-03-27 23:21:43 +08:00
|
|
|
nodeDefinition.prefix.width = UI::NavigationTreePrefixWidth();
|
|
|
|
|
nodeDefinition.prefix.draw = DrawHierarchyTreePrefix;
|
|
|
|
|
nodeDefinition.callbacks.onInteraction = [this, gameObject](const UI::TreeNodeResult& node) {
|
|
|
|
|
if (node.clicked) {
|
|
|
|
|
Actions::HandleHierarchySelectionClick(*m_context, gameObject->GetID(), ImGui::GetIO().KeyCtrl);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 00:03:20 +08:00
|
|
|
if (node.secondaryClicked) {
|
|
|
|
|
Actions::HandleHierarchyItemContextRequest(*m_context, gameObject, m_itemContextMenu);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 23:21:43 +08:00
|
|
|
if (node.doubleClicked) {
|
|
|
|
|
BeginRename(gameObject);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
nodeDefinition.callbacks.onRenderExtras = [this, gameObject]() {
|
|
|
|
|
Actions::BeginHierarchyEntityDrag(gameObject);
|
|
|
|
|
Actions::AcceptHierarchyEntityDrop(*m_context, gameObject);
|
|
|
|
|
};
|
2026-03-27 22:05:05 +08:00
|
|
|
|
|
|
|
|
const UI::TreeNodeResult node = UI::DrawTreeNode(
|
2026-03-27 23:21:43 +08:00
|
|
|
&m_treeState,
|
2026-03-26 21:18:33 +08:00
|
|
|
(void*)gameObject->GetUUID(),
|
|
|
|
|
gameObject->GetName().c_str(),
|
2026-03-27 23:21:43 +08:00
|
|
|
nodeDefinition);
|
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
|
|
|
}
|