Skip to content

Commit

Permalink
Make Single-Axis Inputs Work in Virtual DPad
Browse files Browse the repository at this point in the history
This makes sure that SingleAxis inputs will report a value of 0.0 if the
axis value is not withing the triggering zone. This makes it possible to
create a virtual gamepad made up of multiple single-axis inputs on the
same stick.
  • Loading branch information
zicklag committed Jul 15, 2022
1 parent e544b70 commit 8e655b7
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/input_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use petitset::PetitSet;
use bevy_ecs::prelude::{Events, Res, ResMut, World};
use bevy_ecs::system::SystemState;

use crate::axislike::{AxisType, DualAxisData, MouseWheelAxisType, VirtualDPad};
use crate::axislike::{AxisType, DualAxisData, MouseWheelAxisType, SingleAxis, VirtualDPad};
use crate::buttonlike::MouseWheelDirection;
use crate::user_input::{InputKind, UserInput};

Expand Down Expand Up @@ -171,10 +171,10 @@ impl<'a> InputStreams<'a> {

axis_pair.length() != 0.0
}
InputKind::SingleAxis(axis) => {
InputKind::SingleAxis(_) => {
let value = self.input_value(&UserInput::Single(button));

value < axis.negative_low || value > axis.positive_low
value != 0.0
}
InputKind::GamepadButton(gamepad_button) => {
// If a gamepad was registered, just check that one
Expand Down Expand Up @@ -295,14 +295,28 @@ 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 {
0.0
} else {
value
}
};

match input {
UserInput::Single(InputKind::SingleAxis(single_axis)) => {
match single_axis.axis_type {
AxisType::Gamepad(axis_type) => {
if let Some(axes) = self.gamepad_axes {
if let Some(gamepad) = self.associated_gamepad {
axes.get(GamepadAxis { gamepad, axis_type })
.unwrap_or_default()
let value = axes
.get(GamepadAxis { gamepad, axis_type })
.unwrap_or_default();

value_in_axis_range(single_axis, value)

// If no gamepad is registered, return the first non-zero input found
} else if let Some(gamepads) = self.gamepads {
for &gamepad in gamepads.iter() {
Expand All @@ -312,6 +326,7 @@ impl<'a> InputStreams<'a> {
axis_type: single_axis.axis_type.try_into().unwrap(),
})
.unwrap_or_default();
let value = value_in_axis_range(single_axis, value);

if value != 0.0 {
// A matching input was pressed on a gamepad
Expand Down Expand Up @@ -344,7 +359,7 @@ impl<'a> InputStreams<'a> {
}
}
}
total_mouse_wheel_movement
value_in_axis_range(single_axis, total_mouse_wheel_movement)
}
}
}
Expand Down Expand Up @@ -431,9 +446,9 @@ impl<'a> InputStreams<'a> {
left,
right,
}) => {
let x = self.input_value(&UserInput::Single(*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))
let y = self.input_value(&UserInput::Single(*up)).abs()
- self.input_value(&UserInput::Single(*down)).abs();
Some(DualAxisData::new(x, y))
}
Expand Down

0 comments on commit 8e655b7

Please sign in to comment.