Namespace: CB_Controllers

CB_Controllers

Static class to manage different controllers (gamepads, joysticks, remotes, etc.). It will return itself if it is tried to be instantiated. It uses gamepad-plus.

Source:
To Do:
  • Find a way to do button mapping (so we could use names as LEFT, RIGHT, etc.) and normalization for most web clients and gamepads (without forgetting proprietary ones as WII U, etc.).
  • Consider using always a numeric index instead of the ID of the gamepad (array instead of object), which could be a string, for CB_Controllers._gamepads (for optimization purposes).
  • Consider modifying CB_Controllers.isButtonDown function or add a new one to check whether any button (or axes) is down or not.

Methods


<static> getAxes( [gamepadId] [, gamepadIdNumeric] [, caseSensitive] [, avoidProprietary]) → {array}

Gets an array with information about the status of the axes of a given gamepad (or from all if none is given).

Parameters:
Name Type Argument Default Description
gamepadId integer | string <optional>
""

If an empty string is given, all gamepads available will be checked. Otherwise, it will only check the gamepad with the given ID or index (if any). When more than one gamepad is checked, the first axes values found which are not zero (0) will have priority.

gamepadIdNumeric boolean <optional>
false|true

If the "gamepadId" parameter is an empty string, this parameter will be ignored. Otherwise, if the "gamepadId" parameter is not empty and this parameter is set to true, the gamepadId parameter will be converted to integer using "parseInt" internally. By default, this parameter is false unless the given "gamepadId" is numeric.

caseSensitive boolean <optional>
false

Defines whether the "gamepadId" given should match the ID of the gamepad being case sensitive or not. This parameter will only be considered when the "gamepadIdNumeric" parameter is set to false.

avoidProprietary boolean <optional>
CB_Configuration.CrossBase.CB_Controllers_avoidProprietary_DEFAULT

If set to true, proprietary gamepads will not be checked (performance will be faster).

Source:
Returns:

Returns an array with information about the status of the axes of a given gamepad (or from all if none is given).

Type
array

<static> getAxesDown( [gamepadId] [, minimumValue] [, maximumValue] [, gamepadIdNumeric] [, caseSensitive] [, avoidProprietary]) → {Object}

Gets an array with the axes pressed of a given gamepad (or from all if none is given). If more than one gamepad is pressing the same axis, it will return the value of the first one found.

Parameters:
Name Type Argument Default Description
gamepadId integer | string <optional>
""

If an empty string is given, all gamepads available will be checked. Otherwise, it will only check the gamepad with the given ID or index (if any).

minimumValue float <optional>
0.5

The minimum value (included) that the checked axis or axes must have to consider them to be pressed.

maximumValue float <optional>
1|-0.5

The maximum value (included) that the checked axis or axes must have to consider them to be pressed. The default value will be -0.5 if the given "minimumValue" is negative or zero (0) or it will be 1 otherwise.

gamepadIdNumeric boolean <optional>
false|true

If the "gamepadId" parameter is an empty string, this parameter will be ignored. Otherwise, if the "gamepadId" parameter is not empty and this parameter is set to true, the gamepadId parameter will be converted to integer using "parseInt" internally. By default, this parameter is false unless the given "gamepadId" is numeric.

caseSensitive boolean <optional>
false

Defines whether the "gamepadId" given should match the ID of the gamepad being case sensitive or not. This parameter will only be considered when the "gamepadIdNumeric" parameter is set to false.

avoidProprietary boolean <optional>
CB_Configuration.CrossBase.CB_Controllers_avoidProprietary_DEFAULT

If set to true, proprietary gamepads will not be checked (performance will be faster).

Source:
Returns:

Returns an array with the axes pressed of a given gamepad (or from all if none is given). If more than one gamepad is pressing the same axis, it will return the value of the first one found.

Type
Object

<static> getButtons( [gamepadId] [, gamepadIdNumeric] [, caseSensitive] [, avoidProprietary]) → {Object}

Gets an object with all the buttons of a given gamepad (or from all if none is given).

Parameters:
Name Type Argument Default Description
gamepadId integer | string <optional>
""

If an empty string is given, all gamepads available will be checked. Otherwise, it will only check the gamepad with the given ID or index (if any).

gamepadIdNumeric boolean <optional>
false|true

If the "gamepadId" parameter is an empty string, this parameter will be ignored. Otherwise, if the "gamepadId" parameter is not empty and this parameter is set to true, the gamepadId parameter will be converted to integer using "parseInt" internally. By default, this parameter is false unless the given "gamepadId" is numeric.

caseSensitive boolean <optional>
false

Defines whether the "gamepadId" given should match the ID of the gamepad being case sensitive or not. This parameter will only be considered when the "gamepadIdNumeric" parameter is set to false.

avoidProprietary boolean <optional>
CB_Configuration.CrossBase.CB_Controllers_avoidProprietary_DEFAULT

If set to true, proprietary gamepads will not be checked (performance will be faster).

Source:
Returns:

Returns an object with all the buttons of a given gamepad (or from all if none is given). The indexes will be the button code and the values will be an object with information about the button (with properties such as "pressed", "value", etc.).

Type
Object

<static> getButtonsDown( [gamepadId] [, gamepadIdNumeric] [, caseSensitive] [, avoidProprietary]) → {Object}

Gets an object with the buttons pressed of a given gamepad (or from all if none is given).

Parameters:
Name Type Argument Default Description
gamepadId integer | string <optional>
""

If an empty string is given, all gamepads available will be checked. Otherwise, it will only check the gamepad with the given ID or index (if any).

gamepadIdNumeric boolean <optional>
false|true

If the "gamepadId" parameter is an empty string, this parameter will be ignored. Otherwise, if the "gamepadId" parameter is not empty and this parameter is set to true, the gamepadId parameter will be converted to integer using "parseInt" internally. By default, this parameter is false unless the given "gamepadId" is numeric.

caseSensitive boolean <optional>
false

Defines whether the "gamepadId" given should match the ID of the gamepad being case sensitive or not. This parameter will only be considered when the "gamepadIdNumeric" parameter is set to false.

avoidProprietary boolean <optional>
CB_Configuration.CrossBase.CB_Controllers_avoidProprietary_DEFAULT

If set to true, proprietary gamepads will not be checked (performance will be faster).

Source:
Returns:

Returns an object with the buttons pressed of a given gamepad (or from all if none is given).

Type
Object

<static> getGamePads( [avoidProprietary]) → {Object}

Returns an object with the current gamepad objects, if any (using the GamePad API for non-proprietary gamepads).

Parameters:
Name Type Argument Default Description
avoidProprietary boolean <optional>
CB_Configuration.CrossBase.CB_Controllers_avoidProprietary_DEFAULT

If set to true, proprietary gamepads which do not use the GamePad API (as WII, WI U gamepads, etc.) will not be included in the returning object and it will only include the ones detected by the GamePad API. Otherwise, if set to true, the returning object will contain one property called "standard" which will contain an object with the "gamepads" property with the gamepad objects that use the GamePad API and possibly different properties (one per each proprietary system) and inside of them an object with the "gamepads" property and maybe also other properties for other kind of devices which will include the status objects of those devices for each proprietary system. For gamepads that support the GamePad API, the indexes will be the identifier for each gamepad and the value its gamepad object. For proprietary gamepads, it will contain the returning value of the "getGamePads" function of each proprietary API.

Source:
To Do:
  • The proprietary ones should return better-normalized values to simulate the GamePad API.
Returns:

Returns an object with the current gamepad objects, if any (using the GamePad API for non-proprietary gamepads).

Type
Object

<static> isAxisDown(axesNumbers [, gamepadId] [, minimumValue] [, maximumValue] [, allPressed] [, gamepadIdNumeric] [, caseSensitive] [, avoidProprietary]) → {boolean}

Returns whether a given axis or axes are being pressed or not (accepts one index or more than one with an array).

Parameters:
Name Type Argument Default Description
axesNumbers integer | array

Number with the index of the axis or array of numbers with the indexes of the axes that we want to check.

gamepadId integer | string <optional>
""

If an empty string is given, all gamepads available will be checked. Otherwise, it will only check the gamepad with the given ID or index (if any).

minimumValue float <optional>
0.5

The minimum value (included) that the checked axis or axes must have to consider them to be pressed.

maximumValue float <optional>
1|-0.5

The maximum value (included) that the checked axis or axes must have to consider them to be pressed. The default value will be -0.5 if the given "minimumValue" is negative or zero (0) or it will be 1 otherwise.

allPressed boolean <optional>
false

If set to true, the function will return true only in the case that all given axes are being pressed. In the case that this parameter is set to true and the "gamepadId" parameter is set to an empty string, it will not have in mind whether the axes are pressed by a unique gamepad or by different ones as long as all axes are pressed.

gamepadIdNumeric boolean <optional>
false|true

If the "gamepadId" parameter is an empty string, this parameter will be ignored. Otherwise, if the "gamepadId" parameter is not empty and this parameter is set to true, the gamepadId parameter will be converted to integer using "parseInt" internally. By default, this parameter is false unless the given "gamepadId" is numeric.

caseSensitive boolean <optional>
false

Defines whether the "gamepadId" given should match the ID of the gamepad being case sensitive or not. This parameter will only be considered when the "gamepadIdNumeric" parameter is set to false.

avoidProprietary boolean <optional>
CB_Configuration.CrossBase.CB_Controllers_avoidProprietary_DEFAULT

If set to true, proprietary gamepads will not be checked (performance will be faster).

Source:
To Do:
  • Make it compatible with proprietary gamepads (if possible).
Returns:

If the "allPressed" parameter is false, returns true if the given code or any of the given codes is pressed. If the "allPressed" parameter is set to true, it will return true only in the case that all given axes are being pressed. In the case that the "allPressed" parameter is set to true and the gamepadId parameter is set to an empty string, it will not have in mind whether the axes are pressed by a unique gamepad or by different ones as long as all axes are pressed.

Type
boolean

<static> isButtonDown(buttonCodes [, gamepadId] [, allPressed] [, gamepadIdNumeric] [, caseSensitive] [, avoidProprietary]) → {boolean}

Returns whether a given button or buttons are being pressed or not (accepts one key code or more than one with an array).

Parameters:
Name Type Argument Default Description
buttonCodes integer | array

Number with the button code or array of numbers with the button codes that we want to check.

gamepadId integer | string <optional>
""

If an empty string is given, all gamepads available will be checked. Otherwise, it will only check the gamepad with the given ID or index (if any).

allPressed boolean <optional>
false

If set to true, the function will return true only in the case that all given buttons are being pressed. In the case that this parameter is set to true and the "gamepadId" parameter is set to an empty string, it will not have in mind whether the buttons are pressed by a unique gamepad or by different ones as long as all buttons are pressed.

gamepadIdNumeric boolean <optional>
false|true

If the "gamepadId" parameter is an empty string, this parameter will be ignored. Otherwise, if the "gamepadId" parameter is not empty and this parameter is set to true, the gamepadId parameter will be converted to integer using "parseInt" internally. By default, this parameter is false unless the given "gamepadId" is numeric.

caseSensitive boolean <optional>
false

Defines whether the "gamepadId" given should match the ID of the gamepad being case sensitive or not. This parameter will only be considered when the "gamepadIdNumeric" parameter is set to false.

avoidProprietary boolean <optional>
CB_Configuration.CrossBase.CB_Controllers_avoidProprietary_DEFAULT

If set to true, proprietary gamepads will not be checked (performance will be faster).

Source:
To Do:
  • Make it compatible with proprietary gamepads (if possible).
Returns:

If the "allPressed" parameter is false, returns true if the given code or any of the given codes is pressed. If the "allPressed" parameter is set to true, it will return true only in the case that all given buttons are being pressed. In the case that the "allPressed" parameter is set to true and the gamepadId parameter is set to an empty string, it will not have in mind whether the buttons are pressed by a unique gamepad or by different ones as long as all buttons are pressed.

Type
boolean

<static> onConnect(callbackFunction [, keepOldFunction])

Sets a function to execute when a gamepad is connected ("onGamepadConnected" event is fired) or removes it. More information: Gamepad API.

Parameters:
Name Type Argument Default Description
callbackFunction function | null

The function (event listener) that we want to execute when the event is fired. The first parameter received for this function will be the gamepad object affected and the second parameter will be an object containing all current gamepads objects (properties will be the index of each gamepad). If a null value is used, the event will be removed.

keepOldFunction boolean <optional>
true

Defines whether we want to keep any possible previous event listener for the same target and event name or not.

Source:
To Do:
  • Make it compatible with proprietary ones (if possible).

<static> onDisconnect(callbackFunction [, keepOldFunction])

Sets a function to execute when a gamepad is disconnected ("onGamepadDisconnected" event is fired) or removes it. More information: Gamepad API.

Parameters:
Name Type Argument Default Description
callbackFunction function | null

The function (event listener) that we want to execute when the event is fired. The first parameter received for this function will be the gamepad object affected and the second parameter will be an object containing all current gamepads objects (properties will be the index of each gamepad). If a null value is used, the event will be removed.

keepOldFunction boolean <optional>
true

Defines whether we want to keep any possible previous event listener for the same target and event name or not.

Source:
To Do:
  • Make it compatible with proprietary ones (if possible).