Files
XCEngine/editor/src/Actions/InspectorActionRouter.h

66 lines
2.0 KiB
C
Raw Normal View History

#pragma once
#include "EditorActions.h"
#include "Commands/ComponentCommands.h"
#include "ComponentEditors/ComponentEditorRegistry.h"
#include "Core/IEditorContext.h"
#include "UI/UI.h"
#include <string>
namespace XCEngine {
namespace Editor {
namespace Actions {
inline std::string BuildAddComponentMenuLabel(const IComponentEditor& editor, ::XCEngine::Components::GameObject* gameObject) {
std::string label = editor.GetDisplayName();
if (Commands::CanAddComponent(editor, gameObject)) {
return label;
}
const char* reason = editor.GetAddDisabledReason(gameObject);
if (reason && reason[0] != '\0') {
label += " (";
label += reason;
label += ")";
}
return label;
}
inline bool DrawInspectorAddComponentMenu(IEditorContext& context, ::XCEngine::Components::GameObject* gameObject) {
bool drewAnyEntry = false;
for (const auto& editor : ComponentEditorRegistry::Get().GetEditors()) {
if (!editor || !editor->ShowInAddComponentMenu()) {
continue;
}
drewAnyEntry = true;
const bool canAdd = Commands::CanAddComponent(*editor, gameObject);
const std::string label = BuildAddComponentMenuLabel(*editor, gameObject);
DrawMenuAction(MakeAddComponentMenuAction(label, canAdd), [&]() {
if (Commands::AddComponent(context, *editor, gameObject)) {
ImGui::CloseCurrentPopup();
}
});
}
return drewAnyEntry;
}
inline bool DrawInspectorComponentMenu(
IEditorContext& context,
::XCEngine::Components::Component* component,
::XCEngine::Components::GameObject* gameObject,
const IComponentEditor* editor) {
const bool canRemove = Commands::CanRemoveComponent(component, editor);
return DrawMenuAction(MakeRemoveComponentAction(canRemove), [&]() {
Commands::RemoveComponent(context, component, gameObject, editor);
});
}
} // namespace Actions
} // namespace Editor
} // namespace XCEngine