259 lines
8.1 KiB
C++
259 lines
8.1 KiB
C++
#include "EditorContext.h"
|
|
|
|
#include "Composition/EditorShellAssetBuilder.h"
|
|
#include "Scene/EditorSceneRuntime.h"
|
|
#include "State/EditorSelectionStamp.h"
|
|
|
|
#include <XCEditor/App/EditorPanelIds.h>
|
|
|
|
#include <sstream>
|
|
#include <utility>
|
|
|
|
namespace XCEngine::UI::Editor::App {
|
|
|
|
namespace {
|
|
|
|
using ::XCEngine::UI::Editor::BuildEditorShellShortcutManager;
|
|
using ::XCEngine::UI::Editor::UIEditorWorkspacePanelPresentationModel;
|
|
using ::XCEngine::UI::Editor::Widgets::UIEditorViewportSlotToolItem;
|
|
using ::XCEngine::UI::Editor::Widgets::UIEditorViewportSlotToolSlot;
|
|
|
|
std::string ComposeStatusText(
|
|
std::string_view status,
|
|
std::string_view message) {
|
|
if (status.empty()) {
|
|
return std::string(message);
|
|
}
|
|
|
|
if (message.empty()) {
|
|
return std::string(status);
|
|
}
|
|
|
|
return std::string(status) + ": " + std::string(message);
|
|
}
|
|
|
|
UIEditorWorkspacePanelPresentationModel* FindMutablePresentation(
|
|
std::vector<UIEditorWorkspacePanelPresentationModel>& presentations,
|
|
std::string_view panelId) {
|
|
for (UIEditorWorkspacePanelPresentationModel& presentation : presentations) {
|
|
if (presentation.panelId == panelId) {
|
|
return &presentation;
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
UIEditorViewportSlotToolItem BuildSceneToolItem(
|
|
std::string itemId,
|
|
std::string label,
|
|
UIEditorViewportSlotToolSlot slot,
|
|
bool selected,
|
|
float desiredWidth) {
|
|
UIEditorViewportSlotToolItem item = {};
|
|
item.itemId = std::move(itemId);
|
|
item.label = std::move(label);
|
|
item.slot = slot;
|
|
item.enabled = true;
|
|
item.selected = selected;
|
|
item.desiredWidth = desiredWidth;
|
|
return item;
|
|
}
|
|
|
|
std::vector<UIEditorViewportSlotToolItem> BuildSceneViewportTopBarItems(
|
|
const EditorSceneRuntime& sceneRuntime) {
|
|
return {
|
|
BuildSceneToolItem(
|
|
"scene.pivot.pivot",
|
|
"Pivot",
|
|
UIEditorViewportSlotToolSlot::Leading,
|
|
sceneRuntime.GetToolPivotMode() == SceneToolPivotMode::Pivot,
|
|
52.0f),
|
|
BuildSceneToolItem(
|
|
"scene.pivot.center",
|
|
"Center",
|
|
UIEditorViewportSlotToolSlot::Leading,
|
|
sceneRuntime.GetToolPivotMode() == SceneToolPivotMode::Center,
|
|
58.0f),
|
|
BuildSceneToolItem(
|
|
"scene.space.world",
|
|
"World",
|
|
UIEditorViewportSlotToolSlot::Leading,
|
|
sceneRuntime.GetToolSpaceMode() == SceneToolSpaceMode::World,
|
|
58.0f),
|
|
BuildSceneToolItem(
|
|
"scene.space.local",
|
|
"Local",
|
|
UIEditorViewportSlotToolSlot::Leading,
|
|
sceneRuntime.GetToolSpaceMode() == SceneToolSpaceMode::Local,
|
|
56.0f)
|
|
};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool EditorContext::Initialize(const std::filesystem::path& repoRoot) {
|
|
m_shellAsset = BuildEditorApplicationShellAsset(repoRoot);
|
|
m_shellValidation = ValidateEditorShellAsset(m_shellAsset);
|
|
if (!m_shellValidation.IsValid()) {
|
|
return false;
|
|
}
|
|
|
|
m_session = {};
|
|
m_session.repoRoot = repoRoot;
|
|
m_session.projectRoot = (repoRoot / "project").lexically_normal();
|
|
m_projectRuntime = {};
|
|
m_projectRuntime.Initialize(repoRoot);
|
|
m_sceneRuntime = {};
|
|
m_sceneRuntime.Initialize(m_session.projectRoot);
|
|
m_hostCommandBridge.BindSession(m_session);
|
|
m_shortcutManager = BuildEditorShellShortcutManager(m_shellAsset);
|
|
m_shortcutManager.SetHostCommandHandler(&m_hostCommandBridge);
|
|
m_shellServices = {};
|
|
m_shellServices.commandDispatcher = &m_shortcutManager.GetCommandDispatcher();
|
|
m_shellServices.shortcutManager = &m_shortcutManager;
|
|
SetReadyStatus();
|
|
return true;
|
|
}
|
|
|
|
void EditorContext::AttachTextMeasurer(
|
|
const UIEditorTextMeasurer& textMeasurer) {
|
|
m_shellServices.textMeasurer = &textMeasurer;
|
|
}
|
|
|
|
void EditorContext::BindEditCommandRoutes(
|
|
EditorEditCommandRoute* hierarchyRoute,
|
|
EditorEditCommandRoute* projectRoute,
|
|
EditorEditCommandRoute* sceneRoute,
|
|
EditorEditCommandRoute* inspectorRoute) {
|
|
m_hostCommandBridge.BindEditCommandRoutes(
|
|
hierarchyRoute,
|
|
projectRoute,
|
|
sceneRoute,
|
|
inspectorRoute);
|
|
}
|
|
|
|
void EditorContext::SetExitRequestHandler(std::function<void()> handler) {
|
|
m_hostCommandBridge.SetExitRequestHandler(std::move(handler));
|
|
}
|
|
|
|
void EditorContext::SyncSessionFromWorkspace(
|
|
const UIEditorWorkspaceController& workspaceController) {
|
|
SyncEditorSessionFromWorkspace(m_session, workspaceController);
|
|
}
|
|
|
|
bool EditorContext::IsValid() const {
|
|
return m_shellValidation.IsValid();
|
|
}
|
|
|
|
const std::string& EditorContext::GetValidationMessage() const {
|
|
return m_shellValidation.message;
|
|
}
|
|
|
|
const EditorShellAsset& EditorContext::GetShellAsset() const {
|
|
return m_shellAsset;
|
|
}
|
|
|
|
const EditorSession& EditorContext::GetSession() const {
|
|
return m_session;
|
|
}
|
|
|
|
EditorProjectRuntime& EditorContext::GetProjectRuntime() {
|
|
return m_projectRuntime;
|
|
}
|
|
|
|
const EditorProjectRuntime& EditorContext::GetProjectRuntime() const {
|
|
return m_projectRuntime;
|
|
}
|
|
|
|
EditorSceneRuntime& EditorContext::GetSceneRuntime() {
|
|
return m_sceneRuntime;
|
|
}
|
|
|
|
const EditorSceneRuntime& EditorContext::GetSceneRuntime() const {
|
|
return m_sceneRuntime;
|
|
}
|
|
|
|
void EditorContext::SetSelection(EditorSelectionState selection) {
|
|
selection.stamp = GenerateEditorSelectionStamp();
|
|
m_session.selection = std::move(selection);
|
|
}
|
|
|
|
void EditorContext::ClearSelection() {
|
|
m_session.selection = {};
|
|
m_session.selection.stamp = GenerateEditorSelectionStamp();
|
|
}
|
|
|
|
void EditorContext::SyncSessionFromProjectRuntime() {
|
|
m_session.selection = m_projectRuntime.GetSelection();
|
|
}
|
|
|
|
UIEditorWorkspaceController EditorContext::BuildWorkspaceController() const {
|
|
return UIEditorWorkspaceController(
|
|
m_shellAsset.panelRegistry,
|
|
m_shellAsset.workspace,
|
|
m_shellAsset.workspaceSession);
|
|
}
|
|
|
|
const UIEditorShellInteractionServices& EditorContext::GetShellServices() const {
|
|
return m_shellServices;
|
|
}
|
|
|
|
UIEditorShellInteractionDefinition EditorContext::BuildShellDefinition(
|
|
const UIEditorWorkspaceController& workspaceController,
|
|
std::string_view captureText,
|
|
EditorShellVariant variant) const {
|
|
UIEditorShellInteractionDefinition definition =
|
|
BuildEditorApplicationShellInteractionDefinition(
|
|
m_shellAsset,
|
|
workspaceController,
|
|
ComposeStatusText(m_lastStatus, m_lastMessage),
|
|
captureText,
|
|
variant);
|
|
|
|
if (UIEditorWorkspacePanelPresentationModel* scenePresentation =
|
|
FindMutablePresentation(definition.workspacePresentations, kScenePanelId);
|
|
scenePresentation != nullptr) {
|
|
scenePresentation->viewportShellModel.spec.chrome.showTopBar = true;
|
|
scenePresentation->viewportShellModel.spec.toolItems =
|
|
BuildSceneViewportTopBarItems(m_sceneRuntime);
|
|
scenePresentation->viewportShellModel.spec.visualState.hoveredToolIndex =
|
|
m_sceneRuntime.GetToolState().toolbarHoveredIndex;
|
|
scenePresentation->viewportShellModel.spec.visualState.activeToolIndex =
|
|
m_sceneRuntime.GetToolState().toolbarActiveIndex;
|
|
}
|
|
|
|
return definition;
|
|
}
|
|
|
|
std::string EditorContext::DescribeWorkspaceState(
|
|
const UIEditorWorkspaceController& workspaceController,
|
|
const UIEditorShellInteractionState& interactionState) const {
|
|
std::ostringstream stream = {};
|
|
stream << "active=" << workspaceController.GetWorkspace().activePanelId;
|
|
const auto visiblePanels =
|
|
CollectUIEditorWorkspaceVisiblePanels(
|
|
workspaceController.GetWorkspace(),
|
|
workspaceController.GetSession());
|
|
stream << " visible=[";
|
|
for (std::size_t index = 0; index < visiblePanels.size(); ++index) {
|
|
if (index > 0u) {
|
|
stream << ',';
|
|
}
|
|
stream << visiblePanels[index].panelId;
|
|
}
|
|
stream << ']';
|
|
|
|
const auto& dockState =
|
|
interactionState.workspaceInteractionState.dockHostInteractionState;
|
|
stream << " dragNode=" << dockState.activeTabDragNodeId;
|
|
stream << " dragPanel=" << dockState.activeTabDragPanelId;
|
|
if (dockState.dockHostState.dropPreview.visible) {
|
|
stream << " dropTarget=" << dockState.dockHostState.dropPreview.targetNodeId;
|
|
}
|
|
return stream.str();
|
|
}
|
|
|
|
} // namespace XCEngine::UI::Editor::App
|
|
|