Skip to content

Commit bebfb01

Browse files
1e1001james7132
authored andcommitted
add Axis::devices to get all the input devices (bevyengine#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<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.
1 parent b0b7e58 commit bebfb01

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/bevy_input/src/axis.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ where
5252
pub fn remove(&mut self, input_device: T) -> Option<f32> {
5353
self.axis_data.remove(&input_device)
5454
}
55+
/// Returns an iterator of all the input devices that have position data
56+
pub fn devices(&self) -> impl ExactSizeIterator<Item = &T> {
57+
self.axis_data.keys()
58+
}
5559
}
5660

5761
#[cfg(test)]
@@ -108,4 +112,34 @@ mod tests {
108112
assert_eq!(expected, actual);
109113
}
110114
}
115+
116+
#[test]
117+
fn test_axis_devices() {
118+
let mut axis = Axis::<GamepadButton>::default();
119+
assert_eq!(axis.devices().count(), 0);
120+
121+
axis.set(
122+
GamepadButton::new(Gamepad::new(1), GamepadButtonType::RightTrigger),
123+
0.1,
124+
);
125+
assert_eq!(axis.devices().count(), 1);
126+
127+
axis.set(
128+
GamepadButton::new(Gamepad::new(1), GamepadButtonType::LeftTrigger),
129+
0.5,
130+
);
131+
assert_eq!(axis.devices().count(), 2);
132+
133+
axis.set(
134+
GamepadButton::new(Gamepad::new(1), GamepadButtonType::RightTrigger),
135+
-0.1,
136+
);
137+
assert_eq!(axis.devices().count(), 2);
138+
139+
axis.remove(GamepadButton::new(
140+
Gamepad::new(1),
141+
GamepadButtonType::RightTrigger,
142+
));
143+
assert_eq!(axis.devices().count(), 1);
144+
}
111145
}

0 commit comments

Comments
 (0)