Skip to content

Commit

Permalink
Add support to get gamepad button/trigger values using Axis<GamepadBu…
Browse files Browse the repository at this point in the history
…tton> (#683)
  • Loading branch information
simpuid authored Oct 15, 2020
1 parent 76cc258 commit dd91f8e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
27 changes: 23 additions & 4 deletions crates/bevy_gilrs/src/gilrs_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn gilrs_startup_system(_world: &mut World, resources: &mut Resources) {
let mut gamepad_event = resources.get_mut::<Events<GamepadEvent>>().unwrap();
let mut inputs = resources.get_mut::<Input<GamepadButton>>().unwrap();
let mut axes = resources.get_mut::<Axis<GamepadAxis>>().unwrap();
let mut button_axes = resources.get_mut::<Axis<GamepadButton>>().unwrap();
gamepad_event.update();
inputs.update();
for (gilrs_id, gilrs_gamepad) in gilrs.gamepads() {
Expand All @@ -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,
);
}
}
Expand All @@ -27,6 +29,7 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) {
let mut gamepad_event = resources.get_mut::<Events<GamepadEvent>>().unwrap();
let mut inputs = resources.get_mut::<Input<GamepadButton>>().unwrap();
let mut axes = resources.get_mut::<Axis<GamepadAxis>>().unwrap();
let mut button_axes = resources.get_mut::<Axis<GamepadButton>>().unwrap();

gamepad_event.update();
inputs.update();
Expand All @@ -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 => {
Expand All @@ -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, _) => {
Expand All @@ -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(
Expand Down Expand Up @@ -118,13 +131,17 @@ fn connect_gamepad(
events: &mut Events<GamepadEvent>,
inputs: &mut Input<GamepadButton>,
axes: &mut Axis<GamepadAxis>,
button_axes: &mut Axis<GamepadButton>,
) {
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());
}
}
}
Expand All @@ -142,11 +159,13 @@ fn disconnect_gamepad(
events: &mut Events<GamepadEvent>,
inputs: &mut Input<GamepadButton>,
axes: &mut Axis<GamepadAxis>,
button_axes: &mut Axis<GamepadButton>,
) {
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() {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Plugin for InputPlugin {
)
.add_event::<GamepadEvent>()
.init_resource::<Input<GamepadButton>>()
.init_resource::<Axis<GamepadAxis>>();
.init_resource::<Axis<GamepadAxis>>()
.init_resource::<Axis<GamepadButton>>();
}
}
25 changes: 20 additions & 5 deletions examples/input/gamepad_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ fn connection_system(mut lobby: ResMut<Lobby>, gamepad_event: Res<Events<Gamepad
}
}

fn button_system(manager: Res<Lobby>, inputs: Res<Input<GamepadButton>>) {
fn button_system(
manager: Res<Lobby>,
inputs: Res<Input<GamepadButton>>,
button_axes: Res<Axis<GamepadButton>>,
) {
let button_types = [
GamepadButtonType::South,
GamepadButtonType::East,
Expand Down Expand Up @@ -63,6 +67,15 @@ fn button_system(manager: Res<Lobby>, inputs: Res<Input<GamepadButton>>) {
} 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
);
}
}
}
}
}
Expand All @@ -81,13 +94,15 @@ fn axis_system(manager: Res<Lobby>, axes: Res<Axis<GamepadAxis>>) {
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
}

0 comments on commit dd91f8e

Please sign in to comment.