VR-Engage  2.2
Loading...
Searching...
No Matches
playerStationState.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file playerStationState.h
7//! \brief Defines base classes for the VR-Engage state management system
8//!
9//! This file provides the foundational classes for state management in VR-Engage:
10//! - DtPlayerStationStateArgs: Used to pass arguments between states during transitions
11//! - DtPlayerStationState: Base class that all application states inherit from
12//!
13//! States in VR-Engage are organized in a stack, with the top state being "exposed"
14//! and actively controlling the application. States can be pushed, popped, and
15//! transitioned between to manage application flow.
16
17#pragma once
18
20
22
23// #include <memory>
24
25// Helper macro for logging
26#define LOG_STATE_VERBOSE LOG_VERBOSE("State")
27#define LOG_STATE_INFO LOG_INFO("State")
28#define LOG_STATE_WARN LOG_WARN("State")
29#define LOG_STATE_FATAL LOG_FATAL("State")
30
31namespace makVre
32{
33//! \brief Forward declaration of the player station application class
34class DtPlayerStationApp;
35
36//! \brief Forward declaration of the VR-Engage message class
37class DtVreMessage;
38
39//! \brief Class for passing arguments during state transitions
40//!
41//! DtPlayerStationStateArgs provides a mechanism to pass information from one state
42//! to another during state transitions. It includes the source state name and
43//! an optional message containing additional data.
45{
46public:
47 //! \brief Constructor with raw message pointer
48 //! \param fromStateType Name of the source state
49 //! \param message Optional pointer to a message with additional data
50 //!
51 //! Creates a state arguments object, taking ownership of the message if provided
52 DtPlayerStationStateArgs(const std::string& fromStateType, DtVreMessage* message = NULL);
53
54 //! \brief Constructor with shared pointer to message
55 //! \param fromStateType Name of the source state
56 //! \param message Shared pointer to a message with additional data
57 DtPlayerStationStateArgs(const std::string& fromStateType, std::shared_ptr<DtVreMessage> message);
58
59 //! \brief Gets the name of the source state
60 //! \return Reference to the source state name
61 const std::string& fromState() const;
62
63 //! \brief Gets the message as a raw pointer
64 //! \return Raw pointer to the message, or NULL if not present
65 const DtVreMessage* message() const;
66
67 //! \brief Gets the message as a shared pointer
68 //! \return Shared pointer to the message
69 std::shared_ptr<DtVreMessage> message_ptr() const;
70
71private:
72 //! \brief Name of the source state
73 std::string myFromState;
74
75 //! \brief Shared pointer to the optional message
76 std::shared_ptr<DtVreMessage> myMessage;
77};
78
79//! \brief Base class for all VR-Engage application states
80//!
81//! DtPlayerStationState serves as the foundation for all application states
82//! in VR-Engage. It defines the lifecycle methods that states must implement
83//! to respond to state stack operations, and provides utility methods for
84//! transitioning between states.
86{
87 // Allow the state manager direct access to private methods
89
90public:
91 //! \brief Constructor
92 //! \param app Reference to the player station application
93 //! \param stateType String identifier for this state type
94 //!
95 //! Initializes a new state with its type name and a reference to the application.
96 //! States are created by the application and registered with the state manager.
98
99 //! \brief Virtual destructor
100 //!
101 //! Ensures proper cleanup of derived state classes.
102 //! Only called when VR-Engage is shutting down.
104
105 //! \brief Deleted copy constructor
106 //!
107 //! Prevents copying of state instances
109
110 //! \brief Deleted assignment operator
111 //!
112 //! Prevents assignment of state instances
114
115 //! \brief Called when the state is added to the stack
116 //! \param args Pointer to arguments from the previous state
117 //!
118 //! Invoked when this state is newly added to the stack, either via push or transition.
119 //! Override to perform initialization specific to this state.
120 virtual void onEnter(const DtPlayerStationStateArgs* args) {};
121
122 //! \brief Overloaded version without arguments
123 //!
124 //! Provided for convenience when no arguments are needed.
125 virtual void onEnter() {};
126
127 //! \brief Called when a state is added to the stack on top of this one
128 //!
129 //! Invoked when another state is pushed on top of this state, causing
130 //! this state to become inactive but remain in the stack.
131 //! Override to pause operations that should resume later.
132 virtual void onStacked() {};
133
134 //! \brief Called when all states above this state are removed
135 //! \param args Pointer to arguments from the previous state
136 //!
137 //! Invoked when this state becomes the top state on the stack again
138 //! after states above it are popped. Override to resume operations.
139 virtual void onExposed(const DtPlayerStationStateArgs* args) {};
140
141 //! \brief Overloaded version without arguments
142 //!
143 //! Provided for convenience when no arguments are needed.
144 virtual void onExposed() {};
145
146 //! \brief Called when this state is being removed from the stack
147 //!
148 //! Invoked just before this state is removed from the stack,
149 //! either via pop or transition. Override to perform cleanup.
150 virtual void onExit() {};
151
152 //! \brief Main update function called while this state is active
153 //! \param dt Delta time in seconds since the last tick
154 //!
155 //! Called once per frame while this state is in the stack.
156 //! This is where most of the state's logic should be implemented.
157 virtual void tick(double dt) {};
158
159 //! \brief Checks if this state is currently in the stack
160 //! \return True if the state is in the stack, false otherwise
161 //!
162 //! A state is active when it's somewhere in the state stack,
163 //! regardless of whether it's the topmost state.
164 bool active() const;
165
166 //! \brief Checks if this state is currently the topmost state
167 //! \return True if the state is at the top of the stack, false otherwise
168 //!
169 //! A state is exposed when it's at the top of the state stack,
170 //! making it the currently controlling state in the application.
171 bool exposed() const;
172
173 //! \brief Checks if this state is queued to be exited
174 //! \return True if the state is marked for exit, false otherwise
175 //!
176 //! Indicates that an operation has been queued that will remove
177 //! this state from the stack during the next tick.
178 bool exiting() const;
179
180 //! \brief Checks if this state is queued to be stacked
181 //! \return True if the state is marked for stacking, false otherwise
182 //!
183 //! Indicates that an operation has been queued that will push
184 //! another state on top of this one during the next tick.
185 bool stacking() const;
186
187 //! \brief Gets the string identifier for this state's type
188 //! \return Constant reference to the state type string
189 //!
190 //! This is the unique name used to identify and look up the state.
191 const std::string& stateType() const;
192
193 //! \brief Gets a reference to the player station application
194 //! \return Reference to the application object
195 //!
196 //! Provides access to the application for states that need it.
198
199protected:
200 //! \brief Pushes a new state onto the stack
201 //! \param newState Name of the state to push
202 //! \param args Optional arguments to pass to the new state
203 //! \return True if the operation was queued successfully, false otherwise
204 //!
205 //! Queues an operation to push the specified state onto the stack,
206 //! making it the new active state while preserving this state.
207 bool pushState(const std::string& newState, DtPlayerStationStateArgs* args = NULL);
208
209 //! \brief Pops this state from the stack
210 //! \return True if the operation was queued successfully, false otherwise
211 //!
212 //! Queues an operation to remove this state from the stack,
213 //! exposing the state beneath it.
214 bool popSelf();
215
216 //! \brief Pops states until this state is exposed
217 //! \return True if the operation was queued successfully, false otherwise
218 //!
219 //! Queues an operation to pop all states above this one,
220 //! making this state the active state.
222
223 //! \brief Pops states until a specific parent state is exposed
224 //! \param parentState Name of the state to expose
225 //! \param args Optional arguments to pass to the exposed state
226 //! \return True if the operation was queued successfully, false otherwise
227 //!
228 //! Queues an operation to pop states until the specified parent
229 //! state is at the top of the stack.
230 bool popUntilState(const std::string& parentState, DtPlayerStationStateArgs* args = NULL);
231
232 //! \brief Transitions from this state to a new state
233 //! \param newState Name of the state to transition to
234 //! \param args Optional arguments to pass to the new state
235 //! \return True if the operation was queued successfully, false otherwise
236 //!
237 //! Queues an operation to replace this state with the specified state.
238 bool switchToState(const std::string& newState, DtPlayerStationStateArgs* args = NULL);
239
240 //! \brief Pushes a copy of this state onto the stack
241 //! \return True if the operation was queued successfully, false otherwise
242 //!
243 //! Queues an operation to push the same state type onto the stack.
244 //! Note: This creates a new instance of the state class.
245 bool pushSelf();
246
247 //! \brief Reference to the player station application
249
250private:
251 //! \brief Internal implementation of onEnter
252 //! \param args Pointer to arguments from the previous state
253 //!
254 //! Handles common functionality for state entry, including sending
255 //! an "EnterPlayerStationStateMessage", then calls the public onEnter.
257
258 //! \brief Internal implementation of onStacked
259 //!
260 //! Handles common functionality when a state is stacked, then
261 //! calls the public onStacked.
263
264 //! \brief Internal implementation of onExposed
265 //! \param args Pointer to arguments for resuming
266 //!
267 //! Handles common functionality when a state becomes exposed again,
268 //! then calls the public onExposed.
270
271 //! \brief Internal implementation of onExit
272 //!
273 //! Handles common functionality for state exit, including sending
274 //! an "ExitPlayerStationStateMessage", then calls the public onExit.
275 void _onExit();
276
277 //! \brief String identifier for this state's type
278 std::string myStateType;
279
280 //! \brief Position of this state in the stack (0 is bottom)
282
283 //! \brief Flag indicating whether this state is in the stack
285
286 //! \brief Flag indicating whether this state is at the top of the stack
288
289 //! \brief Flag indicating whether a state transition is pending
291
292 //! \brief Flag indicating whether a state push is pending
294
295 //! \brief Flag indicating whether the state is currently processing
296 //!
297 //! Set true during event methods and tick, false otherwise.
298 //! Used to manage error checking in state stack operations differently
299 //! depending on whether we are in one of our event functions,
300 //! or outside of them (e.g., in a message handler).
302};
303} // 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
std::shared_ptr< DtVreMessage > message_ptr() const
Gets the message as a shared pointer.
DtPlayerStationStateArgs(const std::string &fromStateType, DtVreMessage *message=NULL)
Constructor with raw message pointer.
std::shared_ptr< DtVreMessage > myMessage
Shared pointer to the optional message.
Definition playerStationState.h:76
std::string myFromState
Name of the source state.
Definition playerStationState.h:73
const std::string & fromState() const
Gets the name of the source state.
const DtVreMessage * message() const
Gets the message as a raw pointer.
DtPlayerStationStateArgs(const std::string &fromStateType, std::shared_ptr< DtVreMessage > message)
Constructor with shared pointer to message.
bool stacking() const
Checks if this state is queued to be stacked.
std::string myStateType
String identifier for this state's type.
Definition playerStationState.h:278
virtual void onExit()
Called when this state is being removed from the stack.
Definition playerStationState.h:150
bool switchToState(const std::string &newState, DtPlayerStationStateArgs *args=NULL)
Transitions from this state to a new state.
virtual void onEnter()
Overloaded version without arguments.
Definition playerStationState.h:125
bool popUntilSelf()
Pops states until this state is exposed.
virtual void onExposed()
Overloaded version without arguments.
Definition playerStationState.h:144
bool active() const
Checks if this state is currently in the stack.
bool myProcessing
Flag indicating whether the state is currently processing.
Definition playerStationState.h:301
void _onExit()
Internal implementation of onExit.
DtPlayerStationApp & app() const
Gets a reference to the player station application.
DtPlayerStationState(DtPlayerStationApp &app, std::string stateType)
Constructor.
DtPlayerStationApp & myApp
Reference to the player station application.
Definition playerStationState.h:248
bool exiting() const
Checks if this state is queued to be exited.
virtual void onEnter(const DtPlayerStationStateArgs *args)
Called when the state is added to the stack.
Definition playerStationState.h:120
virtual void onExposed(const DtPlayerStationStateArgs *args)
Called when all states above this state are removed.
Definition playerStationState.h:139
bool popSelf()
Pops this state from the stack.
DtPlayerStationState(const DtPlayerStationState &)=delete
Deleted copy constructor.
void _onEnter(const DtPlayerStationStateArgs *args)
Internal implementation of onEnter.
friend class DtPlayerStationStateManager
Definition playerStationState.h:88
void _onExposed(const DtPlayerStationStateArgs *args)
Internal implementation of onExposed.
void _onStacked()
Internal implementation of onStacked.
DtPlayerStationState & operator=(const DtPlayerStationState &)=delete
Deleted assignment operator.
virtual ~DtPlayerStationState()
Virtual destructor.
virtual void tick(double dt)
Main update function called while this state is active.
Definition playerStationState.h:157
virtual void onStacked()
Called when a state is added to the stack on top of this one.
Definition playerStationState.h:132
bool myStacking
Flag indicating whether a state push is pending.
Definition playerStationState.h:293
int myStackPos
Position of this state in the stack (0 is bottom)
Definition playerStationState.h:281
bool myExiting
Flag indicating whether a state transition is pending.
Definition playerStationState.h:290
bool myExposed
Flag indicating whether this state is at the top of the stack.
Definition playerStationState.h:287
bool popUntilState(const std::string &parentState, DtPlayerStationStateArgs *args=NULL)
Pops states until a specific parent state is exposed.
bool myActive
Flag indicating whether this state is in the stack.
Definition playerStationState.h:284
bool pushState(const std::string &newState, DtPlayerStationStateArgs *args=NULL)
Pushes a new state onto the stack.
bool exposed() const
Checks if this state is currently the topmost state.
bool pushSelf()
Pushes a copy of this state onto the stack.
const std::string & stateType() const
Gets the string identifier for this state's type.
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
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
Defines the base class for all VREngage messages.