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

Deadzone shapes #370

Merged
merged 13 commits into from
Aug 7, 2023
131 changes: 125 additions & 6 deletions src/axislike.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Tools for working with directional axis-like user inputs (gamesticks, D-Pads and emulated equivalents)

use crate::buttonlike::{MouseMotionDirection, MouseWheelDirection};
use crate::input_streams::InputStreams;
use crate::orientation::{Direction, Rotation};
use crate::prelude::QwertyScanCode;
use crate::user_input::InputKind;
use crate::user_input::{InputKind, UserInput};
use bevy::input::{
gamepad::{GamepadAxisType, GamepadButtonType},
keyboard::KeyCode,
Expand Down Expand Up @@ -186,6 +187,8 @@ pub struct DualAxis {
pub x: SingleAxis,
/// The axis representing vertical movement.
pub y: SingleAxis,
/// The shape of the deadzone
pub deadzone_shape: DeadZoneShape,
}

impl DualAxis {
Expand All @@ -194,16 +197,26 @@ impl DualAxis {
/// This cannot be changed, but the struct can be easily manually constructed.
pub const DEFAULT_DEADZONE: f32 = 0.1;

/// Creates a [`DualAxis`] with both `positive_low` and `negative_low` in both axes set to `threshold`.
/// The default shape of the deadzone used by constructor methods.
///
/// This cannot be changed, but the struct can be easily manually constructed.
pub const DEFAULT_DEADZONE_SHAPE: DeadZoneShape = DeadZoneShape::Cross {
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
horizontal_width: Self::DEFAULT_DEADZONE,
vertical_width: Self::DEFAULT_DEADZONE,
};

/// Creates a [`DualAxis`] with both `positive_low` and `negative_low` in both axes set to `threshold` with a `deadzone_shape`.
#[must_use]
pub fn symmetric(
x_axis_type: impl Into<AxisType>,
y_axis_type: impl Into<AxisType>,
threshold: f32,
deadzone_shape: DeadZoneShape,
) -> DualAxis {
DualAxis {
x: SingleAxis::symmetric(x_axis_type, threshold),
y: SingleAxis::symmetric(y_axis_type, threshold),
deadzone_shape,
}
}

Expand All @@ -221,6 +234,7 @@ impl DualAxis {
DualAxis {
x: SingleAxis::from_value(x_axis_type, x_value),
y: SingleAxis::from_value(y_axis_type, y_value),
deadzone_shape: Self::DEFAULT_DEADZONE_SHAPE,
}
}

Expand All @@ -231,6 +245,7 @@ impl DualAxis {
GamepadAxisType::LeftStickX,
GamepadAxisType::LeftStickY,
Self::DEFAULT_DEADZONE,
Self::DEFAULT_DEADZONE_SHAPE,
)
}

Expand All @@ -241,6 +256,7 @@ impl DualAxis {
GamepadAxisType::RightStickX,
GamepadAxisType::RightStickY,
Self::DEFAULT_DEADZONE,
Self::DEFAULT_DEADZONE_SHAPE,
)
}

Expand All @@ -249,6 +265,7 @@ impl DualAxis {
DualAxis {
x: SingleAxis::mouse_wheel_x(),
y: SingleAxis::mouse_wheel_y(),
deadzone_shape: Self::DEFAULT_DEADZONE_SHAPE,
}
}

Expand All @@ -257,14 +274,14 @@ impl DualAxis {
DualAxis {
x: SingleAxis::mouse_motion_x(),
y: SingleAxis::mouse_motion_y(),
deadzone_shape: Self::DEFAULT_DEADZONE_SHAPE,
}
}

/// Returns this [`DualAxis`] with the deadzone set to the specified value
/// Returns this [`DualAxis`] with the deadzone set to the specified values and shape
#[must_use]
pub fn with_deadzone(mut self, deadzone: f32) -> DualAxis {
self.x = self.x.with_deadzone(deadzone);
self.y = self.y.with_deadzone(deadzone);
pub fn with_deadzone(mut self, deadzone: DeadZoneShape) -> DualAxis {
self.deadzone_shape = deadzone;
self
}

Expand Down Expand Up @@ -688,3 +705,105 @@ impl From<DualAxisData> for Vec2 {
data.xy
}
}

/// The shape of the deadzone for a [`DualAxis`] input.
///
/// Uses the x and y thresholds on [`DualAxis`] to set the shape diamensions
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum DeadZoneShape {
/// Deadzone with a cross
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
///
/// Uses the x threshold as the horizontal width and y threshold as the vertical width.
Cross {
/// The width of the horizontal rectangle.
horizontal_width: f32,
/// The width of the vertical rectangle.
vertical_width: f32,
},
/// Deadzone with a rectangle.
///
/// Uses the x threshold as the width and y threshold as the length.
Rect {
/// The width of the rectangle.
width: f32,
/// The height of the rectangle.
height: f32,
},
/// Deadzone with a circle.
///
/// Uses the larger of the x or y threshold as the radius
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
Ellipse {
/// The horizontal radius
radius_x: f32,
/// The vertical radius
radius_y: f32,
},
}

impl Eq for DeadZoneShape {}
impl std::hash::Hash for DeadZoneShape {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {}
}

impl DeadZoneShape {
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
///
pub fn get_dual_axis_data(
&self,
dual_axis: &DualAxis,
input_stream: &InputStreams,
) -> DualAxisData {
match self {
DeadZoneShape::Cross {
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
horizontal_width,
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
vertical_width,
} => {
let x = input_stream
.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.x)), true);
let y = input_stream
.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.y)), true);

DualAxisData::new(x, y)
}
DeadZoneShape::Rect { width, height } => {
let x = input_stream.input_value(
&UserInput::Single(InputKind::SingleAxis(dual_axis.x)),
false,
);
let y = input_stream.input_value(
&UserInput::Single(InputKind::SingleAxis(dual_axis.y)),
false,
);

if DeadZoneShape::outside_rectangle(x, y, *width, *height) {
DualAxisData::new(x, y)
} else {
DualAxisData::new(0.0, 0.0)
}
}
DeadZoneShape::Ellipse { radius_x, radius_y } => {
let x = input_stream.input_value(
&UserInput::Single(InputKind::SingleAxis(dual_axis.x)),
false,
);
let y = input_stream.input_value(
&UserInput::Single(InputKind::SingleAxis(dual_axis.y)),
false,
);

if DeadZoneShape::outside_ellipse(x, y, *radius_x, *radius_y) {
DualAxisData::new(x, y)
} else {
DualAxisData::new(0.0, 0.0)
}
}
}
}

fn outside_ellipse(x: f32, y: f32, radius_x: f32, radius_y: f32) -> bool {
(x.powi(2) / radius_x.powi(2) + y.powi(2) / radius_y.powi(2)) >= 1.0
}

fn outside_rectangle(x: f32, y: f32, width: f32, height: f32) -> bool {
x > width || x < -width || y > height || y < -height
}
}
2 changes: 1 addition & 1 deletion src/input_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<A: Actionlike> InputMap<A> {
if input_streams.input_pressed(input) {
inputs.push(input.clone());

action.value += input_streams.input_value(input);
action.value += input_streams.input_value(input, true);
}
}

Expand Down
Loading
Loading