29#ifndef NOMLIB_SYSTEM_STATE_MACHINE_HPP
30#define NOMLIB_SYSTEM_STATE_MACHINE_HPP
39#include "nomlib/config.hpp"
40#include "nomlib/system/IState.hpp"
57 std::unique_ptr<IState> state;
67enum class NOM_EXPORT TransitionAction : uint32
89 TransitionAction
action = TransitionAction::None;
117 std::function<IState::unique_ptr(
void* data )>
factory;
131 void add( StateSymbol active, EventSymbol event, StateSymbol stack_top,
137 void add( StateSymbol active, EventSymbol event,
const Transition& transition );
143 StateSymbol stack_top )
const;
151 StateSymbol stack_top )
const;
226 StateSymbol stack_top,
const Transition& transition );
260 void set_state( std::unique_ptr<IState> state, void_ptr data );
278 void push_state( StateSymbol symbol, void_ptr data =
nullptr );
287 void push( StateSymbol child, void_ptr data =
nullptr );
296 void pop( StateSymbol parent, void_ptr data =
nullptr );
302 void replace( StateSymbol symbol, void_ptr data =
nullptr );
348 void activate_input_context(
const IState& state );
349 void disable_input_context(
const IState& state );
350 void erase_input_context(
const IState& state );
359 IState::unique_ptr
make_state( StateSymbol symbol, void_ptr data =
nullptr )
const;
362 bool can_push( StateSymbol child, StateSymbol parent )
const;
Abstract interface for game states.
void set_transition_fallback(const TransitionTable::Callback &callback)
Install a fallback transition resolver.
InputStateMapper * input_mapper() const
Obtain the installed input mapper, or nullptr.
bool empty() const
Check if the state stack is empty.
void register_state(const StateDescriptor &desc)
Register a state factory and optional parent relationship.
void on_event(const Event &ev)
Event handling logic for the active state.
void on_event(EventSymbol event_symbol, const Event *ev)
Event handling logic with transition-table evaluation.
StateMachine()
Default constructor.
void add_transition(StateSymbol active, EventSymbol event, const Transition &transition)
Add a wildcard event-driven transition to the machine's table.
void push(StateSymbol child, void_ptr data=nullptr)
Push a registered child state onto the active parent.
TransitionTable transitions_
Event-driven transition table.
void pop_state(void_ptr data)
uint32 current_state_id(void) const
Obtain the current state's identifier.
void update(float delta)
State logic handling.
uint32 next_state_id(void) const
Obtain the next pending state's identifier.
void set_state(std::unique_ptr< IState > state, void_ptr data)
void push_state(StateSymbol symbol, void_ptr data=nullptr)
Push a registered state onto the stack by symbol.
void set_input_mapper(InputStateMapper *mapper)
Install the input mapper used to auto-manage per-state input contexts during state transitions.
bool can_push(StateSymbol child, StateSymbol parent) const
Validate that child is declared as a child of the given parent.
std::vector< std::unique_ptr< IState > > states_
Stack of states.
std::map< StateSymbol, StateDescriptor > registry_
Registered state factories keyed by symbol.
IState::unique_ptr make_state(StateSymbol symbol, void_ptr data=nullptr) const
Construct a registered state by symbol.
priv::DeferredState next_state_
The pending state to be set on the next frame update.
void pop(StateSymbol parent, void_ptr data=nullptr)
Pop states until the requested parent state is active.
void add_transition(StateSymbol active, EventSymbol event, StateSymbol stack_top, const Transition &transition)
Add an exact event-driven transition to the machine's table.
void set_deferred_state(std::unique_ptr< IState > state, void_ptr data)
Set the next state.
void replace(StateSymbol symbol, void_ptr data=nullptr)
Replace the active state with a registered state by symbol.
StateSymbol current_state_symbol() const
Obtain the current active state symbol.
~StateMachine()
Destructor.
void draw(RenderWindow &)
State rendering handling.
uint32 count() const
Get the number of active states.
A transition table keyed by active state, event, and stack top.
std::map< std::tuple< StateSymbol, EventSymbol, StateSymbol >, Transition > transitions_
Exact-match transitions.
void set_fallback(Callback callback)
Install a fallback resolver.
Callback fallback_
Optional fallback resolver.
std::function< Transition(const IState &active, EventSymbol event, StateSymbol stack_top)> Callback
Function signature for fallback transition resolution.
void add(StateSymbol active, EventSymbol event, const Transition &transition)
Add a transition that ignores the stack top.
std::map< std::pair< StateSymbol, EventSymbol >, Transition > wildcards_
Wildcard transitions (stack top ignored).
Transition query(StateSymbol active, EventSymbol event, StateSymbol stack_top) const
Query the table for a transition.
void add(StateSymbol active, EventSymbol event, StateSymbol stack_top, const Transition &transition)
Add an exact transition.
Transition resolve(const IState &active, EventSymbol event, StateSymbol stack_top) const
Resolve a transition with optional fallback.
Description used to register a state with StateMachine.
StateSymbol parent
Stack symbol of the state's declared parent, or NO_STATE for a root.
StateSymbol symbol
Stack symbol for the state.
std::function< IState::unique_ptr(void *data)> factory
Result of an event-driven transition lookup.
TransitionAction action
Action to perform.
void * data
User-defined data passed to lifecycle hooks.
StateSymbol target
Target state symbol for Push/Replace/PopToParent actions.
StateSymbol pop_target
Target symbol for a multi-pop transition; 0 means "not set.".