VR-Engage  2.3
Loading...
Searching...
No Matches
playerStation.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file playerStation.h
7//! \brief Defines the DtPlayerStation class for managing engaged roles
8//!
9//! This file contains the DtPlayerStation class, which manages the user interface
10//! for engaged roles in VR-Engage. It handles input, views, components, and connectors
11//! to control entities in the simulation. DtPlayerStation instances are created when
12//! a user engages with an entity to play a specific role.
13
14#pragma once
15
18#include "vreUtil/attribute.h"
21
22#include "vreUtil/initializer.h"
23
24#include <vrvCore/DtUniqueID.h>
25
26#include <string>
27#include <map>
28#include <vector>
29
30namespace makVre
31{
32//! \brief Class for managing the user interface for engaged roles
33//!
34//! DtPlayerStation manages all aspects of a user's engagement with a simulation entity.
35//! It is responsible for handling user input, presenting the appropriate views, and
36//! controlling the entity according to the selected role.
37//!
38//! Each instance is configured with a role definition that specifies:
39//! - Components that provide specific functionality
40//! - Connectors that handle communication with simulation protocols
41//! - Input mappings that translate user actions to simulation commands
42//! - Menu configurations for role-specific UI elements
43//!
44//! The class maintains an attribute store for data sharing between components and
45//! manages the lifecycle of all component instances.
46//!
47//! This class may be referred to as a Role, Station, or Player in different contexts.
48//!
49//! \warning Not thread safe. Used only in the display engine's main render thread.
51{
52public:
53 //! \brief Type definition for a list of player components
54 using ComponentList = std::vector<DtPlayerComponent*>;
55
56 //! \brief Type definition for a list of connector names
57 using ConnectorNameList = std::vector<std::string>;
58
59 //! \brief Constructor for a player station
60 //! \param app Pointer to the player station application
61 //! \param playerDefinition Table containing the role definition
62 //! \param attachTo ID of the entity this station will control
63 //! \param stationName Name of the chosen role
64 //! \param displayLayout Name of the display layout to use
65 //!
66 //! Creates a new player station instance when a role is engaged. This happens when:
67 //! - A user clicks Engage from the Choose Role menu
68 //! - The station is configured to auto-engage at startup
69 //! - A secondary role is created when an entity embarks in a host slot
70 //!
71 //! The playerDefinition parameter contains the complete role configuration,
72 //! which is assembled by merging the role definition from the .lua file,
73 //! overrides from the .entity file, and display layout-specific settings.
74 //! The stationName parameter identifies the chosen role from those available
75 //! for the entity type. Available roles are defined in the .entity file, with
76 //! each role specifying a .lua file containing its definition.
77 //!
78 //! Role configuration files are located in the vre-roles-dir folder as defined
79 //! in the .sms file (default: "data/simulationModelSets/VR-Engage/roles").
80 //!
81 //! The displayLayout parameter specifies which layout to use with the chosen role.
82 //! Roles can support multiple layouts that arrange views differently. Some roles
83 //! only support a single layout, often called "default".
84 //!
85 //! \warning DtPlayerStation instances should only be created using
86 //! DtPlayerStationApp::createPlayerStation and destroyed using
87 //! DtPlayerStationApp::destroyPlayerStation.
88 //!
89 //! \see DtPlayerStationApp::createPlayerStation
90 DtPlayerStation(DtPlayerStationApp* app, DtInitTable playerDefinition, DtEntityIdentifier attachTo,
91 std::string stationName, std::string displayLayout);
92
93 //! \brief Destructor for a player station
94 //!
95 //! If this instance was created as a secondary role as a result of the primary ownship embarking
96 //! in a role slot, then this secondary instance is destroyed when the primary ownship leaves
97 //! that slot. If this instance is a primary role, then this instance is destroyed either when the
98 //! VR-Engage app is closed, or when the user returns to the Choose Role panel.
100
101 //! \brief Pre-initialization: validation, attribute setup, and state flags
102 //! \return True if validation passed, false on fatal error
103 //!
104 //! Validates the entity, element, type, and role definition. Sets up
105 //! initial attributes in the attribute store and resets internal state
106 //! flags. Must be called before initializeComponents().
107 virtual bool preInit();
108
109 //! \brief Instantiates all components in the role definition and calls
110 //! initialize() on each.
111 //! \return True if all components initialized successfully
112 //!
113 //! Called after preInit(). Creates component instances via the component
114 //! factory and invokes initialize() with the per-component config table.
115 virtual bool initializeComponents();
116
117 //! \brief Calls postInitialize() on all components.
118 //! \return True if all components return true from postInitialize()
119 //!
120 //! Called after initializeComponents(). Gives each component a chance
121 //! to perform setup that depends on peer components being initialized.
123
124 //! \brief Initializes menus via the DtActionMenuUpdater component.
125 //! \return True if menu initialization succeeded (or no menu component)
126 //!
127 //! Called after postInitializeComponents(). Passes the menuConfig table
128 //! from the role definition to the action menu updater component.
129 virtual bool initializeMenus();
130
131 //! \brief Post-pipeline finalization: connectors, PlayerCreatedMessage
132 //! \return True always (currently cannot fail)
133 //!
134 //! Sets up connectors, sends the PlayerCreatedMessage, and performs
135 //! any remaining post-menu-init work. Must be called after
136 //! initializeMenus().
137 virtual bool finishInit();
138
139 //! \brief Updates the player station
140 //! \param dt Seconds elapsed since the previous tick
141 //!
142 //! Called once per display engine frame to update the player station.
143 //! Updates all components and processes any pending operations.
144 virtual void tick(double dt);
145
146 //! \brief Shuts down the player station
147 //!
148 //! Performs teardown of all setup done during initialization:
149 //! - Destroys all components
150 //! - Requests connectors to be stopped
151 //! - Unregisters message handlers
152 //!
153 //! Called when disengaging from this role, or when suspending this
154 //! role temporarily when embarking into a different role slot.
155 virtual void shutdown();
156
157 //! \brief Acquires VR-Engage role control for this station.
158 //!
159 //! Queues a VrfSetVreControlledMessage that marks this station's role types as controlled.
160 //! Does not queue a request for spectators, stations without role types, or meta roles. The
161 //! optional override lets a sub-role report control using its owning station's name.
162 //!
163 //! \param stationNameOverride Station name to include in the control request. Uses stationName()
164 //! when empty.
165 //!
166 //! DtLoadingPlayerState calls this method when initialization reaches its Complete phase.
167 //! Override this method when a specialized station needs a different role-acquisition operation.
168 virtual void takeVreControl(const std::string& stationNameOverride = "");
169
170 //! \brief Releases VR-Engage role control for this station.
171 //!
172 //! Called by DtPlayerStationApp immediately before permanently deleting a station. Shutdown
173 //! preserves role occupancy so a suspended station remains engaged while another station is
174 //! active. Does not queue a request for stations without role types or meta roles. The optional
175 //! override lets a sub-role release control using its owning station's name.
176 //!
177 //! \param stationNameOverride Station name to include in the release request. Uses stationName()
178 //! when empty.
179 //!
180 //! Override this method when a specialized station needs a different role-release operation.
181 virtual void releaseVreControl(const std::string& stationNameOverride = "");
182
183 //! \brief Provides debugging information
184 //!
185 //! Invoked from the ImGui debug menu system to populate the Player tab
186 //! of the Shift-F4 debug menu. Displays component status, attribute values,
187 //! and other diagnostic information about the player station.
188 virtual void debugPlayer();
189
190 //! \brief Gets the player attribute store
191 //! \return Reference to the attribute store
192 //!
193 //! The attribute store contains player/role data shared between components.
195
196 //! \brief Gets the player attribute store (const version)
197 //! \return Const reference to the attribute store
199
200 //! \brief Gets the ownship state attribute handle
201 //! \return Const reference to the ownship state attribute handle
202 virtual const DtAttributeHandle& ownshipState() const;
203
204 //! \brief Finds a component by type and optional name
205 //! \tparam COMP_TYPE The component type to find
206 //! \param name Optional name to match (empty to match by type only)
207 //! \return Pointer to the found component, or NULL if not found
208 //!
209 //! Returns the first component that matches the template type. If a name is provided,
210 //! both the type and name must match to find the component.
211 template <typename COMP_TYPE>
212 COMP_TYPE* findComponent(const std::string& name = "");
213
214 //! \brief Finds all components of a specific type
215 //! \tparam COMP_TYPE The component type to find
216 //! \return Vector of pointers to matching components
217 //!
218 //! Returns a list of all components that match the specified template type.
219 template <typename COMP_TYPE>
220 std::vector<COMP_TYPE*> findComponents();
221
222 //! \brief Gets the list of connectors by type
223 //! \param connectionType The type of connectors to retrieve
224 //! \return List of connector names for the specified type
225 //!
226 //! Returns a list of connectors belonging to this role with the
227 //! ConnectorType protocol suffix appended to each name.
229
230 //! \brief Gets the player station application
231 //! \return Pointer to the application
232 //!
233 //! Returns a pointer to the VR-Engage DtPlayerStationApp singleton.
234 //! This pointer is provided in the constructor and saved for convenience.
235 //! It should never be null and should remain valid for the life of this class.
236 virtual DtPlayerStationApp* app() const;
237
238 //! \brief Gets the display engine
239 //! \return Pointer to the display engine
240 //!
241 //! Returns a pointer to the VR-Vantage DtDe singleton. This pointer is
242 //! obtained from app() during construction and saved for convenience.
243 //! It should never be null and should remain valid for the life of this class.
244 virtual makVrv::DtDe* de() const;
245
246 //! \brief Gets the element ID for the controlled entity
247 //! \return Element ID of the controlled entity
248 //!
249 //! Returns the DtElementID associated with the entity that
250 //! this player station controls in the display engine.
251 virtual makVrv::DtElementID elementId() const;
252
253 //! \brief Gets the entity ID for the controlled entity
254 //! \return Reference to the entity identifier
255 //!
256 //! Returns the DtEntityIdentifier of the entity that this player station controls.
257 virtual const DtEntityIdentifier& entityId() const;
258
259 //! \brief Gets the entity type for the controlled entity
260 //! \return Reference to the entity type
261 //!
262 //! Returns the DtEntityType of the entity that this player station controls.
263 virtual const DtEntityType& entityType() const;
264
265 //! \brief Gets the name of the role
266 //! \return String containing the station name
267 //!
268 //! Returns the name of the role that this player station represents.
269 //! This name is provided in the constructor and saved for reference.
270 //! It should not change during the life of the player station.
271 virtual std::string stationName() const;
272
273 //! \brief Gets the display layout name
274 //! \return String containing the display layout name
275 //!
276 //! Returns the name of the display layout chosen when the role was engaged.
277 //! This is provided in the constructor and saved for reference.
278 //! It should not change during the life of the player station.
279 virtual std::string displayLayout() const;
280
281 //! \brief Gets the list of joystick function groups
282 //! \return List of joystick function groups
283 //!
284 //! Returns a list of joystick function groups that this station will control.
285 //! Used internally with the VR-Forces Remote Control joystick API.
286 //! This list is defined by the "roleTypes" table in the Role definition.
287 //! Note that these strings do not correspond to "Roles" as the term is generally used
288 //! elsewhere, meaning a PlayerStation definition or instance.
289 virtual std::vector<std::string> roleTypes() const;
290
291 //! @returns true unless the Role has been suspended in the case of an emplicit engagement
292 //! of an embarked Role. If the player embarks in a role slot, the active primary PlayerStation
293 //! is supsneded and a new secondary PlayerStation instance is created for the embarked Role.
294 virtual bool isActive() const;
295
296 //! @returns True after shutdown has been called and remains True until the PlayerStation
297 //! instance is either destroyed or re-initialized by calling init again.
298 virtual bool isShuttingDown() const;
299
300 //! @returns the lua config table defining the Connector, Component, and Menu configuration
301 //! for this Role. This is the final configuration for this Role, after filtering and merging
302 //! the associated Display Layout configuration. This table is built when the user engages
303 //! a role and before the DtPlayerStation instance is created. It should not change thereafter.
304 virtual DtInitTable definition() const;
305
306 //! \brief Calls finalizeInitTick(dt) on every component where componentReady() is false.
307 //!
308 //! For each unready component, calls finalizeInitTick(dt). The component itself is
309 //! responsible for setting myComponentReady = true when returning Ready.
310 //! If finalizeInitTick() returns Failed, records the failure (queryable via
311 //! anyComponentFailed()).
312 //!
313 //! Called by LOADING_PLAYER_STATE during its ComponentReadiness phase.
314 //!
315 //! \param dt Time in seconds since the last tick
316 virtual void tickUnreadyComponents(double dt);
317
318 //! \brief Returns true when every component reports componentReady() == true.
319 //!
320 //! Used by LOADING_PLAYER_STATE to determine when to advance from the
321 //! ComponentReadiness phase to Complete.
322 virtual bool allComponentsReady() const;
323
324 //! \brief Returns true if any component's most recent finalizeInitTick() returned Failed.
325 //!
326 //! Used by LOADING_PLAYER_STATE to detect unrecoverable initialization failures
327 //! and abort back to CHOOSE_ROLE_STATE.
328 virtual bool anyComponentFailed() const;
329
330protected:
331 //! Handler for the SimulationStateMessage for this station's ownship entity. This function
332 //! is used only to cache initial state values of the ownship entity from the first message
333 //! receied after engagement. This handler is then unregistered. The initial state values
334 //! are saved in the AttributeStore as:
335 //! <DtEntityType> "entityType"
336 //! <DtVector> "InitialPosition"
337 //! <DtTaitBryan> "InitialOrientation"
338 //! <DtEntityIdentifier> "InitialParent"
340
341 //! Writes name, type, and value of each attribute in Attribute Store to a human-readable
342 //! string. Returns vector of these strings, one per attribute. Used by debugPlayer to write
343 //! the data in the Attribute Store to the F4 menu or log file.
344 // static std::list<std::string> serializeState(DtAttributeHandle& state, int depth = 0);
345
346 //! Pointer to DtPlayerStationApp singleton that created and owns this DtPlayerStation
347 //! instance. This should never be null.
349
350 // Contains definition for this PlayerStation/Role, includes all Connectors and Components
351 // and their config data. Table has been filtered and merged using config for the chosen
352 // display layout. Not a copy of original role config in lua file; does not contain data
353 // for other display layouts.
355
356 // Identifiers and properties of ownship entity controlled by this station
357 makVrv::DtElementID myElementId;
358 DtEntityIdentifier myEntityId;
359 DtEntityType myEntityType;
360 DtForceType myForceType;
361
362 // List of string identfiers for joystick function groups, see roleTypes() function above.
363 std::vector<std::string> myRoleTypes;
364
365 // Role and Display Layout name chosen when engaging this role. These were used to
366 // compose the final playerDefinition, and are saved here for later reference. These
367 // are not expected to change for the life of the DtPlayerStation instance.
368 std::string myStationName;
369 std::string myDisplayLayout;
370
371 // Stores data shared between components
373
375
376 // Component instances for this station/role.
378
379 // Names of Connectors for this station/role. Note the actual instances are owned and
380 // managed by the ConnectorAccessory in the net thread.
382
383 // Status of this PlayerStation instance. myActive == !myShuttingDown. Set in init()
384 // and shutdown().
385 // TODO: These are redundant, we don't need both of these.
388
389 // Convenience pointer found from app()->de(); should never be null, should never change.
390 makVrv::DtDe* myDe;
391
392 //! \brief Set to true if any component's finalizeInitTick() returns Failed.
393 //!
394 //! Reset in preInit(). Checked by LOADING_PLAYER_STATE via anyComponentFailed().
396
397 //! \brief Cached result of allComponentsReady().
398 //!
399 //! Once all components report ready, this is set to true so subsequent
400 //! calls avoid iterating the entire component list every tick.
401 //! Reset in preInit(). Mutable because allComponentsReady() is const
402 //! and this is a pure cache of the iteration result.
403 mutable bool myAllComponentsReady = false;
404
405 //! \brief True once the full init pipeline (preInit through finishInit)
406 //! has completed. Prevents tick() from running on a partially built station.
407 //!
408 //! Reset in preInit(), set in finishInit().
409 bool myFullyInitialized = false;
410
411 std::map<size_t, bool[4]> myDebugAttributeTraceChecks;
412};
413
414template <typename COMP_TYPE>
415COMP_TYPE* DtPlayerStation::findComponent(const std::string& name)
416{
417 for (int compIndex = 0; compIndex < myComponents.size(); compIndex++)
418 {
419 if (COMP_TYPE* comp = dynamic_cast<COMP_TYPE*>(myComponents[compIndex]))
420 {
421 if (name.empty())
422 {
423 return comp;
424 }
425 else
426 {
427 if (name == comp->name())
428 {
429 return comp;
430 }
431 }
432 }
433 }
434
435 return nullptr;
436}
437
438template <typename COMP_TYPE>
439std::vector<COMP_TYPE*> DtPlayerStation::findComponents()
440{
441 std::vector<COMP_TYPE*> ret;
442
443 for (int compIndex = 0; compIndex < myComponents.size(); compIndex++)
444 {
445 if (COMP_TYPE* comp = dynamic_cast<COMP_TYPE*>(myComponents[compIndex]))
446 {
447 ret.push_back(comp);
448 }
449 }
450
451 return ret;
452}
453
454} // namespace makVre
Provides attribute handling system for hierarchical data storage and manipulation.
Handle class for safe access to attributes.
Definition attributeHandle.h:30
DtConnectorType
Enumeration of supported connector types.
Definition connector.h:40
Table-based access to Lua state for configuration data.
Definition initializer.h:38
Top-level class representing the VR-Engage application.
Definition playerStationApp.h:155
virtual const DtAttributeHandle & ownshipState() const
Gets the ownship state attribute handle.
virtual void releaseVreControl(const std::string &stationNameOverride="")
Releases VR-Engage role control for this station.
virtual ConnectorNameList connectors(DtConnector::DtConnectorType connectionType)
Gets the list of connectors by type.
makVrv::DtDe * myDe
Definition playerStation.h:390
COMP_TYPE * findComponent(const std::string &name="")
Finds a component by type and optional name.
Definition playerStation.h:415
virtual void tickUnreadyComponents(double dt)
Calls finalizeInitTick(dt) on every component where componentReady() is false.
std::vector< DtPlayerComponent * > ComponentList
Type definition for a list of player components.
Definition playerStation.h:54
std::vector< std::string > myRoleTypes
Definition playerStation.h:363
virtual bool preInit()
Pre-initialization: validation, attribute setup, and state flags.
DtInitTable myPlayerDefinition
Definition playerStation.h:354
std::map< size_t, bool[4]> myDebugAttributeTraceChecks
Definition playerStation.h:411
DtPlayerStation(DtPlayerStationApp *app, DtInitTable playerDefinition, DtEntityIdentifier attachTo, std::string stationName, std::string displayLayout)
Constructor for a player station.
virtual ~DtPlayerStation()
Destructor for a player station.
std::string myStationName
Definition playerStation.h:368
bool myFullyInitialized
True once the full init pipeline (preInit through finishInit) has completed. Prevents tick() from run...
Definition playerStation.h:409
DtEntityType myEntityType
Definition playerStation.h:359
virtual void tick(double dt)
Updates the player station.
virtual std::string displayLayout() const
Gets the display layout name.
const DtAttributeHandle myOwnshipState
Definition playerStation.h:374
virtual DtAttributeHandle & playerAttributeStore()
Gets the player attribute store.
virtual void debugPlayer()
Provides debugging information.
virtual void takeVreControl(const std::string &stationNameOverride="")
Acquires VR-Engage role control for this station.
DtForceType myForceType
Definition playerStation.h:360
virtual DtInitTable definition() const
virtual bool isActive() const
virtual bool postInitializeComponents()
Calls postInitialize() on all components.
makVrv::DtElementID myElementId
Definition playerStation.h:357
std::string myDisplayLayout
Definition playerStation.h:369
virtual DtPlayerStationApp * app() const
Gets the player station application.
std::vector< COMP_TYPE * > findComponents()
Finds all components of a specific type.
Definition playerStation.h:439
virtual bool initializeComponents()
Instantiates all components in the role definition and calls initialize() on each.
DtAttributeHandle myAttributeStore
Definition playerStation.h:372
virtual bool allComponentsReady() const
Returns true when every component reports componentReady() == true.
virtual void shutdown()
Shuts down the player station.
bool myActive
Definition playerStation.h:386
std::vector< std::string > ConnectorNameList
Type definition for a list of connector names.
Definition playerStation.h:57
ConnectorNameList myConnectorNames
Definition playerStation.h:381
virtual const DtAttributeHandle & playerAttributeStore() const
Gets the player attribute store (const version)
virtual makVrv::DtElementID elementId() const
Gets the element ID for the controlled entity.
virtual bool anyComponentFailed() const
Returns true if any component's most recent finalizeInitTick() returned Failed.
bool myAllComponentsReady
Cached result of allComponentsReady().
Definition playerStation.h:403
virtual makVrv::DtDe * de() const
Gets the display engine.
virtual DtVreMessageResult handleSimulationStateMessage(makVre::DtVreMessage *msg)
Handler for the SimulationStateMessage for this station's ownship entity. This function is used only ...
virtual std::string stationName() const
Gets the name of the role.
virtual bool isShuttingDown() const
DtPlayerStationApp * myApp
Writes name, type, and value of each attribute in Attribute Store to a human-readable string....
Definition playerStation.h:348
bool myAnyComponentFailed
Set to true if any component's finalizeInitTick() returns Failed.
Definition playerStation.h:395
virtual const DtEntityIdentifier & entityId() const
Gets the entity ID for the controlled entity.
ComponentList myComponents
Definition playerStation.h:377
bool myShuttingDown
Definition playerStation.h:387
virtual bool finishInit()
Post-pipeline finalization: connectors, PlayerCreatedMessage.
virtual const DtEntityType & entityType() const
Gets the entity type for the controlled entity.
DtEntityIdentifier myEntityId
Definition playerStation.h:358
virtual std::vector< std::string > roleTypes() const
Gets the list of joystick function groups.
virtual bool initializeMenus()
Initializes menus via the DtActionMenuUpdater component.
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
Provides configuration initialization for VREngage components.
!
Definition glsVreMessageUtil.h:50
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
Defines the DtPlayerComponent base class for VR-Engage role components.
Defines the DtPlayerStationApp class for the VR-Engage application.
Defines the base class for all VREngage messages.