VR-Engage  2.3
Loading...
Searching...
No Matches
DtVirtualRealityControllerState.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file DtVirtualRealityControllerState.h
7//! \brief Defines a class that encapsulates the state of a VR controller
8//!
9//! This file contains the definition of the DtVirtualRealityControllerState class,
10//! which represents the complete state of a VR controller at a specific point in time.
11//! This includes its position, orientation, button states, touch states, and analog
12//! input values. The class also tracks which aspects of the controller state have
13//! changed since the last update cycle.
14
15#pragma once
16
18
19#include <vrvUtil/DtQuat.h>
20#include <vrvMath/DtVector2.h>
21
22#include <matrix/vlVector.h>
23
24namespace makVrv
25{
26//! \brief Encapsulates the complete state of a virtual reality controller
27//!
28//! This class represents the state of a VR controller at a specific point in time,
29//! including its position, orientation, button states, touch states, and analog
30//! input values. It also tracks which aspects of the controller state have changed
31//! since the last update cycle, making it easier for event handlers to respond
32//! to specific types of input events (e.g., button presses, controller movements).
33class DT_DLL_VRVVRCTLR DtVirtualRealityControllerState
34{
35public:
36 //! \brief Type used for bit flags throughout the class
37 //!
38 //! This type is used for representing sets of buttons, touch surfaces,
39 //! or axes as bit flags, where each bit corresponds to a specific item.
40 using Bitfield = unsigned long long;
41
42 //! \brief Type used for controller identification
43 //!
44 //! This type is used to uniquely identify controllers in the VR system.
45 using ControllerId = unsigned int;
46
47 //! \brief Type used for 2D vector values
48 //!
49 //! This type is used for representing 2D analog input values, such as
50 //! thumbstick or touchpad positions.
51 using DtVector2f = DtVector2<float32>;
52
53 //! \brief Special value indicating no controller ID
54 //!
55 //! This constant is used to indicate that a controller ID is not set or
56 //! not applicable in the current context.
57 static const ControllerId NoId = ControllerId(-1);
58
59 //! \brief Non-exclusive controller device properties
61 {
63 Type_Left = 1 << 1, // Controller is associated with left hand
64 Type_Right = 1 << 2, // Associated with right hand
65 Type_Tracked = 1 << 3, // Position/orientation is tracked
66 Type_Input = 1 << 4, // Indicates a controller with buttons, touch and/or axes
67 Type_HMD = 1 | Type_Tracked, // Only ever one HMD, not left nor right, is tracked, no input
68 Type_LeftHand = Type_Left | Type_Input | Type_Tracked, // Indicates left hand controller
69 Type_RightHand = Type_Right | Type_Input | Type_Tracked, // Indicates right hand controller
70 };
71
72 //! \brief Unique known button IDs; controller may have additional buttons
74 {
75 Button_System = 1 << 0,
76 Button_Menu = 1 << 1,
78 Button_Grip = 1 << 3,
79 Button_A = 1 << 4,
80 Button_B = 1 << 5,
81 Button_Thumb = 1 << 6,
82 Button_Left = 1 << 7,
83 Button_Up = 1 << 8,
84 Button_Right = 1 << 9,
85 Button_Down = 1 << 10,
86 };
87
88 //! \brief Unique known touch surface IDs; controller may have additional surfaces
90 {
91 Touch_Trigger = 1 << 0,
92 Touch_Grip = 1 << 1,
93 Touch_A = 1 << 2,
94 Touch_B = 1 << 3,
95 Touch_Thumb = 1 << 4,
96 };
97
98 //! \brief Unique known axis IDs; controller may have additional axes
99 enum AxisId
100 {
106 };
107
108 //! \brief Gets the maximum number of analog axes supported by the system
109 //! \return Maximum number of supported axes
110 //!
111 //! Returns the maximum number of analog axes that can be tracked for a controller.
112 //! Not all controllers will have this many axes, but the system allocates
113 //! space for this many to accommodate all supported controller types.
114 static unsigned int MaxAxes();
115
116 //! \brief Converts an integer to the corresponding bit in a bitfield
117 //! \param n The bit position (0-63)
118 //! \return A bitfield with only the nth bit set
119 //!
120 //! This convenience function converts an integer n to a bitfield with
121 //! only the nth bit set (equivalent to 1 << n). This is useful for
122 //! working with the various bitfields used throughout this class.
123 static Bitfield bit(unsigned int n);
124
125 //! \brief Overall status of this controller.
126 //!
127 //! The first time a controller is added to the map, the event
128 //! type will be ControllerDiscovered, and all change flags will be true.
129 //! When a controller is lost, it will appear for one more event with
130 //! type ControllerLost, with the last known state. All change flags
131 //! will be false, and the controller will be removed from the map
132 //! after the event is processed.
133 //!
134 //! During normal operation, the event type for a controller will be
135 //! either NoEvent or StateChanged, indicating whether there has been
136 //! any change in the state of this controller. If event type is
137 //! StateChanged, individual change flags can be used to determine
138 //! what component or components have changed since the last event cycle.
146
147 //! \brief Default constructor
148 //!
149 //! Initializes a new controller state with default values. The controller
150 //! ID is set to NoId, event type is set to NoEvent, and all state values
151 //! and change flags are initialized to zero/false.
153
154 //! \brief Copy constructor
155 //! \param orig The controller state to copy
156 //!
157 //! Creates a new controller state that is a copy of the original.
159
160 //! \brief Virtual destructor
161 //!
162 //! Cleans up any resources associated with the controller state.
164
165 //! \brief Assignment operator
166 //! \param rhs The controller state to copy
167 //! \return Reference to this object after assignment
168 //!
169 //! Assigns the contents of another controller state to this one.
171
172 //! \brief Unique integer identifier for this VR Controller device
173 virtual ControllerId controllerId() const;
175
176 //! \brief Bitfield of controller DeviceType properties
177 virtual Bitfield controllerType() const;
179
180 //! \brief Current position of this controller.
181 //! This is an East-North-Up coordinate system with respect to the observer.
182 virtual DtVector position() const;
183 virtual void setPosition(const DtVector& pos);
184
185 //! \brief Current orientation of this controller.
186 //! This is an East-North-Up coordinate system with respect to the observer.
187 virtual DtQuat orientation() const;
188 virtual void setOrientation(const DtQuat& ori);
189
190 //! \brief Bitfield of all the button states of the controller.
191 //! Each bit represents a button state, as 1 << buttonId.
192 //! See ButtonId enum for known button IDs.
193 virtual Bitfield buttonStates() const;
195
196 //! \brief Get an individual button state by unique buttonId.
197 //! true == button down, buttonId must be < number of bits in long long.
198 virtual bool buttonState(ButtonId buttonId) const;
199 virtual void setButtonState(ButtonId buttonId, bool down);
200
201 //! \brief Bitfield of all the touch surface states of the controller.
202 //! Each bit represents a surface state, as 1 << touchId.
203 //! See TouchId enum for known touch surface IDs.
204 virtual Bitfield touchStates() const;
206
207 //! \brief Get an individual surface state by unique touchId.
208 //! true == touched, touchId must be < number of bits in long long.
209 virtual bool touchState(TouchId touchId) const;
210 virtual void setTouchState(TouchId touchId, bool touched);
211
212 //! \brief Returns the current values of a given axis.
213 //! All axes are 2-dimensional, though some only use one, usually x.
214 //! Values will be 0,0 if this controller does not have the given axis.
215 //! See AxisId enum for known axes; axisId must be < MaxAxes().
216 virtual DtVector2f axisPosition(AxisId axisId) const;
217 virtual void setAxisPosition(AxisId axisId, const DtVector2f& value);
218
219 //! \brief Returns the dead-zone values of a given axis.
220 //! Dead-zones are defined independently for x and y axis components.
221 //! Default dead-zone values are 0.01 for both dimensions.
222 //! See AxisId enum for known axes; axisId must be < MaxAxes().
223 virtual DtVector2f axisDeadZone(AxisId axisId) const;
224 virtual void setAxisDeadZone(AxisId axisId, const DtVector2f& value);
225
226 //! \brief Marks this controller as lost; event type is set to
227 //! ControllerLost. Is is expected that this controller state will be
228 //! deleted after the event processor has processed this final state.
229 virtual void setControllerAsLost();
230 virtual bool controllerWasLost() const;
231
232 //! \brief Returns a bitfield identifying all the buttons which
233 //! have changed since the last event cycle. Each bit represents a
234 //! button that changed, as 1 << buttonId.
235 //! See enum ButtonId for known button IDs.
236 virtual Bitfield buttonsChanged() const;
237
238 //! \brief Returns whether a particular button with specified ID
239 //! has changed state since the last event cycle.
240 virtual bool buttonChanged(ButtonId buttonId) const;
241
242 //! \brief Returns whether the button with specified ID was
243 //! pressed/released since the last event cycle.
244 virtual bool buttonPressed(ButtonId buttonId) const;
245 virtual bool buttonReleased(ButtonId buttonId) const;
246
247 //! \brief Returns a bitfield identifying all the surfaces which
248 //! have changed touch state since the last event cycle. Each bit
249 //! represents a touch surface that changed, as 1 << touchId.
250 //! See enum TouchId for known button IDs.
251 virtual Bitfield touchChanged() const;
252
253 //! \brief Returns whether a particular surface with specified ID
254 //! has changed touch state since the last event cycle.
255 virtual bool touchChanged(TouchId touchid) const;
256
257 //! \brief Returns whether the specified touch surface was
258 //! touched or un-touched since the last event cycle.
259 virtual bool touchStarted(TouchId touchId) const;
260 virtual bool touchEnded(TouchId touchId) const;
261
262 //! \brief Returns bitfield identifying all axes which have changed
263 //! since the last event cycle. Each bit represents an axis, as
264 //! 1 << axisId. See enum AxisId for known axes.
265 virtual Bitfield axesChanged() const;
266
267 //! \brief Returns whether a particular axis with specified ID
268 //! has had a value changed since the last event cycle.
269 virtual bool axisChanged(AxisId axisId) const;
270
271 //! \brief Returns whether there has been any change to the
272 //! tracked position or orientation of this controller since the
273 //! last event cycle.
274 virtual bool controllerMoved() const;
275
276 //! \brief Returns ControllerEventType describing the state
277 //! of this controller after the most recent update.
279
280 //! \brief Resets all change flags to 0/false and event type to NoEvent.
281 //! This should be called after the event processor has processed the
282 //! current state of events, before this controller state is updated.
283 virtual void clearEvent();
284
285protected:
286 // Called by all component set functions to indicate that the
287 // controller state has changed. If myEventType is NoEvent,
288 // it will be set to StateChanged.
289 inline virtual void markStateChanged();
290
291 // Properties of this controller, which should not change
292 //-------------------------------------------------------
294 Bitfield myControllerType; // bitfield of DeviceType(s)
295
296 // Current state of this controller
297 //---------------------------------
298 DtVector myPosition;
300
303
304 std::vector<DtVector2f> myAxisStates;
305 std::vector<DtVector2f> myAxisDeadZones;
306
307 // Flags indicating a change in state
308 //-----------------------------------
310
315};
316} // namespace makVrv
virtual void setAxisPosition(AxisId axisId, const DtVector2f &value)
DtVector2< float32 > DtVector2f
Type used for 2D vector values.
Definition DtVirtualRealityControllerState.h:51
static const ControllerId NoId
Special value indicating no controller ID.
Definition DtVirtualRealityControllerState.h:57
virtual void setButtonStates(Bitfield buttonStates)
ButtonId
Unique known button IDs; controller may have additional buttons.
Definition DtVirtualRealityControllerState.h:74
@ Button_Menu
Definition DtVirtualRealityControllerState.h:76
@ Button_B
Definition DtVirtualRealityControllerState.h:80
@ Button_Down
Definition DtVirtualRealityControllerState.h:85
@ Button_Trigger
Definition DtVirtualRealityControllerState.h:77
@ Button_Grip
Definition DtVirtualRealityControllerState.h:78
@ Button_Up
Definition DtVirtualRealityControllerState.h:83
@ Button_Right
Definition DtVirtualRealityControllerState.h:84
@ Button_System
Definition DtVirtualRealityControllerState.h:75
@ Button_Thumb
Definition DtVirtualRealityControllerState.h:81
@ Button_Left
Definition DtVirtualRealityControllerState.h:82
@ Button_A
Definition DtVirtualRealityControllerState.h:79
virtual Bitfield controllerType() const
Bitfield of controller DeviceType properties.
std::vector< DtVector2f > myAxisDeadZones
Definition DtVirtualRealityControllerState.h:305
virtual ControllerId controllerId() const
Unique integer identifier for this VR Controller device.
static Bitfield bit(unsigned int n)
Converts an integer to the corresponding bit in a bitfield.
DeviceType
Non-exclusive controller device properties.
Definition DtVirtualRealityControllerState.h:61
@ Type_Tracked
Definition DtVirtualRealityControllerState.h:65
@ Type_Undefined
Definition DtVirtualRealityControllerState.h:62
@ Type_Left
Definition DtVirtualRealityControllerState.h:63
@ Type_HMD
Definition DtVirtualRealityControllerState.h:67
@ Type_RightHand
Definition DtVirtualRealityControllerState.h:69
@ Type_Input
Definition DtVirtualRealityControllerState.h:66
@ Type_LeftHand
Definition DtVirtualRealityControllerState.h:68
@ Type_Right
Definition DtVirtualRealityControllerState.h:64
std::vector< DtVector2f > myAxisStates
Definition DtVirtualRealityControllerState.h:304
virtual bool touchChanged(TouchId touchid) const
Returns whether a particular surface with specified ID has changed touch state since the last event c...
virtual void setTouchStates(Bitfield touchStates)
virtual void setControllerType(Bitfield)
virtual DtVector position() const
Current position of this controller. This is an East-North-Up coordinate system with respect to the o...
DtVector myPosition
Definition DtVirtualRealityControllerState.h:298
virtual bool controllerMoved() const
Returns whether there has been any change to the tracked position or orientation of this controller s...
ControllerEventType
Overall status of this controller.
Definition DtVirtualRealityControllerState.h:140
@ ControllerLost
Definition DtVirtualRealityControllerState.h:143
@ StateChanged
Definition DtVirtualRealityControllerState.h:144
@ ControllerDiscovered
Definition DtVirtualRealityControllerState.h:142
@ NoEvent
Definition DtVirtualRealityControllerState.h:141
virtual bool axisChanged(AxisId axisId) const
Returns whether a particular axis with specified ID has had a value changed since the last event cycl...
virtual bool buttonReleased(ButtonId buttonId) const
virtual DtVector2f axisPosition(AxisId axisId) const
Returns the current values of a given axis. All axes are 2-dimensional, though some only use one,...
virtual bool touchState(TouchId touchId) const
Get an individual surface state by unique touchId. true == touched, touchId must be < number of bits ...
Bitfield myButtonsChanged
Definition DtVirtualRealityControllerState.h:311
TouchId
Unique known touch surface IDs; controller may have additional surfaces.
Definition DtVirtualRealityControllerState.h:90
@ Touch_B
Definition DtVirtualRealityControllerState.h:94
@ Touch_Trigger
Definition DtVirtualRealityControllerState.h:91
@ Touch_Thumb
Definition DtVirtualRealityControllerState.h:95
@ Touch_Grip
Definition DtVirtualRealityControllerState.h:92
@ Touch_A
Definition DtVirtualRealityControllerState.h:93
DtVirtualRealityControllerState()
Default constructor.
virtual DtQuat orientation() const
Current orientation of this controller. This is an East-North-Up coordinate system with respect to th...
virtual bool touchEnded(TouchId touchId) const
virtual ControllerEventType controllerEvent() const
Returns ControllerEventType describing the state of this controller after the most recent update.
virtual void setTouchState(TouchId touchId, bool touched)
virtual ~DtVirtualRealityControllerState()
Virtual destructor.
virtual bool buttonPressed(ButtonId buttonId) const
Returns whether the button with specified ID was pressed/released since the last event cycle.
unsigned long long Bitfield
Type used for bit flags throughout the class.
Definition DtVirtualRealityControllerState.h:40
virtual void clearEvent()
Resets all change flags to 0/false and event type to NoEvent. This should be called after the event p...
Bitfield myButtonStates
Definition DtVirtualRealityControllerState.h:301
virtual Bitfield touchChanged() const
Returns a bitfield identifying all the surfaces which have changed touch state since the last event c...
virtual void setAxisDeadZone(AxisId axisId, const DtVector2f &value)
DtQuat myOrientation
Definition DtVirtualRealityControllerState.h:299
virtual bool touchStarted(TouchId touchId) const
Returns whether the specified touch surface was touched or un-touched since the last event cycle.
virtual void setOrientation(const DtQuat &ori)
virtual bool buttonState(ButtonId buttonId) const
Get an individual button state by unique buttonId. true == button down, buttonId must be < number of ...
bool myControllerMoved
Definition DtVirtualRealityControllerState.h:314
virtual Bitfield touchStates() const
Bitfield of all the touch surface states of the controller. Each bit represents a surface state,...
virtual bool buttonChanged(ButtonId buttonId) const
Returns whether a particular button with specified ID has changed state since the last event cycle.
DtVirtualRealityControllerState(const DtVirtualRealityControllerState &orig)
Copy constructor.
unsigned int ControllerId
Type used for controller identification.
Definition DtVirtualRealityControllerState.h:45
virtual void setControllerAsLost()
Marks this controller as lost; event type is set to ControllerLost. Is is expected that this controll...
Bitfield myControllerType
Definition DtVirtualRealityControllerState.h:294
static unsigned int MaxAxes()
Gets the maximum number of analog axes supported by the system.
Bitfield myTouchStates
Definition DtVirtualRealityControllerState.h:302
Bitfield myTouchChanged
Definition DtVirtualRealityControllerState.h:312
ControllerId myControllerId
Definition DtVirtualRealityControllerState.h:293
virtual Bitfield buttonStates() const
Bitfield of all the button states of the controller. Each bit represents a button state,...
ControllerEventType myEventType
Definition DtVirtualRealityControllerState.h:309
AxisId
Unique known axis IDs; controller may have additional axes.
Definition DtVirtualRealityControllerState.h:100
@ Axis_Trigger
Definition DtVirtualRealityControllerState.h:102
@ Axis_Thumb
Definition DtVirtualRealityControllerState.h:101
@ Axis_4
Definition DtVirtualRealityControllerState.h:105
@ Axis_3
Definition DtVirtualRealityControllerState.h:104
@ Axis_Grip
Definition DtVirtualRealityControllerState.h:103
virtual Bitfield buttonsChanged() const
Returns a bitfield identifying all the buttons which have changed since the last event cycle....
virtual void setControllerId(ControllerId)
virtual Bitfield axesChanged() const
Returns bitfield identifying all axes which have changed since the last event cycle....
Bitfield myAxesChanged
Definition DtVirtualRealityControllerState.h:313
virtual DtVector2f axisDeadZone(AxisId axisId) const
Returns the dead-zone values of a given axis. Dead-zones are defined independently for x and y axis c...
virtual void setButtonState(ButtonId buttonId, bool down)
DtVirtualRealityControllerState & operator=(const DtVirtualRealityControllerState &rhs)
Assignment operator.
virtual void setPosition(const DtVector &pos)
Definition appLauncherComponent.h:28
Main header file for the VRV Virtual Reality Controller module.