From b08f682e5c3f7a541494dd6404e436468c232fe5 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Wed, 25 Mar 2026 15:54:22 +0800 Subject: [PATCH] Migrate InspectorPanel to use IEditorContext and EventBus - Replace SelectionManager::Get() singleton with m_context->GetSelectionManager() - Use EventBus for selection change events instead of direct subscription - Store entity ID (uint64_t) instead of GameObject* pointer - Resolve entity ID to GameObject* via SceneManager::GetEntity() - Remove direct includes of SelectionManager.h --- Editor重构计划.md | 6 +++++ editor/src/panels/InspectorPanel.cpp | 33 +++++++++++++++++++++------- editor/src/panels/InspectorPanel.h | 4 ++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Editor重构计划.md b/Editor重构计划.md index 9b4d70b6..5801c4f9 100644 --- a/Editor重构计划.md +++ b/Editor重构计划.md @@ -474,6 +474,12 @@ private: - `editor/src/Core/IEditorContext.h` - 编辑器上下文接口 - `editor/src/Core/EditorContextImpl.h` - 编辑器上下文实现 +**2026-03-25 更新**: +- `editor/src/panels/HierarchyPanel.h/cpp` - 已迁移至 IEditorContext +- `editor/src/panels/InspectorPanel.h/cpp` - 已迁移至 IEditorContext + EventBus +- 移除了所有 Panel 中的 `SelectionManager::Get()` 和 `EditorSceneManager::Get()` 单例调用 +- SceneManager::RenameEntity 实现已添加 + ### 阶段二:核心功能(第 3-4 周) | 任务 | 依赖 | 状态 | diff --git a/editor/src/panels/InspectorPanel.cpp b/editor/src/panels/InspectorPanel.cpp index 168c5d75..04881e82 100644 --- a/editor/src/panels/InspectorPanel.cpp +++ b/editor/src/panels/InspectorPanel.cpp @@ -1,6 +1,6 @@ #include "InspectorPanel.h" +#include "Core/EditorContextImpl.h" #include "Managers/SceneManager.h" -#include "Managers/SelectionManager.h" #include "UI/UI.h" #include #include @@ -11,23 +11,40 @@ namespace Editor { InspectorPanel::InspectorPanel() : Panel("Inspector") { Debug::Logger::Get().Debug(Debug::LogCategory::General, "InspectorPanel constructed"); - m_selectionHandlerId = SelectionManager::Get().OnSelectionChanged.Subscribe([this](uint64_t) { - m_selectedGameObject = SelectionManager::Get().GetSelectedEntity(); - }); } InspectorPanel::~InspectorPanel() { - SelectionManager::Get().OnSelectionChanged.Unsubscribe(m_selectionHandlerId); + if (m_context) { + m_context->GetEventBus().Unsubscribe(m_selectionHandlerId); + } +} + +void InspectorPanel::OnSelectionChanged(const SelectionChangedEvent& event) { + m_selectedEntityId = event.primarySelection; } void InspectorPanel::Render() { Debug::Logger::Get().Debug(Debug::LogCategory::General, "InspectorPanel::Render START"); ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None); - m_selectedGameObject = SelectionManager::Get().GetSelectedEntity(); + if (!m_selectionHandlerId && m_context) { + m_selectionHandlerId = m_context->GetEventBus().Subscribe( + [this](const SelectionChangedEvent& event) { + OnSelectionChanged(event); + } + ); + } - if (m_selectedGameObject) { - RenderGameObject(m_selectedGameObject); + m_selectedEntityId = m_context->GetSelectionManager().GetSelectedEntity(); + + if (m_selectedEntityId) { + auto* sceneManager = static_cast(m_context->GetSceneManager()); + auto* gameObject = sceneManager->GetEntity(m_selectedEntityId); + if (gameObject) { + RenderGameObject(gameObject); + } else { + ImGui::Text("Object not found"); + } } else { ImGui::Text("No object selected"); ImGui::TextColored(ImVec4(0.5f, 0.5f, 0.5f, 1.0f), "Select an object in Hierarchy"); diff --git a/editor/src/panels/InspectorPanel.h b/editor/src/panels/InspectorPanel.h index 9921f6bb..e579ff81 100644 --- a/editor/src/panels/InspectorPanel.h +++ b/editor/src/panels/InspectorPanel.h @@ -1,7 +1,6 @@ #pragma once #include "Panel.h" -#include #include namespace XCEngine { @@ -19,9 +18,10 @@ private: void RenderAddComponentPopup(::XCEngine::Components::GameObject* gameObject); void RenderComponent(::XCEngine::Components::Component* component, ::XCEngine::Components::GameObject* gameObject); void RemoveComponentByType(::XCEngine::Components::Component* component, ::XCEngine::Components::GameObject* gameObject); + void OnSelectionChanged(const struct SelectionChangedEvent& event); uint64_t m_selectionHandlerId = 0; - ::XCEngine::Components::GameObject* m_selectedGameObject = nullptr; + uint64_t m_selectedEntityId = 0; }; }