Skip to content

Commit

Permalink
Update document
Browse files Browse the repository at this point in the history
  • Loading branch information
Shute052 committed Feb 23, 2024
1 parent 6d1d202 commit 9c3697a
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions src/user_inputs/axislike_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl SingleAxisSettings {
Self::new(sensitivity, SingleAxisDeadzone::DEFAULT)
}

/// Creates a new [`SingleAxisSettings`] with the given settings.
/// Creates a new [`SingleAxisSettings`] with the given deadzone.
pub const fn with_deadzone(deadzone: SingleAxisDeadzone) -> Self {
Self::new(1.0, deadzone)
}
Expand All @@ -112,19 +112,19 @@ impl SingleAxisSettings {
///
/// # Arguments
///
/// - `negative_low` - The lower limit for negative values.
pub fn with_negative_only(negative_low: f32) -> Self {
let deadzone = SingleAxisDeadzone::negative_only(negative_low);
/// - `negative_max` - The maximum limit for negative values.
pub fn with_negative_only(negative_max: f32) -> Self {
let deadzone = SingleAxisDeadzone::negative_only(negative_max);
Self::new(1.0, deadzone)
}

/// Creates a new [`SingleAxisSettings`] with only positive values being filtered.
///
/// # Arguments
///
/// - `positive_low` - The lower limit for positive values.
pub fn with_positive_only(positive_low: f32) -> Self {
let deadzone = SingleAxisDeadzone::positive_only(positive_low);
/// - `positive_min` - The minimum limit for positive values.
pub fn with_positive_only(positive_min: f32) -> Self {
let deadzone = SingleAxisDeadzone::positive_only(positive_min);
Self::new(1.0, deadzone)
}

Expand Down Expand Up @@ -153,83 +153,83 @@ impl SingleAxisSettings {
/// Deadzone settings for single-axis inputs.
#[derive(Debug, Clone, PartialEq, Reflect, Serialize, Deserialize)]
pub struct SingleAxisDeadzone {
/// The lower limit for negative values.
negative_low: f32,
/// The maximum limit for negative values.
negative_max: f32,

/// The lower limit for positive values.
positive_low: f32,
/// The minimum limit for positive values.
positive_min: f32,

/// The width of the deadzone around the negative axis.
///
/// This value represents the absolute value of `negative_low`,
/// This value represents the absolute value of `negative_max`,
/// reducing the performance cost of using `abs` during value computation.
negative_low_width: f32,
negative_deadzone_width: f32,
}

impl SingleAxisDeadzone {
/// The default deadzone with a small offset to filter out near-zero input values.
///
/// This deadzone excludes input values within the range [-0.1, 0.1].
/// This deadzone excludes input values within the range `[-0.1, 0.1]`.
pub const DEFAULT: Self = Self {
negative_low: -0.1,
positive_low: 0.1,
negative_low_width: 0.1,
negative_max: -0.1,
positive_min: 0.1,
negative_deadzone_width: 0.1,
};

/// The deadzone that only filters out the zeroes.
///
/// This deadzone does not filter out near-zero negative or positive values.
/// This deadzone doesn't filter out near-zero negative or positive values.
pub const ZERO: Self = Self {
negative_low: 0.0,
positive_low: 0.0,
negative_low_width: 0.0,
negative_max: 0.0,
positive_min: 0.0,
negative_deadzone_width: 0.0,
};

/// Creates a new [`SingleAxisDeadzone`] with the given settings.
/// Creates a new [`SingleAxisDeadzone`] to filter out input values within the range `[negative_max, positive_min]`.
///
/// # Arguments
///
/// - `negative_low` - The lower limit for negative values, clamped to `0.0` if greater than `0.0`.
/// - `positive_low` - The lower limit for positive values, clamped to `0.0` if less than `0.0`.
pub fn new(negative_low: f32, positive_low: f32) -> Self {
/// - `negative_max` - The maximum limit for negative values, clamped to `0.0` if greater than `0.0`.
/// - `positive_min` - The minimum limit for positive values, clamped to `0.0` if less than `0.0`.
pub fn new(negative_max: f32, positive_min: f32) -> Self {
Self {
negative_low: negative_low.min(0.0),
positive_low: positive_low.max(0.0),
negative_low_width: negative_low.abs(),
negative_max: negative_max.min(0.0),
positive_min: positive_min.max(0.0),
negative_deadzone_width: negative_max.abs(),
}
}

/// Creates a new [`SingleAxisDeadzone`] with only negative values being filtered.
///
/// # Arguments
///
/// - `negative_low` - The lower limit for negative values, clamped to `0.0` if greater than `0.0`.
pub fn negative_only(negative_low: f32) -> Self {
/// - `negative_max` - The maximum limit for negative values, clamped to `0.0` if greater than `0.0`.
pub fn negative_only(negative_max: f32) -> Self {
Self {
negative_low: negative_low.min(0.0),
positive_low: f32::MAX,
negative_low_width: negative_low.abs(),
negative_max: negative_max.min(0.0),
positive_min: f32::MAX,
negative_deadzone_width: negative_max.abs(),
}
}

/// Creates a new [`SingleAxisDeadzone`] with only positive values being filtered.
///
/// # Arguments
///
/// - `positive_low` - The lower limit for negative values, clamped to `0.0` if less than `0.0`.
pub fn positive_only(positive_low: f32) -> Self {
/// - `positive_min` - The minimum limit for negative values, clamped to `0.0` if less than `0.0`.
pub fn positive_only(positive_min: f32) -> Self {
Self {
negative_low: f32::MIN,
positive_low: positive_low.max(0.0),
negative_low_width: f32::MAX,
negative_max: f32::MIN,
positive_min: positive_min.max(0.0),
negative_deadzone_width: f32::MAX,
}
}

/// Returns the input value after applying the deadzone.
///
/// This function calculates the deadzone width based on the input value and the deadzone settings.
/// If the input value falls within the deadzone range, it returns `0.0`.
/// Otherwise, it normalizes the input value into the range [-1.0, 1.0] by subtracting the deadzone width.
/// Otherwise, it normalizes the input value into the range `[-1.0, 1.0]` by subtracting the deadzone width.
///
/// # Panics
///
Expand All @@ -238,16 +238,16 @@ impl SingleAxisDeadzone {
/// If this happens, you might be exploring the quantum realm!
/// Consider offering your computer a cup of coffee and politely asking for a less mysterious explanation.
pub fn apply_deadzone(&self, input_value: f32) -> f32 {
let is_negative_active = self.negative_low > input_value;
let is_positive_active = self.positive_low < input_value;
let is_negative_active = self.negative_max > input_value;
let is_positive_active = self.positive_min < input_value;

let deadzone_width = match (is_negative_active, is_positive_active) {
// The input value is within the deadzone and falls back to `0.0`
(false, false) => return 0.0,
// The input value is outside the negative deadzone range
(true, false) => self.negative_low_width,
(true, false) => self.negative_deadzone_width,
// The input value is outside the positive deadzone range
(false, true) => self.positive_low,
(false, true) => self.positive_min,
// This case must never be reached.
// Unless you've discovered the elusive quantum deadzone!
// Please check your quantum computer and contact the Rust Team.
Expand Down Expand Up @@ -278,8 +278,8 @@ impl Eq for SingleAxisDeadzone {}

impl std::hash::Hash for SingleAxisDeadzone {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
FloatOrd(self.negative_low).hash(state);
FloatOrd(self.positive_low).hash(state);
FloatOrd(self.negative_low_width).hash(state);
FloatOrd(self.negative_max).hash(state);
FloatOrd(self.positive_min).hash(state);
FloatOrd(self.negative_deadzone_width).hash(state);
}
}

0 comments on commit 9c3697a

Please sign in to comment.