Skip to content

Commit

Permalink
feat(input): Allow movement / menu nav with dpad and joystick (#991)
Browse files Browse the repository at this point in the history
This adds `movement_alt` control bindings that default to dpad on
gamepad and None on keyboard mappings.

This allows the dpad or joystick to be used to move player and navigate
menu. We may not want this if we choose to use dpad for other stuff
later, but for now I think this is nice as it avoids player having to
remap gamepad controls to use dpad. Menu navigation on joystick is rough
as have to flick the joystick repeatedly, dpad is a better experience
for me / this came up in feedback too.

Added a helper func to merge both the joystick and dpad input - the
`movement` binding (joystick) is preferred, but if no input it then
takes the `movement_alt` (dpad).

Had a rough time testing this as can't get xbox pro controller 2 to work
(probably macOS issue as it's got bad driver support), or gilrs issue.
And my PS5 controller crashes (is a gilrs issue) lol. but I tested on
steamdeck and this seems to work as intended.

@zicklag @erlend-sh let me know if any concerns with this
  • Loading branch information
MaxCWhitehead authored Apr 25, 2024
1 parent 2d60c4d commit 07004c0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
5 changes: 5 additions & 0 deletions assets/game.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ default_settings:
left: !AxisNegative LeftStickX
down: !AxisNegative LeftStickY
right: !AxisPositive LeftStickX
movement_alt:
up: !Button DPadUp
left: !Button DPadLeft
down: !Button DPadDown
right: !Button DPadRight
jump: !Button South
grab: !Button East
shoot: !Button West
Expand Down
4 changes: 4 additions & 0 deletions assets/locales/en-US/controls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ move-up = Move Up
move-down = Move Down
move-left = Move Left
move-right = Move Right
move-up-alt = Move Up Alt
move-down-alt = Move Down Alt
move-left-alt = Move Left Alt
move-right-alt = Move Right Alt
jump = Jump
grab-drop = Grab / Drop
shoot = Shoot
Expand Down
34 changes: 30 additions & 4 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,42 @@ impl<'a>
}
}

if let Some(left) = get_input_value(&mapping.movement.left, source) {
// helper for merging two inputs (like dpad + joystick for example) allowing multiple bindings
// for same control
let merge_inputs = |input1: &InputKind, input2: &InputKind| -> Option<f32> {
let mut out: Option<f32> = None;
if let Some(value1) = get_input_value(input1, source) {
out = Some(value1.abs());
}
if let Some(value2) = get_input_value(input2, source) {
match out {
Some(prev) if prev == 0.0 => {
// If first input is 0.0, override with second input
out = Some(value2.abs());
}
None => {
// No input from first, use second
out = Some(value2.abs());
}
// If first input is non-zero input, use it and ignore second.
Some(_) => {}
}
}

out
};

if let Some(left) = merge_inputs(&mapping.movement.left, &mapping.movement_alt.left) {
control.left = left.abs();
}
if let Some(right) = get_input_value(&mapping.movement.right, source) {
if let Some(right) = merge_inputs(&mapping.movement.right, &mapping.movement_alt.right)
{
control.right = right.abs();
}
if let Some(up) = get_input_value(&mapping.movement.up, source) {
if let Some(up) = merge_inputs(&mapping.movement.up, &mapping.movement_alt.up) {
control.up = up.abs();
}
if let Some(down) = get_input_value(&mapping.movement.down, source) {
if let Some(down) = merge_inputs(&mapping.movement.down, &mapping.movement_alt.down) {
control.down = down.abs();
}
};
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl PlayerControlMapping {
#[repr(C)]
pub struct PlayerControlSetting {
pub movement: VirtualDPad,
pub movement_alt: VirtualDPad,
pub pause: InputKind,
pub jump: InputKind,
pub grab: InputKind,
Expand Down
32 changes: 32 additions & 0 deletions src/ui/main_menu/settings/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,38 @@ pub(super) fn widget(
&mut mapping.gamepad.movement.right,
],
),
(
localization.get("move-up-alt"),
[
&mut mapping.keyboard1.movement_alt.up,
&mut mapping.keyboard2.movement_alt.up,
&mut mapping.gamepad.movement_alt.up,
],
),
(
localization.get("move-down-alt"),
[
&mut mapping.keyboard1.movement_alt.down,
&mut mapping.keyboard2.movement_alt.down,
&mut mapping.gamepad.movement_alt.down,
],
),
(
localization.get("move-left-alt"),
[
&mut mapping.keyboard1.movement_alt.left,
&mut mapping.keyboard2.movement_alt.left,
&mut mapping.gamepad.movement_alt.left,
],
),
(
localization.get("move-right-alt"),
[
&mut mapping.keyboard1.movement_alt.right,
&mut mapping.keyboard2.movement_alt.right,
&mut mapping.gamepad.movement_alt.right,
],
),
(
localization.get("jump"),
[
Expand Down

0 comments on commit 07004c0

Please sign in to comment.