carldanley / node-gamepad

node-gamepad is a package for node that allows you to effortlessly interface your node applications with a variety of gamepad controllers.
82 stars 32 forks source link

Use path as identifier for controller passed to HID.HID #7

Open lukekarrys opened 10 years ago

lukekarrys commented 10 years ago

For a couple reasons:

  1. This seems like the preferred way, at least according to the node-hid docs: https://github.com/node-hid/node-hid#opening-a-device
  2. Opens up the possibility for multiple controller support, since paths are unique (at least in my tests with Retrolink SNES controllers). Although that would require a few more changes (which I think would be very cool).

I think this wouldn't require too much code after #6 since we already can look through the output from HID.devices.

I can have a PR for this sometime soon (with a multicontroller example), but wanted to make sure there was interest first.

andrew commented 10 years ago

Multiple controller support would be :sparkles:, I think what you're describing is basically how the https://github.com/andrew/node-xbox-controller library works as well if you need another example.

lukekarrys commented 10 years ago

Thanks @andrew, and yeah this is pretty close to what I was thinking. As for proposed multicontroller support, here's what I was thinking for possible syntax:

Option1: Have the user manage it themselves

var snes1 = new GamePad('snes').connect();
var snes2 = new GamePad('snes', { usedPaths: snes1.getPath() }).connect();

Option 2: pass an array to the constructor

var snes = new GamePad(['snes', 'snes']);
snes.connect();
snes[0].on('a:press', console.log);
snes[1].on('a:press', console.log);

Option 2 would require a lot more changes under the hood, but then node-gamepad could keep track of what paths have already been used. Or tell me both of these are bad, and there's something better :smile:

carldanley commented 10 years ago

Great idea for multi-controller support! I'm not sold on the idea of passing an array in simply because they really should be separate objects.

Just thinking through a few different options here:

Option A:

Have GamePad detect any controllers that are found and store meta information about each controller...

var detectedControllers = GamePad.detectControllers();
var playerOne = new GamePad( detectedControllers.snes[0].path );
var playerTwo = new GamePad( detectedControllers.snes[1].path );
var ps3 = new GamePad( detectedControllers.ps3.path );

Option B:

Do they really need to specify which controller they want to use? This feature would be really cool but it also scares me (potentially something extra we'd have to support).

var controllers = GamePad.loadControllers();
controllers.snes[0].on( 'connected', callback );
controllers.snes[1].on( 'connected', callback );
controllers.ps3.on( 'connected', callback );
lukekarrys commented 10 years ago

I like Option B. It is something extra to support, but I think it'd very useful as a kind of auto-setup feature. Any other opinions/options? I might try to take a stab at this in the coming week.

mikeytdan commented 10 years ago

Any update on this? I would like support for multiple controllers.

carldanley commented 10 years ago

hey @mikeytdan: yes! when I get back from Boston, I'll test the code I have against multiple controllers and PR it for others to test. I only have a PS4 controller with me atm.

mikeytdan commented 10 years ago

Thanks! I can't wait to hear how the testing goes.

lukekarrys commented 10 years ago

@carldanley Let me know if you need help testing or anything. I have multiple controllers (2 SNES and 2 N64) and would be happy to help. I've had a todo task in my "later" folder for this for 2 months :grin:

carldanley commented 10 years ago

@lukekarrys I'll def. need your help with testing the double controllers :)