Files
XCEngine/new_editor/app/State/EditorSelectionService.h

112 lines
3.4 KiB
C++

#pragma once
#include "State/EditorSelectionStamp.h"
#include "State/EditorSession.h"
#include <filesystem>
#include <string>
#include <string_view>
#include <utility>
namespace XCEngine::UI::Editor::App {
class EditorSelectionService {
public:
const EditorSelectionState& GetSelection() const {
return m_selection;
}
bool HasSelection() const {
return m_selection.kind != EditorSelectionKind::None &&
!m_selection.itemId.empty();
}
bool HasSelectionKind(EditorSelectionKind kind) const {
return m_selection.kind == kind && HasSelection();
}
std::uint64_t GetStamp() const {
return m_selection.stamp;
}
bool SetProjectSelection(
std::string_view itemId,
std::string_view displayName,
const std::filesystem::path& absolutePath,
bool directory) {
EditorSelectionState selection = {};
selection.kind = EditorSelectionKind::ProjectItem;
selection.itemId = std::string(itemId);
selection.displayName = std::string(displayName);
selection.absolutePath = absolutePath;
selection.directory = directory;
const bool changed =
m_selection.kind != selection.kind ||
m_selection.itemId != selection.itemId ||
m_selection.absolutePath != selection.absolutePath ||
m_selection.directory != selection.directory;
ApplySelection(std::move(selection), changed);
return changed;
}
bool SetHierarchySelection(
std::string_view itemId,
std::string_view displayName = {}) {
EditorSelectionState selection = {};
selection.kind = EditorSelectionKind::HierarchyNode;
selection.itemId = std::string(itemId);
selection.displayName = std::string(displayName);
const bool changed =
m_selection.kind != selection.kind ||
m_selection.itemId != selection.itemId;
ApplySelection(std::move(selection), changed);
return changed;
}
void SetSelection(EditorSelectionState selection) {
bool changed = true;
switch (selection.kind) {
case EditorSelectionKind::ProjectItem:
changed =
m_selection.kind != selection.kind ||
m_selection.itemId != selection.itemId ||
m_selection.absolutePath != selection.absolutePath ||
m_selection.directory != selection.directory;
break;
case EditorSelectionKind::HierarchyNode:
changed =
m_selection.kind != selection.kind ||
m_selection.itemId != selection.itemId;
break;
case EditorSelectionKind::None:
default:
ClearSelection();
return;
}
ApplySelection(std::move(selection), changed);
}
void ClearSelection() {
m_selection = {};
m_selection.stamp = GenerateEditorSelectionStamp();
}
private:
void ApplySelection(
EditorSelectionState selection,
bool changed) {
const std::uint64_t previousStamp = m_selection.stamp;
selection.stamp = changed
? GenerateEditorSelectionStamp()
: (previousStamp != 0u ? previousStamp : GenerateEditorSelectionStamp());
m_selection = std::move(selection);
}
EditorSelectionState m_selection = {};
};
} // namespace XCEngine::UI::Editor::App