QuasarEngine
Cross-platform 3D engine and editor
Loading...
Searching...
No Matches
Gamepads.h
Go to the documentation of this file.
1#pragma once
2
7
10
11#include <array>
12#include <functional>
13#include <optional>
14#include <string>
15
16namespace Quasar
17{
20 {
21 std::array<bool, GamepadButtonCount> Buttons{};
22 std::array<float, GamepadAxisCount> Axes{};
23
25 bool IsPressed(GamepadButton button) const { return Buttons.at(static_cast<std::size_t>(button)); }
26
28 float GetAxis(GamepadAxis axis) const { return Axes.at(static_cast<std::size_t>(axis)); }
29 };
30
33 {
34 std::string Name;
36 };
37
40 {
43 float StickDeadZone = 0.15f;
44
47 float TriggerDeadZone = 0.05f;
48
51 float AxisEventThreshold = 0.01f;
52 };
53
64 {
65 public:
67 using EventCallback = std::function<void(Event&)>;
68
70 using Reader = std::function<std::optional<GamepadReading>(GamepadId)>;
71
74
76 explicit Gamepads(Reader reader);
77
89 void Update(const EventCallback& send);
90
92 bool IsConnected(GamepadId gamepad) const;
93
95 const std::string& GetName(GamepadId gamepad) const;
96
98 bool IsButtonPressed(GamepadId gamepad, GamepadButton button) const;
99
105 float GetAxis(GamepadId gamepad, GamepadAxis axis) const;
106
108 GamepadSettings& GetSettings() { return m_Settings; }
109
111 const GamepadSettings& GetSettings() const { return m_Settings; }
112
114 void SetReader(Reader reader);
115
116 private:
117 struct Slot
118 {
119 bool Connected = false;
120 std::string Name;
121 GamepadState Current;
122 GamepadState Reported;
123 };
124
125 const Slot* FindConnected(GamepadId gamepad) const;
126 void UpdateSlot(GamepadId gamepad, Slot& slot, const EventCallback& send);
127
128 Reader m_Reader;
129 GamepadSettings m_Settings;
130 std::array<Slot, MaxGamepads> m_Slots{};
131 };
132}
Base class of all events, with their types and categories.
Codes of the gamepads, of their buttons and of their axes, independent from GLFW.
Base class of all events.
Definition Event.h:111
void SetReader(Reader reader)
Replaces the function that reads the gamepads, for example to simulate gamepads in the tests.
std::function< std::optional< GamepadReading >(GamepadId)> Reader
Function that reads a gamepad: the reading, or no value when no gamepad has this id.
Definition Gamepads.h:70
void Update(const EventCallback &send)
Reads every gamepad, keeps its new state, and sends the events of the changes since the last call.
Gamepads(Reader reader)
Creates the gamepads, read with another function, for example by the tests.
GamepadSettings & GetSettings()
Returns the settings of the dead zones and of the events, which can be changed at any time.
Definition Gamepads.h:108
Gamepads()
Creates the gamepads, read with GLFW. GLFW must be started before the first call to Update().
bool IsConnected(GamepadId gamepad) const
Returns true if a gamepad is connected with this id.
bool IsButtonPressed(GamepadId gamepad, GamepadButton button) const
Returns true if the button of the gamepad is held down. False when the gamepad is not connected.
float GetAxis(GamepadId gamepad, GamepadAxis axis) const
Returns the value of the axis of the gamepad, after the dead zones.
const std::string & GetName(GamepadId gamepad) const
Returns the name of the gamepad, or an empty string if no gamepad is connected with this id.
std::function< void(Event &)> EventCallback
Function that receives the events of the gamepads.
Definition Gamepads.h:67
const GamepadSettings & GetSettings() const
Returns the settings of the dead zones and of the events.
Definition Gamepads.h:111
Contains all the engine code.
GamepadButton
Button of a gamepad, at the position of the buttons of an Xbox controller.
Definition GamepadCodes.h:34
GamepadId
Identifies a connected gamepad, from 0 to MaxGamepads - 1.
Definition GamepadCodes.h:26
GamepadAxis
Axis of a gamepad.
Definition GamepadCodes.h:57
What is read from a connected gamepad, before the dead zones are applied.
Definition Gamepads.h:33
std::string Name
Name of the gamepad, for example "Xbox Controller".
Definition Gamepads.h:34
GamepadState State
Raw state: the stick axes go from -1 to 1, and the triggers too, -1 being at rest.
Definition Gamepads.h:35
Settings that filter the axes of the gamepads.
Definition Gamepads.h:40
float TriggerDeadZone
Value under which a trigger reads as 0, on its range from 0 to 1.
Definition Gamepads.h:47
float AxisEventThreshold
Change needed to send a GamepadAxisMovedEvent, so that a moving stick does not send an event every fr...
Definition Gamepads.h:51
float StickDeadZone
Distance from the center under which a stick reads as 0.
Definition Gamepads.h:43
State of the buttons and axes of a gamepad.
Definition Gamepads.h:20
std::array< float, GamepadAxisCount > Axes
Value of each axis, indexed by GamepadAxis.
Definition Gamepads.h:22
float GetAxis(GamepadAxis axis) const
Returns the value of the axis.
Definition Gamepads.h:28
bool IsPressed(GamepadButton button) const
Returns true if the button is held down.
Definition Gamepads.h:25
std::array< bool, GamepadButtonCount > Buttons
True for each button held down, indexed by GamepadButton.
Definition Gamepads.h:21