ECSTASY
All in the name
Loading...
Searching...
No Matches
Gamepad.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTASY_INTEGRATIONS_EVENT_INPUTS_GAMEPAD_HPP_
13#define ECSTASY_INTEGRATIONS_EVENT_INPUTS_GAMEPAD_HPP_
14
15#include <array>
16#include <cstdint>
17#include <iostream>
18#include <unordered_map>
19
21
23{
30 class Gamepad {
31 public:
32#ifndef _DOXYGEN_
33
34 // LCOV_EXCL_START
35
40
41 // LCOV_EXCL_STOP
42#else
44 enum class Button {
45 Unknown = -1,
47 FaceUp,
48 FaceRight,
49 FaceDown,
50 FaceLeft,
56 Middle,
59 ThumbLeft,
61
62 Count
63 };
64
66 enum class Axis {
67 Unknown = -1,
68 LeftX,
69 LeftY,
70 RightX,
71 RightY,
74 DPadX,
75 DPadY,
76
77 Count
78
79 };
80#endif
81
83 enum class Joystick {
84 Unknown = -1,
85 Left = 0,
86 Right,
87
88 Count
89 };
90
97 constexpr Gamepad(std::size_t id = 0) noexcept : _id(id), _connected(false), _buttons({false}), _axis({0.f})
98 {
101 }
102
109 ~Gamepad() noexcept = default;
110
118 [[nodiscard]] constexpr std::size_t getId() const noexcept
119 {
120 return _id;
121 }
122
133 constexpr void setId(std::size_t id) noexcept
134 {
135 _id = id;
136 }
137
146 [[nodiscard]] constexpr bool isConnected() const noexcept
147 {
148 return _connected;
149 }
150
161 constexpr void setConnected(bool connected) noexcept
162 {
163 _connected = connected;
164 }
165
178 [[nodiscard]] constexpr bool isButtonDown(Button button) const
179 {
180 assertButtonValid(button);
181 return _buttons[static_cast<std::size_t>(button)];
182 }
183
196 [[nodiscard]] constexpr bool isButtonUp(Button button) const
197 {
198 assertButtonValid(button);
199 return !isButtonDown(button);
200 }
201
215 constexpr void setButtonState(Button button, bool down)
216 {
217 assertButtonValid(button);
218 _buttons[static_cast<std::size_t>(button)] = down;
219 }
220
233 [[nodiscard]] constexpr float getAxisValue(Axis axis) const
234 {
235 assertAxisValid(axis);
236 return _axis[static_cast<std::size_t>(axis)];
237 }
238
252 constexpr void setAxisValue(Axis axis, float value)
253 {
254 assertAxisValid(axis);
255 _axis[static_cast<std::size_t>(axis)] = value;
256 }
257
269 constexpr static void assertButtonValid(Button button, bool allowUnknown = false)
270 {
271 if (static_cast<std::size_t>(button) >= static_cast<std::size_t>(Button::Count)
272 || (!allowUnknown && button == Button::Unknown)) [[unlikely]]
273 throw std::invalid_argument("Invalid button");
274 }
275
287 constexpr static void assertAxisValid(Axis axis, bool allowUnknown = false)
288 {
289 if (static_cast<std::size_t>(axis) >= static_cast<std::size_t>(Axis::Count)
290 || (!allowUnknown && axis == Axis::Unknown)) [[unlikely]]
291 throw std::invalid_argument("Invalid axis");
292 }
293
294 private:
303 };
304} // namespace ecstasy::integration::event
305
306#endif /* !ECSTASY_INTEGRATIONS_EVENT_INPUTS_GAMEPAD_HPP_ */
Create a serializable enumeration type.
#define SERIALIZABLE_ENUM(NAME, STARTING_AT,...)
Create a serializable enumeration type.
constexpr bool isButtonDown(Button button) const
Check whether a button is down.
Definition Gamepad.hpp:178
static constexpr void assertAxisValid(Axis axis, bool allowUnknown=false)
Check whether an axis is valid.
Definition Gamepad.hpp:287
constexpr Gamepad(std::size_t id=0) noexcept
Construct a new Gamepad object.
Definition Gamepad.hpp:97
constexpr std::size_t getId() const noexcept
Get the gamepad id.
Definition Gamepad.hpp:118
constexpr float getAxisValue(Axis axis) const
Get the given axis value.
Definition Gamepad.hpp:233
Axis
Gamepad axis, associated value must be in range [-1, 1].
Definition Gamepad.hpp:66
@ TriggerLeft
Left trigger (default: -1)
@ TriggerRight
Right trigger (default: -1)
@ LeftY
Left joystick Y axis (default: 0)
@ LeftX
Left joystick X axis (default: 0)
@ Count
Keep last – the total number of gamepad axis.
@ RightX
Right joystick X axis (default: 0)
@ RightY
Right joystick Y axis (default: 0)
constexpr void setId(std::size_t id) noexcept
Change the gamepad id.
Definition Gamepad.hpp:133
constexpr void setConnected(bool connected) noexcept
Update the connected state of the gamepad.
Definition Gamepad.hpp:161
std::array< float, static_cast< std::size_t >(Axis::Count)> _axis
Gamepad axis values.
Definition Gamepad.hpp:302
static constexpr void assertButtonValid(Button button, bool allowUnknown=false)
Check whether a button is valid.
Definition Gamepad.hpp:269
constexpr bool isConnected() const noexcept
Check whether the gamepad is connected or not.
Definition Gamepad.hpp:146
constexpr void setButtonState(Button button, bool down)
Update a given button state.
Definition Gamepad.hpp:215
constexpr bool isButtonUp(Button button) const
Check whether a button is up.
Definition Gamepad.hpp:196
bool _connected
Gamepad connection state.
Definition Gamepad.hpp:298
Joystick
Gamepad joysticks (a joystick has 2 combined axis)
Definition Gamepad.hpp:83
~Gamepad() noexcept=default
Default destructor.
std::array< bool, static_cast< std::size_t >(Button::Count)> _buttons
Gamepad buttons state.
Definition Gamepad.hpp:300
constexpr void setAxisValue(Axis axis, float value)
Update an axis value.
Definition Gamepad.hpp:252
@ MiddleRight
Right center button (i.e.
@ FaceRight
Face button right (i.e. PS: Square, Xbox: X)
@ Middle
Center buttons (i.e. PS: PS, Xbox: XBOX)
@ Count
Keep last – the total number of gamepad buttons.
@ FaceDown
Face button down (i.e. PS: Cross, Xbox: A)
Event integration.
Definition Event.hpp:25