Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix axis settings constructor #7233

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions crates/bevy_input/src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ impl ButtonSettings {
/// Otherwise, values will not be rounded.
///
/// The valid range is `[-1.0, 1.0]`.
#[derive(Debug, Clone, Reflect, FromReflect)]
#[derive(Debug, Clone, Reflect, FromReflect, PartialEq)]
#[reflect(Debug, Default)]
pub struct AxisSettings {
/// Values that are higher than `livezone_upperbound` will be rounded up to -1.0.
Expand Down Expand Up @@ -611,7 +611,27 @@ impl Default for AxisSettings {
}

impl AxisSettings {
/// Creates a new `AxisSettings` instance.
/// Same as [`Self::new`], but the bounds are defined as gap from extremities.
///
/// The default [`AxisSettings`] would be `AxisSettings::new_symmetric(0.05, 0.01)`.
///
/// The arguments must follow those rules otherwise this returns an `Err`:
/// + `0.0 <= zone_size <= 0.5`
/// + `0.0 <= threshold <= 2.0`
pub fn new_symmetric(
zone_size: f32,
threshold: f32,
) -> Result<AxisSettings, AxisSettingsError> {
Self::new(
-1.0 + zone_size,
-zone_size,
zone_size,
1.0 - zone_size,
threshold,
)
}

/// Creates a new [`AxisSettings`] instance.
///
/// # Arguments
///
Expand All @@ -622,9 +642,10 @@ impl AxisSettings {
/// + `threshold` - the minimum value by which input must change before the change is registered.
///
/// Restrictions:
/// + `-1.0 <= ``livezone_lowerbound`` <= ``deadzone_lowerbound`` <= 0.0 <= ``deadzone_upperbound`` <=
/// ``livezone_upperbound`` <= 1.0`
/// + `0.0 <= ``threshold`` <= 2.0`
///
/// + `-1.0 <= livezone_lowerbound <= deadzone_lowerbound <= 0.0`
/// + `0.0 <= deadzone_upperbound <= livezone_upperbound <= 1.0`
/// + `0.0 <= threshold <= 2.0`
///
/// # Errors
///
Expand All @@ -638,19 +659,21 @@ impl AxisSettings {
livezone_upperbound: f32,
threshold: f32,
) -> Result<AxisSettings, AxisSettingsError> {
if !(-1.0..=0.0).contains(&livezone_lowerbound) {
let within = |value, lower: f32, upper: f32| (lower..=upper).contains(&value);
nicopap marked this conversation as resolved.
Show resolved Hide resolved

if !within(livezone_lowerbound, -1.0, 0.0) {
Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange(
livezone_lowerbound,
))
} else if !(-1.0..=0.0).contains(&deadzone_lowerbound) {
} else if !within(deadzone_lowerbound, -1.0, 0.0) {
Err(AxisSettingsError::DeadZoneLowerBoundOutOfRange(
deadzone_lowerbound,
))
} else if !(-1.0..=0.0).contains(&deadzone_upperbound) {
} else if !within(deadzone_upperbound, 0.0, 1.0) {
Err(AxisSettingsError::DeadZoneUpperBoundOutOfRange(
deadzone_upperbound,
))
} else if !(-1.0..=0.0).contains(&livezone_upperbound) {
} else if !within(livezone_upperbound, 0.0, 1.0) {
Err(AxisSettingsError::LiveZoneUpperBoundOutOfRange(
livezone_upperbound,
))
Expand All @@ -668,7 +691,7 @@ impl AxisSettings {
deadzone_upperbound,
},
)
} else if !(0.0..=2.0).contains(&threshold) {
} else if !within(threshold, 0.0, 2.0) {
Err(AxisSettingsError::Threshold(threshold))
} else {
Ok(Self {
Expand Down Expand Up @@ -1489,6 +1512,16 @@ mod tests {
#[test]
fn test_try_out_of_range_axis_settings() {
let mut axis_settings = AxisSettings::default();
assert_eq!(
AxisSettings::new(-0.95, -0.05, 0.05, 0.95, 0.001),
Ok(AxisSettings {
livezone_lowerbound: -0.95,
deadzone_lowerbound: -0.05,
deadzone_upperbound: 0.05,
livezone_upperbound: 0.95,
threshold: 0.001,
})
);
assert_eq!(
Err(AxisSettingsError::LiveZoneLowerBoundOutOfRange(-2.0)),
axis_settings.try_set_livezone_lowerbound(-2.0)
Expand Down