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
44 changes: 39 additions & 5 deletions src/axislike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,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 +196,23 @@ 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;

/// 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 +230,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 +241,7 @@ impl DualAxis {
GamepadAxisType::LeftStickX,
GamepadAxisType::LeftStickY,
Self::DEFAULT_DEADZONE,
Self::DEFAULT_DEADZONE_SHAPE,
)
}

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

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

Expand All @@ -257,14 +270,16 @@ 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_x: f32, deadzone_y: f32, deadzone: DeadzoneShape) -> DualAxis {
self.x = self.x.with_deadzone(deadzone_x);
self.y = self.y.with_deadzone(deadzone_y);
self.deadzone_shape = deadzone;
self
}

Expand Down Expand Up @@ -688,3 +703,22 @@ 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, Eq, Hash)]
pub enum DeadzoneShape{
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
/// Deadzone with a rectangle.
///
/// Uses the x threshold as the width and y threshold as the length.
Rect,
/// 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
Circle,
}
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
63 changes: 42 additions & 21 deletions src/input_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy::ecs::system::SystemState;

use crate::axislike::{
AxisType, DualAxisData, MouseMotionAxisType, MouseWheelAxisType, SingleAxis, VirtualAxis,
VirtualDPad,
VirtualDPad, DeadzoneShape,
};
use crate::buttonlike::{MouseMotionDirection, MouseWheelDirection};
use crate::prelude::DualAxis;
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'a> InputStreams<'a> {
|| self.button_pressed(InputKind::SingleAxis(axis.y))
}
InputKind::SingleAxis(axis) => {
let value = self.input_value(&UserInput::Single(button));
let value = self.input_value(&UserInput::Single(button), true);

value < axis.negative_low || value > axis.positive_low
}
Expand Down Expand Up @@ -247,7 +247,7 @@ impl<'a> InputStreams<'a> {
///
/// If you need to ensure that this value is always in the range `[-1., 1.]`,
/// be sure to clamp the returned data.
pub fn input_value(&self, input: &UserInput) -> f32 {
pub fn input_value(&self, input: &UserInput, include_deadzone: bool) -> f32 {
let use_button_value = || -> f32 {
if self.input_pressed(input) {
1.0
Expand All @@ -259,7 +259,7 @@ impl<'a> InputStreams<'a> {
// Helper that takes the value returned by an axis and returns 0.0 if it is not within the
// triggering range.
let value_in_axis_range = |axis: &SingleAxis, value: f32| -> f32 {
if value >= axis.negative_low && value <= axis.positive_low {
if value >= axis.negative_low && value <= axis.positive_low && include_deadzone{
0.0
} else if axis.inverted {
-value
Expand Down Expand Up @@ -317,8 +317,8 @@ impl<'a> InputStreams<'a> {
}
}
UserInput::VirtualAxis(VirtualAxis { negative, positive }) => {
self.input_value(&UserInput::Single(*positive)).abs()
- self.input_value(&UserInput::Single(*negative)).abs()
self.input_value(&UserInput::Single(*positive), true).abs()
- self.input_value(&UserInput::Single(*negative), true).abs()
}
UserInput::Single(InputKind::DualAxis(_)) => {
self.input_axis_pair(input).unwrap_or_default().length()
Expand Down Expand Up @@ -375,28 +375,49 @@ impl<'a> InputStreams<'a> {
left,
right,
}) => {
let x = self.input_value(&UserInput::Single(*right)).abs()
- self.input_value(&UserInput::Single(*left)).abs();
let y = self.input_value(&UserInput::Single(*up)).abs()
- self.input_value(&UserInput::Single(*down)).abs();
let x = self.input_value(&UserInput::Single(*right), true).abs()
- self.input_value(&UserInput::Single(*left), true).abs();
let y = self.input_value(&UserInput::Single(*up), true).abs()
- self.input_value(&UserInput::Single(*down), true).abs();
Some(DualAxisData::new(x, y))
}
_ => None,
}
}

fn extract_dual_axis_data(&self, dual_axis: &DualAxis) -> DualAxisData {
let x = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.x)));
let y = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.y)));

if x > dual_axis.x.positive_low
|| x < dual_axis.x.negative_low
|| y > dual_axis.y.positive_low
|| y < dual_axis.y.negative_low
{
DualAxisData::new(x, y)
} else {
DualAxisData::new(0.0, 0.0)
match dual_axis.deadzone_shape {
DeadzoneShape::Cross => {
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
let x = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.x)), true);
let y = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.y)), true);

DualAxisData::new(x, y)
},
DeadzoneShape::Rect => {
let x = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.x)), false);
let y = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.y)), false);
if x > dual_axis.x.positive_low
|| x < dual_axis.x.negative_low
|| y > dual_axis.y.positive_low
|| y < dual_axis.y.negative_low
{
DualAxisData::new(x, y)
} else {
DualAxisData::new(0.0, 0.0)
}
},
DeadzoneShape::Circle => {
let x = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.x)), false);
let y = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.y)), false);
let radius = dual_axis.x.positive_low.max(dual_axis.y.positive_low);

if (x.powi(2) + y.powi(2)).sqrt() >= radius
{
DualAxisData::new(x, y)
} else {
DualAxisData::new(0.0, 0.0)
}
},
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/gamepad_axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn game_pad_dual_axis_mocking() {
negative_low: 0.0,
inverted: false,
},
deadzone_shape: DualAxis::DEFAULT_DEADZONE_SHAPE,
};
app.send_input(input);
let mut events = app.world.resource_mut::<Events<GamepadEvent>>();
Expand Down
1 change: 1 addition & 0 deletions tests/mouse_motion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn mouse_motion_dual_axis_mocking() {
negative_low: 0.0,
inverted: false,
},
deadzone_shape: DualAxis::DEFAULT_DEADZONE_SHAPE,
};
app.send_input(input);
let mut events = app.world.resource_mut::<Events<MouseMotion>>();
Expand Down
1 change: 1 addition & 0 deletions tests/mouse_wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn mouse_wheel_dual_axis_mocking() {
negative_low: 0.0,
inverted: false,
},
deadzone_shape: DualAxis::DEFAULT_DEADZONE_SHAPE,
};
app.send_input(input);
let mut events = app.world.resource_mut::<Events<MouseWheel>>();
Expand Down
Loading