indigo
indigo copied to clipboard
Add support for lone Joy-Con (and other would be controller with only one axis)
This is a bug that make the game stop working if you try to use a "lone" Joy-Con. I noticed the bug on Windows but, given that it comes from the gamepad API, I can't imagine it's only there. Worth noting: if both Joy-Con are connected as one, then everything works fine.
Steps to reproduce:
- connect only one Joy-Con to your computer
- start an indigo game
- use the joy-con (for example pressing a button or moving the stick)
Result: The game stops working because every time it tries to read the second axis, it can't because it does not exist:
Uncaught org.scalajs.linker.runtime.UndefinedBehaviorError
at $throwClassCastException (main.js:48:9)
at $uD (main.js:512:73)
at $c_Lindigo_platform_input_GamepadInputCaptureImpl$.giveGamepadState__Lindigo_shared_input_Gamepad (GamepadInputCaptureImpl.scala:71:28)
at $p_Lindigo_gameengine_GameLoop__runFrameNormal__D__D__V (GamepadInputCaptureImpl.scala:20:9)
at $p_Lindigo_gameengine_GameLoop__runFrame__D__D__V (GameLoop.scala:107:24)
at GameLoop.scala:84:29
at $c_sjsr_AnonFunction0.apply__O (AnonFunctions.scala:22:30)
at $c_sjs_js_Any$.fromFunction0__F0__sjs_js_Function0 (Any.scala:145:82)
Possible Solution:
On line 71 in the GamepadInputCaptureImpl, first check if the axes
array has length at least 3, and, if not, fill with zeroes instead.
Perhaps also add a member numberOfAxes
on the gamepad object, so that a developer can decide how to handle this fact.
Other (bigger) possible solution: Make the gamepad case class a trait that would have a common core, but various implementation depending on the type of controller.
For reference, here are the mapping from the button to the index in the buttons
array from the gamepad api:
joy-con right:
a: 0
b: 2
x: 1
y: 3
sl: 4
sr: 5
stick: 10
home: 16
+: 9
r: 8
zr: 7
joy-con left:
(directions of the cross when Joy-Con is held vertically)
left: 0
down: 1
up: 2
right: 3
sl: 4
sr: 5
zl: 6
l: 8
-: 9
stick: 10
screenshot: 16
assembled:
right stick axis: 2 and 3
left stick axis: 0 and 1
a: 1
b: 0
y: 2
x: 3
cross left: 14
cross right: 15
cross up: 12
cross down: 13
-: 8
+: 9
press right stick: 11
press left stick: 10
l: 4
zl: 6
r: 5
zr: 7
home: 16
screenshot: 17