VR-Engage  2.2
Loading...
Searching...
No Matches
playerStationStateManager.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file playerStationStateManager.h
7//! \brief Defines the DtPlayerStationStateManager class for managing application states
8//!
9//! This class implements a state stack manager for VR-Engage, providing functionality
10//! for registering states, managing state transitions, and maintaining a stack of
11//! active states. It uses a queue-based approach to ensure orderly state changes.
12
13#pragma once
14
16
17#include <list>
18#include <map>
19#include <deque>
20#include <tuple>
21#include <string>
22#include <memory>
23
24namespace makVre
25{
26//! \brief Forward declaration of the player station application class
28
29//! \brief Forward declaration of the player station state class
31
32//! \brief Forward declaration of the player station state arguments class
34
35//! \brief Manager for player station states and state transitions
36//!
37//! DtPlayerStationStateManager provides a robust state management system for VR-Engage.
38//! It maintains a stack of states and handles transitions between them in a controlled
39//! manner. State operations are queued and processed during the tick cycle to ensure
40//! proper ordering and prevent state inconsistencies.
42{
43public:
44 //! \brief Type definition for the state stack
45 using DtStateList = std::list<DtPlayerStationState*>;
46
47 //! \brief Type definition for the map of registered states
48 using DtRegisteredStates = std::map<std::string, DtPlayerStationState*>;
49
50 //! \brief Enumeration of possible stack operations
52 {
53 NO_OP, //!< No operation
54 OP_PUSH, //!< Push a new state onto the stack
55 OP_TRANSITION, //!< Replace the current state
56 OP_POP, //!< Pop the top state from the stack
57 OP_POP_UNTIL //!< Pop states until a specific state is at the top
58 };
59
60 //! \brief Type definition for a queued state operation
61 //! Contains operation type, state name, and arguments
62 using DtQueuedOp = std::tuple<DtStackOp, std::string, std::shared_ptr<DtPlayerStationStateArgs>>;
63
64 //! \brief Type definition for the queue of state operations
65 using DtStackOpQueue = std::deque<DtQueuedOp>;
66
67 //! \brief Deleted copy constructor
68 //!
69 //! Prevents copying of the state manager instance
71
72 //! \brief Deleted assignment operator
73 //!
74 //! Prevents assignment of the state manager instance
76
77 //! \brief Registers a new state with the manager
78 //! \param state Pointer to the state to register
79 //!
80 //! Adds a state to the internal map of registered states, making it
81 //! available for transitions. The state name is used as the key.
82 virtual void registerState(DtPlayerStationState* state);
83
84 //! \brief Main tick function called from the player station tick
85 //! \param dt Delta time since the last tick in seconds
86 //!
87 //! Processes any queued state operations and calls the tick method
88 //! of the current active state. State operations are processed in
89 //! the order they were queued.
90 virtual void tick(double dt);
91
92 //! \brief Queues a state to push onto the stack
93 //! \param state Name of the state to push
94 //! \param args Optional arguments to pass to the state
95 //!
96 //! The pushed state becomes the new active state, but the previous
97 //! state remains on the stack. When the new state is later popped,
98 //! the previous state will become active again.
99 virtual void push(const std::string& state, DtPlayerStationStateArgs* args = NULL);
100
101 //! \brief Queues a state to transition from the current active state
102 //! \param state Name of the state to transition to
103 //! \param args Optional arguments to pass to the new state
104 //!
105 //! Replaces the current active state with the new state. The previous
106 //! state is removed from the stack and its exit method is called.
107 virtual void transition(const std::string& state, DtPlayerStationStateArgs* args = NULL);
108
109 //! \brief Queues an operation to pop the active state off the stack
110 //!
111 //! Removes the current active state from the stack and makes the
112 //! state below it the new active state. If there is no state
113 //! below, the stack will be empty after this operation.
114 virtual void pop();
115
116 //! \brief Queues an operation to pop states until a specific state is exposed
117 //! \param state Name of the state to expose
118 //! \param args Optional arguments to pass to the exposed state
119 //!
120 //! Pops states from the stack until the specified state becomes the
121 //! active state. If the state is not in the stack, this will result
122 //! in an empty stack.
123 virtual void popUntil(const std::string& state, DtPlayerStationStateArgs* args = NULL);
124
125 //! \brief Gets a state by its name
126 //! \param state Name of the state to retrieve
127 //! \return Pointer to the state, or NULL if not found
128 //!
129 //! Looks up a registered state by its name and returns a pointer to it.
130 //! Returns NULL if the state is not registered.
131 DtPlayerStationState* getState(const std::string& state);
132
133 //! \brief Gets the currently active state
134 //! \return Pointer to the current active state, or NULL if no state is active
135 //!
136 //! Returns the state at the top of the stack, which is the currently
137 //! active state. Returns NULL if the stack is empty.
139
140 //! \brief Checks if a specific state type is in the stack
141 //! \param state Name of the state to check for
142 //! \return True if the state is in the stack, false otherwise
143 //!
144 //! Searches the state stack for a state with the specified name.
145 //! Returns true if found, false otherwise.
146 virtual bool containsStateType(const std::string& state);
147
148 //! \brief Finds the position of a state in the stack
149 //! \param state Name of the state to find
150 //! \return Zero-based position in the stack, or -1 if not found
151 //!
152 //! Searches for a state in the stack and returns its position.
153 //! Position 0 is the bottom of the stack, with higher indices moving toward the top.
154 //! Returns -1 if the state is not in the stack.
155 int findStackPos(const std::string& state);
156
157 //! \brief Dumps the contents of the operation queue for debugging
158 //!
159 //! Outputs the current contents of the operation queue to the debug log,
160 //! showing each pending operation and its associated state and arguments.
161 const void debugDumpDeque();
162
163 //! \brief Debug callback function
164 //!
165 //! Called during debugging to allow inspection of the state manager
166 virtual void debugCallback();
167
168protected:
169 // These classes can access protected members and methods
170 friend class DtPlayerStationApp;
171 friend class DtVreDebugManager;
172
173 //! \brief Constructor
174 //! \param app Reference to the player station application
175 //!
176 //! Initializes the state manager with a reference to the app.
177 //! Constructor is protected to ensure only DtPlayerStationApp can create instances.
179
180 //! \brief Virtual destructor
181 //!
182 //! Cleans up any remaining states and resources
184
185 //! \brief Performs a state transition operation
186 //! \param toState Name of the state to transition to
187 //! \param args Pointer to arguments for the new state
188 //!
189 //! Internal implementation for transitioning from the current state
190 //! to a new state. Calls exit on the old state and enter on the new state.
191 void doTransition(const std::string& toState, const DtPlayerStationStateArgs* args);
192
193 //! \brief Performs a pop operation on the state stack
194 //! \param args Optional arguments to pass to the newly exposed state
195 //!
196 //! Internal implementation for popping the top state off the stack.
197 //! Calls exit on the popped state and resume on the newly exposed state.
198 void doPop(const DtPlayerStationStateArgs* args = NULL);
199
200 //! \brief Performs a push operation on the state stack
201 //! \param newState Name of the state to push
202 //! \param args Pointer to arguments for the new state
203 //!
204 //! Internal implementation for pushing a new state onto the stack.
205 //! Calls pause on the current state and enter on the new state.
206 void doPush(const std::string& newState, const DtPlayerStationStateArgs* args);
207
208 //! \brief Performs a popUntil operation on the state stack
209 //! \param exposeState Name of the state to expose
210 //! \param args Pointer to arguments for the exposed state
211 //!
212 //! Internal implementation for popping states until a specific state is exposed.
213 //! Calls exit on all popped states and resume on the newly exposed state.
214 void doPopUntil(const std::string& exposeState, const DtPlayerStationStateArgs* args);
215
216 //! \brief Checks if a state is registered
217 //! \param state Name of the state to check
218 //! \return True if the state is registered, false otherwise
219 //!
220 //! Internal function that verifies if a state with the given name
221 //! is registered in the state manager.
222 bool isRegistered(const std::string& state);
223
224protected:
225 //! \brief The state stack
226 //!
227 //! Contains the currently active states with the most recent at the front
229
230 //! \brief Map of all registered states
231 //!
232 //! Maps state names to their corresponding state objects
234
235 //! \brief Reference to the player station application
236 //!
237 //! Provides access to the application for states that need it
239
240 //! \brief Queue of pending state operations
241 //!
242 //! Contains operations to be processed during the next tick,
243 //! each with an operation type, state name, and arguments
245};
246} // namespace makVre
Top-level class representing the VR-Engage application.
Definition playerStationApp.h:163
Class for passing arguments during state transitions.
Definition playerStationState.h:45
Base class for all VR-Engage application states.
Definition playerStationState.h:86
virtual void transition(const std::string &state, DtPlayerStationStateArgs *args=NULL)
Queues a state to transition from the current active state.
friend class DtPlayerStationApp
Definition playerStationStateManager.h:170
DtPlayerStationStateManager & operator=(const DtPlayerStationStateManager &)=delete
Deleted assignment operator.
std::map< std::string, DtPlayerStationState * > DtRegisteredStates
Type definition for the map of registered states.
Definition playerStationStateManager.h:48
virtual void pop()
Queues an operation to pop the active state off the stack.
std::tuple< DtStackOp, std::string, std::shared_ptr< DtPlayerStationStateArgs > > DtQueuedOp
Type definition for a queued state operation Contains operation type, state name, and arguments.
Definition playerStationStateManager.h:62
void doTransition(const std::string &toState, const DtPlayerStationStateArgs *args)
Performs a state transition operation.
DtPlayerStationState * getState(const std::string &state)
Gets a state by its name.
bool isRegistered(const std::string &state)
Checks if a state is registered.
DtPlayerStationStateManager(const DtPlayerStationStateManager &)=delete
Deleted copy constructor.
virtual void popUntil(const std::string &state, DtPlayerStationStateArgs *args=NULL)
Queues an operation to pop states until a specific state is exposed.
const void debugDumpDeque()
Dumps the contents of the operation queue for debugging.
virtual DtPlayerStationState * currentState()
Gets the currently active state.
void doPop(const DtPlayerStationStateArgs *args=NULL)
Performs a pop operation on the state stack.
void doPopUntil(const std::string &exposeState, const DtPlayerStationStateArgs *args)
Performs a popUntil operation on the state stack.
std::deque< DtQueuedOp > DtStackOpQueue
Type definition for the queue of state operations.
Definition playerStationStateManager.h:65
virtual bool containsStateType(const std::string &state)
Checks if a specific state type is in the stack.
DtStateList myStateStack
The state stack.
Definition playerStationStateManager.h:228
DtStackOp
Enumeration of possible stack operations.
Definition playerStationStateManager.h:52
@ OP_PUSH
Push a new state onto the stack.
Definition playerStationStateManager.h:54
@ OP_POP
Pop the top state from the stack.
Definition playerStationStateManager.h:56
@ OP_TRANSITION
Replace the current state.
Definition playerStationStateManager.h:55
@ OP_POP_UNTIL
Pop states until a specific state is at the top.
Definition playerStationStateManager.h:57
@ NO_OP
No operation.
Definition playerStationStateManager.h:53
DtPlayerStationApp & myApp
Reference to the player station application.
Definition playerStationStateManager.h:238
std::list< DtPlayerStationState * > DtStateList
Type definition for the state stack.
Definition playerStationStateManager.h:45
int findStackPos(const std::string &state)
Finds the position of a state in the stack.
friend class DtVreDebugManager
Definition playerStationStateManager.h:171
virtual void debugCallback()
Debug callback function.
void doPush(const std::string &newState, const DtPlayerStationStateArgs *args)
Performs a push operation on the state stack.
DtStackOpQueue myStackOpQueue
Queue of pending state operations.
Definition playerStationStateManager.h:244
virtual void registerState(DtPlayerStationState *state)
Registers a new state with the manager.
virtual ~DtPlayerStationStateManager()
Virtual destructor.
virtual void tick(double dt)
Main tick function called from the player station tick.
DtPlayerStationStateManager(DtPlayerStationApp &app)
Constructor.
DtRegisteredStates myRegisteredStates
Map of all registered states.
Definition playerStationStateManager.h:233
virtual void push(const std::string &state, DtPlayerStationStateArgs *args=NULL)
Queues a state to push onto the stack.
Defines export macros for the VR-Engage Player Station library.
#define PLAYERSTATION_DLL
Definition export.h:24
Include export definitions for this library.
Definition glsVreMessageUtil.h:49