-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathgilrs_system.rs
58 lines (56 loc) · 2.14 KB
/
gilrs_system.rs
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
use crate::converter::{convert_axis, convert_button, convert_gamepad_id};
use bevy_app::Events;
use bevy_ecs::world::World;
use bevy_input::{gamepad::GamepadEventRaw, prelude::*};
use gilrs::{EventType, Gilrs};
pub fn gilrs_event_startup_system(world: &mut World) {
let world = world.cell();
let gilrs = world.get_non_send::<Gilrs>().unwrap();
let mut event = world.get_resource_mut::<Events<GamepadEventRaw>>().unwrap();
for (id, _) in gilrs.gamepads() {
event.send(GamepadEventRaw(
convert_gamepad_id(id),
GamepadEventType::Connected,
));
}
}
pub fn gilrs_event_system(world: &mut World) {
let world = world.cell();
let mut gilrs = world.get_non_send_mut::<Gilrs>().unwrap();
let mut event = world.get_resource_mut::<Events<GamepadEventRaw>>().unwrap();
event.update();
while let Some(gilrs_event) = gilrs.next_event() {
match gilrs_event.event {
EventType::Connected => {
event.send(GamepadEventRaw(
convert_gamepad_id(gilrs_event.id),
GamepadEventType::Connected,
));
}
EventType::Disconnected => {
event.send(GamepadEventRaw(
convert_gamepad_id(gilrs_event.id),
GamepadEventType::Disconnected,
));
}
EventType::ButtonChanged(gilrs_button, value, _) => {
if let Some(button_type) = convert_button(gilrs_button) {
event.send(GamepadEventRaw(
convert_gamepad_id(gilrs_event.id),
GamepadEventType::ButtonChanged(button_type, value),
));
}
}
EventType::AxisChanged(gilrs_axis, value, _) => {
if let Some(axis_type) = convert_axis(gilrs_axis) {
event.send(GamepadEventRaw(
convert_gamepad_id(gilrs_event.id),
GamepadEventType::AxisChanged(axis_type, value),
));
}
}
_ => (),
};
}
gilrs.inc();
}