From 41a5c30fb713a16c9b3f1a8eca10b0ff869a52ff Mon Sep 17 00:00:00 2001 From: 1e1001 Date: Fri, 6 Jan 2023 18:00:22 +0000 Subject: [PATCH] add `Axis::devices` to get all the input devices (#5400) (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::devices()` which returns a `FixedSizeIterator` - Adds a (probably unneeded) `test_axis_devices` test because tests are cool. --- ## Changelog - Added `Axis::devices()` method ## Migration Guide Not a breaking change. --- crates/bevy_input/src/axis.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/bevy_input/src/axis.rs b/crates/bevy_input/src/axis.rs index 228365f16bdff..6c88145da13b7 100644 --- a/crates/bevy_input/src/axis.rs +++ b/crates/bevy_input/src/axis.rs @@ -52,6 +52,10 @@ where pub fn remove(&mut self, input_device: T) -> Option { self.axis_data.remove(&input_device) } + /// Returns an iterator of all the input devices that have position data + pub fn devices(&self) -> impl ExactSizeIterator { + self.axis_data.keys() + } } #[cfg(test)] @@ -108,4 +112,34 @@ mod tests { assert_eq!(expected, actual); } } + + #[test] + fn test_axis_devices() { + let mut axis = Axis::::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); + } }