-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathgamepad.hpp
More file actions
230 lines (188 loc) · 3.98 KB
/
Copy pathgamepad.hpp
File metadata and controls
230 lines (188 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @file liblava/frame/gamepad.hpp
* @brief Gamepad manager
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/core/id.hpp"
namespace lava {
/**
* @brief Gamepad ids
*/
enum class gamepad_id : index {
_1 = 0,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
_10,
_11,
_12,
_13,
_14,
_15,
_16,
last = _16,
};
/// Reference to gamepad id
using gamepad_id_ref = gamepad_id const&;
/**
* @brief Gamepad buttons
*/
enum class gamepad_button : index {
a = 0,
b,
x,
y,
left_bumper,
right_bumper,
back,
start,
guide,
left_thumb,
right_thumb,
dpad_up,
dpad_right,
dpad_down,
dpad_left,
last = dpad_left,
cross = a,
circle = b,
square = x,
triangle = y,
};
/// Reference to gamepad button
using gamepad_button_ref = gamepad_button const&;
/**
* @brief Gamepad axis
*/
enum class gamepad_axis : index {
left_x = 0,
left_y,
right_x,
right_y,
left_trigger,
right_trigger,
last = right_trigger,
};
/// Reference to gamepad axis
using gamepad_axis_ref = gamepad_axis const&;
/**
* @brief Gamepad
*/
struct gamepad {
/// List of gamepads
using list = std::vector<gamepad>;
/// Reference to gamepad
using ref = gamepad const&;
/**
* @brief Construct a new gamepad
* @param pad_id Gamepad id
*/
explicit gamepad(gamepad_id_ref pad_id = gamepad_id::_1);
/**
* @brief Check if gamepad is active
* @return Gamepad is active or not
*/
bool ready() const;
/**
* @brief Update gamepad
* @return Update was successful or failed
*/
bool update();
/**
* @brief Check if gamepad button is pressed
* @param button Gamepad button to check
* @return Button is pressed or not
*/
bool pressed(gamepad_button_ref button) const {
return m_state.buttons[to_ui32(button)];
}
/**
* @brief Get value of axis
* @param axis Target axis
* @return r32 Axis value
*/
r32 value(gamepad_axis_ref axis) const {
return m_state.axes[to_ui32(axis)];
}
/**
* @brief Get the gamepad id
* @return gamepad_id_ref Gamepad id
*/
gamepad_id_ref get_pad_id() const {
return m_pad_id;
}
/**
* @brief Get the gamepad id as integer
* @return ui32 Integer gamepad id
*/
ui32 get_id() const {
return to_ui32(get_pad_id());
}
/**
* @brief Get the name
* @return name Name of gamepad
*/
name get_name() const;
private:
/// Gamepad id
gamepad_id m_pad_id;
/**
* @brief Gamepad state
*/
struct state {
/// Gamepad buttons
uchar buttons[15];
/// Gamepad axes
r32 axes[6];
};
/// Gamepad state
gamepad::state m_state;
};
/**
* @brief Get list of all gamepads
* @return gamepad::list List of gamepads
*/
gamepad::list gamepads();
/**
* @brief Gamepad manager
*/
struct gamepad_manager {
/// Gamepad listener function
using listener_func = std::function<bool(gamepad, bool)>;
/**
* @brief Get gamepad manager singleton
* @return gamepad_manager& Gamepad manager
*/
static gamepad_manager& singleton() {
static gamepad_manager manager;
return manager;
}
/**
* @brief Add listener
* @param listener Gamepad listener function
* @return id Id of function
*/
id add(listener_func listener);
/**
* @brief Remove listener
* @param func_id Id of function
*/
void remove(id::ref func_id);
private:
/**
* @brief Construct a new gamepad manager
*/
explicit gamepad_manager();
/// Map of gamepad listeners
using listener_map = std::map<id, listener_func>;
/// Map of gamepad listeners
listener_map m_map;
};
} // namespace lava