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

[Merged by Bors] - add Axis::devices to get all the input devices #5400

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 34 additions & 0 deletions crates/bevy_input/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -107,4 +111,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);
}
}