From 81f1f7cc87d2e274f53b0a4dcbc17c6be571c245 Mon Sep 17 00:00:00 2001 From: Utkarsh <37181756+simpuid@users.noreply.github.com> Date: Fri, 16 Oct 2020 01:15:34 +0530 Subject: [PATCH] Add support to get gamepad button/trigger values using Axis (#683) --- crates/bevy_gilrs/src/gilrs_system.rs | 27 +++++++++++++++++++++++---- crates/bevy_input/src/lib.rs | 3 ++- examples/input/gamepad_input.rs | 25 ++++++++++++++++++++----- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/crates/bevy_gilrs/src/gilrs_system.rs b/crates/bevy_gilrs/src/gilrs_system.rs index f5c83628bd5675..22bb3b527583e4 100644 --- a/crates/bevy_gilrs/src/gilrs_system.rs +++ b/crates/bevy_gilrs/src/gilrs_system.rs @@ -9,6 +9,7 @@ pub fn gilrs_startup_system(_world: &mut World, resources: &mut Resources) { let mut gamepad_event = resources.get_mut::>().unwrap(); let mut inputs = resources.get_mut::>().unwrap(); let mut axes = resources.get_mut::>().unwrap(); + let mut button_axes = resources.get_mut::>().unwrap(); gamepad_event.update(); inputs.update(); for (gilrs_id, gilrs_gamepad) in gilrs.gamepads() { @@ -18,6 +19,7 @@ pub fn gilrs_startup_system(_world: &mut World, resources: &mut Resources) { &mut gamepad_event, &mut inputs, &mut axes, + &mut button_axes, ); } } @@ -27,6 +29,7 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) { let mut gamepad_event = resources.get_mut::>().unwrap(); let mut inputs = resources.get_mut::>().unwrap(); let mut axes = resources.get_mut::>().unwrap(); + let mut button_axes = resources.get_mut::>().unwrap(); gamepad_event.update(); inputs.update(); @@ -39,6 +42,7 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) { &mut gamepad_event, &mut inputs, &mut axes, + &mut button_axes, ); } EventType::Disconnected => { @@ -47,6 +51,7 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) { &mut gamepad_event, &mut inputs, &mut axes, + &mut button_axes, ); } EventType::ButtonPressed(gilrs_button, _) => { @@ -65,6 +70,14 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) { )); } } + EventType::ButtonChanged(gilrs_button, value, _) => { + if let Some(button_type) = convert_button(gilrs_button) { + button_axes.set( + GamepadButton(convert_gamepad_id(gilrs_event.id), button_type), + value, + ); + } + } EventType::AxisChanged(gilrs_axis, value, _) => { if let Some(axis_type) = convert_axis(gilrs_axis) { axes.set( @@ -118,13 +131,17 @@ fn connect_gamepad( events: &mut Events, inputs: &mut Input, axes: &mut Axis, + button_axes: &mut Axis, ) { for gilrs_button in ALL_GILRS_BUTTONS.iter() { if let Some(button_type) = convert_button(*gilrs_button) { - let gamepad_button = GamepadButton(gamepad, button_type); - inputs.reset(gamepad_button); - if gilrs_gamepad.is_pressed(*gilrs_button) { - inputs.press(gamepad_button); + if let Some(button_data) = gilrs_gamepad.button_data(*gilrs_button) { + let gamepad_button = GamepadButton(gamepad, button_type); + inputs.reset(gamepad_button); + if button_data.is_pressed() { + inputs.press(gamepad_button); + } + button_axes.set(gamepad_button, button_data.value()); } } } @@ -142,11 +159,13 @@ fn disconnect_gamepad( events: &mut Events, inputs: &mut Input, axes: &mut Axis, + button_axes: &mut Axis, ) { for gilrs_button in ALL_GILRS_BUTTONS.iter() { if let Some(button_type) = convert_button(*gilrs_button) { let gamepad_button = GamepadButton(gamepad, button_type); inputs.reset(gamepad_button); + button_axes.remove(&gamepad_button); } } for gilrs_axis in ALL_GILRS_AXES.iter() { diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index 278405c9f540a7..7bf2bbb2cf3be4 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -49,6 +49,7 @@ impl Plugin for InputPlugin { ) .add_event::() .init_resource::>() - .init_resource::>(); + .init_resource::>() + .init_resource::>(); } } diff --git a/examples/input/gamepad_input.rs b/examples/input/gamepad_input.rs index d1c07699d1453f..dfa146c2420864 100644 --- a/examples/input/gamepad_input.rs +++ b/examples/input/gamepad_input.rs @@ -34,7 +34,11 @@ fn connection_system(mut lobby: ResMut, gamepad_event: Res, inputs: Res>) { +fn button_system( + manager: Res, + inputs: Res>, + button_axes: Res>, +) { let button_types = [ GamepadButtonType::South, GamepadButtonType::East, @@ -63,6 +67,15 @@ fn button_system(manager: Res, inputs: Res>) { } else if inputs.just_released(GamepadButton(*gamepad, *button_type)) { println!("Released {:?}", GamepadButton(*gamepad, *button_type)); } + if let Some(value) = button_axes.get(&GamepadButton(*gamepad, *button_type)) { + if value_check(value) { + println!( + "Button as Axis {:?} is {}", + GamepadButton(*gamepad, *button_type), + value + ); + } + } } } } @@ -81,13 +94,15 @@ fn axis_system(manager: Res, axes: Res>) { for gamepad in manager.gamepad.iter() { for axis_type in axis_types.iter() { if let Some(value) = axes.get(&GamepadAxis(*gamepad, *axis_type)) { - if value.abs() > 0.01f32 - && (value - 1.0f32).abs() > 0.01f32 - && (value + 1.0f32).abs() > 0.01f32 - { + if value_check(value) { println!("Axis {:?} is {}", GamepadAxis(*gamepad, *axis_type), value); } } } } } + +fn value_check(value: f32) -> bool { + let value = value.abs(); + value > 0.1f32 && value < 0.9f32 +}