75 lines
2.2 KiB
C++
75 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include "ComponentEditors/ComponentEditorRegistry.h"
|
|
#include "ComponentEditors/IComponentEditor.h"
|
|
#include "Core/IEditorContext.h"
|
|
#include "Core/ISceneManager.h"
|
|
#include "Utils/UndoUtils.h"
|
|
|
|
#include <string>
|
|
|
|
namespace XCEngine {
|
|
namespace Editor {
|
|
namespace Commands {
|
|
|
|
inline bool CanAddComponent(const IComponentEditor& editor, ::XCEngine::Components::GameObject* gameObject) {
|
|
return gameObject != nullptr && editor.CanAddTo(gameObject);
|
|
}
|
|
|
|
inline bool AddComponent(
|
|
IEditorContext& context,
|
|
const IComponentEditor& editor,
|
|
::XCEngine::Components::GameObject* gameObject,
|
|
const std::string& commandLabel = {}) {
|
|
if (!CanAddComponent(editor, gameObject)) {
|
|
return false;
|
|
}
|
|
|
|
bool added = false;
|
|
const std::string label =
|
|
commandLabel.empty() ? std::string("Add ") + editor.GetDisplayName() + " Component" : commandLabel;
|
|
UndoUtils::ExecuteSceneCommand(context, label, [&]() {
|
|
added = editor.AddTo(gameObject) != nullptr;
|
|
if (added) {
|
|
context.GetSceneManager().MarkSceneDirty();
|
|
}
|
|
});
|
|
return added;
|
|
}
|
|
|
|
inline bool CanRemoveComponent(
|
|
::XCEngine::Components::Component* component,
|
|
const IComponentEditor* editor = nullptr) {
|
|
if (!component) {
|
|
return false;
|
|
}
|
|
|
|
const IComponentEditor* resolvedEditor =
|
|
editor ? editor : ComponentEditorRegistry::Get().FindEditor(component);
|
|
return resolvedEditor != nullptr && resolvedEditor->CanRemove(component);
|
|
}
|
|
|
|
inline bool RemoveComponent(
|
|
IEditorContext& context,
|
|
::XCEngine::Components::Component* component,
|
|
::XCEngine::Components::GameObject* gameObject,
|
|
const IComponentEditor* editor = nullptr,
|
|
const std::string& commandLabel = {}) {
|
|
if (!gameObject || !CanRemoveComponent(component, editor)) {
|
|
return false;
|
|
}
|
|
|
|
const std::string label = commandLabel.empty()
|
|
? std::string("Remove ") + component->GetName() + " Component"
|
|
: commandLabel;
|
|
UndoUtils::ExecuteSceneCommand(context, label, [&]() {
|
|
gameObject->RemoveComponent(component);
|
|
context.GetSceneManager().MarkSceneDirty();
|
|
});
|
|
return true;
|
|
}
|
|
|
|
} // namespace Commands
|
|
} // namespace Editor
|
|
} // namespace XCEngine
|