|
| | SceneManager ()=default |
| | SceneManager (const SceneManager &)=delete |
| | SceneManager (SceneManager &&)=delete |
| template<typename Owner> |
| void | attach (Owner &owner) |
| | Wire SceneManager into the owner's state machine.
|
| void | back () |
| | Navigate to the previously visited state.
|
| void | bind (StateEnum state, Core &scene) |
| | Bind an existing scene instance to a state (eager).
|
| template<typename SceneType, typename... Args> |
| void | bindLazy (StateEnum state, Args &&... args) |
| | Bind a lazily-constructed scene type to a state.
|
| void | clear () |
| | Clear all bindings and history.
|
| StateEnum | current () const |
| | Return the currently active state.
|
| bool | has (StateEnum state) const |
| | Return true if a scene has been bound to state.
|
| bool | isActive (StateEnum state) const |
| | Return true if state is currently the active scene.
|
| SceneManager & | operator= (const SceneManager &)=delete |
| SceneManager & | operator= (SceneManager &&)=delete |
| void | start (StateEnum state) |
| | Activate the initial scene without recording a history entry.
|
template<typename StateEnum>
class ml::SceneManager< StateEnum >
State-driven scene router for Malena state machines.
SceneManager<StateEnum> binds scenes (any Core-derived object or lazily-constructed type) to state enum values. When the owner calls setState(), SceneManager intercepts the transition via onStateEnter / onStateExit and performs the scene swap automatically — removing the outgoing scene from the owner and adding the incoming one.
Ownership
A SceneManager is a plain object you own — typically a member of the thing that drives it. That owner can be an Application or any Component, so the same router works at the top level of an app and for sub-routing inside a component. Each instance has its own bindings and history; the router lives and dies with its owner.
The owner (the type passed to attach) must expose:
onStateEnter(std::function<void(StateEnum)>)
onStateExit(std::function<void(StateEnum)>)
setState(StateEnum)
syncState(StateEnum)
addComponent(Core&)
removeComponent(Core&)
Application and Component both satisfy this contract.
- Warning
- The owner must outlive the
SceneManager. attach() stores callbacks that reference the owner and this; destroying either while still attached is undefined behaviour. Holding the SceneManager as a member of the owner satisfies this naturally.
Design contract
setState() on the owner is the only navigation primitive. SceneManager reacts to it; it does not replace it.
back() is the one exception — it calls setState() with the previously visited state, keeping the state machine as the single source of truth.
- Scenes bound by reference (
bind) are always in memory. Scenes bound lazily (bindLazy) are constructed on first visit and destroyed on back() or when another scene replaces them.
Typical setup (Application)
{
enum class State { MainMenu, Game, Settings };
};
{
using State = AppManifest::State;
Scenes _scenes;
MainMenuScene _mainMenu;
SettingsScene _settings;
public:
MyApp() :
ml::ApplicationWith<AppManifest>(1280, 720, 32,
"My Game") {}
{
_scenes.bind (State::MainMenu, _mainMenu);
_scenes.bind (State::Settings, _settings);
_scenes.bindLazy<GameScene>(State::Game);
_scenes.attach(*this);
_scenes.start (State::MainMenu);
}
{
_mainMenu.playButton().onClick([this]{ setState(State::Game); });
_mainMenu.settingsButton().onClick([this]{ setState(State::Settings); });
_settings.backButton().onClick([this]{ _scenes.back(); });
}
};
Entry point for Malena applications with a manifest.
virtual void onInit()
Called once after construction, before the first frame.
virtual void onReady()
Called once after onInit(), when all components are registered.
Base class for all Malena manifests.
State-driven scene router for Malena state machines.
Sub-routing inside a Component
{
using State = WorkspaceManifest::State;
Scenes _scenes;
ChatPanel _chat;
QuizPanel _quiz;
public:
Workspace()
{
_scenes.
bind(State::Chat, _chat);
_scenes.bind(State::Quiz, _quiz);
_scenes.attach(*this);
_scenes.start (State::Chat);
}
};
Primary base class for all user-facing Malena components.
void bind(StateEnum state, Core &scene)
Bind an existing scene instance to a state (eager).
Lazy scenes
Lazy scenes are heap-allocated on first visit and freed when they are navigated away from. This is useful for heavy scenes (lots of textures, many components) that should not occupy memory while inactive.
_scenes.bindLazy<GameScene>(State::Game);
_scenes.bindLazy<GameScene>(State::Game, level);
History and back()
Every setState() call pushes the departing state onto a history stack. back() pops it and calls setState() with that value. The initial scene set via start() is NOT pushed — it acts as the history root.
- Template Parameters
-
| StateEnum | The enum class declared in the owner's manifest's State typedef. Must match the enum used by the owner's own state machine. |
- See also
- ApplicationWith, Component, StateManager, CoreManager, Core
Definition at line 152 of file SceneManager.h.