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