refactor(editor): Complete architecture refactoring

- SceneManager: remove singleton, use dependency injection via EditorContext
- SelectionManager: already interface-based via ISelectionManager
- Panel: now receives IEditorContext for accessing managers
- HierarchyPanel: migrated to use IEditorContext instead of singletons
- Add ISceneManager interface and SceneManagerImpl
- EditorContextImpl: holds all editor subsystems

Architecture now follows dependency injection pattern:
Application -> EditorContext -> SceneManager/SelectionManager
EditorLayer -> Panels (receive context via SetContext)

All Manager singletons removed: EditorSceneManager::Get(), SelectionManager::Get()
This commit is contained in:
2026-03-25 15:51:27 +08:00
parent 56ec2e9b85
commit 008fb98dee
18 changed files with 1491 additions and 181 deletions

View File

@@ -5,7 +5,13 @@
namespace XCEngine {
namespace Editor {
::XCEngine::Components::GameObject* EditorSceneManager::CreateEntity(const std::string& name, ::XCEngine::Components::GameObject* parent) {
SceneManager::SceneManager() = default;
void SceneManager::SetSelectionManager(ISelectionManager* selectionManager) {
m_selectionManager = selectionManager;
}
::XCEngine::Components::GameObject* SceneManager::CreateEntity(const std::string& name, ::XCEngine::Components::GameObject* parent) {
if (!m_scene) {
m_scene = new ::XCEngine::Components::Scene("EditorScene");
}
@@ -20,7 +26,7 @@ namespace Editor {
return entity;
}
void EditorSceneManager::DeleteEntity(::XCEngine::Components::GameObject::ID id) {
void SceneManager::DeleteEntity(::XCEngine::Components::GameObject::ID id) {
if (!m_scene) return;
::XCEngine::Components::GameObject* entity = m_scene->Find(std::to_string(id));
@@ -35,15 +41,15 @@ void EditorSceneManager::DeleteEntity(::XCEngine::Components::GameObject::ID id)
m_rootEntities.erase(std::remove(m_rootEntities.begin(), m_rootEntities.end(), entity), m_rootEntities.end());
}
if (SelectionManager::Get().GetSelectedEntity() == entity) {
SelectionManager::Get().ClearSelection();
if (m_selectionManager && m_selectionManager->GetSelectedEntity() == entity->GetID()) {
m_selectionManager->ClearSelection();
}
m_scene->DestroyGameObject(entity);
OnEntityDeleted.Invoke(id);
}
EditorSceneManager::ClipboardData EditorSceneManager::CopyEntityRecursive(const ::XCEngine::Components::GameObject* entity) {
SceneManager::ClipboardData SceneManager::CopyEntityRecursive(const ::XCEngine::Components::GameObject* entity) {
ClipboardData data;
data.name = entity->GetName();
@@ -60,7 +66,7 @@ EditorSceneManager::ClipboardData EditorSceneManager::CopyEntityRecursive(const
return data;
}
void EditorSceneManager::CopyEntity(::XCEngine::Components::GameObject::ID id) {
void SceneManager::CopyEntity(::XCEngine::Components::GameObject::ID id) {
if (!m_scene) return;
::XCEngine::Components::GameObject* entity = m_scene->Find(std::to_string(id));
@@ -69,7 +75,7 @@ void EditorSceneManager::CopyEntity(::XCEngine::Components::GameObject::ID id) {
m_clipboard = CopyEntityRecursive(entity);
}
::XCEngine::Components::GameObject::ID EditorSceneManager::PasteEntityRecursive(const ClipboardData& data, ::XCEngine::Components::GameObject::ID parent) {
::XCEngine::Components::GameObject::ID SceneManager::PasteEntityRecursive(const ClipboardData& data, ::XCEngine::Components::GameObject::ID parent) {
::XCEngine::Components::GameObject* parentObj = nullptr;
if (parent != 0) {
parentObj = m_scene->Find(std::to_string(parent));
@@ -94,12 +100,12 @@ void EditorSceneManager::CopyEntity(::XCEngine::Components::GameObject::ID id) {
return newEntity->GetID();
}
::XCEngine::Components::GameObject::ID EditorSceneManager::PasteEntity(::XCEngine::Components::GameObject::ID parent) {
::XCEngine::Components::GameObject::ID SceneManager::PasteEntity(::XCEngine::Components::GameObject::ID parent) {
if (!m_clipboard || !m_scene) return 0;
return PasteEntityRecursive(*m_clipboard, parent);
}
::XCEngine::Components::GameObject::ID EditorSceneManager::DuplicateEntity(::XCEngine::Components::GameObject::ID id) {
::XCEngine::Components::GameObject::ID SceneManager::DuplicateEntity(::XCEngine::Components::GameObject::ID id) {
if (!m_scene) return 0;
::XCEngine::Components::GameObject* entity = m_scene->Find(std::to_string(id));
@@ -113,22 +119,32 @@ void EditorSceneManager::CopyEntity(::XCEngine::Components::GameObject::ID id) {
return PasteEntity(parentId);
}
void EditorSceneManager::MoveEntity(::XCEngine::Components::GameObject::ID id, ::XCEngine::Components::GameObject::ID newParentId) {
void SceneManager::RenameEntity(::XCEngine::Components::GameObject::ID id, const std::string& newName) {
if (!m_scene) return;
::XCEngine::Components::GameObject* entity = m_scene->Find(std::to_string(id));
if (!entity) return;
::XCEngine::Components::GameObject* obj = m_scene->Find(std::to_string(id));
if (!obj) return;
obj->SetName(newName);
OnEntityChanged.Invoke(id);
}
void SceneManager::MoveEntity(::XCEngine::Components::GameObject::ID id, ::XCEngine::Components::GameObject::ID newParentId) {
if (!m_scene) return;
::XCEngine::Components::GameObject* obj = m_scene->Find(std::to_string(id));
if (!obj) return;
::XCEngine::Components::GameObject* newParent = nullptr;
if (newParentId != 0) {
newParent = m_scene->Find(std::to_string(newParentId));
}
entity->SetParent(newParent);
obj->SetParent(newParent);
OnEntityChanged.Invoke(id);
}
void EditorSceneManager::CreateDemoScene() {
void SceneManager::CreateDemoScene() {
if (m_scene) {
delete m_scene;
}
@@ -143,7 +159,6 @@ void EditorSceneManager::CreateDemoScene() {
::XCEngine::Components::GameObject* cube = CreateEntity("Cube", nullptr);
cube->AddComponent<::XCEngine::Components::TransformComponent>();
// MeshRendererComponent 需要添加到 Engine
::XCEngine::Components::GameObject* sphere = CreateEntity("Sphere", nullptr);
sphere->AddComponent<::XCEngine::Components::TransformComponent>();

View File

@@ -4,6 +4,8 @@
#include <vector>
#include <memory>
#include <optional>
#include <string>
#include <cstdint>
#include <XCEngine/Core/Event.h>
#include <XCEngine/Core/Math/Vector3.h>
@@ -11,16 +13,17 @@
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Scene/Scene.h>
#include "Core/ISelectionManager.h"
namespace XCEngine {
namespace Editor {
class EditorSceneManager {
public:
static EditorSceneManager& Get() {
static EditorSceneManager instance;
return instance;
}
class ISelectionManager;
class SceneManager {
public:
SceneManager();
::XCEngine::Components::GameObject* CreateEntity(const std::string& name, ::XCEngine::Components::GameObject* parent = nullptr);
::XCEngine::Components::GameObject* GetEntity(::XCEngine::Components::GameObject::ID id) {
@@ -37,12 +40,7 @@ public:
void DeleteEntity(::XCEngine::Components::GameObject::ID id);
void RenameEntity(::XCEngine::Components::GameObject::ID id, const std::string& newName) {
if (auto* entity = GetEntity(id)) {
entity->SetName(newName);
OnEntityChanged.Invoke(id);
}
}
void RenameEntity(::XCEngine::Components::GameObject::ID id, const std::string& newName);
void CopyEntity(::XCEngine::Components::GameObject::ID id);
::XCEngine::Components::GameObject::ID PasteEntity(::XCEngine::Components::GameObject::ID parent = 0);
@@ -52,6 +50,8 @@ public:
void CreateDemoScene();
bool HasClipboardData() const { return m_clipboard.has_value(); }
void SetSelectionManager(ISelectionManager* selectionManager);
::XCEngine::Core::Event<::XCEngine::Components::GameObject::ID> OnEntityCreated;
::XCEngine::Core::Event<::XCEngine::Components::GameObject::ID> OnEntityDeleted;
@@ -59,8 +59,6 @@ public:
::XCEngine::Core::Event<> OnSceneChanged;
private:
EditorSceneManager() = default;
struct ClipboardData {
std::string name;
Math::Vector3 localPosition = Math::Vector3::Zero();
@@ -75,6 +73,7 @@ private:
::XCEngine::Components::Scene* m_scene = nullptr;
std::vector<::XCEngine::Components::GameObject*> m_rootEntities;
std::optional<ClipboardData> m_clipboard;
ISelectionManager* m_selectionManager = nullptr;
};
}