feat(editor): unify component registration pipeline
This commit is contained in:
@@ -33,6 +33,7 @@ add_executable(${PROJECT_NAME} WIN32
|
||||
src/Application.cpp
|
||||
src/Theme.cpp
|
||||
src/Core/UndoManager.cpp
|
||||
src/ComponentEditors/ComponentEditorRegistry.cpp
|
||||
src/Managers/SceneManager.cpp
|
||||
src/Managers/ProjectManager.cpp
|
||||
src/Core/EditorConsoleSink.cpp
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComponentEditor.h"
|
||||
#include "Core/IUndoManager.h"
|
||||
#include "UI/UI.h"
|
||||
|
||||
#include <XCEngine/Components/CameraComponent.h>
|
||||
@@ -10,12 +11,12 @@ namespace Editor {
|
||||
|
||||
class CameraComponentEditor : public IComponentEditor {
|
||||
public:
|
||||
const char* GetDisplayName() const override {
|
||||
const char* GetComponentTypeName() const override {
|
||||
return "Camera";
|
||||
}
|
||||
|
||||
bool CanEdit(::XCEngine::Components::Component* component) const override {
|
||||
return dynamic_cast<::XCEngine::Components::CameraComponent*>(component) != nullptr;
|
||||
const char* GetDisplayName() const override {
|
||||
return "Camera";
|
||||
}
|
||||
|
||||
bool Render(::XCEngine::Components::Component* component, IUndoManager* undoManager) override {
|
||||
@@ -119,10 +120,6 @@ public:
|
||||
return gameObject->GetComponent<::XCEngine::Components::CameraComponent>() ? "Already Added" : nullptr;
|
||||
}
|
||||
|
||||
::XCEngine::Components::Component* AddTo(::XCEngine::Components::GameObject* gameObject) const override {
|
||||
return gameObject ? gameObject->AddComponent<::XCEngine::Components::CameraComponent>() : nullptr;
|
||||
}
|
||||
|
||||
bool CanRemove(::XCEngine::Components::Component* component) const override {
|
||||
return CanEdit(component);
|
||||
}
|
||||
|
||||
45
editor/src/ComponentEditors/ComponentEditorRegistry.cpp
Normal file
45
editor/src/ComponentEditors/ComponentEditorRegistry.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "ComponentEditors/ComponentEditorRegistry.h"
|
||||
|
||||
#include "ComponentEditors/CameraComponentEditor.h"
|
||||
#include "ComponentEditors/LightComponentEditor.h"
|
||||
#include "ComponentEditors/TransformComponentEditor.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Editor {
|
||||
|
||||
ComponentEditorRegistry& ComponentEditorRegistry::Get() {
|
||||
static ComponentEditorRegistry registry;
|
||||
return registry;
|
||||
}
|
||||
|
||||
ComponentEditorRegistry::ComponentEditorRegistry() {
|
||||
RegisterEditor(std::make_unique<TransformComponentEditor>());
|
||||
RegisterEditor(std::make_unique<CameraComponentEditor>());
|
||||
RegisterEditor(std::make_unique<LightComponentEditor>());
|
||||
}
|
||||
|
||||
void ComponentEditorRegistry::RegisterEditor(std::unique_ptr<IComponentEditor> editor) {
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
|
||||
IComponentEditor* editorPtr = editor.get();
|
||||
m_editorsByType[editor->GetComponentTypeName()] = editorPtr;
|
||||
m_editors.push_back(std::move(editor));
|
||||
}
|
||||
|
||||
IComponentEditor* ComponentEditorRegistry::FindEditor(::XCEngine::Components::Component* component) const {
|
||||
return component ? FindEditorByTypeName(component->GetName()) : nullptr;
|
||||
}
|
||||
|
||||
IComponentEditor* ComponentEditorRegistry::FindEditorByTypeName(const std::string& componentTypeName) const {
|
||||
const auto it = m_editorsByType.find(componentTypeName);
|
||||
return it != m_editorsByType.end() ? it->second : nullptr;
|
||||
}
|
||||
|
||||
const std::vector<std::unique_ptr<IComponentEditor>>& ComponentEditorRegistry::GetEditors() const {
|
||||
return m_editors;
|
||||
}
|
||||
|
||||
} // namespace Editor
|
||||
} // namespace XCEngine
|
||||
30
editor/src/ComponentEditors/ComponentEditorRegistry.h
Normal file
30
editor/src/ComponentEditors/ComponentEditorRegistry.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComponentEditor.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Editor {
|
||||
|
||||
class ComponentEditorRegistry {
|
||||
public:
|
||||
static ComponentEditorRegistry& Get();
|
||||
|
||||
void RegisterEditor(std::unique_ptr<IComponentEditor> editor);
|
||||
IComponentEditor* FindEditor(::XCEngine::Components::Component* component) const;
|
||||
IComponentEditor* FindEditorByTypeName(const std::string& componentTypeName) const;
|
||||
const std::vector<std::unique_ptr<IComponentEditor>>& GetEditors() const;
|
||||
|
||||
private:
|
||||
ComponentEditorRegistry();
|
||||
|
||||
std::vector<std::unique_ptr<IComponentEditor>> m_editors;
|
||||
std::unordered_map<std::string, IComponentEditor*> m_editorsByType;
|
||||
};
|
||||
|
||||
} // namespace Editor
|
||||
} // namespace XCEngine
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <XCEngine/Components/ComponentFactoryRegistry.h>
|
||||
#include <XCEngine/Components/Component.h>
|
||||
#include <XCEngine/Components/GameObject.h>
|
||||
|
||||
@@ -12,8 +13,11 @@ class IComponentEditor {
|
||||
public:
|
||||
virtual ~IComponentEditor() = default;
|
||||
|
||||
virtual const char* GetComponentTypeName() const = 0;
|
||||
virtual const char* GetDisplayName() const = 0;
|
||||
virtual bool CanEdit(::XCEngine::Components::Component* component) const = 0;
|
||||
virtual bool CanEdit(::XCEngine::Components::Component* component) const {
|
||||
return component && component->GetName() == GetComponentTypeName();
|
||||
}
|
||||
virtual bool Render(::XCEngine::Components::Component* component, IUndoManager* undoManager) = 0;
|
||||
|
||||
virtual bool ShowInAddComponentMenu() const { return true; }
|
||||
@@ -23,8 +27,9 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
virtual ::XCEngine::Components::Component* AddTo(::XCEngine::Components::GameObject* gameObject) const {
|
||||
(void)gameObject;
|
||||
return nullptr;
|
||||
return gameObject
|
||||
? ::XCEngine::Components::ComponentFactoryRegistry::Get().CreateComponent(gameObject, GetComponentTypeName())
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
virtual bool CanRemove(::XCEngine::Components::Component* component) const {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComponentEditor.h"
|
||||
#include "Core/IUndoManager.h"
|
||||
#include "UI/UI.h"
|
||||
|
||||
#include <XCEngine/Components/LightComponent.h>
|
||||
@@ -10,12 +11,12 @@ namespace Editor {
|
||||
|
||||
class LightComponentEditor : public IComponentEditor {
|
||||
public:
|
||||
const char* GetDisplayName() const override {
|
||||
const char* GetComponentTypeName() const override {
|
||||
return "Light";
|
||||
}
|
||||
|
||||
bool CanEdit(::XCEngine::Components::Component* component) const override {
|
||||
return dynamic_cast<::XCEngine::Components::LightComponent*>(component) != nullptr;
|
||||
const char* GetDisplayName() const override {
|
||||
return "Light";
|
||||
}
|
||||
|
||||
bool Render(::XCEngine::Components::Component* component, IUndoManager* undoManager) override {
|
||||
@@ -103,10 +104,6 @@ public:
|
||||
return gameObject->GetComponent<::XCEngine::Components::LightComponent>() ? "Already Added" : nullptr;
|
||||
}
|
||||
|
||||
::XCEngine::Components::Component* AddTo(::XCEngine::Components::GameObject* gameObject) const override {
|
||||
return gameObject ? gameObject->AddComponent<::XCEngine::Components::LightComponent>() : nullptr;
|
||||
}
|
||||
|
||||
bool CanRemove(::XCEngine::Components::Component* component) const override {
|
||||
return CanEdit(component);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "IComponentEditor.h"
|
||||
#include "Core/IUndoManager.h"
|
||||
#include "UI/UI.h"
|
||||
|
||||
#include <XCEngine/Components/TransformComponent.h>
|
||||
@@ -10,12 +11,12 @@ namespace Editor {
|
||||
|
||||
class TransformComponentEditor : public IComponentEditor {
|
||||
public:
|
||||
const char* GetDisplayName() const override {
|
||||
const char* GetComponentTypeName() const override {
|
||||
return "Transform";
|
||||
}
|
||||
|
||||
bool CanEdit(::XCEngine::Components::Component* component) const override {
|
||||
return dynamic_cast<::XCEngine::Components::TransformComponent*>(component) != nullptr;
|
||||
const char* GetDisplayName() const override {
|
||||
return "Transform";
|
||||
}
|
||||
|
||||
bool Render(::XCEngine::Components::Component* component, IUndoManager* undoManager) override {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#include "SceneManager.h"
|
||||
#include "Core/EventBus.h"
|
||||
#include "Core/EditorEvents.h"
|
||||
#include <XCEngine/Components/ComponentFactoryRegistry.h>
|
||||
#include <XCEngine/Components/CameraComponent.h>
|
||||
#include <XCEngine/Components/LightComponent.h>
|
||||
#include <XCEngine/Components/AudioSourceComponent.h>
|
||||
#include <XCEngine/Components/AudioListenerComponent.h>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@@ -21,29 +20,6 @@ std::pair<std::string, std::string> SerializeComponent(const ::XCEngine::Compone
|
||||
return { component->GetName(), payload.str() };
|
||||
}
|
||||
|
||||
::XCEngine::Components::Component* CreateComponentByName(
|
||||
::XCEngine::Components::GameObject* gameObject,
|
||||
const std::string& componentName) {
|
||||
if (!gameObject) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (componentName == "Camera") {
|
||||
return gameObject->AddComponent<::XCEngine::Components::CameraComponent>();
|
||||
}
|
||||
if (componentName == "Light") {
|
||||
return gameObject->AddComponent<::XCEngine::Components::LightComponent>();
|
||||
}
|
||||
if (componentName == "AudioSource") {
|
||||
return gameObject->AddComponent<::XCEngine::Components::AudioSourceComponent>();
|
||||
}
|
||||
if (componentName == "AudioListener") {
|
||||
return gameObject->AddComponent<::XCEngine::Components::AudioListenerComponent>();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SceneManager::SceneManager(EventBus* eventBus)
|
||||
@@ -152,7 +128,7 @@ void SceneManager::CopyEntity(::XCEngine::Components::GameObject::ID id) {
|
||||
}
|
||||
|
||||
for (const auto& componentData : data.components) {
|
||||
if (auto* component = CreateComponentByName(newEntity, componentData.first)) {
|
||||
if (auto* component = ::XCEngine::Components::ComponentFactoryRegistry::Get().CreateComponent(newEntity, componentData.first)) {
|
||||
if (!componentData.second.empty()) {
|
||||
std::istringstream payloadStream(componentData.second);
|
||||
component->Deserialize(payloadStream);
|
||||
|
||||
@@ -5,10 +5,8 @@
|
||||
#include "Core/IUndoManager.h"
|
||||
#include "Core/EventBus.h"
|
||||
#include "Core/EditorEvents.h"
|
||||
#include "ComponentEditors/CameraComponentEditor.h"
|
||||
#include "ComponentEditors/ComponentEditorRegistry.h"
|
||||
#include "ComponentEditors/IComponentEditor.h"
|
||||
#include "ComponentEditors/LightComponentEditor.h"
|
||||
#include "ComponentEditors/TransformComponentEditor.h"
|
||||
#include "Utils/UndoUtils.h"
|
||||
#include <imgui.h>
|
||||
#include <string>
|
||||
@@ -16,9 +14,7 @@
|
||||
namespace XCEngine {
|
||||
namespace Editor {
|
||||
|
||||
InspectorPanel::InspectorPanel() : Panel("Inspector") {
|
||||
RegisterDefaultComponentEditors();
|
||||
}
|
||||
InspectorPanel::InspectorPanel() : Panel("Inspector") {}
|
||||
|
||||
InspectorPanel::~InspectorPanel() {
|
||||
if (m_context) {
|
||||
@@ -33,34 +29,6 @@ void InspectorPanel::OnSelectionChanged(const SelectionChangedEvent& event) {
|
||||
m_selectedEntityId = event.primarySelection;
|
||||
}
|
||||
|
||||
void InspectorPanel::RegisterDefaultComponentEditors() {
|
||||
RegisterComponentEditor(std::make_unique<TransformComponentEditor>());
|
||||
RegisterComponentEditor(std::make_unique<CameraComponentEditor>());
|
||||
RegisterComponentEditor(std::make_unique<LightComponentEditor>());
|
||||
}
|
||||
|
||||
void InspectorPanel::RegisterComponentEditor(std::unique_ptr<IComponentEditor> editor) {
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_componentEditors.push_back(std::move(editor));
|
||||
}
|
||||
|
||||
IComponentEditor* InspectorPanel::GetEditorFor(::XCEngine::Components::Component* component) const {
|
||||
if (!component) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (const auto& editor : m_componentEditors) {
|
||||
if (editor && editor->CanEdit(component)) {
|
||||
return editor.get();
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void InspectorPanel::Render() {
|
||||
ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None);
|
||||
|
||||
@@ -126,7 +94,7 @@ void InspectorPanel::RenderAddComponentPopup(::XCEngine::Components::GameObject*
|
||||
ImGui::Separator();
|
||||
|
||||
bool drewAnyEntry = false;
|
||||
for (const auto& editor : m_componentEditors) {
|
||||
for (const auto& editor : ComponentEditorRegistry::Get().GetEditors()) {
|
||||
if (!editor || !editor->ShowInAddComponentMenu()) {
|
||||
continue;
|
||||
}
|
||||
@@ -167,7 +135,7 @@ void InspectorPanel::RenderAddComponentPopup(::XCEngine::Components::GameObject*
|
||||
void InspectorPanel::RenderComponent(::XCEngine::Components::Component* component, ::XCEngine::Components::GameObject* gameObject) {
|
||||
if (!component) return;
|
||||
|
||||
IComponentEditor* editor = GetEditorFor(component);
|
||||
IComponentEditor* editor = ComponentEditorRegistry::Get().FindEditor(component);
|
||||
|
||||
const char* name = component->GetName().c_str();
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
#include "Panel.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Components {
|
||||
@@ -14,8 +12,6 @@ class GameObject;
|
||||
|
||||
namespace Editor {
|
||||
|
||||
class IComponentEditor;
|
||||
|
||||
class InspectorPanel : public Panel {
|
||||
public:
|
||||
InspectorPanel();
|
||||
@@ -24,9 +20,6 @@ public:
|
||||
void Render() override;
|
||||
|
||||
private:
|
||||
void RegisterDefaultComponentEditors();
|
||||
void RegisterComponentEditor(std::unique_ptr<IComponentEditor> editor);
|
||||
IComponentEditor* GetEditorFor(::XCEngine::Components::Component* component) const;
|
||||
void RenderGameObject(::XCEngine::Components::GameObject* gameObject);
|
||||
void RenderAddComponentPopup(::XCEngine::Components::GameObject* gameObject);
|
||||
void RenderComponent(::XCEngine::Components::Component* component, ::XCEngine::Components::GameObject* gameObject);
|
||||
@@ -35,7 +28,6 @@ private:
|
||||
|
||||
uint64_t m_selectionHandlerId = 0;
|
||||
uint64_t m_selectedEntityId = 0;
|
||||
std::vector<std::unique_ptr<IComponentEditor>> m_componentEditors;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user