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

215 lines
7.0 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 {
void DrawHierarchyTreePrefix(const XCEngine::Editor::UI::TreeNodePrefixContext& context) {
if (!context.drawList) {
return;
}
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);
}
} // namespace
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() {
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-27 22:05:05 +08:00
Actions::ObserveFocusedActionRoute(*m_context, EditorActionRoute::Hierarchy);
UI::PanelContentScope content("EntityList", UI::HierarchyPanelContentPadding());
2026-03-27 22:05:05 +08:00
if (!content.IsOpen()) {
ImGui::PopStyleColor(2);
return;
}
2026-03-27 22:05:05 +08:00
auto& sceneManager = m_context->GetSceneManager();
auto rootEntities = sceneManager.GetRootEntities();
UI::ResetTreeLayout();
2026-03-27 22:05:05 +08:00
for (auto* gameObject : rootEntities) {
RenderEntity(gameObject);
}
Actions::DrawHierarchyBackgroundInteraction(*m_context, m_renameState);
Actions::DrawHierarchyEntityContextPopup(*m_context, m_itemContextMenu);
static bool s_backgroundContextOpen = false;
if (UI::BeginContextMenuForLastItem("##HierarchyBackgroundContext")) {
if (!s_backgroundContextOpen) {
Actions::TraceHierarchyPopup("Hierarchy background popup opened via background surface");
s_backgroundContextOpen = true;
}
Actions::DrawHierarchyCreateActions(*m_context, nullptr);
UI::EndContextMenu();
} else if (s_backgroundContextOpen) {
Actions::TraceHierarchyPopup("Hierarchy background popup closed");
s_backgroundContextOpen = false;
}
}
2026-03-27 22:05:05 +08:00
ImGui::PopStyleColor(2);
}
2026-03-27 22:05:05 +08:00
void HierarchyPanel::RenderEntity(::XCEngine::Components::GameObject* gameObject) {
if (!gameObject) return;
2026-03-27 22:05:05 +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())) {
const UI::InlineRenameFieldResult renameField = UI::DrawInlineRenameField(
"##Rename",
m_renameState.Buffer(),
m_renameState.BufferSize(),
-1.0f,
m_renameState.ConsumeFocusRequest());
if (renameField.cancelRequested) {
2026-03-26 21:18:33 +08:00
CancelRename();
} else if (renameField.submitted || renameField.deactivated) {
2026-03-26 21:18:33 +08:00
CommitRename();
}
} else {
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;
nodeDefinition.style = UI::HierarchyTreeStyle();
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);
}
if (node.secondaryClicked) {
Actions::HandleHierarchyItemContextRequest(*m_context, gameObject, m_itemContextMenu);
}
};
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(
&m_treeState,
2026-03-26 21:18:33 +08:00
(void*)gameObject->GetUUID(),
gameObject->GetName().c_str(),
nodeDefinition);
2026-03-26 21:18:33 +08:00
if (node.open) {
for (size_t i = 0; i < gameObject->GetChildCount(); i++) {
2026-03-27 22:05:05 +08:00
RenderEntity(gameObject->GetChild(i));
}
2026-03-27 22:05:05 +08:00
UI::EndTreeNode();
}
}
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();
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();
}
}
}