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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#[cfg(all(not(any(target_arch="wasm32", target_os="macos")), feature = "gamepads"))]
extern crate gilrs;
#[cfg(all(not(any(target_arch="wasm32", target_os="macos")), feature = "gamepads"))]
use gilrs::Button;
use input::{ButtonState, Event};
use std::ops::Index;
#[derive(Copy, Clone, Debug)]
pub struct Gamepad {
id: u32,
buttons: [ButtonState; 17],
axes: [f32; 4],
}
impl Gamepad {
pub(crate) fn clear_temporary_states(&mut self) {
for button in self.buttons.iter_mut() {
*button = button.clear_temporary();
}
}
pub(crate) fn set_previous(&mut self, previous: &Gamepad, events: &mut Vec<Event>) {
for button in GAMEPAD_BUTTON_LIST.iter() {
if self[*button].is_down() != previous[*button].is_down() {
self.buttons[*button as usize] = if self[*button].is_down() {
ButtonState::Pressed
} else {
ButtonState::Released
};
events.push(Event::GamepadButton(self.id(), *button, self[*button]));
}
}
for axis in GAMEPAD_AXIS_LIST.iter() {
if self[*axis] != previous[*axis] {
events.push(Event::GamepadAxis(self.id(), *axis, self[*axis]));
}
}
}
pub fn id(&self) -> u32 {
self.id
}
}
pub(crate) struct GamepadProvider {
#[cfg(all(not(any(target_arch="wasm32", target_os="macos")), feature = "gamepads"))]
gilrs: gilrs::Gilrs
}
impl GamepadProvider {
pub fn new() -> GamepadProvider {
GamepadProvider {
#[cfg(all(not(any(target_arch="wasm32", target_os="macos")), feature = "gamepads"))]
gilrs: gilrs::Gilrs::new().unwrap()
}
}
pub fn provide_gamepads(&mut self, buffer: &mut Vec<Gamepad>) {
self.provide_gamepads_impl(buffer);
}
#[cfg(all(not(any(target_arch="wasm32", target_os="macos")), feature = "gamepads"))]
fn provide_gamepads_impl(&mut self, buffer: &mut Vec<Gamepad>) {
while let Some(ev) = self.gilrs.next_event() {
self.gilrs.update(&ev);
}
use gilrs::{
Axis,
ev::state::AxisData
};
fn axis_value(data: Option<&AxisData>) -> f32 {
match data {
Some(ref data) => data.value(),
None => 0.0
}
}
buffer.extend(self.gilrs.gamepads().map(|(id, gamepad)| {
let id = id as u32;
let axes = [
axis_value(gamepad.axis_data(Axis::LeftStickX)),
axis_value(gamepad.axis_data(Axis::LeftStickY)),
axis_value(gamepad.axis_data(Axis::RightStickX)),
axis_value(gamepad.axis_data(Axis::RightStickY))
];
let mut buttons = [ButtonState::NotPressed; 17];
for i in 0..GAMEPAD_BUTTON_LIST.len() {
let button = GAMEPAD_BUTTON_LIST[i];
let value = match gamepad.button_data(GILRS_GAMEPAD_LIST[i]) {
Some(ref data) => data.is_pressed(),
None => false
};
let state = if value { ButtonState::Pressed } else { ButtonState::Released };
buttons[button as usize] = state;
}
Gamepad { id, axes, buttons }
}));
}
#[cfg(target_arch="wasm32")]
fn provide_gamepads_impl(&self, buffer: &mut Vec<Gamepad>) {
fn new(id: u32) -> Gamepad {
Gamepad { id, buttons: [ButtonState::NotPressed; 17], axes: [0.0; 4] }
}
use std::os::raw::c_void;
use ffi::wasm;
buffer.push(new(0));
buffer.push(new(0));
unsafe {
let gamepad_count = wasm::gamepad_count() as usize;
buffer.reserve(gamepad_count);
let start = &mut buffer[0] as *mut Gamepad as *mut c_void;
let id_ptr = &mut buffer[0].id as *mut u32;
let button_ptr = &mut buffer[0].buttons[0] as *mut ButtonState as *mut u32;
let axis_ptr = &mut buffer[0].axes[0] as *mut f32;
let next_id = &mut buffer[1] as *mut Gamepad as *mut c_void;
wasm::gamepad_data(start, id_ptr, button_ptr, axis_ptr, next_id);
buffer.set_len(gamepad_count);
if gamepad_count < 2 {
buffer.truncate(2 - gamepad_count);
}
}
}
#[cfg(any(target_os="macos", not(feature = "gamepads")))]
fn provide_gamepads_impl(&self, _buffer: &mut Vec<Gamepad>) {
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum GamepadAxis {
LeftStickX = 0,
LeftStickY = 1,
RightStickX = 2,
RightStickY = 3
}
#[repr(u32)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum GamepadButton {
FaceDown,
FaceRight,
FaceLeft,
FaceUp,
ShoulderLeft,
ShoulderRight,
TriggerLeft,
TriggerRight,
Select,
Start,
StickButtonLeft,
StickButtonRight,
DpadUp,
DpadDown,
DpadLeft,
DpadRight,
Home
}
impl Index<GamepadAxis> for Gamepad {
type Output = f32;
fn index(&self, index: GamepadAxis) -> &f32 {
&self.axes[index as usize]
}
}
impl Index<GamepadButton> for Gamepad {
type Output = ButtonState;
fn index(&self, index: GamepadButton) -> &ButtonState {
&self.buttons[index as usize]
}
}
#[doc(hidden)]
pub const GAMEPAD_BUTTON_LIST: &[GamepadButton] = &[
GamepadButton::FaceDown,
GamepadButton::FaceRight,
GamepadButton::FaceUp,
GamepadButton::FaceLeft,
GamepadButton::ShoulderLeft,
GamepadButton::TriggerLeft,
GamepadButton::ShoulderRight,
GamepadButton::TriggerRight,
GamepadButton::Select,
GamepadButton::Start,
GamepadButton::Home,
GamepadButton::StickButtonLeft,
GamepadButton::StickButtonRight,
GamepadButton::DpadUp,
GamepadButton::DpadDown,
GamepadButton::DpadLeft,
GamepadButton::DpadRight,
];
#[doc(hidden)]
#[cfg(all(not(any(target_arch="wasm32", target_os="macos")), feature = "gamepads"))]
const GILRS_GAMEPAD_LIST: &[gilrs::Button] = &[
Button::South,
Button::East,
Button::North,
Button::West,
Button::LeftTrigger,
Button::LeftTrigger2,
Button::RightTrigger,
Button::RightTrigger2,
Button::South,
Button::South,
Button::Mode,
Button::LeftThumb,
Button::RightThumb,
Button::DPadUp,
Button::DPadDown,
Button::DPadLeft,
Button::DPadRight,
];
#[doc(hidden)]
pub const GAMEPAD_AXIS_LIST: &[GamepadAxis] = &[
GamepadAxis::LeftStickX,
GamepadAxis::LeftStickY,
GamepadAxis::RightStickX,
GamepadAxis::RightStickY,
];