Skip to content

Commit

Permalink
add Axis::devices to get all the input devices (#5400)
Browse files Browse the repository at this point in the history
(github made me type out a message for the commit which looked like it was for the pr, sorry)

# Objective

- Add a way to get all of the input devices of an `Axis`, primarily useful for looping through them

## Solution

- Adds `Axis<T>::devices()` which returns a `FixedSizeIterator<Item = &T>`
- Adds a (probably unneeded) `test_axis_devices` test because tests are cool.

---

## Changelog

- Added `Axis<T>::devices()` method

## Migration Guide

Not a breaking change.
  • Loading branch information
1e1001 committed Jan 6, 2023
1 parent ebc5cb3 commit 41a5c30
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/bevy_input/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ where
pub fn remove(&mut self, input_device: T) -> Option<f32> {
self.axis_data.remove(&input_device)
}
/// Returns an iterator of all the input devices that have position data
pub fn devices(&self) -> impl ExactSizeIterator<Item = &T> {
self.axis_data.keys()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -108,4 +112,34 @@ mod tests {
assert_eq!(expected, actual);
}
}

#[test]
fn test_axis_devices() {
let mut axis = Axis::<GamepadButton>::default();
assert_eq!(axis.devices().count(), 0);

axis.set(
GamepadButton::new(Gamepad::new(1), GamepadButtonType::RightTrigger),
0.1,
);
assert_eq!(axis.devices().count(), 1);

axis.set(
GamepadButton::new(Gamepad::new(1), GamepadButtonType::LeftTrigger),
0.5,
);
assert_eq!(axis.devices().count(), 2);

axis.set(
GamepadButton::new(Gamepad::new(1), GamepadButtonType::RightTrigger),
-0.1,
);
assert_eq!(axis.devices().count(), 2);

axis.remove(GamepadButton::new(
Gamepad::new(1),
GamepadButtonType::RightTrigger,
));
assert_eq!(axis.devices().count(), 1);
}
}

0 comments on commit 41a5c30

Please sign in to comment.