From f28c35f20280a92f45764e7c20dbc5dcff95d561 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Mon, 16 Jan 2023 16:30:52 +0100 Subject: [PATCH 1/4] Fix AxisSettings::new forbidding creating valid bounds --- crates/bevy_input/src/gamepad.rs | 41 +++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 810db8bed6fbf..2121344a3366e 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -611,7 +611,27 @@ impl Default for AxisSettings { } impl AxisSettings { - /// Creates a new `AxisSettings` instance. + /// Same as [`Self::new`], but the bounds are defined as gap from extremities. + /// + /// The default [`AxisSettings`] would be `AxisSettings::new_symmetric(0.05, 0.01)`. + /// + /// The arguments must follow those rules otherwise this returns an `Err`: + /// + `0.0 <= zone_size <= 0.5` + /// + `0.0 <= threshold <= 2.0` + pub fn new_symmetric( + zone_size: f32, + threshold: f32, + ) -> Result { + Self::new( + -1.0 + zone_size, + -zone_size, + zone_size, + 1.0 - zone_size, + threshold, + ) + } + + /// Creates a new [`AxisSettings`] instance. /// /// # Arguments /// @@ -622,9 +642,10 @@ impl AxisSettings { /// + `threshold` - the minimum value by which input must change before the change is registered. /// /// Restrictions: - /// + `-1.0 <= ``livezone_lowerbound`` <= ``deadzone_lowerbound`` <= 0.0 <= ``deadzone_upperbound`` <= - /// ``livezone_upperbound`` <= 1.0` - /// + `0.0 <= ``threshold`` <= 2.0` + /// + /// + `-1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0` + /// + `0.0 <= deadzone_upperbound <= livezone_upperbound <= 1.0` + /// + `0.0 <= threshold <= 2.0` /// /// # Errors /// @@ -638,19 +659,21 @@ impl AxisSettings { livezone_upperbound: f32, threshold: f32, ) -> Result { - if !(-1.0..=0.0).contains(&livezone_lowerbound) { + let within = |value, lower: f32, upper: f32| (lower..=upper).contains(&value); + + if !within(livezone_lowerbound, -1.0, 0.0) { Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange( livezone_lowerbound, )) - } else if !(-1.0..=0.0).contains(&deadzone_lowerbound) { + } else if !within(deadzone_lowerbound, -1.0, 0.0) { Err(AxisSettingsError::DeadZoneLowerBoundOutOfRange( deadzone_lowerbound, )) - } else if !(-1.0..=0.0).contains(&deadzone_upperbound) { + } else if !within(deadzone_upperbound, 0.0, 1.0) { Err(AxisSettingsError::DeadZoneUpperBoundOutOfRange( deadzone_upperbound, )) - } else if !(-1.0..=0.0).contains(&livezone_upperbound) { + } else if !within(livezone_upperbound, 0.0, 1.0) { Err(AxisSettingsError::LiveZoneUpperBoundOutOfRange( livezone_upperbound, )) @@ -668,7 +691,7 @@ impl AxisSettings { deadzone_upperbound, }, ) - } else if !(0.0..=2.0).contains(&threshold) { + } else if !within(threshold, 0.0, 2.0) { Err(AxisSettingsError::Threshold(threshold)) } else { Ok(Self { From d4832b3b631991b712159de3b0aab1ffe2ebc8bf Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Mon, 16 Jan 2023 16:39:16 +0100 Subject: [PATCH 2/4] Add test for fix --- crates/bevy_input/src/gamepad.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 2121344a3366e..9cb33669dca53 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -583,7 +583,7 @@ impl ButtonSettings { /// Otherwise, values will not be rounded. /// /// The valid range is `[-1.0, 1.0]`. -#[derive(Debug, Clone, Reflect, FromReflect)] +#[derive(Debug, Clone, Reflect, FromReflect, PartialEq)] #[reflect(Debug, Default)] pub struct AxisSettings { /// Values that are higher than `livezone_upperbound` will be rounded up to -1.0. @@ -1512,6 +1512,16 @@ mod tests { #[test] fn test_try_out_of_range_axis_settings() { let mut axis_settings = AxisSettings::default(); + assert_eq!( + AxisSettings::new(-0.95, -0.05, 0.05, 0.95, 0.001), + Ok(AxisSettings { + livezone_lowerbound: -0.95, + deadzone_lowerbound: -0.05, + deadzone_upperbound: 0.05, + livezone_upperbound: 0.95, + threshold: 0.001, + }) + ); assert_eq!( Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange(-2.0)), axis_settings.try_set_livezone_lowerbound(-2.0) From cdbd43a13783756a680d58f4951f2236b2fb3377 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Mon, 16 Jan 2023 17:13:54 +0100 Subject: [PATCH 3/4] Use range syntax --- crates/bevy_input/src/gamepad.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 9cb33669dca53..32a86def572dc 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -659,21 +659,19 @@ impl AxisSettings { livezone_upperbound: f32, threshold: f32, ) -> Result { - let within = |value, lower: f32, upper: f32| (lower..=upper).contains(&value); - - if !within(livezone_lowerbound, -1.0, 0.0) { + if !(-1.0..=0.0).contains(&livezone_lowerbound) { Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange( livezone_lowerbound, )) - } else if !within(deadzone_lowerbound, -1.0, 0.0) { + } else if !(-1.0..=0.0).contains(&deadzone_lowerbound) { Err(AxisSettingsError::DeadZoneLowerBoundOutOfRange( deadzone_lowerbound, )) - } else if !within(deadzone_upperbound, 0.0, 1.0) { + } else if !(0.0..=1.0).contains(&deadzone_upperbound) { Err(AxisSettingsError::DeadZoneUpperBoundOutOfRange( deadzone_upperbound, )) - } else if !within(livezone_upperbound, 0.0, 1.0) { + } else if !(0.0..=1.0).contains(&livezone_upperbound) { Err(AxisSettingsError::LiveZoneUpperBoundOutOfRange( livezone_upperbound, )) @@ -691,7 +689,7 @@ impl AxisSettings { deadzone_upperbound, }, ) - } else if !within(threshold, 0.0, 2.0) { + } else if !(0.0..=2.0).contains(&threshold) { Err(AxisSettingsError::Threshold(threshold)) } else { Ok(Self { From 6251adc68c7535bc2e36e7fa35bbe6892b54f6aa Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Mon, 16 Jan 2023 17:17:00 +0100 Subject: [PATCH 4/4] Remove new_symmetric --- crates/bevy_input/src/gamepad.rs | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 32a86def572dc..03ac48a241129 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -611,26 +611,6 @@ impl Default for AxisSettings { } impl AxisSettings { - /// Same as [`Self::new`], but the bounds are defined as gap from extremities. - /// - /// The default [`AxisSettings`] would be `AxisSettings::new_symmetric(0.05, 0.01)`. - /// - /// The arguments must follow those rules otherwise this returns an `Err`: - /// + `0.0 <= zone_size <= 0.5` - /// + `0.0 <= threshold <= 2.0` - pub fn new_symmetric( - zone_size: f32, - threshold: f32, - ) -> Result { - Self::new( - -1.0 + zone_size, - -zone_size, - zone_size, - 1.0 - zone_size, - threshold, - ) - } - /// Creates a new [`AxisSettings`] instance. /// /// # Arguments