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