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 0ab4281 commit 15efeea
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/input_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'a> InputStreams<'a> {
InputKind::SingleAxis(axis) => {
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 @@ -300,14 +300,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 @@ -317,6 +331,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 @@ -349,7 +364,7 @@ impl<'a> InputStreams<'a> {
}
}
}
total_mouse_wheel_movement
value_in_axis_range(single_axis, total_mouse_wheel_movement)
}
}
}
Expand Down Expand Up @@ -427,9 +442,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 15efeea

Please sign in to comment.