Skip to content

Commit b4fa833

Browse files
authored
Add get_unclamped to Axis (#8871)
# Objective Add a get_unclamped method to [Axis](https://docs.rs/bevy/0.10.1/bevy/input/struct.Axis.html) to allow it to be used in cases where being able to get a precise relative movement is important. For example, camera zoom with the mouse wheel. This would make it possible for libraries like leafwing input manager to leverage `Axis` for mouse motion and mouse wheel axis mapping. I tried to use it my PR here Leafwing-Studios/leafwing-input-manager#346 but will likely have to revert that and read the mouse wheel events for now which is what prompted this PR. ## Solution Instead of clamping the axis value when it is set, it now stores the raw value and clamps it in the `get` method. This allows a simple get_unclamped method that just returns the raw value. ## Changelog - Added a get_unclamped method to Axis that can return values outside of -1.0 to 1.0
1 parent 6440546 commit b4fa833

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

crates/bevy_input/src/axis.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::hash::Hash;
44

55
/// Stores the position data of the input devices of type `T`.
66
///
7-
/// The values are stored as `f32`s, which range from [`Axis::MIN`] to [`Axis::MAX`], inclusive.
7+
/// The values are stored as `f32`s, using [`Axis::set`].
8+
/// Use [`Axis::get`] to retrieve the value clamped between [`Axis::MIN`] and [`Axis::MAX`]
9+
/// inclusive, or unclamped using [`Axis::get_unclamped`].
810
#[derive(Debug, Resource)]
911
pub struct Axis<T> {
1012
/// The position data of the input devices.
@@ -34,20 +36,34 @@ where
3436

3537
/// Sets the position data of the `input_device` to `position_data`.
3638
///
37-
/// The `position_data` is clamped to be between [`Axis::MIN`] and [`Axis::MAX`], inclusive.
38-
///
3939
/// If the `input_device`:
4040
/// - was present before, the position data is updated, and the old value is returned.
4141
/// - wasn't present before, [None] is returned.
4242
pub fn set(&mut self, input_device: T, position_data: f32) -> Option<f32> {
43-
let new_position_data = position_data.clamp(Self::MIN, Self::MAX);
44-
self.axis_data.insert(input_device, new_position_data)
43+
self.axis_data.insert(input_device, position_data)
4544
}
4645

47-
/// Returns a position data corresponding to the `input_device`.
46+
/// Returns the position data of the provided `input_device`.
47+
///
48+
/// This will be clamped between [`Axis::MIN`] and [`Axis::MAX`] inclusive.
4849
pub fn get(&self, input_device: T) -> Option<f32> {
50+
self.axis_data
51+
.get(&input_device)
52+
.copied()
53+
.map(|value| value.clamp(Self::MIN, Self::MAX))
54+
}
55+
56+
/// Returns the unclamped position data of the provided `input_device`.
57+
///
58+
/// This value may be outside of the [`Axis::MIN`] and [`Axis::MAX`] range.
59+
///
60+
/// Use for things like camera zoom, where you want devices like mouse wheels to be able to
61+
/// exceed the normal range. If being able to move faster on one input device
62+
/// than another would give an unfair advantage, you should likely use [`Axis::get`] instead.
63+
pub fn get_unclamped(&self, input_device: T) -> Option<f32> {
4964
self.axis_data.get(&input_device).copied()
5065
}
66+
5167
/// Removes the position data of the `input_device`, returning the position data if the input device was previously set.
5268
pub fn remove(&mut self, input_device: T) -> Option<f32> {
5369
self.axis_data.remove(&input_device)

0 commit comments

Comments
 (0)