From 73c1cd509b37b5bc32e2863b17dcc2532ab80459 Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 20:31:15 -0400 Subject: [PATCH 01/25] Getters and setters for AxisSettings --- crates/bevy_input/Cargo.toml | 1 + crates/bevy_input/src/gamepad.rs | 195 ++++++++++++++++++++++++++++--- 2 files changed, 178 insertions(+), 18 deletions(-) diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index e3f500db46e09..1ba33b0c58eeb 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -22,3 +22,4 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev" } # other serde = { version = "1", features = ["derive"], optional = true } +thiserror = "1.0.30" diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 7756fc47af89b..58c1fe3475479 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -2,6 +2,16 @@ use crate::{Axis, Input}; use bevy_ecs::event::{EventReader, EventWriter}; use bevy_ecs::system::{Res, ResMut, Resource}; use bevy_utils::{tracing::info, HashMap, HashSet}; +use thiserror::Error; + +/// Errors that occur when setting settings for gamepad input +#[derive(Error, Debug)] +pub enum GamepadSettingsError { + #[error("{0}")] + InvalidAxisSetting(String), +} + +type Result = std::result::Result; /// A gamepad with an associated `ID`. /// @@ -536,16 +546,12 @@ impl ButtonSettings { /// The current value of an axis is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. #[derive(Debug, Clone)] pub struct AxisSettings { - /// The positive high value at which to apply rounding. - pub positive_high: f32, - /// The positive low value at which to apply rounding. - pub positive_low: f32, - /// The negative high value at which to apply rounding. - pub negative_high: f32, - /// The negative low value at which to apply rounding. - pub negative_low: f32, - /// The threshold defining the minimum difference between the old and new values to apply the changes. - pub threshold: f32, + positive_high: f32, + positive_low: f32, + negative_high: f32, + negative_low: f32, + ///`threshold` defines the minimum difference between old and new values to apply the changes. + threshold: f32, } impl Default for AxisSettings { @@ -561,16 +567,169 @@ impl Default for AxisSettings { } impl AxisSettings { - /// Filters the `new_value` according to the specified settings. + /// Get the value above which inputs will be rounded up to 1.0 + pub fn positive_high(&self) -> f32 { + self.positive_high + } + + /// Try to set the value above which inputs will be rounded up to 1.0 /// - /// If the `new_value` is: - /// - in-between `negative_low` and `positive_low` it will be rounded to 0.0. - /// - higher than or equal to `positive_high` it will be rounded to 1.0. - /// - lower than or equal to `negative_high` it will be rounded to -1.0. - /// - Otherwise it will not be rounded. + /// # Errors /// - /// If the difference between the calculated value and the `old_value` is lower or - /// equal to the `threshold`, [`None`] will be returned. + /// If the value passed is less than positive_low or greater than 1.0 + pub fn try_set_positive_high(&mut self, value: f32) -> Result<()> { + if value < self.positive_low || value > 1.0 { + Err(GamepadSettingsError::InvalidAxisSetting( + "positive_high must be greater than positive_low and less than 1.0".to_owned(), + )) + } else { + self.positive_high = value; + Ok(()) + } + } + + /// Set the value above which inputs will be rounded up to 1.0 + /// + /// # Panics + /// + /// If the value passed is less than positive_low or greater than 1.0 + pub fn set_positive_high(&mut self, value: f32) { + if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_positive_high(value) + { + panic!("Invalid axis setting: {}", e); + } + } + + /// Get the value below which positive inputs will be rounded down to 0.0 + pub fn positive_low(&mut self) -> f32 { + self.positive_low + } + + /// Try to set the value below which positive inputs will be rounded down to 0.0 + /// + /// # Errors + /// + /// If the value passed is negative or greater than positive_high + pub fn try_set_positive_low(&mut self, value: f32) -> Result<()> { + if value < 0.0 || value > self.positive_high { + Err(GamepadSettingsError::InvalidAxisSetting( + "positive_low must be positive and less than positive_high".to_owned(), + )) + } else { + self.positive_low = value; + Ok(()) + } + } + + /// Set the value below which positive inputs will be rounded down to 0.0 + /// + /// # Panics + /// + /// If the value passed is negative or greater than positive_high + pub fn set_positive_low(&mut self, value: f32) { + if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_positive_low(value) { + panic!("Invalid axis setting: {}", e); + } + } + + /// Get the value above which negative inputs will be rounded up to 0.0 + pub fn negative_low(&self) -> f32 { + self.negative_low + } + + /// Try to set the value above which negative inputs will be rounded up to 0.0 + /// + /// # Errors + /// + /// If the value passed is positive or less than negative_high + pub fn try_set_negative_low(&mut self, value: f32) -> Result<()> { + if value < self.negative_high || value > 0.0 { + Err(GamepadSettingsError::InvalidAxisSetting( + "negative_low must be negative and greater than negative_high".to_owned(), + )) + } else { + self.negative_low = value; + Ok(()) + } + } + + /// Set the value above which negative inputs will be rounded up to 0.0 + /// + /// # Panics + /// + /// If the value passed is positive or less than negative_high + pub fn set_negative_low(&mut self, value: f32) { + if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_negative_low(value) { + panic!("Invalid axis setting: {}", e); + } + self.negative_low = value; + } + + /// Get the value below which inputs will be rounded down to -1.0 + pub fn negative_high(&self) -> f32 { + self.negative_high + } + + /// Try to get the value below which inputs will be rounded down to -1.0 + /// + /// # Errors + /// + /// If the value passed is less than -1.0 or greater than negative_low + pub fn try_set_negative_high(&mut self, value: f32) -> Result<()> { + if value < -1.0 || value > self.negative_low { + Err(GamepadSettingsError::InvalidAxisSetting( + "negative_high must be greater than -1.0 and less than negative_low".to_owned(), + )) + } else { + self.negative_high = value; + Ok(()) + } + } + + /// Get the value below which inputs will be rounded down to -1.0 + /// + /// # Panics + /// + /// If the value passed is less than -1.0 or greater than negative_low + pub fn set_negative_high(&mut self, value: f32) { + if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_negative_high(value) + { + panic!("Invalid axis setting: {}", e); + } + } + + /// Get the minimum value by which input must change before the changes will be applied + pub fn threshold(&self) -> f32 { + self.threshold + } + + /// Try to set the minimum value by which input must change before the changes will be applied + /// + /// # Errors + /// + /// If the value passed is not within [0.0, 2.0] + pub fn try_set_threshold(&mut self, value: f32) -> Result<()> { + if value < 0.0 || value > 2.0 { + Err(GamepadSettingsError::InvalidAxisSetting( + "threshold must be between 0.0 and 2.0, inclusive".to_owned(), + )) + } else { + self.threshold = value; + Ok(()) + } + } + + /// Set the minimum value by which input must change before the changes will be applied + /// + /// # Panics + /// + /// If the value passed is not within [0.0, 2.0] + pub fn set_threshold(&mut self, value: f32) { + if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_threshold(value) { + panic!("Invalid axis setting: {}", e); + } + } + fn filter(&self, new_value: f32, old_value: Option) -> Option { let new_value = if new_value <= self.positive_low && new_value >= self.negative_low { 0.0 From d7fb8a57c539f30d53a860ae153e3386f71e6f43 Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 20:36:10 -0400 Subject: [PATCH 02/25] Make Clippy happy --- crates/bevy_input/src/gamepad.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 58c1fe3475479..e099b65c93a2e 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -576,7 +576,7 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is less than positive_low or greater than 1.0 + /// If the value passed is less than `positive_low` or greater than 1.0 pub fn try_set_positive_high(&mut self, value: f32) -> Result<()> { if value < self.positive_low || value > 1.0 { Err(GamepadSettingsError::InvalidAxisSetting( @@ -592,7 +592,7 @@ impl AxisSettings { /// /// # Panics /// - /// If the value passed is less than positive_low or greater than 1.0 + /// If the value passed is less than `positive_low` or greater than 1.0 pub fn set_positive_high(&mut self, value: f32) { if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_positive_high(value) { @@ -609,7 +609,7 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is negative or greater than positive_high + /// If the value passed is negative or greater than `positive_high` pub fn try_set_positive_low(&mut self, value: f32) -> Result<()> { if value < 0.0 || value > self.positive_high { Err(GamepadSettingsError::InvalidAxisSetting( @@ -625,7 +625,7 @@ impl AxisSettings { /// /// # Panics /// - /// If the value passed is negative or greater than positive_high + /// If the value passed is negative or greater than `positive_high` pub fn set_positive_low(&mut self, value: f32) { if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_positive_low(value) { panic!("Invalid axis setting: {}", e); @@ -641,7 +641,7 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is positive or less than negative_high + /// If the value passed is positive or less than `negative_high` pub fn try_set_negative_low(&mut self, value: f32) -> Result<()> { if value < self.negative_high || value > 0.0 { Err(GamepadSettingsError::InvalidAxisSetting( @@ -657,7 +657,7 @@ impl AxisSettings { /// /// # Panics /// - /// If the value passed is positive or less than negative_high + /// If the value passed is positive or less than `negative_high` pub fn set_negative_low(&mut self, value: f32) { if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_negative_low(value) { panic!("Invalid axis setting: {}", e); @@ -674,7 +674,7 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is less than -1.0 or greater than negative_low + /// If the value passed is less than -1.0 or greater than `negative_low` pub fn try_set_negative_high(&mut self, value: f32) -> Result<()> { if value < -1.0 || value > self.negative_low { Err(GamepadSettingsError::InvalidAxisSetting( @@ -690,7 +690,7 @@ impl AxisSettings { /// /// # Panics /// - /// If the value passed is less than -1.0 or greater than negative_low + /// If the value passed is less than -1.0 or greater than `negative_low` pub fn set_negative_high(&mut self, value: f32) { if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_negative_high(value) { @@ -709,7 +709,7 @@ impl AxisSettings { /// /// If the value passed is not within [0.0, 2.0] pub fn try_set_threshold(&mut self, value: f32) -> Result<()> { - if value < 0.0 || value > 2.0 { + if !(0.0..=2.0).contains(&value) { Err(GamepadSettingsError::InvalidAxisSetting( "threshold must be between 0.0 and 2.0, inclusive".to_owned(), )) From b2707664c1620587313397356b15cb6d0a6bc637 Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 20:53:20 -0400 Subject: [PATCH 03/25] AxisSettings setters return the new value rather than panicking --- crates/bevy_input/src/gamepad.rs | 74 +++++++++++++------------------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index e099b65c93a2e..49568bfb3820f 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -588,16 +588,12 @@ impl AxisSettings { } } - /// Set the value above which inputs will be rounded up to 1.0 - /// - /// # Panics - /// - /// If the value passed is less than `positive_low` or greater than 1.0 - pub fn set_positive_high(&mut self, value: f32) { - if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_positive_high(value) - { - panic!("Invalid axis setting: {}", e); - } + /// Try to set the value above which inputs will be rounded up to 1.0. If the value is less than + /// `positive_low` or greater than 1.0, the value will not be changed. + /// Returns the new value of `positive_high`. + pub fn set_positive_high(&mut self, value: f32) -> f32 { + self.try_set_positive_high(value).ok(); + self.positive_high } /// Get the value below which positive inputs will be rounded down to 0.0 @@ -621,15 +617,13 @@ impl AxisSettings { } } - /// Set the value below which positive inputs will be rounded down to 0.0 - /// - /// # Panics + /// Try to set the value below which positive inputs will be rounded down to 0.0. If the value + /// passed is negative or greater than `positive_high`, the value will not be changed. /// - /// If the value passed is negative or greater than `positive_high` - pub fn set_positive_low(&mut self, value: f32) { - if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_positive_low(value) { - panic!("Invalid axis setting: {}", e); - } + /// Returns the new value of `positive_low`. + pub fn set_positive_low(&mut self, value: f32) -> f32 { + self.try_set_positive_low(value).ok(); + self.positive_low } /// Get the value above which negative inputs will be rounded up to 0.0 @@ -653,16 +647,13 @@ impl AxisSettings { } } - /// Set the value above which negative inputs will be rounded up to 0.0 + /// Try to set the value above which negative inputs will be rounded up to 0.0. If the value + /// passed is positive or less than `negative_high`, the value will not be changed. /// - /// # Panics - /// - /// If the value passed is positive or less than `negative_high` - pub fn set_negative_low(&mut self, value: f32) { - if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_negative_low(value) { - panic!("Invalid axis setting: {}", e); - } - self.negative_low = value; + /// Returns the new value of `negative_low`. + pub fn set_negative_low(&mut self, value: f32) -> f32 { + self.try_set_negative_low(value).ok(); + self.negative_low } /// Get the value below which inputs will be rounded down to -1.0 @@ -686,16 +677,13 @@ impl AxisSettings { } } - /// Get the value below which inputs will be rounded down to -1.0 + /// Try to set the value below which inputs will be rounded down to -1.0. If the value passed is + /// less than -1.0 or greater than `negative_low`, the value will not be changed. /// - /// # Panics - /// - /// If the value passed is less than -1.0 or greater than `negative_low` - pub fn set_negative_high(&mut self, value: f32) { - if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_negative_high(value) - { - panic!("Invalid axis setting: {}", e); - } + /// Returns the new value of `negative_high`. + pub fn set_negative_high(&mut self, value: f32) -> f32 { + self.try_set_negative_high(value).ok(); + self.negative_high } /// Get the minimum value by which input must change before the changes will be applied @@ -719,15 +707,13 @@ impl AxisSettings { } } - /// Set the minimum value by which input must change before the changes will be applied - /// - /// # Panics + /// Try to set the minimum value by which input must change before the changes will be applied. + /// If the value passed is not within [0.0, 2.0], the value will not be changed. /// - /// If the value passed is not within [0.0, 2.0] - pub fn set_threshold(&mut self, value: f32) { - if let Err(GamepadSettingsError::InvalidAxisSetting(e)) = self.try_set_threshold(value) { - panic!("Invalid axis setting: {}", e); - } + /// Returns the new value of threshold. + pub fn set_threshold(&mut self, value: f32) -> f32 { + self.try_set_threshold(value).ok(); + self.threshold } fn filter(&self, new_value: f32, old_value: Option) -> Option { From 2df74e7fa4fcbfb6e9952df33f4b45e4003e0a63 Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 21:10:23 -0400 Subject: [PATCH 04/25] Rename fields in AxisSettings positive_high => livezone_upperbound positive_low => deadzone_upperbound negative_low => deadzone_lowerbound negative_high => livezone_lowerbound --- crates/bevy_input/src/gamepad.rs | 140 ++++++++++++++++--------------- 1 file changed, 72 insertions(+), 68 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 49568bfb3820f..7148fa30f6e75 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -534,9 +534,9 @@ impl ButtonSettings { /// /// ## Logic /// -/// - Values that are in-between `negative_low` and `positive_low` will be rounded to 0.0. -/// - Values that are higher than or equal to `positive_high` will be rounded to 1.0. -/// - Values that are lower than or equal to `negative_high` will be rounded to -1.0. +/// - Values that are in-between `livezone_lowerbound` and `deadzone_upperbound` will be rounded to 0.0. +/// - Values that are higher than or equal to `livezone_upperbound` will be rounded to 1.0. +/// - Values that are lower than or equal to `deadzone_lowerbound` will be rounded to -1.0. /// - Otherwise, values will not be rounded. /// /// The valid range is from -1.0 to 1.0, inclusive. @@ -546,10 +546,10 @@ impl ButtonSettings { /// The current value of an axis is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. #[derive(Debug, Clone)] pub struct AxisSettings { - positive_high: f32, - positive_low: f32, - negative_high: f32, - negative_low: f32, + livezone_upperbound: f32, + deadzone_upperbound: f32, + deadzone_lowerbound: f32, + livezone_lowerbound: f32, ///`threshold` defines the minimum difference between old and new values to apply the changes. threshold: f32, } @@ -557,10 +557,10 @@ pub struct AxisSettings { impl Default for AxisSettings { fn default() -> Self { AxisSettings { - positive_high: 0.95, - positive_low: 0.05, - negative_high: -0.95, - negative_low: -0.05, + livezone_upperbound: 0.95, + deadzone_upperbound: 0.05, + deadzone_lowerbound: -0.95, + livezone_lowerbound: -0.05, threshold: 0.01, } } @@ -568,122 +568,125 @@ impl Default for AxisSettings { impl AxisSettings { /// Get the value above which inputs will be rounded up to 1.0 - pub fn positive_high(&self) -> f32 { - self.positive_high + pub fn livezone_upperbound(&self) -> f32 { + self.livezone_upperbound } /// Try to set the value above which inputs will be rounded up to 1.0 /// /// # Errors /// - /// If the value passed is less than `positive_low` or greater than 1.0 - pub fn try_set_positive_high(&mut self, value: f32) -> Result<()> { - if value < self.positive_low || value > 1.0 { + /// If the value passed is less than `deadzone_upperbound` or greater than 1.0 + pub fn try_set_livezone_upperbound(&mut self, value: f32) -> Result<()> { + if value < self.deadzone_upperbound || value > 1.0 { Err(GamepadSettingsError::InvalidAxisSetting( - "positive_high must be greater than positive_low and less than 1.0".to_owned(), + "livezone_upperbound must be greater than deadzone_upperbound and less than 1.0" + .to_owned(), )) } else { - self.positive_high = value; + self.livezone_upperbound = value; Ok(()) } } /// Try to set the value above which inputs will be rounded up to 1.0. If the value is less than - /// `positive_low` or greater than 1.0, the value will not be changed. - /// Returns the new value of `positive_high`. - pub fn set_positive_high(&mut self, value: f32) -> f32 { - self.try_set_positive_high(value).ok(); - self.positive_high + /// `deadzone_upperbound` or greater than 1.0, the value will not be changed. + /// Returns the new value of `livezone_upperbound`. + pub fn set_livezone_upperbound(&mut self, value: f32) -> f32 { + self.try_set_livezone_upperbound(value).ok(); + self.livezone_upperbound } /// Get the value below which positive inputs will be rounded down to 0.0 - pub fn positive_low(&mut self) -> f32 { - self.positive_low + pub fn deadzone_upperbound(&mut self) -> f32 { + self.deadzone_upperbound } /// Try to set the value below which positive inputs will be rounded down to 0.0 /// /// # Errors /// - /// If the value passed is negative or greater than `positive_high` - pub fn try_set_positive_low(&mut self, value: f32) -> Result<()> { - if value < 0.0 || value > self.positive_high { + /// If the value passed is negative or greater than `livezone_upperbound` + pub fn try_set_deadzone_upperbound(&mut self, value: f32) -> Result<()> { + if value < 0.0 || value > self.livezone_upperbound { Err(GamepadSettingsError::InvalidAxisSetting( - "positive_low must be positive and less than positive_high".to_owned(), + "deadzone_upperbound must be positive and less than livezone_upperbound".to_owned(), )) } else { - self.positive_low = value; + self.deadzone_upperbound = value; Ok(()) } } /// Try to set the value below which positive inputs will be rounded down to 0.0. If the value - /// passed is negative or greater than `positive_high`, the value will not be changed. + /// passed is negative or greater than `livezone_upperbound`, the value will not be changed. /// - /// Returns the new value of `positive_low`. - pub fn set_positive_low(&mut self, value: f32) -> f32 { - self.try_set_positive_low(value).ok(); - self.positive_low + /// Returns the new value of `deadzone_upperbound`. + pub fn set_deadzone_upperbound(&mut self, value: f32) -> f32 { + self.try_set_deadzone_upperbound(value).ok(); + self.deadzone_upperbound } /// Get the value above which negative inputs will be rounded up to 0.0 - pub fn negative_low(&self) -> f32 { - self.negative_low + pub fn livezone_lowerbound(&self) -> f32 { + self.livezone_lowerbound } /// Try to set the value above which negative inputs will be rounded up to 0.0 /// /// # Errors /// - /// If the value passed is positive or less than `negative_high` - pub fn try_set_negative_low(&mut self, value: f32) -> Result<()> { - if value < self.negative_high || value > 0.0 { + /// If the value passed is positive or less than `deadzone_lowerbound` + pub fn try_set_livezone_lowerbound(&mut self, value: f32) -> Result<()> { + if value < self.deadzone_lowerbound || value > 0.0 { Err(GamepadSettingsError::InvalidAxisSetting( - "negative_low must be negative and greater than negative_high".to_owned(), + "livezone_lowerbound must be negative and greater than deadzone_lowerbound" + .to_owned(), )) } else { - self.negative_low = value; + self.livezone_lowerbound = value; Ok(()) } } /// Try to set the value above which negative inputs will be rounded up to 0.0. If the value - /// passed is positive or less than `negative_high`, the value will not be changed. + /// passed is positive or less than `deadzone_lowerbound`, the value will not be changed. /// - /// Returns the new value of `negative_low`. - pub fn set_negative_low(&mut self, value: f32) -> f32 { - self.try_set_negative_low(value).ok(); - self.negative_low + /// Returns the new value of `livezone_lowerbound`. + pub fn set_livezone_lowerbound(&mut self, value: f32) -> f32 { + self.try_set_livezone_lowerbound(value).ok(); + self.livezone_lowerbound } /// Get the value below which inputs will be rounded down to -1.0 - pub fn negative_high(&self) -> f32 { - self.negative_high + pub fn deadzone_lowerbound(&self) -> f32 { + self.deadzone_lowerbound } /// Try to get the value below which inputs will be rounded down to -1.0 /// /// # Errors /// - /// If the value passed is less than -1.0 or greater than `negative_low` - pub fn try_set_negative_high(&mut self, value: f32) -> Result<()> { - if value < -1.0 || value > self.negative_low { + /// If the value passed is less than -1.0 or greater than `livezone_lowerbound` + pub fn try_set_deadzone_lowerbound(&mut self, value: f32) -> Result<()> { + if value < -1.0 || value > self.livezone_lowerbound { Err(GamepadSettingsError::InvalidAxisSetting( - "negative_high must be greater than -1.0 and less than negative_low".to_owned(), + "deadzone_lowerbound must be greater than -1.0 and less than livezone_lowerbound" + .to_owned(), )) } else { - self.negative_high = value; + self.deadzone_lowerbound = value; Ok(()) } } /// Try to set the value below which inputs will be rounded down to -1.0. If the value passed is - /// less than -1.0 or greater than `negative_low`, the value will not be changed. + /// less than -1.0 or greater than `livezone_lowerbound`, the value will not be changed. /// - /// Returns the new value of `negative_high`. - pub fn set_negative_high(&mut self, value: f32) -> f32 { - self.try_set_negative_high(value).ok(); - self.negative_high + /// Returns the new value of `deadzone_lowerbound`. + pub fn set_deadzone_lowerbound(&mut self, value: f32) -> f32 { + self.try_set_deadzone_lowerbound(value).ok(); + self.deadzone_lowerbound } /// Get the minimum value by which input must change before the changes will be applied @@ -717,15 +720,16 @@ impl AxisSettings { } fn filter(&self, new_value: f32, old_value: Option) -> Option { - let new_value = if new_value <= self.positive_low && new_value >= self.negative_low { - 0.0 - } else if new_value >= self.positive_high { - 1.0 - } else if new_value <= self.negative_high { - -1.0 - } else { - new_value - }; + let new_value = + if new_value <= self.deadzone_upperbound && new_value >= self.livezone_lowerbound { + 0.0 + } else if new_value >= self.livezone_upperbound { + 1.0 + } else if new_value <= self.deadzone_lowerbound { + -1.0 + } else { + new_value + }; if let Some(old_value) = old_value { if (new_value - old_value).abs() <= self.threshold { From d8a92cf917063fcd127fa89f92eccd3396f9c439 Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 21:15:14 -0400 Subject: [PATCH 05/25] Validate button settings --- crates/bevy_input/src/gamepad.rs | 76 ++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 7148fa30f6e75..8340f9650e392 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -9,6 +9,8 @@ use thiserror::Error; pub enum GamepadSettingsError { #[error("{0}")] InvalidAxisSetting(String), + #[error("{0}")] + InvalidButtonSetting(String), } type Result = std::result::Result; @@ -496,17 +498,15 @@ impl GamepadSettings { /// The current value of a button is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. #[derive(Debug, Clone)] pub struct ButtonSettings { - /// The threshold for the button to be considered as pressed. - pub press: f32, - /// The threshold for the button to be considered as released. - pub release: f32, + press_threshold: f32, + release_threshold: f32, } impl Default for ButtonSettings { fn default() -> Self { ButtonSettings { - press: 0.75, - release: 0.65, + press_threshold: 0.75, + release_threshold: 0.65, } } } @@ -516,14 +516,74 @@ impl ButtonSettings { /// /// A button is considered pressed if the `value` passed is greater than or equal to the `press` threshold. fn is_pressed(&self, value: f32) -> bool { - value >= self.press + value >= self.press_threshold } /// Returns `true` if the button is released. /// /// A button is considered released if the `value` passed is lower than or equal to the `release` threshold. fn is_released(&self, value: f32) -> bool { - value <= self.release + value <= self.release_threshold + } + + /// Get the button input threshold above which the button is considered pressed + pub fn press_threshold(&self) -> f32 { + self.press_threshold + } + + /// Try to set the button input threshold above which the button is considered pressed + /// + /// # Errors + /// + /// If the value passed is outside the range [`release_threshold`, 1.0] + pub fn try_set_press_threshold(&mut self, value: f32) -> Result<()> { + if (self.release_threshold..=1.0).contains(&value) { + self.press_threshold = value; + Ok(()) + } else { + Err(GamepadSettingsError::InvalidButtonSetting( + "press_threshold must be in the range [release_threshold, 1.0]".to_owned(), + )) + } + } + + /// Try to set the button input threshold above which the button is considered pressed. If the + /// value passed is outside the range [`release_threshold`, 1.0], the value will not be changed. + /// + /// Returns the new value of `press_threshold`. + pub fn set_press_threshold(&mut self, value: f32) -> f32 { + self.try_set_press_threshold(value).ok(); + self.press_threshold + } + + /// Get the button input threshold below which the button is considered released + pub fn release_threshold(&self) -> f32 { + self.release_threshold + } + + /// Try to set the button input threshold below which the button is considered released + /// + /// # Errors + /// + /// If the value passed is outside the range [0.0, `press_threshold`] + pub fn try_set_release_threshold(&mut self, value: f32) -> Result<()> { + if (0.0..=self.press_threshold).contains(&value) { + self.release_threshold = value; + Ok(()) + } else { + Err(GamepadSettingsError::InvalidButtonSetting( + "release_threshold must be in the range [0.0, press_threshold]".to_owned(), + )) + } + } + + /// Try to set the button input threshold below which the button is considered released. If the + /// value passed is outside the range [0.0, `press_threshold`], the value will not be changed. + /// + /// Returns the new value of `release_threshold`. + pub fn set_release_threshold(&mut self, value: f32) -> f32 { + self.try_set_release_threshold(value).ok(); + self.release_threshold } } From d6a56d79fde78aebc711ff354d060c6fecf8a4f2 Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 21:29:17 -0400 Subject: [PATCH 06/25] Add ButtonSettings::new() --- crates/bevy_input/src/gamepad.rs | 40 ++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 8340f9650e392..931b907dd0401 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -490,8 +490,8 @@ impl GamepadSettings { /// ## Usage /// /// It is used inside of the [`GamepadSettings`] to define the threshold for a gamepad button -/// to be considered pressed or released. A button is considered pressed if the `press` -/// value is surpassed and released if the `release` value is undercut. +/// to be considered pressed or released. A button is considered pressed if the `press_threshold` +/// value is surpassed and released if the `release_threshold` value is undercut. /// /// ## Updating /// @@ -512,6 +512,36 @@ impl Default for ButtonSettings { } impl ButtonSettings { + /// Creates a new `ButtonSettings` instance + /// + /// # Arguments + /// + /// + `press_threshold` - the value above which the button is considered pressed. + /// + `release_threshold` - the value below which the button is considered released. + /// + /// Restrictions: + /// + `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` + /// + /// # Errors + /// + /// If the restrictions are not met, `InvalidButtonSetting` will be returned. + pub fn new(press_threshold: f32, release_threshold: f32) -> Result { + if 0.0 <= release_threshold + && release_threshold <= press_threshold + && press_threshold <= 1.0 + { + Ok(ButtonSettings { + press_threshold, + release_threshold, + }) + } else { + Err(GamepadSettingsError::InvalidButtonSetting( + "The condition 0.0 <= release_threshold <= press_threshold <= 1.0 must hold true" + .to_owned(), + )) + } + } + /// Returns `true` if the button is pressed. /// /// A button is considered pressed if the `value` passed is greater than or equal to the `press` threshold. @@ -535,7 +565,8 @@ impl ButtonSettings { /// /// # Errors /// - /// If the value passed is outside the range [`release_threshold`, 1.0] + /// If the value passed is outside the range [`release_threshold`, 1.0], `InvalidButtonSetting` is + /// returned. pub fn try_set_press_threshold(&mut self, value: f32) -> Result<()> { if (self.release_threshold..=1.0).contains(&value) { self.press_threshold = value; @@ -565,7 +596,8 @@ impl ButtonSettings { /// /// # Errors /// - /// If the value passed is outside the range [0.0, `press_threshold`] + /// If the value passed is outside the range [0.0, `press_threshold`], `InvalidButtonSetting` is + /// returned. pub fn try_set_release_threshold(&mut self, value: f32) -> Result<()> { if (0.0..=self.press_threshold).contains(&value) { self.release_threshold = value; From 17c2400bd90de6d589d9cfa4b6b2e42eaf5895be Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 21:37:44 -0400 Subject: [PATCH 07/25] Add AxisSettings::new() --- crates/bevy_input/src/gamepad.rs | 86 ++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 931b907dd0401..7eb3029d30532 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -511,6 +511,12 @@ impl Default for ButtonSettings { } } +/// Manages settings for gamepad buttons +/// +/// + `press_threshold` is the button input value above which the button is considered pressed. +/// + `release_threshold` is the button input value below which the button is considered released. +/// +/// Allowed values: `0.0 <= release_threshold <= press_threshold <= 1.0` impl ButtonSettings { /// Creates a new `ButtonSettings` instance /// @@ -525,7 +531,7 @@ impl ButtonSettings { /// # Errors /// /// If the restrictions are not met, `InvalidButtonSetting` will be returned. - pub fn new(press_threshold: f32, release_threshold: f32) -> Result { + pub fn new(press_threshold: f32, release_threshold: f32) -> Result { if 0.0 <= release_threshold && release_threshold <= press_threshold && press_threshold <= 1.0 @@ -623,26 +629,24 @@ impl ButtonSettings { /// /// It is used inside of the [`GamepadSettings`] to define the sensitivity range and /// threshold for an axis. +/// Values that are higher than `livezone_upperbound` will be rounded up to 1.0. +/// Values that are lower than `livezone_lowerbound` will be rounded down to -1.0. +/// Values that are in-between `deadzone_lowerbound` and `deadzone_upperbound` will be rounded +/// to 0.0. +/// Otherwise, values will not be rounded. /// -/// ## Logic -/// -/// - Values that are in-between `livezone_lowerbound` and `deadzone_upperbound` will be rounded to 0.0. -/// - Values that are higher than or equal to `livezone_upperbound` will be rounded to 1.0. -/// - Values that are lower than or equal to `deadzone_lowerbound` will be rounded to -1.0. -/// - Otherwise, values will not be rounded. -/// -/// The valid range is from -1.0 to 1.0, inclusive. -/// -/// ## Updating -/// -/// The current value of an axis is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. +/// The valid range is [-1, 1]. #[derive(Debug, Clone)] pub struct AxisSettings { + /// Values that are higher than `livezone_upperbound` will be rounded up to -1.0. livezone_upperbound: f32, + /// Positive values that are less than `deadzone_upperbound` will be rounded down to 0.0. deadzone_upperbound: f32, + /// Negative values that are greater than `deadzone_lowerbound` will be rounded up to 0.0. deadzone_lowerbound: f32, + /// Values that are lower than `livezone_lowerbound` will be rounded down to -1.0. livezone_lowerbound: f32, - ///`threshold` defines the minimum difference between old and new values to apply the changes. + /// `threshold` defines the minimum difference between old and new values to apply the changes. threshold: f32, } @@ -659,6 +663,56 @@ impl Default for AxisSettings { } impl AxisSettings { + /// Creates a new `AxisSettings` instance + /// + /// # Arguments + /// + /// + `livezone_lowerbound` - the value below which inputs will be rounded down to -1.0 + /// + `deadzone_lowerbound` - the value above which negative inputs will be rounded up to 0.0 + /// + `deadzone_upperbound` - the value below which positive inputs will be rounded down to 0.0 + /// + `livezone_upperbound` - the value above which inputs will be rounded up to 1.0 + /// + `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` + /// + /// # Errors + /// + /// If the restrictions are not met, `InvalidButtonSetting` will be returned. + pub fn new( + livezone_lowerbound: f32, + deadzone_lowerbound: f32, + deadzone_upperbound: f32, + livezone_upperbound: f32, + threshold: f32, + ) -> Result { + if -1.0 <= livezone_lowerbound + && livezone_lowerbound <= deadzone_lowerbound + && deadzone_lowerbound <= 0.0 + && 0.0 <= deadzone_upperbound + && deadzone_upperbound <= livezone_upperbound + && livezone_upperbound <= 1.0 + && (0.0..=2.0).contains(&threshold) + { + Ok(Self { + livezone_lowerbound, + deadzone_lowerbound, + deadzone_upperbound, + livezone_upperbound, + threshold, + }) + } else { + Err(GamepadSettingsError::InvalidAxisSetting( + "The conditions -1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0 <=\ + deadzone_upperbound <= livezone_upperbound <= 1.0 and \ + 0.0 <= threshold <= 2.0 must hold true" + .to_owned(), + )) + } + } + /// Get the value above which inputs will be rounded up to 1.0 pub fn livezone_upperbound(&self) -> f32 { self.livezone_upperbound @@ -781,12 +835,12 @@ impl AxisSettings { self.deadzone_lowerbound } - /// Get the minimum value by which input must change before the changes will be applied + /// Get the minimum value by which input must change before the change is registered pub fn threshold(&self) -> f32 { self.threshold } - /// Try to set the minimum value by which input must change before the changes will be applied + /// Try to set the minimum value by which input must change before the change is registered /// /// # Errors /// From 12072d6251fb1ac6b66c2dac4a2ebccf5813a4f6 Mon Sep 17 00:00:00 2001 From: targrub Date: Fri, 23 Sep 2022 21:56:57 -0400 Subject: [PATCH 08/25] Move ButtonSettings doc comment from impl to struct --- crates/bevy_input/src/gamepad.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 7eb3029d30532..599772199fe42 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -485,17 +485,12 @@ impl GamepadSettings { } } -/// Settings for a [`GamepadButton`]. -/// -/// ## Usage -/// -/// It is used inside of the [`GamepadSettings`] to define the threshold for a gamepad button -/// to be considered pressed or released. A button is considered pressed if the `press_threshold` -/// value is surpassed and released if the `release_threshold` value is undercut. +/// Manages settings for gamepad buttons /// -/// ## Updating +/// + `press_threshold` is the button input value above which the button is considered pressed. +/// + `release_threshold` is the button input value below which the button is considered released. /// -/// The current value of a button is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. +/// Allowed values: `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` #[derive(Debug, Clone)] pub struct ButtonSettings { press_threshold: f32, @@ -511,12 +506,6 @@ impl Default for ButtonSettings { } } -/// Manages settings for gamepad buttons -/// -/// + `press_threshold` is the button input value above which the button is considered pressed. -/// + `release_threshold` is the button input value below which the button is considered released. -/// -/// Allowed values: `0.0 <= release_threshold <= press_threshold <= 1.0` impl ButtonSettings { /// Creates a new `ButtonSettings` instance /// From efb85a78c5433dd286d4b8e3aad6f553d190635d Mon Sep 17 00:00:00 2001 From: targrub Date: Sat, 24 Sep 2022 11:34:19 -0400 Subject: [PATCH 09/25] Fix comparisons, default values for AxisSettings. --- crates/bevy_input/src/gamepad.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 599772199fe42..b605d374662d0 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -624,7 +624,7 @@ impl ButtonSettings { /// to 0.0. /// Otherwise, values will not be rounded. /// -/// The valid range is [-1, 1]. +/// The valid range is `[-1.0, 1.0]`. #[derive(Debug, Clone)] pub struct AxisSettings { /// Values that are higher than `livezone_upperbound` will be rounded up to -1.0. @@ -644,8 +644,8 @@ impl Default for AxisSettings { AxisSettings { livezone_upperbound: 0.95, deadzone_upperbound: 0.05, - deadzone_lowerbound: -0.95, - livezone_lowerbound: -0.05, + deadzone_lowerbound: -0.05, + livezone_lowerbound: -0.95, threshold: 0.01, } } @@ -856,11 +856,11 @@ impl AxisSettings { fn filter(&self, new_value: f32, old_value: Option) -> Option { let new_value = - if new_value <= self.deadzone_upperbound && new_value >= self.livezone_lowerbound { + if self.deadzone_lowerbound <= new_value && new_value <= self.deadzone_upperbound { 0.0 } else if new_value >= self.livezone_upperbound { 1.0 - } else if new_value <= self.deadzone_lowerbound { + } else if new_value <= self.livezone_lowerbound { -1.0 } else { new_value From ee4def4ad11d63cca7173c4991fa6ae429886169 Mon Sep 17 00:00:00 2001 From: targrub Date: Sat, 24 Sep 2022 11:39:45 -0400 Subject: [PATCH 10/25] Specify thiserror version as 1.0 rather than 1.0.30 --- crates/bevy_input/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index 1ba33b0c58eeb..29463fef08778 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -22,4 +22,4 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev" } # other serde = { version = "1", features = ["derive"], optional = true } -thiserror = "1.0.30" +thiserror = "1.0" From 6bb0687d53cc93768c75c9382a21b6e2290cf582 Mon Sep 17 00:00:00 2001 From: targrub Date: Sat, 24 Sep 2022 14:23:14 -0400 Subject: [PATCH 11/25] Method should take &self, not &mut self. --- crates/bevy_input/src/gamepad.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index b605d374662d0..913ba922e70b5 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -733,7 +733,7 @@ impl AxisSettings { } /// Get the value below which positive inputs will be rounded down to 0.0 - pub fn deadzone_upperbound(&mut self) -> f32 { + pub fn deadzone_upperbound(&self) -> f32 { self.deadzone_upperbound } From 257880cbf66026808ceff4b834b4facd29e32a99 Mon Sep 17 00:00:00 2001 From: targrub Date: Sat, 24 Sep 2022 14:23:45 -0400 Subject: [PATCH 12/25] Use new access methods. --- examples/tools/gamepad_viewer.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/tools/gamepad_viewer.rs b/examples/tools/gamepad_viewer.rs index a191f81f07dbb..99fb95f9b1ff0 100644 --- a/examples/tools/gamepad_viewer.rs +++ b/examples/tools/gamepad_viewer.rs @@ -256,13 +256,17 @@ fn setup_sticks( gamepad_settings: Res, font: Res, ) { - let dead_upper = STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.positive_low; - let dead_lower = STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.negative_low; + let dead_upper = + STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.deadzone_upperbound(); + let dead_lower = + STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.deadzone_lowerbound(); let dead_size = dead_lower.abs() + dead_upper.abs(); let dead_mid = (dead_lower + dead_upper) / 2.0; - let live_upper = STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.positive_high; - let live_lower = STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.negative_high; + let live_upper = + STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.livezone_upperbound(); + let live_lower = + STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.livezone_lowerbound(); let live_size = live_lower.abs() + live_upper.abs(); let live_mid = (live_lower + live_upper) / 2.0; From d76e5cc1d995b348b40d910ead77b489d9153fe9 Mon Sep 17 00:00:00 2001 From: targrub <62773321+targrub@users.noreply.github.com> Date: Sat, 24 Sep 2022 14:59:28 -0400 Subject: [PATCH 13/25] Update crates/bevy_input/src/gamepad.rs Co-authored-by: Alice Cecile --- crates/bevy_input/src/gamepad.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 913ba922e70b5..0a3a3bb29f2a7 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -507,7 +507,7 @@ impl Default for ButtonSettings { } impl ButtonSettings { - /// Creates a new `ButtonSettings` instance + /// Creates a new [`ButtonSettings`] instance /// /// # Arguments /// From 862aa94416179eeed16bf32030aa60a7b11b3689 Mon Sep 17 00:00:00 2001 From: targrub <62773321+targrub@users.noreply.github.com> Date: Thu, 29 Sep 2022 10:52:53 -0400 Subject: [PATCH 14/25] Update crates/bevy_input/src/gamepad.rs Co-authored-by: Afonso Lage --- crates/bevy_input/src/gamepad.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 0a3a3bb29f2a7..4bb65f241f4b9 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -4,7 +4,7 @@ use bevy_ecs::system::{Res, ResMut, Resource}; use bevy_utils::{tracing::info, HashMap, HashSet}; use thiserror::Error; -/// Errors that occur when setting settings for gamepad input +/// Errors that occur when setting settings for gamepad input. #[derive(Error, Debug)] pub enum GamepadSettingsError { #[error("{0}")] From 983acac17ffd893e219c9ce7b6af83870bb95657 Mon Sep 17 00:00:00 2001 From: targrub <62773321+targrub@users.noreply.github.com> Date: Thu, 29 Sep 2022 10:56:09 -0400 Subject: [PATCH 15/25] Apply suggestions from code review Co-authored-by: Afonso Lage --- crates/bevy_input/src/gamepad.rs | 78 +++++++++++++++++--------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 4bb65f241f4b9..54666497d9151 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -485,7 +485,7 @@ impl GamepadSettings { } } -/// Manages settings for gamepad buttons +/// Manages settings for gamepad buttons. /// /// + `press_threshold` is the button input value above which the button is considered pressed. /// + `release_threshold` is the button input value below which the button is considered released. @@ -507,7 +507,7 @@ impl Default for ButtonSettings { } impl ButtonSettings { - /// Creates a new [`ButtonSettings`] instance + /// Creates a new [`ButtonSettings`] instance. /// /// # Arguments /// @@ -551,12 +551,12 @@ impl ButtonSettings { value <= self.release_threshold } - /// Get the button input threshold above which the button is considered pressed + /// Get the button input threshold above which the button is considered pressed. pub fn press_threshold(&self) -> f32 { self.press_threshold } - /// Try to set the button input threshold above which the button is considered pressed + /// Try to set the button input threshold above which the button is considered pressed. /// /// # Errors /// @@ -573,8 +573,8 @@ impl ButtonSettings { } } - /// Try to set the button input threshold above which the button is considered pressed. If the - /// value passed is outside the range [`release_threshold`, 1.0], the value will not be changed. + /// Try to set the button input threshold above which the button is considered pressed. + /// If the value passed is outside the range [`release_threshold`, 1.0], the value will not be changed. /// /// Returns the new value of `press_threshold`. pub fn set_press_threshold(&mut self, value: f32) -> f32 { @@ -582,12 +582,12 @@ impl ButtonSettings { self.press_threshold } - /// Get the button input threshold below which the button is considered released + /// Get the button input threshold below which the button is considered released. pub fn release_threshold(&self) -> f32 { self.release_threshold } - /// Try to set the button input threshold below which the button is considered released + /// Try to set the button input threshold below which the button is considered released. /// /// # Errors /// @@ -652,15 +652,15 @@ impl Default for AxisSettings { } impl AxisSettings { - /// Creates a new `AxisSettings` instance + /// Creates a new `AxisSettings` instance. /// /// # Arguments /// - /// + `livezone_lowerbound` - the value below which inputs will be rounded down to -1.0 - /// + `deadzone_lowerbound` - the value above which negative inputs will be rounded up to 0.0 - /// + `deadzone_upperbound` - the value below which positive inputs will be rounded down to 0.0 - /// + `livezone_upperbound` - the value above which inputs will be rounded up to 1.0 - /// + `threshold` - the minimum value by which input must change before the change is registered + /// + `livezone_lowerbound` - the value below which inputs will be rounded down to -1.0. + /// + `deadzone_lowerbound` - the value above which negative inputs will be rounded up to 0.0. + /// + `deadzone_upperbound` - the value below which positive inputs will be rounded down to 0.0. + /// + `livezone_upperbound` - the value above which inputs will be rounded up to 1.0. + /// + `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`` <= @@ -702,16 +702,16 @@ impl AxisSettings { } } - /// Get the value above which inputs will be rounded up to 1.0 + /// Get the value above which inputs will be rounded up to 1.0. pub fn livezone_upperbound(&self) -> f32 { self.livezone_upperbound } - /// Try to set the value above which inputs will be rounded up to 1.0 + /// Try to set the value above which inputs will be rounded up to 1.0. /// /// # Errors /// - /// If the value passed is less than `deadzone_upperbound` or greater than 1.0 + /// If the value passed is less than `deadzone_upperbound` or greater than 1.0. pub fn try_set_livezone_upperbound(&mut self, value: f32) -> Result<()> { if value < self.deadzone_upperbound || value > 1.0 { Err(GamepadSettingsError::InvalidAxisSetting( @@ -724,24 +724,25 @@ impl AxisSettings { } } - /// Try to set the value above which inputs will be rounded up to 1.0. If the value is less than - /// `deadzone_upperbound` or greater than 1.0, the value will not be changed. + /// Try to set the value above which inputs will be rounded up to 1.0. + /// If the value is less than `deadzone_upperbound` or greater than 1.0, + /// the value will not be changed. /// Returns the new value of `livezone_upperbound`. pub fn set_livezone_upperbound(&mut self, value: f32) -> f32 { self.try_set_livezone_upperbound(value).ok(); self.livezone_upperbound } - /// Get the value below which positive inputs will be rounded down to 0.0 + /// Get the value below which positive inputs will be rounded down to 0.0. pub fn deadzone_upperbound(&self) -> f32 { self.deadzone_upperbound } - /// Try to set the value below which positive inputs will be rounded down to 0.0 + /// Try to set the value below which positive inputs will be rounded down to 0.0. /// /// # Errors /// - /// If the value passed is negative or greater than `livezone_upperbound` + /// If the value passed is negative or greater than `livezone_upperbound`. pub fn try_set_deadzone_upperbound(&mut self, value: f32) -> Result<()> { if value < 0.0 || value > self.livezone_upperbound { Err(GamepadSettingsError::InvalidAxisSetting( @@ -753,8 +754,9 @@ impl AxisSettings { } } - /// Try to set the value below which positive inputs will be rounded down to 0.0. If the value - /// passed is negative or greater than `livezone_upperbound`, the value will not be changed. + /// Try to set the value below which positive inputs will be rounded down to 0.0. + /// If the value passed is negative or greater than `livezone_upperbound`, + /// the value will not be changed. /// /// Returns the new value of `deadzone_upperbound`. pub fn set_deadzone_upperbound(&mut self, value: f32) -> f32 { @@ -762,16 +764,16 @@ impl AxisSettings { self.deadzone_upperbound } - /// Get the value above which negative inputs will be rounded up to 0.0 + /// Get the value above which negative inputs will be rounded up to 0.0. pub fn livezone_lowerbound(&self) -> f32 { self.livezone_lowerbound } - /// Try to set the value above which negative inputs will be rounded up to 0.0 + /// Try to set the value above which negative inputs will be rounded up to 0.0. /// /// # Errors /// - /// If the value passed is positive or less than `deadzone_lowerbound` + /// If the value passed is positive or less than `deadzone_lowerbound`. pub fn try_set_livezone_lowerbound(&mut self, value: f32) -> Result<()> { if value < self.deadzone_lowerbound || value > 0.0 { Err(GamepadSettingsError::InvalidAxisSetting( @@ -784,8 +786,9 @@ impl AxisSettings { } } - /// Try to set the value above which negative inputs will be rounded up to 0.0. If the value - /// passed is positive or less than `deadzone_lowerbound`, the value will not be changed. + /// Try to set the value above which negative inputs will be rounded up to 0.0. + /// If the value passed is positive or less than `deadzone_lowerbound`, + /// the value will not be changed. /// /// Returns the new value of `livezone_lowerbound`. pub fn set_livezone_lowerbound(&mut self, value: f32) -> f32 { @@ -793,16 +796,16 @@ impl AxisSettings { self.livezone_lowerbound } - /// Get the value below which inputs will be rounded down to -1.0 + /// Get the value below which inputs will be rounded down to -1.0. pub fn deadzone_lowerbound(&self) -> f32 { self.deadzone_lowerbound } - /// Try to get the value below which inputs will be rounded down to -1.0 + /// Try to get the value below which inputs will be rounded down to -1.0. /// /// # Errors /// - /// If the value passed is less than -1.0 or greater than `livezone_lowerbound` + /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`. pub fn try_set_deadzone_lowerbound(&mut self, value: f32) -> Result<()> { if value < -1.0 || value > self.livezone_lowerbound { Err(GamepadSettingsError::InvalidAxisSetting( @@ -815,8 +818,9 @@ impl AxisSettings { } } - /// Try to set the value below which inputs will be rounded down to -1.0. If the value passed is - /// less than -1.0 or greater than `livezone_lowerbound`, the value will not be changed. + /// Try to set the value below which inputs will be rounded down to -1.0. + /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`, + /// the value will not be changed. /// /// Returns the new value of `deadzone_lowerbound`. pub fn set_deadzone_lowerbound(&mut self, value: f32) -> f32 { @@ -824,16 +828,16 @@ impl AxisSettings { self.deadzone_lowerbound } - /// Get the minimum value by which input must change before the change is registered + /// Get the minimum value by which input must change before the change is registered. pub fn threshold(&self) -> f32 { self.threshold } - /// Try to set the minimum value by which input must change before the change is registered + /// Try to set the minimum value by which input must change before the change is registered. /// /// # Errors /// - /// If the value passed is not within [0.0, 2.0] + /// If the value passed is not within [0.0, 2.0]. pub fn try_set_threshold(&mut self, value: f32) -> Result<()> { if !(0.0..=2.0).contains(&value) { Err(GamepadSettingsError::InvalidAxisSetting( From 1a586851afc6ae47fc76ddc19398a695fbabf8a2 Mon Sep 17 00:00:00 2001 From: targrub Date: Thu, 29 Sep 2022 11:03:57 -0400 Subject: [PATCH 16/25] fmt --all fix --- crates/bevy_input/src/gamepad.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 54666497d9151..f0a2c65ce90b9 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -725,7 +725,7 @@ impl AxisSettings { } /// Try to set the value above which inputs will be rounded up to 1.0. - /// If the value is less than `deadzone_upperbound` or greater than 1.0, + /// If the value is less than `deadzone_upperbound` or greater than 1.0, /// the value will not be changed. /// Returns the new value of `livezone_upperbound`. pub fn set_livezone_upperbound(&mut self, value: f32) -> f32 { @@ -755,7 +755,7 @@ impl AxisSettings { } /// Try to set the value below which positive inputs will be rounded down to 0.0. - /// If the value passed is negative or greater than `livezone_upperbound`, + /// If the value passed is negative or greater than `livezone_upperbound`, /// the value will not be changed. /// /// Returns the new value of `deadzone_upperbound`. From 9f79f30761bfe039dec159465e252fe95dc6d822 Mon Sep 17 00:00:00 2001 From: targrub Date: Thu, 29 Sep 2022 11:20:57 -0400 Subject: [PATCH 17/25] Resurrected and edited old comment. --- crates/bevy_input/src/gamepad.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index f0a2c65ce90b9..31514fdf06562 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -490,6 +490,10 @@ impl GamepadSettings { /// + `press_threshold` is the button input value above which the button is considered pressed. /// + `release_threshold` is the button input value below which the button is considered released. /// +/// It is used inside of [`GamepadSettings`] to define the threshold for a gamepad button +/// to be considered pressed or released. A button is considered pressed if the `press_threshold` +/// value is surpassed and released if the `release_threshold` value is undercut. +/// /// Allowed values: `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` #[derive(Debug, Clone)] pub struct ButtonSettings { From 35b8c9f7ccb9ed8b88ba462ca21f8a30997a3f67 Mon Sep 17 00:00:00 2001 From: targrub Date: Mon, 3 Oct 2022 15:32:21 -0400 Subject: [PATCH 18/25] Proper return type for ButtonSettings::new() and test added for it. --- crates/bevy_input/src/gamepad.rs | 60 ++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 31514fdf06562..c8d016a085841 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -524,7 +524,7 @@ impl ButtonSettings { /// # Errors /// /// If the restrictions are not met, `InvalidButtonSetting` will be returned. - pub fn new(press_threshold: f32, release_threshold: f32) -> Result { + pub fn new(press_threshold: f32, release_threshold: f32) -> Result { if 0.0 <= release_threshold && release_threshold <= press_threshold && press_threshold <= 1.0 @@ -1090,6 +1090,8 @@ const ALL_AXIS_TYPES: [GamepadAxisType; 6] = [ #[cfg(test)] mod tests { + use crate::gamepad::GamepadSettingsError; + use super::{AxisSettings, ButtonAxisSettings, ButtonSettings}; fn test_button_axis_settings_filter( @@ -1241,7 +1243,7 @@ mod tests { let settings = ButtonSettings::default(); let actual = settings.is_pressed(value); - assert_eq!(expected, actual, "Testing is pressed for value: {}", value); + assert_eq!(expected, actual, "Testing ButtonSettings::is_pressed() for value: {}", value); } } @@ -1263,7 +1265,59 @@ mod tests { let settings = ButtonSettings::default(); let actual = settings.is_released(value); - assert_eq!(expected, actual, "Testing is released for value: {}", value); + assert_eq!(expected, actual, "Testing ButtonSettings::is_released() for value: {}", value); } } + + #[test] + fn test_new_button_settings_given_valid_parameters() { + let cases = [ + (1.0, 0.0), + (1.0, 1.0), + (1.0, 0.9), + (0.9, 0.9), + (0.9, 0.0), + (0.0, 0.0), + ]; + + for (press_threshold, release_threshold) in cases { + let bs = ButtonSettings::new(press_threshold, release_threshold); + match bs { + Ok(button_settings) => { + assert_eq!(button_settings.press_threshold, press_threshold); + assert_eq!(button_settings.release_threshold, release_threshold); + }, + Err(_) => { assert!(false, "ButtonSettings::new({}, {}) should be valid ", press_threshold, release_threshold); }, + } + } + } + + + #[test] + fn test_new_button_settings_given_invalid_parameters() { + let cases = [ + (1.1, 0.0), + (1.1, 1.0), + (1.0, 1.1), + (-1.0, 0.9), + (-1.0, 0.0), + (-1.0, -0.4), + (0.9, 1.0), + (0.0, 0.1), + ]; + + for (press_threshoid, release_threshold) in cases { + let bs = ButtonSettings::new(press_threshoid, release_threshold); + match bs { + Ok(_) => { assert!(false, "ButtonSettings::new({}, {}) should be invalid ", press_threshoid, release_threshold); }, + Err(err_code) => { + match err_code { + GamepadSettingsError::InvalidButtonSetting(_) => {}, + _ => { assert!(false, "Expected GamepadSettingsError::InvalidButtonSetting for ButtonSettings::new({}, {})", press_threshoid, release_threshold); }, + } + } + } + } + } + } From e6f8a249fa49488655868a0ec5d381db0c8787a3 Mon Sep 17 00:00:00 2001 From: targrub Date: Tue, 4 Oct 2022 20:02:18 -0400 Subject: [PATCH 19/25] Rejiggered the possible GamepadSettingsErrors. Docs improved. An attempt to comply with Rust API Guidelines. --- crates/bevy_input/src/gamepad.rs | 181 +++++++++++++++++-------------- 1 file changed, 97 insertions(+), 84 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index c8d016a085841..710ffe53070ca 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -7,10 +7,19 @@ use thiserror::Error; /// Errors that occur when setting settings for gamepad input. #[derive(Error, Debug)] pub enum GamepadSettingsError { - #[error("{0}")] - InvalidAxisSetting(String), - #[error("{0}")] - InvalidButtonSetting(String), + #[error("the conditions -1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0 <= deadzone_upperbound <= livezone_upperbound <= 1.0 must hold true")] + InvalidAxisZoneSetting, + #[error("invalid threshold {0}, expected 0.0 <= threshold <= 2.0")] + InvalidAxisThresholdSetting(f32), + #[error("invalid release_threshold {0}, expected 0.0 <= release_threshold <= 1.0")] + InvalidButtonSettingReleaseThresholdOutOfRange(f32), + #[error("invalid press_threshold {0}, expected 0.0 <= press_threshold <= 1.0")] + InvalidButtonSettingPressThresholdOutOfRange(f32), + #[error("invalid parameter values release_threshold {} press_threshold {}, expected release_threshold <= press_threshold", .release_threshold, .press_threshold)] + InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { + press_threshold: f32, + release_threshold: f32, + }, } type Result = std::result::Result; @@ -166,7 +175,7 @@ impl GamepadEvent { /// button_inputs: ResMut>, /// ) { /// let gamepad = gamepads.iter().next().unwrap(); -/// let gamepad_button= GamepadButton::new(gamepad, GamepadButtonType::South); +/// let gamepad_button = GamepadButton::new(gamepad, GamepadButtonType::South); /// /// my_resource.0 = button_inputs.pressed(gamepad_button); /// } @@ -487,9 +496,6 @@ impl GamepadSettings { /// Manages settings for gamepad buttons. /// -/// + `press_threshold` is the button input value above which the button is considered pressed. -/// + `release_threshold` is the button input value below which the button is considered released. -/// /// It is used inside of [`GamepadSettings`] to define the threshold for a gamepad button /// to be considered pressed or released. A button is considered pressed if the `press_threshold` /// value is surpassed and released if the `release_threshold` value is undercut. @@ -513,31 +519,36 @@ impl Default for ButtonSettings { impl ButtonSettings { /// Creates a new [`ButtonSettings`] instance. /// - /// # Arguments + /// # Parameters /// - /// + `press_threshold` - the value above which the button is considered pressed. - /// + `release_threshold` - the value below which the button is considered released. + /// + `press_threshold` is the button input value above which the button is considered pressed. + /// + `release_threshold` is the button input value below which the button is considered released. /// /// Restrictions: /// + `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` /// /// # Errors /// - /// If the restrictions are not met, `InvalidButtonSetting` will be returned. - pub fn new(press_threshold: f32, release_threshold: f32) -> Result { - if 0.0 <= release_threshold - && release_threshold <= press_threshold - && press_threshold <= 1.0 - { + /// If the restrictions are not met, returns one of + /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange`, + /// `GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange`, or + /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold`. + pub fn new(press_threshold: f32, release_threshold: f32) -> Result { + if !(0.0..=1.0).contains(&release_threshold) { + Err( + GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange( + release_threshold, + ), + ) + } else if !(0.0..=1.0).contains(&press_threshold) { + Err(GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange(press_threshold)) + } else if release_threshold > press_threshold { + Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { press_threshold, release_threshold } ) + } else { Ok(ButtonSettings { press_threshold, release_threshold, }) - } else { - Err(GamepadSettingsError::InvalidButtonSetting( - "The condition 0.0 <= release_threshold <= press_threshold <= 1.0 must hold true" - .to_owned(), - )) } } @@ -564,16 +575,17 @@ impl ButtonSettings { /// /// # Errors /// - /// If the value passed is outside the range [`release_threshold`, 1.0], `InvalidButtonSetting` is - /// returned. + /// If the value passed is outside the range [`release_threshold`, 1.0], returns either + /// `GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange` or + /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold`. pub fn try_set_press_threshold(&mut self, value: f32) -> Result<()> { if (self.release_threshold..=1.0).contains(&value) { self.press_threshold = value; Ok(()) + } else if !(0.0..1.0).contains(&value) { + Err(GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange(value)) } else { - Err(GamepadSettingsError::InvalidButtonSetting( - "press_threshold must be in the range [release_threshold, 1.0]".to_owned(), - )) + Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { press_threshold: value, release_threshold: self.release_threshold }) } } @@ -595,16 +607,17 @@ impl ButtonSettings { /// /// # Errors /// - /// If the value passed is outside the range [0.0, `press_threshold`], `InvalidButtonSetting` is - /// returned. + /// If the value passed is outside the range [0.0, `press_threshold`], returns + /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange` or + /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold`. pub fn try_set_release_threshold(&mut self, value: f32) -> Result<()> { if (0.0..=self.press_threshold).contains(&value) { self.release_threshold = value; Ok(()) + } else if !(0.0..1.0).contains(&value) { + Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange(value)) } else { - Err(GamepadSettingsError::InvalidButtonSetting( - "release_threshold must be in the range [0.0, press_threshold]".to_owned(), - )) + Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { press_threshold: self.press_threshold, release_threshold: value }) } } @@ -673,36 +686,36 @@ impl AxisSettings { /// /// # Errors /// - /// If the restrictions are not met, `InvalidButtonSetting` will be returned. + /// Returns `GamepadSettingsError::InvalidAxisZoneSetting` if any restrictions on the zone values are not met. + /// If the zone restrictions are met, but the ``threshold`` value restrictions are not met, + /// returns `GamepadSettingsError::InvalidAxisThresholdSetting`. pub fn new( livezone_lowerbound: f32, deadzone_lowerbound: f32, deadzone_upperbound: f32, livezone_upperbound: f32, threshold: f32, - ) -> Result { + ) -> Result { if -1.0 <= livezone_lowerbound && livezone_lowerbound <= deadzone_lowerbound && deadzone_lowerbound <= 0.0 && 0.0 <= deadzone_upperbound && deadzone_upperbound <= livezone_upperbound && livezone_upperbound <= 1.0 - && (0.0..=2.0).contains(&threshold) { - Ok(Self { - livezone_lowerbound, - deadzone_lowerbound, - deadzone_upperbound, - livezone_upperbound, - threshold, - }) + if (0.0..=2.0).contains(&threshold) { + Ok(Self { + livezone_lowerbound, + deadzone_lowerbound, + deadzone_upperbound, + livezone_upperbound, + threshold, + }) + } else { + Err(GamepadSettingsError::InvalidAxisThresholdSetting(threshold)) + } } else { - Err(GamepadSettingsError::InvalidAxisSetting( - "The conditions -1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0 <=\ - deadzone_upperbound <= livezone_upperbound <= 1.0 and \ - 0.0 <= threshold <= 2.0 must hold true" - .to_owned(), - )) + Err(GamepadSettingsError::InvalidAxisZoneSetting) } } @@ -715,13 +728,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is less than `deadzone_upperbound` or greater than 1.0. + /// If the value passed is less than `deadzone_upperbound` or greater than 1.0, returns `GamepadSettingsError::InvalidAxisZoneSetting`. pub fn try_set_livezone_upperbound(&mut self, value: f32) -> Result<()> { if value < self.deadzone_upperbound || value > 1.0 { - Err(GamepadSettingsError::InvalidAxisSetting( - "livezone_upperbound must be greater than deadzone_upperbound and less than 1.0" - .to_owned(), - )) + Err(GamepadSettingsError::InvalidAxisZoneSetting) } else { self.livezone_upperbound = value; Ok(()) @@ -746,12 +756,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is negative or greater than `livezone_upperbound`. + /// If the value passed is negative or greater than `livezone_upperbound`, returns `GamepadSettingsError::InvalidAxisZoneSetting`. pub fn try_set_deadzone_upperbound(&mut self, value: f32) -> Result<()> { if value < 0.0 || value > self.livezone_upperbound { - Err(GamepadSettingsError::InvalidAxisSetting( - "deadzone_upperbound must be positive and less than livezone_upperbound".to_owned(), - )) + Err(GamepadSettingsError::InvalidAxisZoneSetting) } else { self.deadzone_upperbound = value; Ok(()) @@ -777,13 +785,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is positive or less than `deadzone_lowerbound`. + /// If the value passed is positive or less than `deadzone_lowerbound`, returns `GamepadSettingsError::InvalidAxisZoneSetting`. pub fn try_set_livezone_lowerbound(&mut self, value: f32) -> Result<()> { if value < self.deadzone_lowerbound || value > 0.0 { - Err(GamepadSettingsError::InvalidAxisSetting( - "livezone_lowerbound must be negative and greater than deadzone_lowerbound" - .to_owned(), - )) + Err(GamepadSettingsError::InvalidAxisZoneSetting) } else { self.livezone_lowerbound = value; Ok(()) @@ -805,17 +810,14 @@ impl AxisSettings { self.deadzone_lowerbound } - /// Try to get the value below which inputs will be rounded down to -1.0. + /// Try to set the value below which inputs will be rounded down to -1.0. /// /// # Errors /// - /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`. + /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`, returns `GamepadSettingsError::InvalidAxisZoneSetting`. pub fn try_set_deadzone_lowerbound(&mut self, value: f32) -> Result<()> { if value < -1.0 || value > self.livezone_lowerbound { - Err(GamepadSettingsError::InvalidAxisSetting( - "deadzone_lowerbound must be greater than -1.0 and less than livezone_lowerbound" - .to_owned(), - )) + Err(GamepadSettingsError::InvalidAxisZoneSetting) } else { self.deadzone_lowerbound = value; Ok(()) @@ -841,12 +843,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is not within [0.0, 2.0]. + /// If the value passed is not within [0.0..=2.0], returns `GamepadSettingsError::InvalidAxisThresholdSetting`. pub fn try_set_threshold(&mut self, value: f32) -> Result<()> { if !(0.0..=2.0).contains(&value) { - Err(GamepadSettingsError::InvalidAxisSetting( - "threshold must be between 0.0 and 2.0, inclusive".to_owned(), - )) + Err(GamepadSettingsError::InvalidAxisThresholdSetting(value)) } else { self.threshold = value; Ok(()) @@ -854,7 +854,7 @@ impl AxisSettings { } /// Try to set the minimum value by which input must change before the changes will be applied. - /// If the value passed is not within [0.0, 2.0], the value will not be changed. + /// If the value passed is not within [0.0..=2.0], the value will not be changed. /// /// Returns the new value of threshold. pub fn set_threshold(&mut self, value: f32) -> f32 { @@ -1243,7 +1243,11 @@ mod tests { let settings = ButtonSettings::default(); let actual = settings.is_pressed(value); - assert_eq!(expected, actual, "Testing ButtonSettings::is_pressed() for value: {}", value); + assert_eq!( + expected, actual, + "testing ButtonSettings::is_pressed() for value: {}", + value + ); } } @@ -1265,7 +1269,11 @@ mod tests { let settings = ButtonSettings::default(); let actual = settings.is_released(value); - assert_eq!(expected, actual, "Testing ButtonSettings::is_released() for value: {}", value); + assert_eq!( + expected, actual, + "testing ButtonSettings::is_released() for value: {}", + value + ); } } @@ -1286,13 +1294,17 @@ mod tests { Ok(button_settings) => { assert_eq!(button_settings.press_threshold, press_threshold); assert_eq!(button_settings.release_threshold, release_threshold); - }, - Err(_) => { assert!(false, "ButtonSettings::new({}, {}) should be valid ", press_threshold, release_threshold); }, + } + Err(_) => { + panic!( + "ButtonSettings::new({}, {}) should be valid ", + press_threshold, release_threshold + ); + } } } } - #[test] fn test_new_button_settings_given_invalid_parameters() { let cases = [ @@ -1306,18 +1318,19 @@ mod tests { (0.0, 0.1), ]; - for (press_threshoid, release_threshold) in cases { - let bs = ButtonSettings::new(press_threshoid, release_threshold); + for (press_threshold, release_threshold) in cases { + let bs = ButtonSettings::new(press_threshold, release_threshold); match bs { - Ok(_) => { assert!(false, "ButtonSettings::new({}, {}) should be invalid ", press_threshoid, release_threshold); }, + Ok(_) => { panic!("ButtonSettings::new({}, {}) should be invalid ", press_threshold, release_threshold); }, Err(err_code) => { match err_code { - GamepadSettingsError::InvalidButtonSetting(_) => {}, - _ => { assert!(false, "Expected GamepadSettingsError::InvalidButtonSetting for ButtonSettings::new({}, {})", press_threshoid, release_threshold); }, + GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange(_press_threshold) => {}, + GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold {press_threshold: _press_threshold, release_threshold: _release_threshold} => {}, + GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange(_release_threshold) => {}, + _ => { panic!("expected GamepadSettingsError::InvalidButtonSetting... for ButtonSettings::new({}, {})", press_threshold, release_threshold); }, } } } } } - } From 7effc594dc3e399f44523907a5cbba5e2cd9c504 Mon Sep 17 00:00:00 2001 From: targrub <62773321+targrub@users.noreply.github.com> Date: Wed, 5 Oct 2022 10:10:32 -0400 Subject: [PATCH 20/25] Minor text change: One space after a period. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François --- crates/bevy_input/src/gamepad.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 710ffe53070ca..d7b81ee6be389 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -497,7 +497,7 @@ impl GamepadSettings { /// Manages settings for gamepad buttons. /// /// It is used inside of [`GamepadSettings`] to define the threshold for a gamepad button -/// to be considered pressed or released. A button is considered pressed if the `press_threshold` +/// to be considered pressed or released. A button is considered pressed if the `press_threshold` /// value is surpassed and released if the `release_threshold` value is undercut. /// /// Allowed values: `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` From 547866a036ce5c894fe1b9197cc71cdd6907f15f Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 5 Oct 2022 13:11:48 -0400 Subject: [PATCH 21/25] Better naming of GamepadSettingError enum variants. --- crates/bevy_input/src/gamepad.rs | 113 +++++++++++++++++++------------ 1 file changed, 69 insertions(+), 44 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index d7b81ee6be389..2bf6e947ff45c 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -8,15 +8,15 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum GamepadSettingsError { #[error("the conditions -1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0 <= deadzone_upperbound <= livezone_upperbound <= 1.0 must hold true")] - InvalidAxisZoneSetting, + AxisZoneBounds, #[error("invalid threshold {0}, expected 0.0 <= threshold <= 2.0")] - InvalidAxisThresholdSetting(f32), + AxisThreshold(f32), #[error("invalid release_threshold {0}, expected 0.0 <= release_threshold <= 1.0")] - InvalidButtonSettingReleaseThresholdOutOfRange(f32), + ButtonReleaseThresholdOutOfRange(f32), #[error("invalid press_threshold {0}, expected 0.0 <= press_threshold <= 1.0")] - InvalidButtonSettingPressThresholdOutOfRange(f32), + ButtonPressThresholdOutOfRange(f32), #[error("invalid parameter values release_threshold {} press_threshold {}, expected release_threshold <= press_threshold", .release_threshold, .press_threshold)] - InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { + ButtonReleaseThresholdGreaterThanPressThreshold { press_threshold: f32, release_threshold: f32, }, @@ -530,20 +530,25 @@ impl ButtonSettings { /// # Errors /// /// If the restrictions are not met, returns one of - /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange`, - /// `GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange`, or - /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold`. + /// `GamepadSettingsError::ButtonReleaseThresholdOutOfRange`, + /// `GamepadSettingsError::ButtonPressThresholdOutOfRange`, or + /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. pub fn new(press_threshold: f32, release_threshold: f32) -> Result { if !(0.0..=1.0).contains(&release_threshold) { + Err(GamepadSettingsError::ButtonReleaseThresholdOutOfRange( + release_threshold, + )) + } else if !(0.0..=1.0).contains(&press_threshold) { + Err(GamepadSettingsError::ButtonPressThresholdOutOfRange( + press_threshold, + )) + } else if release_threshold > press_threshold { Err( - GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange( + GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + press_threshold, release_threshold, - ), + }, ) - } else if !(0.0..=1.0).contains(&press_threshold) { - Err(GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange(press_threshold)) - } else if release_threshold > press_threshold { - Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { press_threshold, release_threshold } ) } else { Ok(ButtonSettings { press_threshold, @@ -576,16 +581,21 @@ impl ButtonSettings { /// # Errors /// /// If the value passed is outside the range [`release_threshold`, 1.0], returns either - /// `GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange` or - /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold`. + /// `GamepadSettingsError::ButtonPressThresholdOutOfRange` or + /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. pub fn try_set_press_threshold(&mut self, value: f32) -> Result<()> { if (self.release_threshold..=1.0).contains(&value) { self.press_threshold = value; Ok(()) } else if !(0.0..1.0).contains(&value) { - Err(GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange(value)) + Err(GamepadSettingsError::ButtonPressThresholdOutOfRange(value)) } else { - Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { press_threshold: value, release_threshold: self.release_threshold }) + Err( + GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + press_threshold: value, + release_threshold: self.release_threshold, + }, + ) } } @@ -608,16 +618,23 @@ impl ButtonSettings { /// # Errors /// /// If the value passed is outside the range [0.0, `press_threshold`], returns - /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange` or - /// `GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold`. + /// `GamepadSettingsError::ButtonReleaseThresholdOutOfRange` or + /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. pub fn try_set_release_threshold(&mut self, value: f32) -> Result<()> { if (0.0..=self.press_threshold).contains(&value) { self.release_threshold = value; Ok(()) } else if !(0.0..1.0).contains(&value) { - Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange(value)) + Err(GamepadSettingsError::ButtonReleaseThresholdOutOfRange( + value, + )) } else { - Err(GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold { press_threshold: self.press_threshold, release_threshold: value }) + Err( + GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + press_threshold: self.press_threshold, + release_threshold: value, + }, + ) } } @@ -686,9 +703,9 @@ impl AxisSettings { /// /// # Errors /// - /// Returns `GamepadSettingsError::InvalidAxisZoneSetting` if any restrictions on the zone values are not met. + /// Returns `GamepadSettingsError::AxisZoneBounds` if any restrictions on the zone values are not met. /// If the zone restrictions are met, but the ``threshold`` value restrictions are not met, - /// returns `GamepadSettingsError::InvalidAxisThresholdSetting`. + /// returns `GamepadSettingsError::AxisThreshold`. pub fn new( livezone_lowerbound: f32, deadzone_lowerbound: f32, @@ -712,10 +729,10 @@ impl AxisSettings { threshold, }) } else { - Err(GamepadSettingsError::InvalidAxisThresholdSetting(threshold)) + Err(GamepadSettingsError::AxisThreshold(threshold)) } } else { - Err(GamepadSettingsError::InvalidAxisZoneSetting) + Err(GamepadSettingsError::AxisZoneBounds) } } @@ -728,10 +745,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is less than `deadzone_upperbound` or greater than 1.0, returns `GamepadSettingsError::InvalidAxisZoneSetting`. + /// If the value passed is less than `deadzone_upperbound` or greater than 1.0, returns `GamepadSettingsError::AxisZoneBounds`. pub fn try_set_livezone_upperbound(&mut self, value: f32) -> Result<()> { if value < self.deadzone_upperbound || value > 1.0 { - Err(GamepadSettingsError::InvalidAxisZoneSetting) + Err(GamepadSettingsError::AxisZoneBounds) } else { self.livezone_upperbound = value; Ok(()) @@ -756,10 +773,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is negative or greater than `livezone_upperbound`, returns `GamepadSettingsError::InvalidAxisZoneSetting`. + /// If the value passed is negative or greater than `livezone_upperbound`, returns `GamepadSettingsError::AxisZoneBounds`. pub fn try_set_deadzone_upperbound(&mut self, value: f32) -> Result<()> { if value < 0.0 || value > self.livezone_upperbound { - Err(GamepadSettingsError::InvalidAxisZoneSetting) + Err(GamepadSettingsError::AxisZoneBounds) } else { self.deadzone_upperbound = value; Ok(()) @@ -785,10 +802,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is positive or less than `deadzone_lowerbound`, returns `GamepadSettingsError::InvalidAxisZoneSetting`. + /// If the value passed is positive or less than `deadzone_lowerbound`, returns `GamepadSettingsError::AxisZoneBounds`. pub fn try_set_livezone_lowerbound(&mut self, value: f32) -> Result<()> { if value < self.deadzone_lowerbound || value > 0.0 { - Err(GamepadSettingsError::InvalidAxisZoneSetting) + Err(GamepadSettingsError::AxisZoneBounds) } else { self.livezone_lowerbound = value; Ok(()) @@ -814,10 +831,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`, returns `GamepadSettingsError::InvalidAxisZoneSetting`. + /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`, returns `GamepadSettingsError::AxisZoneBounds`. pub fn try_set_deadzone_lowerbound(&mut self, value: f32) -> Result<()> { if value < -1.0 || value > self.livezone_lowerbound { - Err(GamepadSettingsError::InvalidAxisZoneSetting) + Err(GamepadSettingsError::AxisZoneBounds) } else { self.deadzone_lowerbound = value; Ok(()) @@ -843,10 +860,10 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is not within [0.0..=2.0], returns `GamepadSettingsError::InvalidAxisThresholdSetting`. + /// If the value passed is not within [0.0..=2.0], returns `GamepadSettingsError::AxisThreshold`. pub fn try_set_threshold(&mut self, value: f32) -> Result<()> { if !(0.0..=2.0).contains(&value) { - Err(GamepadSettingsError::InvalidAxisThresholdSetting(value)) + Err(GamepadSettingsError::AxisThreshold(value)) } else { self.threshold = value; Ok(()) @@ -1321,15 +1338,23 @@ mod tests { for (press_threshold, release_threshold) in cases { let bs = ButtonSettings::new(press_threshold, release_threshold); match bs { - Ok(_) => { panic!("ButtonSettings::new({}, {}) should be invalid ", press_threshold, release_threshold); }, - Err(err_code) => { - match err_code { - GamepadSettingsError::InvalidButtonSettingPressThresholdOutOfRange(_press_threshold) => {}, - GamepadSettingsError::InvalidButtonSettingReleaseThresholdGreaterThanPressThreshold {press_threshold: _press_threshold, release_threshold: _release_threshold} => {}, - GamepadSettingsError::InvalidButtonSettingReleaseThresholdOutOfRange(_release_threshold) => {}, - _ => { panic!("expected GamepadSettingsError::InvalidButtonSetting... for ButtonSettings::new({}, {})", press_threshold, release_threshold); }, - } + Ok(_) => { + panic!( + "ButtonSettings::new({}, {}) should be invalid", + press_threshold, release_threshold + ); } + Err(err_code) => match err_code { + GamepadSettingsError::ButtonPressThresholdOutOfRange(_press_threshold) => {} + GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + press_threshold: _press_threshold, + release_threshold: _release_threshold, + } => {} + GamepadSettingsError::ButtonReleaseThresholdOutOfRange(_release_threshold) => {} + _ => { + panic!("expected GamepadSettingsError::Button... for ButtonSettings::new({}, {})", press_threshold, release_threshold); + } + }, } } } From 4e3e7deee2aab059ebe7e824f6ec3e2b57d65c82 Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 5 Oct 2022 13:20:45 -0400 Subject: [PATCH 22/25] Don't quote press and release thresholds. Other minor fixes to threshold docs. --- crates/bevy_input/src/gamepad.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 2bf6e947ff45c..5e9104866408b 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -559,14 +559,14 @@ impl ButtonSettings { /// Returns `true` if the button is pressed. /// - /// A button is considered pressed if the `value` passed is greater than or equal to the `press` threshold. + /// A button is considered pressed if the `value` passed is greater than or equal to the press threshold. fn is_pressed(&self, value: f32) -> bool { value >= self.press_threshold } /// Returns `true` if the button is released. /// - /// A button is considered released if the `value` passed is lower than or equal to the `release` threshold. + /// A button is considered released if the `value` passed is lower than or equal to the release threshold. fn is_released(&self, value: f32) -> bool { value <= self.release_threshold } @@ -580,7 +580,7 @@ impl ButtonSettings { /// /// # Errors /// - /// If the value passed is outside the range [`release_threshold`, 1.0], returns either + /// If the value passed is outside the range [release threshold..=1.0], returns either /// `GamepadSettingsError::ButtonPressThresholdOutOfRange` or /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. pub fn try_set_press_threshold(&mut self, value: f32) -> Result<()> { @@ -600,9 +600,9 @@ impl ButtonSettings { } /// Try to set the button input threshold above which the button is considered pressed. - /// If the value passed is outside the range [`release_threshold`, 1.0], the value will not be changed. + /// If the value passed is outside the range [release threshold..=1.0], the value will not be changed. /// - /// Returns the new value of `press_threshold`. + /// Returns the new value of the press threshold. pub fn set_press_threshold(&mut self, value: f32) -> f32 { self.try_set_press_threshold(value).ok(); self.press_threshold @@ -617,7 +617,7 @@ impl ButtonSettings { /// /// # Errors /// - /// If the value passed is outside the range [0.0, `press_threshold`], returns + /// If the value passed is outside the range [0.0..=press threshold], returns /// `GamepadSettingsError::ButtonReleaseThresholdOutOfRange` or /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. pub fn try_set_release_threshold(&mut self, value: f32) -> Result<()> { @@ -639,9 +639,9 @@ impl ButtonSettings { } /// Try to set the button input threshold below which the button is considered released. If the - /// value passed is outside the range [0.0, `press_threshold`], the value will not be changed. + /// value passed is outside the range [0.0..=press threshold], the value will not be changed. /// - /// Returns the new value of `release_threshold`. + /// Returns the new value of the release threshold. pub fn set_release_threshold(&mut self, value: f32) -> f32 { self.try_set_release_threshold(value).ok(); self.release_threshold From e93de7860c2bc2fce8dc7486137ae725278e471a Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 5 Oct 2022 14:00:11 -0400 Subject: [PATCH 23/25] Added docstrings for enum variants. Split `GamepadSettingsError` error into two enums, `ButtonSettingsError` and `AxisSettingsError`. --- crates/bevy_input/src/gamepad.rs | 114 +++++++++++++++++++------------ 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 5e9104866408b..3db629a1f0cfc 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -4,26 +4,52 @@ use bevy_ecs::system::{Res, ResMut, Resource}; use bevy_utils::{tracing::info, HashMap, HashSet}; use thiserror::Error; -/// Errors that occur when setting settings for gamepad input. +/// Errors that occur when setting axis settings for gamepad input. +/// +/// + `ZoneBounds` +/// +/// The given parameters must fit the bounds described by the expression +/// -1.0 <= `livezone_lowerbound` <= `deadzone_lowerbound` <= 0.0 <= `deadzone_upperbound` <= `livezone_upperbound` <= 1.0. +/// These are named parameters to `AxisSettings::new()`. +/// +/// + `Threshold` +/// +/// The given parameter was not in range 0.0..=2.0. #[derive(Error, Debug)] -pub enum GamepadSettingsError { +pub enum AxisSettingsError { #[error("the conditions -1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0 <= deadzone_upperbound <= livezone_upperbound <= 1.0 must hold true")] - AxisZoneBounds, + ZoneBounds, #[error("invalid threshold {0}, expected 0.0 <= threshold <= 2.0")] - AxisThreshold(f32), + Threshold(f32), +} + +/// Errors that occur when setting button settings for gamepad input. +/// +/// + `ReleaseThresholdOutOfRange(f32)` +/// +/// The given parameter was not in range 0.0..=1.0. +/// +/// + `PressThresholdOutOfRange(f32)` +/// +/// The given parameter was not in range 0.0..=1.0. +/// +/// + `ReleaseThresholdGreaterThanPressThreshold {press_threshold: f32, release_threshold: f32,}` +/// +/// Parameter `release_threshold` was not less than `press_threshold`. +/// +#[derive(Error, Debug)] +pub enum ButtonSettingsError { #[error("invalid release_threshold {0}, expected 0.0 <= release_threshold <= 1.0")] - ButtonReleaseThresholdOutOfRange(f32), + ReleaseThresholdOutOfRange(f32), #[error("invalid press_threshold {0}, expected 0.0 <= press_threshold <= 1.0")] - ButtonPressThresholdOutOfRange(f32), + PressThresholdOutOfRange(f32), #[error("invalid parameter values release_threshold {} press_threshold {}, expected release_threshold <= press_threshold", .release_threshold, .press_threshold)] - ButtonReleaseThresholdGreaterThanPressThreshold { + ReleaseThresholdGreaterThanPressThreshold { press_threshold: f32, release_threshold: f32, }, } -type Result = std::result::Result; - /// A gamepad with an associated `ID`. /// /// ## Usage @@ -533,18 +559,21 @@ impl ButtonSettings { /// `GamepadSettingsError::ButtonReleaseThresholdOutOfRange`, /// `GamepadSettingsError::ButtonPressThresholdOutOfRange`, or /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. - pub fn new(press_threshold: f32, release_threshold: f32) -> Result { + pub fn new( + press_threshold: f32, + release_threshold: f32, + ) -> Result { if !(0.0..=1.0).contains(&release_threshold) { - Err(GamepadSettingsError::ButtonReleaseThresholdOutOfRange( + Err(ButtonSettingsError::ReleaseThresholdOutOfRange( release_threshold, )) } else if !(0.0..=1.0).contains(&press_threshold) { - Err(GamepadSettingsError::ButtonPressThresholdOutOfRange( + Err(ButtonSettingsError::PressThresholdOutOfRange( press_threshold, )) } else if release_threshold > press_threshold { Err( - GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + ButtonSettingsError::ReleaseThresholdGreaterThanPressThreshold { press_threshold, release_threshold, }, @@ -583,15 +612,15 @@ impl ButtonSettings { /// If the value passed is outside the range [release threshold..=1.0], returns either /// `GamepadSettingsError::ButtonPressThresholdOutOfRange` or /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. - pub fn try_set_press_threshold(&mut self, value: f32) -> Result<()> { + pub fn try_set_press_threshold(&mut self, value: f32) -> Result<(), ButtonSettingsError> { if (self.release_threshold..=1.0).contains(&value) { self.press_threshold = value; Ok(()) } else if !(0.0..1.0).contains(&value) { - Err(GamepadSettingsError::ButtonPressThresholdOutOfRange(value)) + Err(ButtonSettingsError::PressThresholdOutOfRange(value)) } else { Err( - GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + ButtonSettingsError::ReleaseThresholdGreaterThanPressThreshold { press_threshold: value, release_threshold: self.release_threshold, }, @@ -618,19 +647,17 @@ impl ButtonSettings { /// # Errors /// /// If the value passed is outside the range [0.0..=press threshold], returns - /// `GamepadSettingsError::ButtonReleaseThresholdOutOfRange` or - /// `GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold`. - pub fn try_set_release_threshold(&mut self, value: f32) -> Result<()> { + /// `ButtonSettingsError::ReleaseThresholdOutOfRange` or + /// `ButtonSettingsError::ReleaseThresholdGreaterThanPressThreshold`. + pub fn try_set_release_threshold(&mut self, value: f32) -> Result<(), ButtonSettingsError> { if (0.0..=self.press_threshold).contains(&value) { self.release_threshold = value; Ok(()) } else if !(0.0..1.0).contains(&value) { - Err(GamepadSettingsError::ButtonReleaseThresholdOutOfRange( - value, - )) + Err(ButtonSettingsError::ReleaseThresholdOutOfRange(value)) } else { Err( - GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + ButtonSettingsError::ReleaseThresholdGreaterThanPressThreshold { press_threshold: self.press_threshold, release_threshold: value, }, @@ -703,16 +730,16 @@ impl AxisSettings { /// /// # Errors /// - /// Returns `GamepadSettingsError::AxisZoneBounds` if any restrictions on the zone values are not met. + /// Returns `AxisSettingsError::ZoneBounds` if any restrictions on the zone values are not met. /// If the zone restrictions are met, but the ``threshold`` value restrictions are not met, - /// returns `GamepadSettingsError::AxisThreshold`. + /// returns `AxisSettingsError::Threshold`. pub fn new( livezone_lowerbound: f32, deadzone_lowerbound: f32, deadzone_upperbound: f32, livezone_upperbound: f32, threshold: f32, - ) -> Result { + ) -> Result { if -1.0 <= livezone_lowerbound && livezone_lowerbound <= deadzone_lowerbound && deadzone_lowerbound <= 0.0 @@ -729,10 +756,10 @@ impl AxisSettings { threshold, }) } else { - Err(GamepadSettingsError::AxisThreshold(threshold)) + Err(AxisSettingsError::Threshold(threshold)) } } else { - Err(GamepadSettingsError::AxisZoneBounds) + Err(AxisSettingsError::ZoneBounds) } } @@ -746,9 +773,9 @@ impl AxisSettings { /// # Errors /// /// If the value passed is less than `deadzone_upperbound` or greater than 1.0, returns `GamepadSettingsError::AxisZoneBounds`. - pub fn try_set_livezone_upperbound(&mut self, value: f32) -> Result<()> { + pub fn try_set_livezone_upperbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { if value < self.deadzone_upperbound || value > 1.0 { - Err(GamepadSettingsError::AxisZoneBounds) + Err(AxisSettingsError::ZoneBounds) } else { self.livezone_upperbound = value; Ok(()) @@ -774,9 +801,9 @@ impl AxisSettings { /// # Errors /// /// If the value passed is negative or greater than `livezone_upperbound`, returns `GamepadSettingsError::AxisZoneBounds`. - pub fn try_set_deadzone_upperbound(&mut self, value: f32) -> Result<()> { + pub fn try_set_deadzone_upperbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { if value < 0.0 || value > self.livezone_upperbound { - Err(GamepadSettingsError::AxisZoneBounds) + Err(AxisSettingsError::ZoneBounds) } else { self.deadzone_upperbound = value; Ok(()) @@ -803,9 +830,9 @@ impl AxisSettings { /// # Errors /// /// If the value passed is positive or less than `deadzone_lowerbound`, returns `GamepadSettingsError::AxisZoneBounds`. - pub fn try_set_livezone_lowerbound(&mut self, value: f32) -> Result<()> { + pub fn try_set_livezone_lowerbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { if value < self.deadzone_lowerbound || value > 0.0 { - Err(GamepadSettingsError::AxisZoneBounds) + Err(AxisSettingsError::ZoneBounds) } else { self.livezone_lowerbound = value; Ok(()) @@ -832,9 +859,9 @@ impl AxisSettings { /// # Errors /// /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`, returns `GamepadSettingsError::AxisZoneBounds`. - pub fn try_set_deadzone_lowerbound(&mut self, value: f32) -> Result<()> { + pub fn try_set_deadzone_lowerbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { if value < -1.0 || value > self.livezone_lowerbound { - Err(GamepadSettingsError::AxisZoneBounds) + Err(AxisSettingsError::ZoneBounds) } else { self.deadzone_lowerbound = value; Ok(()) @@ -861,9 +888,9 @@ impl AxisSettings { /// # Errors /// /// If the value passed is not within [0.0..=2.0], returns `GamepadSettingsError::AxisThreshold`. - pub fn try_set_threshold(&mut self, value: f32) -> Result<()> { + pub fn try_set_threshold(&mut self, value: f32) -> Result<(), AxisSettingsError> { if !(0.0..=2.0).contains(&value) { - Err(GamepadSettingsError::AxisThreshold(value)) + Err(AxisSettingsError::Threshold(value)) } else { self.threshold = value; Ok(()) @@ -1107,7 +1134,7 @@ const ALL_AXIS_TYPES: [GamepadAxisType; 6] = [ #[cfg(test)] mod tests { - use crate::gamepad::GamepadSettingsError; + use crate::gamepad::ButtonSettingsError; use super::{AxisSettings, ButtonAxisSettings, ButtonSettings}; @@ -1345,15 +1372,12 @@ mod tests { ); } Err(err_code) => match err_code { - GamepadSettingsError::ButtonPressThresholdOutOfRange(_press_threshold) => {} - GamepadSettingsError::ButtonReleaseThresholdGreaterThanPressThreshold { + ButtonSettingsError::PressThresholdOutOfRange(_press_threshold) => {} + ButtonSettingsError::ReleaseThresholdGreaterThanPressThreshold { press_threshold: _press_threshold, release_threshold: _release_threshold, } => {} - GamepadSettingsError::ButtonReleaseThresholdOutOfRange(_release_threshold) => {} - _ => { - panic!("expected GamepadSettingsError::Button... for ButtonSettings::new({}, {})", press_threshold, release_threshold); - } + ButtonSettingsError::ReleaseThresholdOutOfRange(_release_threshold) => {} }, } } From 213a79bc602f46543387457940f4fea6a573a2a6 Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 5 Oct 2022 16:00:37 -0400 Subject: [PATCH 24/25] Separated out all AxisSettingError zone range violations Added more tests. Fixed docstrings. --- crates/bevy_input/src/gamepad.rs | 254 ++++++++++++++++++++++++++----- 1 file changed, 212 insertions(+), 42 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 3db629a1f0cfc..5623e6888dc82 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -6,19 +6,53 @@ use thiserror::Error; /// Errors that occur when setting axis settings for gamepad input. /// -/// + `ZoneBounds` +/// + `LiveZoneLowerBoundOutOfRange(f32)` /// -/// The given parameters must fit the bounds described by the expression -/// -1.0 <= `livezone_lowerbound` <= `deadzone_lowerbound` <= 0.0 <= `deadzone_upperbound` <= `livezone_upperbound` <= 1.0. -/// These are named parameters to `AxisSettings::new()`. +/// The given parameter `livezone_lowerbound` was not in range -1.0..=0.0. /// -/// + `Threshold` +/// + `DeadZoneLowerBoundOutOfRange(f32)` +/// +/// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. +/// +/// + `DeadZoneUpperBoundOutOfRange(f32)` +/// +/// The given parameter `deadzone_upperbound` was not in range 0.0..=1.0. +/// +/// + `LiveZoneUpperBoundOutOfRange(f32)` +/// +/// The given parameter `livezone_upperbound` was not in range 0.0..=1.0. +/// +/// + `LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { livezone_lowerbound: f32, deadzone_lowerbound: f32, }` +/// +/// Parameter `livezone_lowerbound` was not less than or equal to parameter `deadzone_lowerbound`. +/// +/// + `LiveZoneUpperBoundGreaterThanLiveZoneUpperBound { livezone_upperbound: f32, deadzone_upperbound: f32, }` +/// +/// Parameter `deadzone_upperbound` was not less than or equal to parameter `livezone_upperbound`. +/// +/// + `Threshold(f32)` /// /// The given parameter was not in range 0.0..=2.0. -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum AxisSettingsError { - #[error("the conditions -1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0 <= deadzone_upperbound <= livezone_upperbound <= 1.0 must hold true")] - ZoneBounds, + #[error("invalid livezone_lowerbound {0}, expected value [-1.0..=0.0]")] + LiveZoneLowerBoundOutOfRange(f32), + #[error("invalid deadzone_lowerbound {0}, expected value [-1.0..=0.0]")] + DeadZoneLowerBoundOutOfRange(f32), + #[error("invalid deadzone_upperbound {0}, expected value [0.0..=1.0]")] + DeadZoneUpperBoundOutOfRange(f32), + #[error("invalid livezone_upperbound {0}, expected value [0.0..=1.0]")] + LiveZoneUpperBoundOutOfRange(f32), + #[error("invalid parameter values livezone_lowerbound {} deadzone_lowerbound {}, expected livezone_lowerbound <= deadzone_lowerbound", .livezone_lowerbound, .deadzone_lowerbound)] + LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { + livezone_lowerbound: f32, + deadzone_lowerbound: f32, + }, + #[error("invalid parameter values livezone_upperbound {} deadzone_upperbound {}, expected deadzone_upperbound <= livezone_upperbound", .livezone_upperbound, .deadzone_upperbound)] + DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { + livezone_upperbound: f32, + deadzone_upperbound: f32, + }, #[error("invalid threshold {0}, expected 0.0 <= threshold <= 2.0")] Threshold(f32), } @@ -35,13 +69,13 @@ pub enum AxisSettingsError { /// /// + `ReleaseThresholdGreaterThanPressThreshold {press_threshold: f32, release_threshold: f32,}` /// -/// Parameter `release_threshold` was not less than `press_threshold`. +/// Parameter `release_threshold` was not less than or equal to `press_threshold`. /// -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ButtonSettingsError { - #[error("invalid release_threshold {0}, expected 0.0 <= release_threshold <= 1.0")] + #[error("invalid release_threshold {0}, expected value [0.0..=1.0]")] ReleaseThresholdOutOfRange(f32), - #[error("invalid press_threshold {0}, expected 0.0 <= press_threshold <= 1.0")] + #[error("invalid press_threshold {0}, expected [0.0..=1.0]")] PressThresholdOutOfRange(f32), #[error("invalid parameter values release_threshold {} press_threshold {}, expected release_threshold <= press_threshold", .release_threshold, .press_threshold)] ReleaseThresholdGreaterThanPressThreshold { @@ -730,7 +764,7 @@ impl AxisSettings { /// /// # Errors /// - /// Returns `AxisSettingsError::ZoneBounds` if any restrictions on the zone values are not met. + /// Returns an `AxisSettingsError` if any restrictions on the zone values are not met. /// If the zone restrictions are met, but the ``threshold`` value restrictions are not met, /// returns `AxisSettingsError::Threshold`. pub fn new( @@ -740,26 +774,46 @@ impl AxisSettings { livezone_upperbound: f32, threshold: f32, ) -> Result { - if -1.0 <= livezone_lowerbound - && livezone_lowerbound <= deadzone_lowerbound - && deadzone_lowerbound <= 0.0 - && 0.0 <= deadzone_upperbound - && deadzone_upperbound <= livezone_upperbound - && livezone_upperbound <= 1.0 - { - if (0.0..=2.0).contains(&threshold) { - Ok(Self { + if !(-1.0..=0.0).contains(&livezone_lowerbound) { + Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange( + livezone_lowerbound, + )) + } else if !(-1.0..=0.0).contains(&deadzone_lowerbound) { + Err(AxisSettingsError::DeadZoneLowerBoundOutOfRange( + deadzone_lowerbound, + )) + } else if !(-1.0..=0.0).contains(&deadzone_upperbound) { + Err(AxisSettingsError::DeadZoneUpperBoundOutOfRange( + deadzone_upperbound, + )) + } else if !(-1.0..=0.0).contains(&livezone_upperbound) { + Err(AxisSettingsError::LiveZoneUpperBoundOutOfRange( + livezone_upperbound, + )) + } else if livezone_lowerbound > deadzone_lowerbound { + Err( + AxisSettingsError::LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { livezone_lowerbound, deadzone_lowerbound, - deadzone_upperbound, + }, + ) + } else if deadzone_upperbound > livezone_upperbound { + Err( + AxisSettingsError::DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { livezone_upperbound, - threshold, - }) - } else { - Err(AxisSettingsError::Threshold(threshold)) - } + deadzone_upperbound, + }, + ) + } else if !(0.0..=2.0).contains(&threshold) { + Err(AxisSettingsError::Threshold(threshold)) } else { - Err(AxisSettingsError::ZoneBounds) + Ok(Self { + livezone_lowerbound, + deadzone_lowerbound, + deadzone_upperbound, + livezone_upperbound, + threshold, + }) } } @@ -772,10 +826,19 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is less than `deadzone_upperbound` or greater than 1.0, returns `GamepadSettingsError::AxisZoneBounds`. + /// If the value passed is less than the dead zone upper bound, + /// returns `AxisSettingsError::DeadZoneUpperBoundGreaterThanLiveZoneUpperBound`. + /// If the value passsed is not in range [0.0..=1.0], returns `AxisSettingsError::LiveZoneUpperBoundOutOfRange`. pub fn try_set_livezone_upperbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { - if value < self.deadzone_upperbound || value > 1.0 { - Err(AxisSettingsError::ZoneBounds) + if !(0.0..=1.0).contains(&value) { + Err(AxisSettingsError::LiveZoneUpperBoundOutOfRange(value)) + } else if value < self.deadzone_upperbound { + Err( + AxisSettingsError::DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { + livezone_upperbound: value, + deadzone_upperbound: self.deadzone_upperbound, + }, + ) } else { self.livezone_upperbound = value; Ok(()) @@ -800,10 +863,19 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is negative or greater than `livezone_upperbound`, returns `GamepadSettingsError::AxisZoneBounds`. + /// If the value passed is greater than the live zone upper bound, + /// returns `AxisSettingsError::DeadZoneUpperBoundGreaterThanLiveZoneUpperBound`. + /// If the value passsed is not in range [0.0..=1.0], returns `AxisSettingsError::DeadZoneUpperBoundOutOfRange`. pub fn try_set_deadzone_upperbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { - if value < 0.0 || value > self.livezone_upperbound { - Err(AxisSettingsError::ZoneBounds) + if !(0.0..=1.0).contains(&value) { + Err(AxisSettingsError::DeadZoneUpperBoundOutOfRange(value)) + } else if self.livezone_upperbound < value { + Err( + AxisSettingsError::DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { + livezone_upperbound: self.livezone_upperbound, + deadzone_upperbound: value, + }, + ) } else { self.deadzone_upperbound = value; Ok(()) @@ -829,10 +901,19 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is positive or less than `deadzone_lowerbound`, returns `GamepadSettingsError::AxisZoneBounds`. + /// If the value passed is less than the dead zone lower bound, + /// returns `AxisSettingsError::LiveZoneLowerBoundGreaterThanDeadZoneLowerBound`. + /// If the value passsed is not in range [-1.0..=0.0], returns `AxisSettingsError::LiveZoneLowerBoundOutOfRange`. pub fn try_set_livezone_lowerbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { - if value < self.deadzone_lowerbound || value > 0.0 { - Err(AxisSettingsError::ZoneBounds) + if !(-1.0..=0.0).contains(&value) { + Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange(value)) + } else if value > self.deadzone_lowerbound { + Err( + AxisSettingsError::LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { + livezone_lowerbound: value, + deadzone_lowerbound: self.deadzone_lowerbound, + }, + ) } else { self.livezone_lowerbound = value; Ok(()) @@ -858,10 +939,19 @@ impl AxisSettings { /// /// # Errors /// - /// If the value passed is less than -1.0 or greater than `livezone_lowerbound`, returns `GamepadSettingsError::AxisZoneBounds`. + /// If the value passed is less than the live zone lower bound, + /// returns `AxisSettingsError::LiveZoneLowerBoundGreaterThanDeadZoneLowerBound`. + /// If the value passsed is not in range [-1.0..=0.0], returns `AxisSettingsError::DeadZoneLowerBoundOutOfRange`. pub fn try_set_deadzone_lowerbound(&mut self, value: f32) -> Result<(), AxisSettingsError> { - if value < -1.0 || value > self.livezone_lowerbound { - Err(AxisSettingsError::ZoneBounds) + if !(-1.0..=0.0).contains(&value) { + Err(AxisSettingsError::DeadZoneLowerBoundOutOfRange(value)) + } else if self.livezone_lowerbound > value { + Err( + AxisSettingsError::LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { + livezone_lowerbound: self.livezone_lowerbound, + deadzone_lowerbound: value, + }, + ) } else { self.deadzone_lowerbound = value; Ok(()) @@ -1134,7 +1224,7 @@ const ALL_AXIS_TYPES: [GamepadAxisType; 6] = [ #[cfg(test)] mod tests { - use crate::gamepad::ButtonSettingsError; + use crate::gamepad::{AxisSettingsError, ButtonSettingsError}; use super::{AxisSettings, ButtonAxisSettings, ButtonSettings}; @@ -1382,4 +1472,84 @@ mod tests { } } } + + #[test] + fn test_try_out_of_range_axis_settings() { + let mut axis_settings = AxisSettings::default(); + assert_eq!( + Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange(-2.0)), + axis_settings.try_set_livezone_lowerbound(-2.0) + ); + assert_eq!( + Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange(0.1)), + axis_settings.try_set_livezone_lowerbound(0.1) + ); + assert_eq!( + Err(AxisSettingsError::DeadZoneLowerBoundOutOfRange(-2.0)), + axis_settings.try_set_deadzone_lowerbound(-2.0) + ); + assert_eq!( + Err(AxisSettingsError::DeadZoneLowerBoundOutOfRange(0.1)), + axis_settings.try_set_deadzone_lowerbound(0.1) + ); + + assert_eq!( + Err(AxisSettingsError::DeadZoneUpperBoundOutOfRange(-0.1)), + axis_settings.try_set_deadzone_upperbound(-0.1) + ); + assert_eq!( + Err(AxisSettingsError::DeadZoneUpperBoundOutOfRange(1.1)), + axis_settings.try_set_deadzone_upperbound(1.1) + ); + assert_eq!( + Err(AxisSettingsError::LiveZoneUpperBoundOutOfRange(-0.1)), + axis_settings.try_set_livezone_upperbound(-0.1) + ); + assert_eq!( + Err(AxisSettingsError::LiveZoneUpperBoundOutOfRange(1.1)), + axis_settings.try_set_livezone_upperbound(1.1) + ); + + axis_settings.set_livezone_lowerbound(-0.7); + axis_settings.set_deadzone_lowerbound(-0.3); + assert_eq!( + Err( + AxisSettingsError::LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { + livezone_lowerbound: -0.1, + deadzone_lowerbound: -0.3, + } + ), + axis_settings.try_set_livezone_lowerbound(-0.1) + ); + assert_eq!( + Err( + AxisSettingsError::LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { + livezone_lowerbound: -0.7, + deadzone_lowerbound: -0.9 + } + ), + axis_settings.try_set_deadzone_lowerbound(-0.9) + ); + + axis_settings.set_deadzone_upperbound(0.3); + axis_settings.set_livezone_upperbound(0.7); + assert_eq!( + Err( + AxisSettingsError::DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { + deadzone_upperbound: 0.8, + livezone_upperbound: 0.7 + } + ), + axis_settings.try_set_deadzone_upperbound(0.8) + ); + assert_eq!( + Err( + AxisSettingsError::DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { + deadzone_upperbound: 0.3, + livezone_upperbound: 0.1 + } + ), + axis_settings.try_set_livezone_upperbound(0.1) + ); + } } From ea94b67f488a00c07b398c63de30a57e24d15b07 Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 5 Oct 2022 16:24:49 -0400 Subject: [PATCH 25/25] Moved each docstring next to its enum variant. --- crates/bevy_input/src/gamepad.rs | 51 +++++++------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 5623e6888dc82..3d96932af8c8c 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -5,78 +5,47 @@ use bevy_utils::{tracing::info, HashMap, HashSet}; use thiserror::Error; /// Errors that occur when setting axis settings for gamepad input. -/// -/// + `LiveZoneLowerBoundOutOfRange(f32)` -/// -/// The given parameter `livezone_lowerbound` was not in range -1.0..=0.0. -/// -/// + `DeadZoneLowerBoundOutOfRange(f32)` -/// -/// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. -/// -/// + `DeadZoneUpperBoundOutOfRange(f32)` -/// -/// The given parameter `deadzone_upperbound` was not in range 0.0..=1.0. -/// -/// + `LiveZoneUpperBoundOutOfRange(f32)` -/// -/// The given parameter `livezone_upperbound` was not in range 0.0..=1.0. -/// -/// + `LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { livezone_lowerbound: f32, deadzone_lowerbound: f32, }` -/// -/// Parameter `livezone_lowerbound` was not less than or equal to parameter `deadzone_lowerbound`. -/// -/// + `LiveZoneUpperBoundGreaterThanLiveZoneUpperBound { livezone_upperbound: f32, deadzone_upperbound: f32, }` -/// -/// Parameter `deadzone_upperbound` was not less than or equal to parameter `livezone_upperbound`. -/// -/// + `Threshold(f32)` -/// -/// The given parameter was not in range 0.0..=2.0. #[derive(Error, Debug, PartialEq)] pub enum AxisSettingsError { + /// The given parameter `livezone_lowerbound` was not in range -1.0..=0.0. #[error("invalid livezone_lowerbound {0}, expected value [-1.0..=0.0]")] LiveZoneLowerBoundOutOfRange(f32), + /// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. #[error("invalid deadzone_lowerbound {0}, expected value [-1.0..=0.0]")] DeadZoneLowerBoundOutOfRange(f32), + /// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. #[error("invalid deadzone_upperbound {0}, expected value [0.0..=1.0]")] DeadZoneUpperBoundOutOfRange(f32), + /// The given parameter `deadzone_lowerbound` was not in range -1.0..=0.0. #[error("invalid livezone_upperbound {0}, expected value [0.0..=1.0]")] LiveZoneUpperBoundOutOfRange(f32), + /// Parameter `livezone_lowerbound` was not less than or equal to parameter `deadzone_lowerbound`. #[error("invalid parameter values livezone_lowerbound {} deadzone_lowerbound {}, expected livezone_lowerbound <= deadzone_lowerbound", .livezone_lowerbound, .deadzone_lowerbound)] LiveZoneLowerBoundGreaterThanDeadZoneLowerBound { livezone_lowerbound: f32, deadzone_lowerbound: f32, }, + /// Parameter `deadzone_upperbound` was not less than or equal to parameter `livezone_upperbound`. #[error("invalid parameter values livezone_upperbound {} deadzone_upperbound {}, expected deadzone_upperbound <= livezone_upperbound", .livezone_upperbound, .deadzone_upperbound)] DeadZoneUpperBoundGreaterThanLiveZoneUpperBound { livezone_upperbound: f32, deadzone_upperbound: f32, }, + /// The given parameter was not in range 0.0..=2.0. #[error("invalid threshold {0}, expected 0.0 <= threshold <= 2.0")] Threshold(f32), } /// Errors that occur when setting button settings for gamepad input. -/// -/// + `ReleaseThresholdOutOfRange(f32)` -/// -/// The given parameter was not in range 0.0..=1.0. -/// -/// + `PressThresholdOutOfRange(f32)` -/// -/// The given parameter was not in range 0.0..=1.0. -/// -/// + `ReleaseThresholdGreaterThanPressThreshold {press_threshold: f32, release_threshold: f32,}` -/// -/// Parameter `release_threshold` was not less than or equal to `press_threshold`. -/// #[derive(Error, Debug, PartialEq)] pub enum ButtonSettingsError { + /// The given parameter was not in range 0.0..=2.0. #[error("invalid release_threshold {0}, expected value [0.0..=1.0]")] ReleaseThresholdOutOfRange(f32), + /// The given parameter was not in range 0.0..=2.0. #[error("invalid press_threshold {0}, expected [0.0..=1.0]")] PressThresholdOutOfRange(f32), + /// Parameter `release_threshold` was not less than or equal to `press_threshold`. #[error("invalid parameter values release_threshold {} press_threshold {}, expected release_threshold <= press_threshold", .release_threshold, .press_threshold)] ReleaseThresholdGreaterThanPressThreshold { press_threshold: f32,