leafwing_input_manager/input_processing/single_axis/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
//! Processors for single-axis input values
use std::hash::{Hash, Hasher};
use bevy::{math::FloatOrd, prelude::Reflect};
use serde::{Deserialize, Serialize};
pub use self::custom::*;
pub use self::range::*;
mod custom;
mod range;
/// A processor for single-axis input values,
/// accepting a `f32` input and producing a `f32` output.
#[must_use]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Reflect, Serialize, Deserialize)]
pub enum AxisProcessor {
/// Converts input values into three discrete values,
/// similar to [`f32::signum()`] but returning `0.0` for zero values.
///
/// ```rust
/// use leafwing_input_manager::prelude::*;
///
/// // 1.0 for positive values
/// assert_eq!(AxisProcessor::Digital.process(2.5), 1.0);
/// assert_eq!(AxisProcessor::Digital.process(0.5), 1.0);
///
/// // 0.0 for zero values
/// assert_eq!(AxisProcessor::Digital.process(0.0), 0.0);
/// assert_eq!(AxisProcessor::Digital.process(-0.0), 0.0);
///
/// // -1.0 for negative values
/// assert_eq!(AxisProcessor::Digital.process(-0.5), -1.0);
/// assert_eq!(AxisProcessor::Digital.process(-2.5), -1.0);
/// ```
Digital,
/// Flips the sign of input values, resulting in a directional reversal of control.
///
/// ```rust
/// use leafwing_input_manager::prelude::*;
///
/// assert_eq!(AxisProcessor::Inverted.process(2.5), -2.5);
/// assert_eq!(AxisProcessor::Inverted.process(-2.5), 2.5);
/// ```
Inverted,
/// Scales input values using a specified multiplier to fine-tune the responsiveness of control.
///
/// ```rust
/// use leafwing_input_manager::prelude::*;
///
/// // Doubled!
/// assert_eq!(AxisProcessor::Sensitivity(2.0).process(2.0), 4.0);
///
/// // Halved!
/// assert_eq!(AxisProcessor::Sensitivity(0.5).process(2.0), 1.0);
///
/// // Negated and halved!
/// assert_eq!(AxisProcessor::Sensitivity(-0.5).process(2.0), -1.0);
/// ```
Sensitivity(f32),
/// A wrapper around [`AxisBounds`] to represent value bounds.
ValueBounds(AxisBounds),
/// A wrapper around [`AxisExclusion`] to represent unscaled deadzone.
Exclusion(AxisExclusion),
/// A wrapper around [`AxisDeadZone`] to represent scaled deadzone.
DeadZone(AxisDeadZone),
/// A user-defined processor that implements [`CustomAxisProcessor`].
Custom(Box<dyn CustomAxisProcessor>),
}
impl AxisProcessor {
/// Computes the result by processing the `input_value`.
#[must_use]
#[inline]
pub fn process(&self, input_value: f32) -> f32 {
match self {
Self::Digital => {
if input_value == 0.0 {
0.0
} else {
input_value.signum()
}
}
Self::Inverted => -input_value,
Self::Sensitivity(sensitivity) => sensitivity * input_value,
Self::ValueBounds(bounds) => bounds.clamp(input_value),
Self::Exclusion(exclusion) => exclusion.exclude(input_value),
Self::DeadZone(deadzone) => deadzone.normalize(input_value),
Self::Custom(processor) => processor.process(input_value),
}
}
}
impl Eq for AxisProcessor {}
impl Hash for AxisProcessor {
fn hash<H: Hasher>(&self, state: &mut H) {
std::mem::discriminant(self).hash(state);
match self {
Self::Digital => {}
Self::Inverted => {}
Self::Sensitivity(sensitivity) => FloatOrd(*sensitivity).hash(state),
Self::ValueBounds(bounds) => bounds.hash(state),
Self::Exclusion(exclusion) => exclusion.hash(state),
Self::DeadZone(deadzone) => deadzone.hash(state),
Self::Custom(processor) => processor.hash(state),
}
}
}
/// Provides methods for configuring and manipulating the processing pipeline for single-axis input.
pub trait WithAxisProcessingPipelineExt: Sized {
/// Resets the processing pipeline, removing any currently applied processors.
fn reset_processing_pipeline(self) -> Self;
/// Replaces the current processing pipeline with the given [`AxisProcessor`]s.
fn replace_processing_pipeline(
self,
processors: impl IntoIterator<Item = AxisProcessor>,
) -> Self;
/// Appends the given [`AxisProcessor`] as the next processing step.
fn with_processor(self, processor: impl Into<AxisProcessor>) -> Self;
/// Appends an [`AxisProcessor::Digital`] processor as the next processing step,
/// similar to [`f32::signum`] but returning `0.0` for zero values.
#[inline]
fn digital(self) -> Self {
self.with_processor(AxisProcessor::Digital)
}
/// Appends an [`AxisProcessor::Inverted`] processor as the next processing step,
/// flipping the sign of values on the axis.
#[inline]
fn inverted(self) -> Self {
self.with_processor(AxisProcessor::Inverted)
}
/// Appends an [`AxisProcessor::Sensitivity`] processor as the next processing step,
/// multiplying values on the axis with the given sensitivity factor.
#[inline]
fn sensitivity(self, sensitivity: f32) -> Self {
self.with_processor(AxisProcessor::Sensitivity(sensitivity))
}
/// Appends an [`AxisBounds`] processor as the next processing step,
/// restricting values within the range `[min, max]` on the axis.
#[inline]
fn with_bounds(self, min: f32, max: f32) -> Self {
self.with_processor(AxisBounds::new(min, max))
}
/// Appends an [`AxisBounds`] processor as the next processing step,
/// restricting values within the range `[-threshold, threshold]`.
#[inline]
fn with_bounds_symmetric(self, threshold: f32) -> Self {
self.with_processor(AxisBounds::symmetric(threshold))
}
/// Appends an [`AxisBounds`] processor as the next processing step,
/// restricting values to a minimum value.
#[inline]
fn at_least(self, min: f32) -> Self {
self.with_processor(AxisBounds::at_least(min))
}
/// Appends an [`AxisBounds`] processor as the next processing step,
/// restricting values to a maximum value.
#[inline]
fn at_most(self, max: f32) -> Self {
self.with_processor(AxisBounds::at_most(max))
}
/// Appends an [`AxisDeadZone`] processor as the next processing step,
/// excluding values within the dead zone range `[negative_max, positive_min]` on the axis,
/// treating them as zeros, then normalizing non-excluded input values into the "live zone",
/// the remaining range within the [`AxisBounds::magnitude(1.0)`](AxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(AxisDeadZone::new(negative_max, positive_min))
}
/// Appends an [`AxisDeadZone`] processor as the next processing step,
/// excluding values within the dead zone range `[-threshold, threshold]` on the axis,
/// treating them as zeros, then normalizing non-excluded input values into the "live zone",
/// the remaining range within the [`AxisBounds::magnitude(1.0)`](AxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_symmetric(self, threshold: f32) -> Self {
self.with_processor(AxisDeadZone::symmetric(threshold))
}
/// Appends an [`AxisDeadZone`] processor as the next processing step,
/// only passing positive values that greater than `positive_min`
/// and then normalizing them into the "live zone" range `[positive_min, 1.0]`.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive(self, positive_min: f32) -> Self {
self.with_processor(AxisDeadZone::only_positive(positive_min))
}
/// Appends an [`AxisDeadZone`] processor as the next processing step,
/// only passing negative values that less than `negative_max`
/// and then normalizing them into the "live zone" range `[-1.0, negative_max]`.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative(self, negative_max: f32) -> Self {
self.with_processor(AxisDeadZone::only_negative(negative_max))
}
/// Appends an [`AxisExclusion`] processor as the next processing step,
/// ignoring values within the dead zone range `[negative_max, positive_min]` on the axis,
/// treating them as zeros.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_unscaled(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(AxisExclusion::new(negative_max, positive_min))
}
/// Appends an [`AxisExclusion`] processor as the next processing step,
/// ignoring values within the dead zone range `[-threshold, threshold]` on the axis,
/// treating them as zeros.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_symmetric_unscaled(self, threshold: f32) -> Self {
self.with_processor(AxisExclusion::symmetric(threshold))
}
/// Appends an [`AxisExclusion`] processor as the next processing step,
/// only passing positive values that greater than `positive_min`.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive_unscaled(self, positive_min: f32) -> Self {
self.with_processor(AxisExclusion::only_positive(positive_min))
}
/// Appends an [`AxisExclusion`] processor as the next processing step,
/// only passing negative values that less than `negative_max`.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative_unscaled(self, negative_max: f32) -> Self {
self.with_processor(AxisExclusion::only_negative(negative_max))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_axis_inversion_processor() {
for value in -300..300 {
let value = value as f32 * 0.01;
assert_eq!(AxisProcessor::Inverted.process(value), -value);
assert_eq!(AxisProcessor::Inverted.process(-value), value);
}
}
#[test]
fn test_axis_sensitivity_processor() {
for value in -300..300 {
let value = value as f32 * 0.01;
for sensitivity in -300..300 {
let sensitivity = sensitivity as f32 * 0.01;
let processor = AxisProcessor::Sensitivity(sensitivity);
assert_eq!(processor.process(value), sensitivity * value);
}
}
}
}