Skip to content

Commit

Permalink
Fix Gamepad handling when only one axis is present
Browse files Browse the repository at this point in the history
... in particular, a single connected Joy-Con.

Fixes #760
  • Loading branch information
sherpal authored and davesmith00000 committed Nov 8, 2024
1 parent f358c45 commit 8762461
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ object GamepadInputCaptureImpl {
// Filter won't work here since some browsers like Chromium return `null` values in the gamepads array
gamepads.find(Option(_).exists(_.connected)) match {
case Some(gp) =>
val gameAnalogControls = {
val numberOfAxes = gp.axes.length / 2
GamepadAnalogControls(
AnalogAxis(gp.axes(0), gp.axes(1), gp.buttons(10).pressed),
if numberOfAxes >= 2 then AnalogAxis(gp.axes(2), gp.axes(3), gp.buttons(11).pressed)
else AnalogAxis.default,
numberOfAxes
)
}
new Gamepad(
connected = true,
new GamepadAnalogControls(
new AnalogAxis(gp.axes(0), gp.axes(1), gp.buttons(10).pressed),
new AnalogAxis(gp.axes(2), gp.axes(3), gp.buttons(11).pressed)
),
gameAnalogControls,
new GamepadDPad(
gp.buttons(12).pressed,
gp.buttons(13).pressed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ object GamepadDPad {
GamepadDPad(false, false, false, false)
}

final case class GamepadAnalogControls(left: AnalogAxis, right: AnalogAxis)
final case class GamepadAnalogControls(left: AnalogAxis, right: AnalogAxis, numberOfAxes: Int)
object GamepadAnalogControls {
val default: GamepadAnalogControls =
GamepadAnalogControls(AnalogAxis.default, AnalogAxis.default)
GamepadAnalogControls(AnalogAxis.default, AnalogAxis.default, 0)
}

final case class AnalogAxis(x: Double, y: Double, pressed: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ class InputStateTests extends munit.FunSuite {
true,
new GamepadAnalogControls(
new AnalogAxis(-1.0, -1.0, false),
new AnalogAxis(0.5, 0.0, true)
new AnalogAxis(0.5, 0.0, true),
2
),
new GamepadDPad(true, false, true, false),
new GamepadButtons(
Expand Down

0 comments on commit 8762461

Please sign in to comment.