love-joycon - Switch controller support for LÖVE

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
MarkSill
Prole
Posts: 13
Joined: Fri Mar 14, 2014 9:14 am

love-joycon - Switch controller support for LÖVE

Post 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
Attachments
love-joycon.love
The demo.
(4.66 KiB) Downloaded 261 times
Heyo, I'm MarkSill. I'm a programmer, and I like chips with salsa.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
MarkSill
Prole
Posts: 13
Joined: Fri Mar 14, 2014 9:14 am

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

Post 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.
Heyo, I'm MarkSill. I'm a programmer, and I like chips with salsa.
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

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

Post by KyleFlores1014 »

Can you attach an example? The demo doesnt print out anything when I press something on the joycons.
Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests