Skip to content

Followup: TODO for gamepad support #724

@parasyte

Description

@parasyte

Follows #34

  • Add a gamepad tester to examples; for validating mappings
  • Remap axes[4] and axes[5] extension (L2 and R2 analog triggers) to buttons[me.GAMEPAD.L2].value and buttons[me.GAMEPAD.R2].value
  • Add a new API for reading and writing action states
    • me.input.isActionSet(action) (was me.input.isKeyPressed)
    • me.input.getActionState(action) (was me.input.keyState)
    • me.input.getActionValue(action) (NEW; returns action analog value)
    • me.input.getAction(action) (NEW; returns action analog value, digital state, and digital edge)
    • me.input.setAction(action, state, value) (NEW; sets action analog value and digital state)
  • Add a new method for custom gamepad mapping, with a normalization function
    • me.input.setGamepadMapping(id, mapping)
    • The function signature has been changed to better fit the needs of remapping axes to button values; see below.
    • mapping is a hash table containing the following keys:
      • {Number[]} mapping.axes Standard analog control stick axis locations
      • {Number[]} mapping.buttons Standard digital button locations
      • {Number[]} [mapping.analog] Analog axis locations for buttons. See: https://developer.mozilla.org/en-US/docs/Web/API/GamepadButton
      • {Function} [mapping.normalize_fn] will be called on an unmapped axis or button with the following parameters:
        • {Number} value The raw value read from the gamepad driver
        • {Number} axis The axis index from the standard mapping, or -1 if not an axis
        • {Number} button The button index from the standard mapping, or -1 if not a button
      • normalize_fn must return a normalized value in range [-1.0..1.0], or 0.0 if the axis is unknown.

mapping.analog is specially for mapping analog buttons or pressure-sensitive buttons from an axis location to a button location. This is for things like the L2 and R2 analog triggers, and pressure sensitive D-pad and face buttons on a PS3 controller. Here's how it might look for remapping L2 and R2 triggers on a wired XBox 360 controller:

{
    "axes" : [ 0, 1, 3, 4 ],
    "buttons" : [ 11, 12, 13, 14, 8, 9, -1, -1, 5, 4, 6, 7, 0, 1, 2, 3, 10 ],
    "analog" : [ -1, -1, -1, -1, -1, -1, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1 ]
}

This will fill in that gap in the buttons array during remapping. A little extra code is necessary to combine the axis and the button to produce a meaningful result.


Some mapping test reports:

Chrome (45+) :

  • PS3 Gamepad
  • PS4 Gamepad
  • XBox One Gamepad
  • XBox 360 USB Gamepad
  • XBox 360 Wireless Gamepad
  • Wii Remote (not recognised by the browser)
  • Wii "Classic" Gamepad (not recognised by the browser)
  • Wii U Gamepad
  • Ouya Gamepad

Firefox (39+) :

  • PS3 Gamepad
  • PS4 Gamepad
  • XBox One Gamepad
  • XBox 360 USB Gamepad
  • XBox 360 Wireless Gamepad
  • Wii Remote (not recognised by the browser)
  • Wii "Classic" Gamepad (not recognised by the browser)
  • Wii U Gamepad
  • Ouya Gamepad

Wiimote on Mac requires a driver: https://github.com/alxn1/wjoy

I haven't mapped it out, yet. But initial testing shows a huge caveat; this driver reports the gamepad id as a string containing a MAC address (which is unique for each device). For this reason, I want to defer Wiimote support to a melonJS plugin; which is better suited to doing things like RegExp over gamepad id.


Ouya gamepad on Mac requires a driver: http://ouyaforum.com/showthread.php?3185-Ouya-controller-input-on-computer-macosx&p=78432&viewfull=1#post78432 This driver is unsigned, so requires disabling KEXT signature checking on OSX 10.10+:

$ sudo nvram boot-args=kext-dev-mode=1

After restarting, load the driver:

$ sudo kextload /System/Library/Extensions/OUYAGameController.kext

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions