diff --git a/crates/bevy_input/src/axis.rs b/crates/bevy_input/src/axis.rs index 228365f16bdffa..6c88145da13b71 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); + } }