Refactor new editor scene runtime ownership
This commit is contained in:
183
new_editor/app/Scene/EditorSceneRuntime.cpp
Normal file
183
new_editor/app/Scene/EditorSceneRuntime.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "Scene/EditorSceneRuntime.h"
|
||||
|
||||
#include <XCEngine/Components/GameObject.h>
|
||||
#include <XCEngine/Scene/Scene.h>
|
||||
|
||||
namespace XCEngine::UI::Editor::App {
|
||||
|
||||
namespace {
|
||||
|
||||
using ::XCEngine::Components::GameObject;
|
||||
using ::XCEngine::Components::Scene;
|
||||
|
||||
std::string ResolveGameObjectDisplayName(const GameObject& gameObject) {
|
||||
return gameObject.GetName().empty()
|
||||
? std::string("GameObject")
|
||||
: gameObject.GetName();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool EditorSceneRuntime::Initialize(const std::filesystem::path& projectRoot) {
|
||||
m_projectRoot = projectRoot;
|
||||
m_startupSceneResult = EnsureEditorStartupScene(projectRoot);
|
||||
RefreshScene();
|
||||
return m_startupSceneResult.ready;
|
||||
}
|
||||
|
||||
void EditorSceneRuntime::RefreshScene() {
|
||||
if (!HasValidSelection()) {
|
||||
m_selectedGameObjectId.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void EditorSceneRuntime::EnsureSceneSelection() {
|
||||
if (HasValidSelection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SelectFirstAvailableGameObject();
|
||||
}
|
||||
|
||||
const EditorStartupSceneResult& EditorSceneRuntime::GetStartupResult() const {
|
||||
return m_startupSceneResult;
|
||||
}
|
||||
|
||||
Scene* EditorSceneRuntime::GetActiveScene() const {
|
||||
return GetActiveEditorScene();
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::HasSceneSelection() const {
|
||||
return HasValidSelection();
|
||||
}
|
||||
|
||||
std::optional<GameObject::ID> EditorSceneRuntime::GetSelectedGameObjectId() const {
|
||||
return HasValidSelection() ? m_selectedGameObjectId : std::nullopt;
|
||||
}
|
||||
|
||||
std::string EditorSceneRuntime::GetSelectedItemId() const {
|
||||
const std::optional<GameObject::ID> selectedId = GetSelectedGameObjectId();
|
||||
return selectedId.has_value()
|
||||
? MakeEditorGameObjectItemId(selectedId.value())
|
||||
: std::string();
|
||||
}
|
||||
|
||||
std::string EditorSceneRuntime::GetSelectedDisplayName() const {
|
||||
const GameObject* gameObject = GetSelectedGameObject();
|
||||
return gameObject != nullptr
|
||||
? ResolveGameObjectDisplayName(*gameObject)
|
||||
: std::string();
|
||||
}
|
||||
|
||||
const GameObject* EditorSceneRuntime::GetSelectedGameObject() const {
|
||||
if (!m_selectedGameObjectId.has_value()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Scene* scene = GetActiveScene();
|
||||
return scene != nullptr
|
||||
? scene->FindByID(m_selectedGameObjectId.value())
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::SetSelection(std::string_view itemId) {
|
||||
const std::optional<GameObject::ID> gameObjectId =
|
||||
ParseEditorGameObjectItemId(itemId);
|
||||
if (!gameObjectId.has_value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return SetSelection(gameObjectId.value());
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::SetSelection(GameObject::ID id) {
|
||||
if (id == GameObject::INVALID_ID) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Scene* scene = GetActiveScene();
|
||||
if (scene == nullptr || scene->FindByID(id) == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool changed =
|
||||
!m_selectedGameObjectId.has_value() ||
|
||||
m_selectedGameObjectId.value() != id;
|
||||
m_selectedGameObjectId = id;
|
||||
return changed;
|
||||
}
|
||||
|
||||
void EditorSceneRuntime::ClearSelection() {
|
||||
m_selectedGameObjectId.reset();
|
||||
}
|
||||
|
||||
GameObject* EditorSceneRuntime::FindGameObject(std::string_view itemId) const {
|
||||
return FindEditorGameObject(itemId);
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::RenameGameObject(
|
||||
std::string_view itemId,
|
||||
std::string_view newName) {
|
||||
const bool renamed = RenameEditorGameObject(itemId, newName);
|
||||
RefreshScene();
|
||||
return renamed;
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::DeleteGameObject(std::string_view itemId) {
|
||||
const bool deleted = DeleteEditorGameObject(itemId);
|
||||
RefreshScene();
|
||||
EnsureSceneSelection();
|
||||
return deleted;
|
||||
}
|
||||
|
||||
std::string EditorSceneRuntime::DuplicateGameObject(std::string_view itemId) {
|
||||
const std::string duplicatedItemId = DuplicateEditorGameObject(itemId);
|
||||
if (!duplicatedItemId.empty()) {
|
||||
SetSelection(duplicatedItemId);
|
||||
} else {
|
||||
RefreshScene();
|
||||
}
|
||||
return duplicatedItemId;
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::ReparentGameObject(
|
||||
std::string_view itemId,
|
||||
std::string_view parentItemId) {
|
||||
const bool reparented =
|
||||
ReparentEditorGameObject(itemId, parentItemId);
|
||||
RefreshScene();
|
||||
return reparented;
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::MoveGameObjectToRoot(std::string_view itemId) {
|
||||
const bool moved = MoveEditorGameObjectToRoot(itemId);
|
||||
RefreshScene();
|
||||
return moved;
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::HasValidSelection() const {
|
||||
return GetSelectedGameObject() != nullptr;
|
||||
}
|
||||
|
||||
bool EditorSceneRuntime::SelectFirstAvailableGameObject() {
|
||||
Scene* scene = GetActiveScene();
|
||||
if (scene == nullptr) {
|
||||
m_selectedGameObjectId.reset();
|
||||
return false;
|
||||
}
|
||||
|
||||
for (GameObject* root : scene->GetRootGameObjects()) {
|
||||
if (root == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
m_selectedGameObjectId = root->GetID();
|
||||
return true;
|
||||
}
|
||||
|
||||
m_selectedGameObjectId.reset();
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace XCEngine::UI::Editor::App
|
||||
|
||||
Reference in New Issue
Block a user