2026-02-11 19:40:26 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
2026-02-25 06:23:53 +08:00
|
|
|
#include <core/types.h>
|
2026-02-26 00:59:16 +08:00
|
|
|
#include <functional>
|
2026-02-25 06:23:53 +08:00
|
|
|
#include <scene/scene.h>
|
2026-02-11 19:40:26 +08:00
|
|
|
#include <stack>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2026-02-26 00:59:16 +08:00
|
|
|
|
2026-02-11 19:40:26 +08:00
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
// 前向声明
|
|
|
|
|
struct RenderCommand;
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// 场景管理器 - 管理场景的生命周期和切换
|
|
|
|
|
// ============================================================================
|
|
|
|
|
class SceneManager {
|
|
|
|
|
public:
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 单例访问
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
static SceneManager &getInstance();
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 场景栈操作
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// 运行第一个场景
|
2026-02-26 21:17:11 +08:00
|
|
|
void runWithScene(IntrusivePtr<Scene> scene);
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
// 替换当前场景
|
2026-02-26 21:17:11 +08:00
|
|
|
void replaceScene(IntrusivePtr<Scene> scene);
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
// 压入新场景(当前场景暂停)
|
2026-02-26 21:17:11 +08:00
|
|
|
void pushScene(IntrusivePtr<Scene> scene);
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
// 弹出当前场景(恢复上一个场景)
|
|
|
|
|
void popScene();
|
|
|
|
|
|
|
|
|
|
// 弹出到根场景
|
|
|
|
|
void popToRootScene();
|
|
|
|
|
|
|
|
|
|
// 弹出到指定场景
|
|
|
|
|
void popToScene(const std::string &name);
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 获取场景
|
|
|
|
|
// ------------------------------------------------------------------------
|
2026-02-26 21:17:11 +08:00
|
|
|
IntrusivePtr<Scene> getCurrentScene() const;
|
|
|
|
|
IntrusivePtr<Scene> getPreviousScene() const;
|
|
|
|
|
IntrusivePtr<Scene> getRootScene() const;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
// 通过名称获取场景
|
2026-02-26 21:17:11 +08:00
|
|
|
IntrusivePtr<Scene> getSceneByName(const std::string &name) const;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 查询
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
size_t getSceneCount() const { return sceneStack_.size(); }
|
|
|
|
|
bool isEmpty() const { return sceneStack_.empty(); }
|
|
|
|
|
bool hasScene(const std::string &name) const;
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 更新和渲染
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void update(float dt);
|
2026-02-26 00:59:16 +08:00
|
|
|
void render(Renderer &renderer);
|
2026-02-11 19:40:26 +08:00
|
|
|
void collectRenderCommands(std::vector<RenderCommand> &commands);
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 清理
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void end();
|
|
|
|
|
void purgeCachedScenes();
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SceneManager() = default;
|
|
|
|
|
~SceneManager() = default;
|
|
|
|
|
SceneManager(const SceneManager &) = delete;
|
|
|
|
|
SceneManager &operator=(const SceneManager &) = delete;
|
|
|
|
|
|
|
|
|
|
// 场景切换(供 Application 使用)
|
2026-02-26 21:17:11 +08:00
|
|
|
void enterScene(IntrusivePtr<Scene> scene);
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void doSceneSwitch();
|
|
|
|
|
void dispatchPointerEvents(Scene &scene);
|
|
|
|
|
|
2026-02-26 21:17:11 +08:00
|
|
|
std::stack<IntrusivePtr<Scene>> sceneStack_;
|
|
|
|
|
std::unordered_map<std::string, IntrusivePtr<Scene>> namedScenes_;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
// Next scene to switch to (queued during transition)
|
2026-02-26 21:17:11 +08:00
|
|
|
IntrusivePtr<Scene> nextScene_;
|
2026-02-11 19:40:26 +08:00
|
|
|
bool sendCleanupToScene_ = false;
|
|
|
|
|
|
|
|
|
|
Node *hoverTarget_ = nullptr;
|
|
|
|
|
Node *captureTarget_ = nullptr;
|
|
|
|
|
Vec2 lastPointerWorld_ = Vec2::Zero();
|
|
|
|
|
bool hasLastPointerWorld_ = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|