Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split buttons and axes and improve ergonomics of working with gamepad input #3419

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions crates/bevy_gilrs/src/converter.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
use bevy_input::gamepad::{Gamepad, GamepadAxisType, GamepadButtonType};
use bevy_input::gamepad::{Gamepad, GamepadAxis, GamepadButton};

pub fn convert_gamepad_id(gamepad_id: gilrs::GamepadId) -> Gamepad {
Gamepad(gamepad_id.into())
}

pub fn convert_button(button: gilrs::Button) -> Option<GamepadButtonType> {
pub fn convert_button(button: gilrs::Button) -> Option<GamepadButton> {
match button {
gilrs::Button::South => Some(GamepadButtonType::South),
gilrs::Button::East => Some(GamepadButtonType::East),
gilrs::Button::North => Some(GamepadButtonType::North),
gilrs::Button::West => Some(GamepadButtonType::West),
gilrs::Button::C => Some(GamepadButtonType::C),
gilrs::Button::Z => Some(GamepadButtonType::Z),
gilrs::Button::LeftTrigger => Some(GamepadButtonType::LeftTrigger),
gilrs::Button::LeftTrigger2 => Some(GamepadButtonType::LeftTrigger2),
gilrs::Button::RightTrigger => Some(GamepadButtonType::RightTrigger),
gilrs::Button::RightTrigger2 => Some(GamepadButtonType::RightTrigger2),
gilrs::Button::Select => Some(GamepadButtonType::Select),
gilrs::Button::Start => Some(GamepadButtonType::Start),
gilrs::Button::Mode => Some(GamepadButtonType::Mode),
gilrs::Button::LeftThumb => Some(GamepadButtonType::LeftThumb),
gilrs::Button::RightThumb => Some(GamepadButtonType::RightThumb),
gilrs::Button::DPadUp => Some(GamepadButtonType::DPadUp),
gilrs::Button::DPadDown => Some(GamepadButtonType::DPadDown),
gilrs::Button::DPadLeft => Some(GamepadButtonType::DPadLeft),
gilrs::Button::DPadRight => Some(GamepadButtonType::DPadRight),
gilrs::Button::South => Some(GamepadButton::South),
gilrs::Button::East => Some(GamepadButton::East),
gilrs::Button::North => Some(GamepadButton::North),
gilrs::Button::West => Some(GamepadButton::West),
gilrs::Button::C => Some(GamepadButton::C),
gilrs::Button::Z => Some(GamepadButton::Z),
gilrs::Button::LeftTrigger => Some(GamepadButton::LeftTrigger),
gilrs::Button::LeftTrigger2 => Some(GamepadButton::LeftTrigger2),
gilrs::Button::RightTrigger => Some(GamepadButton::RightTrigger),
gilrs::Button::RightTrigger2 => Some(GamepadButton::RightTrigger2),
gilrs::Button::Select => Some(GamepadButton::Select),
gilrs::Button::Start => Some(GamepadButton::Start),
gilrs::Button::Mode => Some(GamepadButton::Mode),
gilrs::Button::LeftThumb => Some(GamepadButton::LeftThumb),
gilrs::Button::RightThumb => Some(GamepadButton::RightThumb),
gilrs::Button::DPadUp => Some(GamepadButton::DPadUp),
gilrs::Button::DPadDown => Some(GamepadButton::DPadDown),
gilrs::Button::DPadLeft => Some(GamepadButton::DPadLeft),
gilrs::Button::DPadRight => Some(GamepadButton::DPadRight),
gilrs::Button::Unknown => None,
}
}

pub fn convert_axis(axis: gilrs::Axis) -> Option<GamepadAxisType> {
pub fn convert_axis(axis: gilrs::Axis) -> Option<GamepadAxis> {
match axis {
gilrs::Axis::LeftStickX => Some(GamepadAxisType::LeftStickX),
gilrs::Axis::LeftStickY => Some(GamepadAxisType::LeftStickY),
gilrs::Axis::LeftZ => Some(GamepadAxisType::LeftZ),
gilrs::Axis::RightStickX => Some(GamepadAxisType::RightStickX),
gilrs::Axis::RightStickY => Some(GamepadAxisType::RightStickY),
gilrs::Axis::RightZ => Some(GamepadAxisType::RightZ),
gilrs::Axis::DPadX => Some(GamepadAxisType::DPadX),
gilrs::Axis::DPadY => Some(GamepadAxisType::DPadY),
gilrs::Axis::LeftStickX => Some(GamepadAxis::LeftStickX),
gilrs::Axis::LeftStickY => Some(GamepadAxis::LeftStickY),
gilrs::Axis::LeftZ => Some(GamepadAxis::LeftZ),
gilrs::Axis::RightStickX => Some(GamepadAxis::RightStickX),
gilrs::Axis::RightStickY => Some(GamepadAxis::RightStickY),
gilrs::Axis::RightZ => Some(GamepadAxis::RightZ),
gilrs::Axis::DPadX => Some(GamepadAxis::DPadX),
gilrs::Axis::DPadY => Some(GamepadAxis::DPadY),
gilrs::Axis::Unknown => None,
}
}
3 changes: 3 additions & 0 deletions crates/bevy_input/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

# other
serde = { version = "1", features = ["derive"], optional = true }
strum = {version = "0.23"}
strum_macros = {version = "0.23"}

43 changes: 20 additions & 23 deletions crates/bevy_input/src/axis.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
use bevy_utils::HashMap;
use std::hash::Hash;
use strum::IntoEnumIterator;

/// Stores the position data of input devices of type T
///
/// Values are stored as `f32` values, which range from `min` to `max`.
/// The valid range is from -1.0 to 1.0, inclusive.
///
/// If you need to represent a continous input with a scalar intensity such as a trigger,
/// use [Input](crate::Input) instead.
#[derive(Debug)]
pub struct Axis<T> {
pub struct Axis<T: Axislike> {
axis_data: HashMap<T, f32>,
}

impl<T> Default for Axis<T>
where
T: Copy + Eq + Hash,
{
pub trait Axislike: Clone + Copy + PartialEq + Eq + Hash + IntoEnumIterator {}

impl<T: Axislike> Default for Axis<T> {
fn default() -> Self {
Axis {
axis_data: HashMap::default(),
}
}
}

impl<T> Axis<T>
where
T: Copy + Eq + Hash,
{
impl<T: Axislike> Axis<T> {
pub const MIN: f32 = -1.0;
pub const MAX: f32 = 1.0;

Expand Down Expand Up @@ -53,10 +53,7 @@ where

#[cfg(test)]
mod tests {
use crate::{
gamepad::{Gamepad, GamepadButton, GamepadButtonType},
Axis,
};
use crate::{gamepad::GamepadAxis, Axis};

#[test]
fn test_axis_set() {
Expand All @@ -75,12 +72,12 @@ mod tests {
];

for (value, expected) in cases {
let gamepad_button = GamepadButton(Gamepad(1), GamepadButtonType::RightTrigger);
let mut axis = Axis::<GamepadButton>::default();
let gamepad_axis = GamepadAxis::LeftStickX;
let mut axis = Axis::<GamepadAxis>::default();

axis.set(gamepad_button, value);
axis.set(gamepad_axis, value);

let actual = axis.get(gamepad_button);
let actual = axis.get(gamepad_axis);
assert_eq!(expected, actual);
}
}
Expand All @@ -90,14 +87,14 @@ mod tests {
let cases = [-1.0, -0.9, -0.1, 0.0, 0.1, 0.9, 1.0];

for value in cases {
let gamepad_button = GamepadButton(Gamepad(1), GamepadButtonType::RightTrigger);
let mut axis = Axis::<GamepadButton>::default();
let gamepad_axis = GamepadAxis::LeftStickX;
let mut axis = Axis::<GamepadAxis>::default();

axis.set(gamepad_button, value);
assert!(axis.get(gamepad_button).is_some());
axis.set(gamepad_axis, value);
assert!(axis.get(gamepad_axis).is_some());

axis.remove(gamepad_button);
let actual = axis.get(gamepad_button);
axis.remove(gamepad_axis);
let actual = axis.get(gamepad_axis);
let expected = None;

assert_eq!(expected, actual);
Expand Down
Loading