Page 1 of 1

love-joycon - Switch controller support for LÖVE

Posted: Tue Mar 06, 2018 4:59 am
by MarkSill
love-joycon
love-joycon is a smallish library that adds Joy-Con and Pro Controller support.

The library is fairly simple to use (unless you're supporting Windows, which you probably should be). Just

Code: Select all

joycon = require("joycon")
and call the joycon.joystick* functions. If you support Windows, you'll have to register Switch controllers manually. The demo includes a simple example of how to do this.

API Reference

Code: Select all

joycon.joystickpressed(joystick, button)
This is one of the required functions. Call this from love.joystickpressed and you should be good to go. The function returns true if it did anything.

Code: Select all

joycon.joystickreleased(joystick, button)
This is the second required function. Call this from love.joystickreleased and you're done. The function returns true if it did anything.

Code: Select all

joycon.joystickhat(joystick, hat, direction)
The third required function that should be called from love.joystickhat. The function returns true if it did anything. This function is currently used to update the Joy-Con joystick axes, since their set up as hats in LÖVE.

Code: Select all

joycon.joystickaxis(joystick, axis, value)
The fourth required function isn't technically required. It's really just there in case the Joy-Cons' joystick axes are fixed in the future.

Code: Select all

joycon.registerController(joystick, name)
This function registers a joystick as a Switch controller. The type of controller is determined using the name. If the name is Pro Controller, then the joystick is registered as a Pro Controller. If the name is Joy-Con (R) or Joy-Con (L) the Joy-Con is registered according to which Joy-Con it is. If the name doesn't match any of these, it's ignored.

This function should only be required for Windows.

Code: Select all

joycon.joystickadded(joystick)
This is just a helper function that attempts to register the joystick automatically. On Windows, it doesn't do anything since automatic registration is broken on Windows.

Code: Select all

joycon.merge(joystick1, joystick2)
Merges two Joy-Cons into one. The two joysticks should be the left and right Joy-Cons. The order they're put in doesn't matter.

It is probably possible to merge two Joy-Cons of the same type, but it would almost certainly break a lot of the code.

Code: Select all

joycon.separate(joystick)
Separates merged Joy-Cons. Only one of the two merged joysticks needs to be passed to this function.

Code: Select all

joycon.isJoyCon(joystick)
Returns true if the given joystick is registered as a Joy-Con.

Code: Select all

joycon.isSwitchController(joystick)
Returns true if the given joystick is registered as any type of Switch controller.

Code: Select all

joycon.isProController(joystick)
Returns true if the given joystick is registered as a Pro Controller.

Code: Select all

joycon.isLeftJoyCon(joystick)
Returns true if the given joystick is a left Joy-Con.

Bugs
  • Can't detect Switch controllers automatically on Windows. This appears to be a problem with SDL(?). The library still works if you manually register the controllers. The demo has an example of how you could do that.
  • The Joy-Cons' joysticks only have eight degrees of motion. It seems SDL thinks their joysticks are d-pads and so they're only available as hats in LÖVE.
Links

Re: love-joycon - Switch controller support for LÖVE

Posted: Tue Mar 06, 2018 2:28 pm
by raidho36
Can you rewrite it so it works exactly like normal joystick API? So that, you know, joycons are automatically supported, without you having to explicitly write its own code for them? Imagine you had to write its own code for XBox controller, PS4 controller, all the third party controllers, etc.

Re: love-joycon - Switch controller support for LÖVE

Posted: Tue Mar 06, 2018 10:22 pm
by MarkSill
raidho36 wrote:
Tue Mar 06, 2018 2:28 pm
Can you rewrite it so it works exactly like normal joystick API? So that, you know, joycons are automatically supported, without you having to explicitly write its own code for them? Imagine you had to write its own code for XBox controller, PS4 controller, all the third party controllers, etc.
The Joy-Cons work with the joystick API, but this is for getting them to work with the more useful gamepad functions. The Pro Controller is just mapped (except for the capture button and ZR/ZL triggers). I didn't map the Joy-Cons because then the merging part wouldn't really work. When merged, I always pass the left Joy-Con's joystick object to the gamepad functions.

Re: love-joycon - Switch controller support for LÖVE

Posted: Sun Dec 16, 2018 6:58 am
by KyleFlores1014
Can you attach an example? The demo doesnt print out anything when I press something on the joycons.