VR-Engage  2.2
Loading...
Searching...
No Matches
virtualDpad.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file virtualDpad.h
7//! \ingroup vreUtil
8//! \brief Provides a virtual directional pad implementation
9//!
10//! This file defines the DtVirtualDpad class which simulates a directional pad (d-pad)
11//! based on analog input values. It converts continuous x and y axis inputs into
12//! discrete directional button states and tracks button press/release events.
13
14#pragma once
15
16//! \brief A class that simulates a directional pad from analog inputs
17//!
18//! The DtVirtualDpad converts analog x and y inputs (such as from a joystick or thumbstick)
19//! into discrete directional button states (up, down, left, right). It tracks both the
20//! current button states and state changes to provide pressed and released events.
22{
23public:
24 //! \brief Enumeration of d-pad button directions
25 enum Button
26 {
27 Up, //!< Up direction button
28 Down, //!< Down direction button
29 Left, //!< Left direction button
30 Right //!< Right direction button
31 };
32
33 //! \brief Threshold value for axis activation (default 0.5)
34 //!
35 //! Axis values with magnitude greater than this threshold will
36 //! trigger a button state change in the corresponding direction
37 const float AxisThreshold = 0.5f;
38
39 //! \brief Default constructor
40 //!
41 //! Initializes the d-pad with all buttons in the released state
42 //! and no pending state changes
44 {
45 memset(&myButtonStates, 0, sizeof(myButtonStates));
46 memset(&myButtonsChanged, 0, sizeof(myButtonsChanged));
47 }
48 //! \brief Virtual destructor
49 virtual ~DtVirtualDpad() {}
50
51 //! \brief Updates the d-pad state based on x and y input values
52 //! \param x The x-axis input value (positive = right, negative = left)
53 //! \param y The y-axis input value (positive = up, negative = down)
54 //!
55 //! This method converts the continuous x and y inputs into discrete directional
56 //! button states based on the axis with the greater magnitude. Only one direction
57 //! can be active at a time (up, down, left, or right).
58 virtual void update(const float x, const float y)
59 {
61
62 int dpad = -1;
63 if (abs(y) > abs(x)) // y wins, dpad is up/down or nothing
64 {
65 if (y > AxisThreshold)
66 {
67 dpad = Up;
68 }
69 else if (y < -AxisThreshold)
70 {
71 dpad = Down;
72 }
73 }
74 else // x wins, dpad is left/right or nothing
75 {
76 if (x > AxisThreshold)
77 {
78 dpad = Right;
79 }
80 else if (x < -AxisThreshold)
81 {
82 dpad = Left;
83 }
84 }
85
86 setButtonState(Up, dpad == Up);
87 setButtonState(Down, dpad == Down);
88 setButtonState(Right, dpad == Right);
89 setButtonState(Left, dpad == Left);
90 }
91
92 //! \brief Checks if a button was just pressed
93 //! \param button The button to check
94 //! \return True if the button was just pressed, false otherwise
95 //!
96 //! A button is considered "just pressed" if its state changed from released to pressed
97 //! since the last call to update().
98 virtual bool buttonPressed(const Button button) { return myButtonsChanged[button] && myButtonStates[button]; }
99 //! \brief Checks if a button was just released
100 //! \param button The button to check
101 //! \return True if the button was just released, false otherwise
102 //!
103 //! A button is considered "just released" if its state changed from pressed to released
104 //! since the last call to update().
105 virtual bool buttonReleased(const Button button) { return myButtonsChanged[button] && !myButtonStates[button]; }
106
107 //! \brief Clears all button events (pressed/released flags)
108 //!
109 //! This method resets the state change tracking for all buttons without
110 //! modifying their current pressed/released states. It is called automatically
111 //! at the beginning of each update() call.
112 virtual void clearEvents() { memset(&myButtonsChanged, 0, sizeof(myButtonsChanged)); }
113
114private:
115 //! \brief Sets the state of a specific button
116 //! \param button The button to set the state for
117 //! \param state True for pressed, false for released
118 //!
119 //! This method updates the button state and tracks state changes.
120 //! If the new state is different from the current state, the button
121 //! is marked as changed.
122 virtual void setButtonState(const Button button, bool state)
123 {
124 if (myButtonStates[button] != state)
125 {
126 myButtonStates[button] = state;
127 myButtonsChanged[button] = true;
128 }
129 }
130
131 //! \brief Current state of each button (true = pressed, false = released)
133
134 //! \brief Tracks whether each button state has changed since last update
136};
virtual bool buttonPressed(const Button button)
Checks if a button was just pressed.
Definition virtualDpad.h:98
virtual bool buttonReleased(const Button button)
Checks if a button was just released.
Definition virtualDpad.h:105
virtual void clearEvents()
Clears all button events (pressed/released flags)
Definition virtualDpad.h:112
bool myButtonsChanged[4]
Tracks whether each button state has changed since last update.
Definition virtualDpad.h:135
virtual void update(const float x, const float y)
Updates the d-pad state based on x and y input values.
Definition virtualDpad.h:58
bool myButtonStates[4]
Current state of each button (true = pressed, false = released)
Definition virtualDpad.h:132
virtual void setButtonState(const Button button, bool state)
Sets the state of a specific button.
Definition virtualDpad.h:122
DtVirtualDpad()
Default constructor.
Definition virtualDpad.h:43
Button
Enumeration of d-pad button directions.
Definition virtualDpad.h:26
@ Up
Up direction button.
Definition virtualDpad.h:27
@ Left
Left direction button.
Definition virtualDpad.h:29
@ Right
Right direction button.
Definition virtualDpad.h:30
@ Down
Down direction button.
Definition virtualDpad.h:28
const float AxisThreshold
Threshold value for axis activation (default 0.5)
Definition virtualDpad.h:37
virtual ~DtVirtualDpad()
Virtual destructor.
Definition virtualDpad.h:49