forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad_input.rs
67 lines (61 loc) · 2.06 KB
/
gamepad_input.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
59
60
61
62
63
64
65
66
67
use bevy::{
input::gamepad::{Gamepad, GamepadButton, GamepadEvent, GamepadEventType},
prelude::*,
utils::HashSet,
};
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.init_resource::<GamepadLobby>()
.add_system_to_stage(CoreStage::PreUpdate, connection_system.system())
.add_system(gamepad_system.system())
.run();
}
#[derive(Default)]
struct GamepadLobby {
gamepads: HashSet<Gamepad>,
}
fn connection_system(
mut lobby: ResMut<GamepadLobby>,
mut gamepad_event: EventReader<GamepadEvent>,
) {
for event in gamepad_event.iter() {
match &event {
GamepadEvent(gamepad, GamepadEventType::Connected) => {
lobby.gamepads.insert(*gamepad);
println!("{:?} Connected", gamepad);
}
GamepadEvent(gamepad, GamepadEventType::Disconnected) => {
lobby.gamepads.remove(gamepad);
println!("{:?} Disconnected", gamepad);
}
_ => (),
}
}
}
fn gamepad_system(
lobby: Res<GamepadLobby>,
button_inputs: Res<Input<GamepadButton>>,
button_axes: Res<Axis<GamepadButton>>,
axes: Res<Axis<GamepadAxis>>,
) {
for gamepad in lobby.gamepads.iter().cloned() {
if button_inputs.just_pressed(GamepadButton(gamepad, GamepadButtonType::South)) {
println!("{:?} just pressed South", gamepad);
} else if button_inputs.just_released(GamepadButton(gamepad, GamepadButtonType::South)) {
println!("{:?} just released South", gamepad);
}
let right_trigger = button_axes
.get(GamepadButton(gamepad, GamepadButtonType::RightTrigger2))
.unwrap();
if right_trigger.abs() > 0.01 {
println!("{:?} RightTrigger2 value is {}", gamepad, right_trigger);
}
let left_stick_x = axes
.get(GamepadAxis(gamepad, GamepadAxisType::LeftStickX))
.unwrap();
if left_stick_x.abs() > 0.01 {
println!("{:?} LeftStickX value is {}", gamepad, left_stick_x);
}
}
}