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

feat(input): Allow movement / menu nav with dpad and joystick #991

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading