VR-Engage  2.2
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 Initializes the player station
102 //! \return True if all components initialized successfully, false otherwise
103 //!
104 //! Performs all required setup to create a functional role:
105 //! - Instantiates all components defined in the role definition
106 //! - Requests connectors to be started
107 //! - Registers message handlers
108 //! - Calls init() and postInit() on all components
109 //!
110 //! This method is called when initially engaging this role or when
111 //! returning to this role from an embarked role.
112 virtual bool init();
113
114 //! \brief Updates the player station
115 //! \param dt Seconds elapsed since the previous tick
116 //!
117 //! Called once per display engine frame to update the player station.
118 //! Updates all components and processes any pending operations.
119 virtual void tick(double dt);
120
121 //! \brief Shuts down the player station
122 //!
123 //! Performs teardown of all setup done during initialization:
124 //! - Destroys all components
125 //! - Requests connectors to be stopped
126 //! - Unregisters message handlers
127 //!
128 //! Called when disengaging from this role, or when suspending this
129 //! role temporarily when embarking into a different role slot.
130 virtual void shutdown();
131
132 //! \brief Provides debugging information
133 //!
134 //! Invoked from the ImGui debug menu system to populate the Player tab
135 //! of the Shift-F4 debug menu. Displays component status, attribute values,
136 //! and other diagnostic information about the player station.
137 virtual void debugPlayer();
138
139 //! \brief Gets the player attribute store
140 //! \return Reference to the attribute store
141 //!
142 //! The attribute store contains player/role data shared between components.
144
145 //! \brief Gets the player attribute store (const version)
146 //! \return Const reference to the attribute store
148
149 //! \brief Finds a component by type and optional name
150 //! \tparam COMP_TYPE The component type to find
151 //! \param name Optional name to match (empty to match by type only)
152 //! \return Pointer to the found component, or NULL if not found
153 //!
154 //! Returns the first component that matches the template type. If a name is provided,
155 //! both the type and name must match to find the component.
156 template <typename COMP_TYPE>
157 COMP_TYPE* findComponent(const std::string& name = "");
158
159 //! \brief Finds all components of a specific type
160 //! \tparam COMP_TYPE The component type to find
161 //! \return Vector of pointers to matching components
162 //!
163 //! Returns a list of all components that match the specified template type.
164 template <typename COMP_TYPE>
165 std::vector<COMP_TYPE*> findComponents();
166
167 //! \brief Gets the list of connectors by type
168 //! \param connectionType The type of connectors to retrieve
169 //! \return List of connector names for the specified type
170 //!
171 //! Returns a list of connectors belonging to this role with the
172 //! ConnectorType protocol suffix appended to each name.
174
175 //! \brief Gets the player station application
176 //! \return Pointer to the application
177 //!
178 //! Returns a pointer to the VR-Engage DtPlayerStationApp singleton.
179 //! This pointer is provided in the constructor and saved for convenience.
180 //! It should never be null and should remain valid for the life of this class.
181 virtual DtPlayerStationApp* app() const;
182
183 //! \brief Gets the display engine
184 //! \return Pointer to the display engine
185 //!
186 //! Returns a pointer to the VR-Vantage DtDe singleton. This pointer is
187 //! obtained from app() during construction and saved for convenience.
188 //! It should never be null and should remain valid for the life of this class.
189 virtual makVrv::DtDe* de() const;
190
191 //! \brief Gets the element ID for the controlled entity
192 //! \return Element ID of the controlled entity
193 //!
194 //! Returns the DtElementID associated with the entity that
195 //! this player station controls in the display engine.
196 virtual makVrv::DtElementID elementId() const;
197
198 //! \brief Gets the entity ID for the controlled entity
199 //! \return Reference to the entity identifier
200 //!
201 //! Returns the DtEntityIdentifier of the entity that this player station controls.
202 virtual const DtEntityIdentifier& entityId() const;
203
204 //! \brief Gets the entity type for the controlled entity
205 //! \return Reference to the entity type
206 //!
207 //! Returns the DtEntityType of the entity that this player station controls.
208 virtual const DtEntityType& entityType() const;
209
210 //! \brief Gets the name of the role
211 //! \return String containing the station name
212 //!
213 //! Returns the name of the role that this player station represents.
214 //! This name is provided in the constructor and saved for reference.
215 //! It should not change during the life of the player station.
216 virtual std::string stationName() const;
217
218 //! \brief Gets the display layout name
219 //! \return String containing the display layout name
220 //!
221 //! Returns the name of the display layout chosen when the role was engaged.
222 //! This is provided in the constructor and saved for reference.
223 //! It should not change during the life of the player station.
224 virtual std::string displayLayout() const;
225
226 //! \brief Gets the list of joystick function groups
227 //! \return List of joystick function groups
228 //!
229 //! Returns a list of joystick function groups that this station will control.
230 //! Used internally with the VR-Forces Remote Control joystick API.
231 //! This list is defined by the "roles" table in the Role definition.
232 //! Note that these strings do not correspond to "Roles" as the term is generally used
233 //! elsewhere, meaning a PlayerStation definition or instance.
234 // TODO: rename this function and associated member var and config table; this is confusing.
235 virtual std::vector<std::string> roles() const;
236
237 //! @returns true unless the Role has been suspended in the case of an emplicit engagement
238 //! of an embarked Role. If the player embarks in a role slot, the active primary PlayerStation
239 //! is supsneded and a new secondary PlayerStation instance is created for the embarked Role.
240 virtual bool isActive() const;
241
242 //! @returns True after shutdown has been called and remains True until the PlayerStation
243 //! instance is either destroyed or re-initialized by calling init again.
244 virtual bool isShuttingDown() const;
245
246 //! @returns the lua config table defining the Connector, Component, and Menu configuration
247 //! for this Role. This is the final configuration for this Role, after filtering and merging
248 //! the associated Display Layout configuration. This table is built when the user engages
249 //! a role and before the DtPlayerStation instance is created. It should not change thereafter.
250 virtual DtInitTable definition() const;
251
252protected:
253 //! Handler for the SimulationStateMessage for this station's ownship entity. This function
254 //! is used only to cache initial state values of the ownship entity from the first message
255 //! receied after engagement. This handler is then unregistered. The initial state values
256 //! are saved in the AttributeStore as:
257 //! <DtEntityType> "entityType"
258 //! <DtVector> "InitialPosition"
259 //! <DtTaitBryan> "InitialOrientation"
260 //! <DtEntityIdentifier> "InitialParent"
262
263 //! Called from init. Calls initializeMenus on the DtActionMenuUpdater Component, if present,
264 //! passing in the menuConfig table from the role definition.
265 virtual bool initializeMenus();
266
267 //! Called from init. Instantiates all components in the role definition and invokes their
268 //! init function. Returns true if all components return true from component init.
269 virtual bool initializeComponents();
270
271 //! Called from init. Invokes postInit function on all components. Called only after and if
272 //! initializeComponents has returned true. Returns true if all components return true from
273 //! postInit.
275
276 //! Writes name, type, and value of each attribute in Attribute Store to a human-readable
277 //! string. Returns vector of these strings, one per attribute. Used by debugPlayer to write
278 //! the data in the Attribute Store to the F4 menu or log file.
279 // static std::list<std::string> serializeState(DtAttributeHandle& state, int depth = 0);
280
281 //! Pointer to DtPlayerStationApp singleton that created and owns this DtPlayerStation
282 //! instance. This should never be null.
284
285 // Contains definition for this PlayerStation/Role, includes all Connectors and Components
286 // and their config data. Table has been filtered and merged using config for the chosen
287 // display layout. Not a copy of original role config in lua file; does not contain data
288 // for other display layouts.
290
291 // Identifiers and properties of ownship entity controlled by this station
292 makVrv::DtElementID myElementId;
293 DtEntityIdentifier myEntityId;
294 DtEntityType myEntityType;
295 DtForceType myForceType;
296
297 // List of string identfiers for joystick function groups, see roles() function above.
298 std::vector<std::string> myRoles;
299
300 // Role and Display Layout name chosen when engaging this role. These were used to
301 // compose the final playerDefinition, and are saved here for later reference. These
302 // are not expected to change for the life of the DtPlayerStation instance.
303 std::string myStationName;
304 std::string myDisplayLayout;
305
306 // Stores data shared between components
308
309 // Component instances for this station/role.
311
312 // Names of Connectors for this station/role. Note the actual instances are owned and
313 // managed by the ConnectorAccessory in the net thread.
315
316 // Status of this PlayerStation instance. myActive == !myShuttingDown. Set in init()
317 // and shutdown().
318 // TODO: These are redundant, we don't need both of these.
321
322 // Convenience pointer found from app()->de(); should never be null, should never change.
323 makVrv::DtDe* myDe;
324
325 std::map<unsigned long long, bool[4]> myDebugAttributeTraceChecks;
326};
327
328template <typename COMP_TYPE>
329COMP_TYPE* DtPlayerStation::findComponent(const std::string& name)
330{
331 for (int compIndex = 0; compIndex < myComponents.size(); compIndex++)
332 {
333 if (COMP_TYPE* comp = dynamic_cast<COMP_TYPE*>(myComponents[compIndex]))
334 {
335 if (name.empty())
336 {
337 return comp;
338 }
339 else
340 {
341 if (name == comp->name())
342 {
343 return comp;
344 }
345 }
346 }
347 }
348
349 return nullptr;
350}
351
352template <typename COMP_TYPE>
353std::vector<COMP_TYPE*> DtPlayerStation::findComponents()
354{
355 std::vector<COMP_TYPE*> ret;
356
357 for (int compIndex = 0; compIndex < myComponents.size(); compIndex++)
358 {
359 if (COMP_TYPE* comp = dynamic_cast<COMP_TYPE*>(myComponents[compIndex]))
360 {
361 ret.push_back(comp);
362 }
363 }
364
365 return ret;
366}
367
368} // 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:36
Table-based access to Lua state for configuration data.
Definition initializer.h:38
Top-level class representing the VR-Engage application.
Definition playerStationApp.h:163
virtual ConnectorNameList connectors(DtConnector::DtConnectorType connectionType)
Gets the list of connectors by type.
makVrv::DtDe * myDe
Definition playerStation.h:323
COMP_TYPE * findComponent(const std::string &name="")
Finds a component by type and optional name.
Definition playerStation.h:329
std::vector< DtPlayerComponent * > ComponentList
Type definition for a list of player components.
Definition playerStation.h:54
virtual bool init()
Initializes the player station.
DtInitTable myPlayerDefinition
Definition playerStation.h:289
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::vector< std::string > myRoles
Definition playerStation.h:298
virtual std::vector< std::string > roles() const
Gets the list of joystick function groups.
std::string myStationName
Definition playerStation.h:303
DtEntityType myEntityType
Definition playerStation.h:294
virtual void tick(double dt)
Updates the player station.
virtual std::string displayLayout() const
Gets the display layout name.
virtual DtAttributeHandle & playerAttributeStore()
Gets the player attribute store.
virtual void debugPlayer()
Provides debugging information.
DtForceType myForceType
Definition playerStation.h:295
virtual DtInitTable definition() const
std::map< unsigned long long, bool[4]> myDebugAttributeTraceChecks
Definition playerStation.h:325
virtual bool isActive() const
virtual bool postInitializeComponents()
Called from init. Invokes postInit function on all components. Called only after and if initializeCom...
makVrv::DtElementID myElementId
Definition playerStation.h:292
std::string myDisplayLayout
Definition playerStation.h:304
virtual DtPlayerStationApp * app() const
Gets the player station application.
std::vector< COMP_TYPE * > findComponents()
Finds all components of a specific type.
Definition playerStation.h:353
virtual bool initializeComponents()
Called from init. Instantiates all components in the role definition and invokes their init function....
DtAttributeHandle myAttributeStore
Definition playerStation.h:307
virtual void shutdown()
Shuts down the player station.
bool myActive
Definition playerStation.h:319
std::vector< std::string > ConnectorNameList
Type definition for a list of connector names.
Definition playerStation.h:57
ConnectorNameList myConnectorNames
Definition playerStation.h:314
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 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:283
virtual const DtEntityIdentifier & entityId() const
Gets the entity ID for the controlled entity.
ComponentList myComponents
Definition playerStation.h:310
bool myShuttingDown
Definition playerStation.h:320
virtual const DtEntityType & entityType() const
Gets the entity type for the controlled entity.
DtEntityIdentifier myEntityId
Definition playerStation.h:293
virtual bool initializeMenus()
Called from init. Calls initializeMenus on the DtActionMenuUpdater Component, if present,...
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.
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
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.