nomlib
Loading...
Searching...
No Matches
StateMachine.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_STATE_MACHINE_HPP
30#define NOMLIB_SYSTEM_STATE_MACHINE_HPP
31
32#include <memory>
33#include <vector>
34#include <queue>
35#include <map>
36#include <functional>
37
38#include "export.hpp"
39#include "nomlib/config.hpp"
40#include "nomlib/system/IState.hpp"
41
42namespace nom {
43
44// Forward declarations
45struct Event;
46class RenderWindow;
48
49namespace priv {
50
55{
56 bool deferred;
57 std::unique_ptr<IState> state;
58 void* data;
59
61 StateSymbol pop_target = NO_STATE;
62};
63
64} // namespace priv
65
67enum class NOM_EXPORT TransitionAction : uint32
68{
70 None = 0,
71
73 Replace,
74
76 Push,
77
79 Pop,
80
82 PopToParent
83};
84
86struct NOM_EXPORT Transition
87{
89 TransitionAction action = TransitionAction::None;
90
94 StateSymbol target = NO_STATE;
95
97 void* data = nullptr;
98};
99
104struct NOM_EXPORT StateDescriptor
105{
107 StateSymbol symbol;
108
110 StateSymbol parent = NO_STATE;
111
117 std::function<IState::unique_ptr( void* data )> factory;
118};
119
121class NOM_EXPORT TransitionTable
122{
123 public:
125 using Callback = std::function<Transition(const IState& active, EventSymbol event, StateSymbol stack_top)>;
126
131 void add( StateSymbol active, EventSymbol event, StateSymbol stack_top,
132 const Transition& transition );
133
137 void add( StateSymbol active, EventSymbol event, const Transition& transition );
138
142 Transition query( StateSymbol active, EventSymbol event,
143 StateSymbol stack_top ) const;
144
150 Transition resolve( const IState& active, EventSymbol event,
151 StateSymbol stack_top ) const;
152
156 void set_fallback( Callback callback );
157
158 private:
160 std::map<std::tuple<StateSymbol, EventSymbol, StateSymbol>, Transition> transitions_;
161
163 std::map<std::pair<StateSymbol, EventSymbol>, Transition> wildcards_;
164
167};
168
169
171class NOM_EXPORT StateMachine
172{
173 public:
176
179
180 // virtual void set_state ( uint32 id, void_ptr data = nullptr );
181
185 uint32 count() const;
186
188 bool empty() const;
189
197 uint32 current_state_id( void ) const;
198
207 uint32 next_state_id( void ) const;
208
213 StateSymbol current_state_symbol() const;
214
222 void register_state( const StateDescriptor& desc );
223
225 void add_transition( StateSymbol active, EventSymbol event,
226 StateSymbol stack_top, const Transition& transition );
227
231 void add_transition( StateSymbol active, EventSymbol event,
232 const Transition& transition );
233
238
250
253
260 void set_state( std::unique_ptr<IState> state, void_ptr data );
261
268 void pop_state( void_ptr data );
269
278 void push_state( StateSymbol symbol, void_ptr data = nullptr );
279
287 void push( StateSymbol child, void_ptr data = nullptr );
288
296 void pop( StateSymbol parent, void_ptr data = nullptr );
297
302 void replace( StateSymbol symbol, void_ptr data = nullptr );
303
311 void on_event(const Event& ev);
312
320 void on_event( EventSymbol event_symbol, const Event* ev );
321
325 void update( float delta );
326
329
330 private:
346 void set_deferred_state( std::unique_ptr<IState> state, void_ptr data );
347
348 void activate_input_context( const IState& state );
349 void disable_input_context( const IState& state );
350 void erase_input_context( const IState& state );
351
359 IState::unique_ptr make_state( StateSymbol symbol, void_ptr data = nullptr ) const;
360
362 bool can_push( StateSymbol child, StateSymbol parent ) const;
363
366
368 std::vector<std::unique_ptr<IState>> states_;
369
371 std::map<StateSymbol, StateDescriptor> registry_;
372
375
376 InputStateMapper* input_mapper_ = nullptr;
377};
378
379} // namespace nom
380
381#endif // include guard defined
Abstract interface for game states.
Definition IState.hpp:54
High-level API for collections of input action maps.
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.
Event handling types.
Definition Event.hpp:458
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.".