Align editor runtime scene handoff

This commit is contained in:
2026-04-29 04:05:54 +08:00
parent 2fde2f16c2
commit 595d39c4c3
32 changed files with 996 additions and 52 deletions

View File

@@ -22,6 +22,7 @@
#include <algorithm>
#include <charconv>
#include <cstdint>
#include <filesystem>
#include <optional>
#include <sstream>
@@ -74,6 +75,27 @@ Scene* ResolvePrimaryScene(SceneManager& sceneManager) {
return nullptr;
}
std::string ResolveUniqueSceneName(
const SceneManager& sceneManager,
std::string_view requestedName) {
const std::string baseName =
requestedName.empty() ? std::string("Untitled") : std::string(requestedName);
if (sceneManager.GetScene(baseName) == nullptr) {
return baseName;
}
for (std::uint32_t suffix = 2u; suffix < 10000u; ++suffix) {
const std::string candidate =
baseName + " " + std::to_string(suffix);
if (sceneManager.GetScene(candidate) == nullptr) {
return candidate;
}
}
return baseName + " " + std::to_string(
static_cast<std::uint64_t>(sceneManager.GetAllScenes().size() + 1u));
}
std::pair<std::string, std::string> SerializeComponent(
const Component* component) {
std::ostringstream payload = {};
@@ -1759,6 +1781,49 @@ public:
return loadedScene != nullptr;
}
bool NewScene(std::string_view sceneName) override {
const std::string resolvedSceneName =
ResolveUniqueSceneName(m_sceneManager, sceneName);
Scene* scene = m_sceneManager.CreateScene(resolvedSceneName);
if (scene == nullptr) {
return false;
}
m_sceneManager.SetActiveScene(scene);
return true;
}
bool SaveActiveScene(const std::filesystem::path& scenePath) override {
if (scenePath.empty()) {
return false;
}
Scene* scene = ResolvePrimaryScene(m_sceneManager);
if (scene == nullptr) {
return false;
}
const std::filesystem::path parentPath = scenePath.parent_path();
if (!parentPath.empty()) {
std::error_code errorCode = {};
std::filesystem::create_directories(parentPath, errorCode);
if (errorCode) {
return false;
}
}
scene->Save(scenePath.string());
std::error_code errorCode = {};
return std::filesystem::exists(scenePath, errorCode) &&
!errorCode &&
std::filesystem::is_regular_file(scenePath, errorCode) &&
!errorCode;
}
Scene* GetActiveScene() const override {
return ResolvePrimaryScene(m_sceneManager);
}
std::optional<EditorSceneObjectSnapshot> GetObjectSnapshot(
std::string_view itemId) const override {
const GameObject* gameObject = FindGameObject(itemId);