2020-11-10 03:26:08 +00:00
|
|
|
use bevy::{
|
|
|
|
input::gamepad::{Gamepad, GamepadButton, GamepadEvent, GamepadEventType},
|
|
|
|
prelude::*,
|
|
|
|
utils::HashSet,
|
|
|
|
};
|
2020-09-18 21:43:47 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-10-21 17:27:00 +00:00
|
|
|
.init_resource::<GamepadLobby>()
|
2021-02-18 21:20:37 +00:00
|
|
|
.add_system_to_stage(CoreStage::PreUpdate, connection_system.system())
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_system(gamepad_system.system())
|
2020-09-18 21:43:47 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2020-10-18 20:20:42 +00:00
|
|
|
struct GamepadLobby {
|
|
|
|
gamepads: HashSet<Gamepad>,
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 06:23:30 +00:00
|
|
|
fn connection_system(
|
|
|
|
mut lobby: ResMut<GamepadLobby>,
|
|
|
|
mut gamepad_event: EventReader<GamepadEvent>,
|
|
|
|
) {
|
|
|
|
for event in gamepad_event.iter() {
|
2020-09-18 21:43:47 +00:00
|
|
|
match &event {
|
|
|
|
GamepadEvent(gamepad, GamepadEventType::Connected) => {
|
2020-10-18 20:20:42 +00:00
|
|
|
lobby.gamepads.insert(*gamepad);
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("{:?} Connected", gamepad);
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
GamepadEvent(gamepad, GamepadEventType::Disconnected) => {
|
2020-10-18 20:20:42 +00:00
|
|
|
lobby.gamepads.remove(gamepad);
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("{:?} Disconnected", gamepad);
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
2020-10-21 17:27:00 +00:00
|
|
|
_ => (),
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 17:27:00 +00:00
|
|
|
fn gamepad_system(
|
2020-10-18 20:20:42 +00:00
|
|
|
lobby: Res<GamepadLobby>,
|
2020-10-21 17:27:00 +00:00
|
|
|
button_inputs: Res<Input<GamepadButton>>,
|
2020-10-15 19:45:34 +00:00
|
|
|
button_axes: Res<Axis<GamepadButton>>,
|
2020-10-21 17:27:00 +00:00
|
|
|
axes: Res<Axis<GamepadAxis>>,
|
2020-10-15 19:45:34 +00:00
|
|
|
) {
|
2020-10-21 22:56:07 +00:00
|
|
|
for gamepad in lobby.gamepads.iter().cloned() {
|
|
|
|
if button_inputs.just_pressed(GamepadButton(gamepad, GamepadButtonType::South)) {
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("{:?} just pressed South", gamepad);
|
2020-10-21 22:56:07 +00:00
|
|
|
} else if button_inputs.just_released(GamepadButton(gamepad, GamepadButtonType::South)) {
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("{:?} just released South", gamepad);
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 22:56:07 +00:00
|
|
|
let right_trigger = button_axes
|
|
|
|
.get(GamepadButton(gamepad, GamepadButtonType::RightTrigger2))
|
|
|
|
.unwrap();
|
|
|
|
if right_trigger.abs() > 0.01 {
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("{:?} RightTrigger2 value is {}", gamepad, right_trigger);
|
2020-10-21 22:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let left_stick_x = axes
|
|
|
|
.get(GamepadAxis(gamepad, GamepadAxisType::LeftStickX))
|
|
|
|
.unwrap();
|
|
|
|
if left_stick_x.abs() > 0.01 {
|
2021-04-22 23:30:48 +00:00
|
|
|
info!("{:?} LeftStickX value is {}", gamepad, left_stick_x);
|
2020-10-21 22:56:07 +00:00
|
|
|
}
|
2020-09-18 21:43:47 +00:00
|
|
|
}
|
|
|
|
}
|