2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-09-09 02:17:13 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2018-03-30 11:03:06 +00:00
|
|
|
var EventEmitter = require('eventemitter3');
|
2017-09-09 02:17:13 +00:00
|
|
|
var Gamepad = require('./Gamepad');
|
2018-06-08 15:17:58 +00:00
|
|
|
var GetValue = require('../../utils/object/GetValue');
|
|
|
|
var InputPluginCache = require('../InputPluginCache');
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-03-19 12:43:19 +00:00
|
|
|
/**
|
|
|
|
* @typedef {object} Pad
|
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* @property {string} id - The ID of the Gamepad.
|
|
|
|
* @property {integer} index - The index of the Gamepad.
|
2018-03-27 13:59:49 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-06-08 15:17:58 +00:00
|
|
|
* The Gamepad Plugin is an input plugin that belongs to the Scene-owned Input system.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* Its role is to listen for native DOM Gamepad Events and then process them.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* You do not need to create this class directly, the Input system will create an instance of it automatically.
|
|
|
|
*
|
|
|
|
* You can access it from within a Scene using `this.input.gamepad`.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* To listen for a gamepad being connected:
|
2018-06-06 22:05:10 +00:00
|
|
|
*
|
|
|
|
* ```javascript
|
2018-06-08 15:17:58 +00:00
|
|
|
* this.input.gamepad.once('connected', function (pad) {
|
|
|
|
* // 'pad' is a reference to the gamepad that was just connected
|
|
|
|
* });
|
2018-06-06 22:05:10 +00:00
|
|
|
* ```
|
2018-02-07 15:27:21 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* Note that the browser may require you to press a button on a gamepad before it will allow you to access it,
|
|
|
|
* this is for security reasons. However, it may also trust the page already, in which case you won't get the
|
|
|
|
* 'connected' event and instead should check `GamepadPlugin.total` to see if it thinks there are any gamepads
|
|
|
|
* already connected.
|
|
|
|
*
|
|
|
|
* Once you have received the connected event, or polled the gamepads and found them enabled, you can access
|
|
|
|
* them via the built-in properties `GamepadPlugin.pad1` to `pad4`, for up to 4 game pads. With a reference
|
|
|
|
* to the gamepads you can poll its buttons and axis sticks. See the properties and methods available on
|
|
|
|
* the `Gamepad` class for more details.
|
|
|
|
*
|
|
|
|
* For more information about Gamepad support in browsers see the following resources:
|
|
|
|
*
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
|
|
|
|
* https://www.smashingmagazine.com/2015/11/gamepad-api-in-web-games/
|
|
|
|
* http://html5gamepad.com/
|
|
|
|
*
|
|
|
|
* @class GamepadPlugin
|
2018-03-30 11:03:06 +00:00
|
|
|
* @extends Phaser.Events.EventEmitter
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Input.Gamepad
|
2018-02-07 15:27:21 +00:00
|
|
|
* @constructor
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-02-07 15:27:21 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @param {Phaser.Input.InputPlugin} sceneInputPlugin - A reference to the Scene Input Plugin that the KeyboardPlugin belongs to.
|
2018-02-07 15:27:21 +00:00
|
|
|
*/
|
2018-06-08 15:17:58 +00:00
|
|
|
var GamepadPlugin = new Class({
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-03-30 11:03:06 +00:00
|
|
|
Extends: EventEmitter,
|
|
|
|
|
2017-09-09 02:17:13 +00:00
|
|
|
initialize:
|
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
function GamepadPlugin (sceneInputPlugin)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-03-30 11:03:06 +00:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-08 15:17:58 +00:00
|
|
|
* A reference to the Scene that this Input Plugin is responsible for.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
this.scene = sceneInputPlugin.scene;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the Scene Systems Settings.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#settings
|
|
|
|
* @type {Phaser.Scenes.Settings.Object}
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
this.settings = this.scene.sys.settings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the Scene Input Plugin that created this Keyboard Plugin.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#sceneInputPlugin
|
|
|
|
* @type {Phaser.Input.InputPlugin}
|
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2018-06-08 15:17:58 +00:00
|
|
|
this.sceneInputPlugin = sceneInputPlugin;
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* A boolean that controls if the Gamepad Manager is enabled or not.
|
|
|
|
* Can be toggled on the fly.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#enabled
|
2018-02-13 01:13:12 +00:00
|
|
|
* @type {boolean}
|
2018-06-06 22:05:10 +00:00
|
|
|
* @default true
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2018-06-06 22:05:10 +00:00
|
|
|
this.enabled = true;
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* The Gamepad Event target, as defined in the Game Config.
|
|
|
|
* Typically the browser window, but can be any interactive DOM element.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#target
|
2018-06-06 22:05:10 +00:00
|
|
|
* @type {any}
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.target;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* An array of the connected Gamepads.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#gamepads
|
2018-03-19 12:43:19 +00:00
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad[]}
|
2018-01-26 06:55:15 +00:00
|
|
|
* @default []
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.gamepads = [];
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* An internal event queue.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#queue
|
2018-03-19 12:43:19 +00:00
|
|
|
* @type {GamepadEvent[]}
|
2018-06-06 22:05:10 +00:00
|
|
|
* @private
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.queue = [];
|
2018-05-25 18:27:26 +00:00
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
/**
|
|
|
|
* Internal event handler.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#onGamepadHandler
|
|
|
|
* @type {function}
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
this.onGamepadHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Gamepad reference.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#_pad1
|
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
2018-06-06 22:05:10 +00:00
|
|
|
this._pad1;
|
2018-06-08 15:17:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Gamepad reference.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#_pad2
|
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
2018-06-06 22:05:10 +00:00
|
|
|
this._pad2;
|
2018-06-08 15:17:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Gamepad reference.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#_pad3
|
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
2018-06-06 22:05:10 +00:00
|
|
|
this._pad3;
|
2018-06-08 15:17:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Gamepad reference.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#_pad4
|
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
2018-06-06 22:05:10 +00:00
|
|
|
this._pad4;
|
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
sceneInputPlugin.pluginEvents.once('boot', this.boot, this);
|
|
|
|
sceneInputPlugin.pluginEvents.on('start', this.start, this);
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-08 15:17:58 +00:00
|
|
|
* This method is called automatically, only once, when the Scene is first created.
|
|
|
|
* Do not invoke it directly.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#boot
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
2018-06-08 15:17:58 +00:00
|
|
|
var game = this.scene.sys.game;
|
|
|
|
var settings = this.settings.input;
|
|
|
|
var config = game.config;
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
this.enabled = GetValue(settings, 'gamepad', config.inputGamepad) && game.device.input.gamepads;
|
|
|
|
this.target = GetValue(settings, 'gamepad.target', config.inputGamepadEventTarget);
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
this.sceneInputPlugin.pluginEvents.once('destroy', this.destroy, this);
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-08 15:17:58 +00:00
|
|
|
* This method is called automatically by the Scene when it is starting up.
|
|
|
|
* It is responsible for creating local systems, properties and listening for Scene events.
|
|
|
|
* Do not invoke it directly.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#start
|
|
|
|
* @private
|
2018-06-06 22:05:10 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2018-06-08 15:17:58 +00:00
|
|
|
start: function ()
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-06-08 15:17:58 +00:00
|
|
|
if (this.enabled)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-06-08 15:17:58 +00:00
|
|
|
this.startListeners();
|
2018-06-06 22:05:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
this.sceneInputPlugin.pluginEvents.once('shutdown', this.shutdown, this);
|
2018-06-06 22:05:10 +00:00
|
|
|
},
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
/**
|
2018-06-08 15:17:58 +00:00
|
|
|
* Checks to see if both this plugin and the Scene to which it belongs is active.
|
2018-06-06 22:05:10 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#isActive
|
2018-06-06 22:05:10 +00:00
|
|
|
* @since 3.10.0
|
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @return {boolean} `true` if the plugin and the Scene it belongs to is active.
|
2018-06-06 22:05:10 +00:00
|
|
|
*/
|
2018-06-08 15:17:58 +00:00
|
|
|
isActive: function ()
|
2018-06-06 22:05:10 +00:00
|
|
|
{
|
2018-06-08 15:17:58 +00:00
|
|
|
return (this.enabled && this.scene.sys.isActive());
|
2018-06-06 22:05:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the Gamepad Event listeners running.
|
|
|
|
* This is called automatically and does not need to be manually invoked.
|
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#startListeners
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
2018-06-06 22:05:10 +00:00
|
|
|
*/
|
|
|
|
startListeners: function ()
|
|
|
|
{
|
2018-06-08 15:17:58 +00:00
|
|
|
var _this = this;
|
2018-01-26 06:55:15 +00:00
|
|
|
var target = this.target;
|
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
var handler = function (event)
|
|
|
|
{
|
|
|
|
// console.log(event);
|
|
|
|
|
|
|
|
if (event.defaultPrevented || !_this.isActive())
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_this.refreshPads();
|
|
|
|
|
|
|
|
_this.queue.push(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onGamepadHandler = handler;
|
|
|
|
|
|
|
|
target.addEventListener('gamepadconnected', handler, false);
|
|
|
|
target.addEventListener('gamepaddisconnected', handler, false);
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
// FF also supports gamepadbuttondown, gamepadbuttonup and gamepadaxismove but
|
|
|
|
// nothing else does, and we can get those values via the gamepads anyway, so we will
|
|
|
|
// until more browsers support this
|
2018-05-25 18:27:26 +00:00
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
// Finally, listen for an update event from the Input Plugin
|
|
|
|
this.sceneInputPlugin.pluginEvents.on('update', this.update, this);
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* Stops the Gamepad Event listeners.
|
|
|
|
* This is called automatically and does not need to be manually invoked.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#stopListeners
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
stopListeners: function ()
|
|
|
|
{
|
2018-06-08 15:17:58 +00:00
|
|
|
this.target.removeEventListener('gamepadconnected', this.onGamepadHandler);
|
|
|
|
this.target.removeEventListener('gamepaddisconnected', this.onGamepadHandler);
|
2018-05-25 18:27:26 +00:00
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
this.sceneInputPlugin.pluginEvents.off('update', this.update);
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* Disconnects all current Gamepads.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#disconnectAll
|
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
disconnectAll: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.gamepads.length; i++)
|
|
|
|
{
|
|
|
|
this.gamepads.connected = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* Refreshes the list of connected Gamepads.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* This is called automatically when a gamepad is connected or disconnected,
|
|
|
|
* and during the update loop.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#refreshPads
|
2018-06-06 22:05:10 +00:00
|
|
|
* @private
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2018-06-06 22:05:10 +00:00
|
|
|
refreshPads: function ()
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
var connectedPads = navigator.getGamepads();
|
|
|
|
|
|
|
|
if (!connectedPads)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
|
|
|
this.disconnectAll();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
var currentPads = this.gamepads;
|
|
|
|
|
|
|
|
for (var i = 0; i < connectedPads.length; i++)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
var livePad = connectedPads[i];
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
// Because sometimes they're null (yes, really)
|
|
|
|
if (!livePad)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
var id = livePad.id;
|
|
|
|
var index = livePad.index;
|
|
|
|
var currentPad = currentPads[index];
|
|
|
|
|
|
|
|
if (!currentPad)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
// A new Gamepad, not currently stored locally
|
2018-06-07 15:04:27 +00:00
|
|
|
var newPad = new Gamepad(this, livePad);
|
2018-06-06 22:05:10 +00:00
|
|
|
|
|
|
|
currentPads[index] = newPad;
|
|
|
|
|
|
|
|
if (!this._pad1)
|
|
|
|
{
|
|
|
|
this._pad1 = newPad;
|
|
|
|
}
|
|
|
|
else if (!this._pad2)
|
|
|
|
{
|
|
|
|
this._pad2 = newPad;
|
|
|
|
}
|
|
|
|
else if (!this._pad3)
|
|
|
|
{
|
|
|
|
this._pad3 = newPad;
|
|
|
|
}
|
|
|
|
else if (!this._pad4)
|
|
|
|
{
|
|
|
|
this._pad4 = newPad;
|
|
|
|
}
|
2017-09-09 02:17:13 +00:00
|
|
|
}
|
2018-06-06 22:05:10 +00:00
|
|
|
else if (currentPad.id !== id)
|
|
|
|
{
|
|
|
|
// A new Gamepad with a different vendor string, but it has got the same index as an old one
|
|
|
|
currentPad.destroy();
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
currentPads[index] = new Gamepad(this, livePad);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If neither of these, it's a pad we've already got, so update it
|
|
|
|
currentPad.update(livePad);
|
|
|
|
}
|
2017-09-09 02:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* Returns an array of all currently connected Gamepads.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#getAll
|
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* @return {Phaser.Input.Gamepad.Gamepad[]} An array of all currently connected Gamepads.
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
getAll: function ()
|
|
|
|
{
|
|
|
|
var out = [];
|
2018-06-06 22:05:10 +00:00
|
|
|
var pads = this.gamepads;
|
2017-09-11 00:28:09 +00:00
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
for (var i = 0; i < pads.length; i++)
|
2017-09-11 00:28:09 +00:00
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
if (pads[i])
|
2017-09-11 00:28:09 +00:00
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
out.push(pads[i]);
|
2017-09-11 00:28:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* Looks-up a single Gamepad based on the given index value.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#getPad
|
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* @param {number} index - The index of the Gamepad to get.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* @return {Phaser.Input.Gamepad.Gamepad} The Gamepad matching the given index, or undefined if none were found.
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
getPad: function (index)
|
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
var pads = this.gamepads;
|
|
|
|
|
|
|
|
for (var i = 0; i < pads.length; i++)
|
2017-09-11 00:28:09 +00:00
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
if (pads[i] && pads[i].index === index)
|
2017-09-11 00:28:09 +00:00
|
|
|
{
|
2018-06-06 22:05:10 +00:00
|
|
|
return pads[i];
|
2017-09-11 00:28:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:05:10 +00:00
|
|
|
* The internal update loop. Refreshes all connected gamepads and processes their events.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* Called automatically by the Input Manager, invoked from the Game step.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#update
|
2018-06-06 22:05:10 +00:00
|
|
|
* @private
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
update: function ()
|
|
|
|
{
|
|
|
|
if (!this.enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
this.refreshPads();
|
2017-09-09 02:17:13 +00:00
|
|
|
|
|
|
|
var len = this.queue.length;
|
|
|
|
|
|
|
|
if (len === 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var queue = this.queue.splice(0, len);
|
|
|
|
|
|
|
|
// Process the event queue, dispatching all of the events that have stored up
|
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
var event = queue[i];
|
2018-06-08 15:17:58 +00:00
|
|
|
var pad = this.getPad(event.gamepad.index);
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
if (event.type === 'gamepadconnected')
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-06-08 15:17:58 +00:00
|
|
|
this.emit('connected', pad, event);
|
|
|
|
}
|
|
|
|
else if (event.type === 'gamepaddisconnected')
|
|
|
|
{
|
|
|
|
this.emit('disconnected', pad, event);
|
2017-09-09 02:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-11 00:28:09 +00:00
|
|
|
},
|
|
|
|
|
2018-01-31 03:38:10 +00:00
|
|
|
/**
|
2018-06-08 15:17:58 +00:00
|
|
|
* Shuts the Gamepad Plugin down.
|
|
|
|
* All this does is remove any listeners bound to it.
|
2018-01-31 03:38:10 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#shutdown
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
2018-01-31 03:38:10 +00:00
|
|
|
*/
|
2018-06-08 15:17:58 +00:00
|
|
|
shutdown: function ()
|
2018-01-31 03:38:10 +00:00
|
|
|
{
|
|
|
|
this.stopListeners();
|
2018-06-08 15:17:58 +00:00
|
|
|
|
2018-01-31 03:38:10 +00:00
|
|
|
this.disconnectAll();
|
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
this.removeAllListeners();
|
2018-06-08 15:17:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this Gamepad Plugin, disconnecting all Gamepads and releasing internal references.
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadPlugin#destroy
|
|
|
|
* @private
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.shutdown();
|
2018-06-06 22:05:10 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < this.gamepads.length; i++)
|
|
|
|
{
|
|
|
|
if (this.gamepads[i])
|
|
|
|
{
|
|
|
|
this.gamepads[i].destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 03:38:10 +00:00
|
|
|
this.gamepads = [];
|
2018-06-06 22:05:10 +00:00
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
this.scene = null;
|
|
|
|
this.settings = null;
|
|
|
|
this.sceneInputPlugin = null;
|
2018-06-06 22:05:10 +00:00
|
|
|
this.target = null;
|
2018-01-31 03:38:10 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* The total number of connected game pads.
|
2018-03-19 12:43:19 +00:00
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#total
|
2018-06-06 22:05:10 +00:00
|
|
|
* @type {integer}
|
2018-06-08 15:17:58 +00:00
|
|
|
* @since 3.10.0
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
total: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.gamepads.length;
|
|
|
|
}
|
|
|
|
|
2018-06-06 22:05:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the first connected Gamepad.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* This will be undefined if either no pads are connected, or the browser
|
|
|
|
* has not yet issued a gamepadconnect, which can happen even if a Gamepad
|
|
|
|
* is plugged in, but hasn't yet had any buttons pressed on it.
|
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#pad1
|
2018-06-06 22:05:10 +00:00
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
pad1: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._pad1;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the second connected Gamepad.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* This will be undefined if either no pads are connected, or the browser
|
|
|
|
* has not yet issued a gamepadconnect, which can happen even if a Gamepad
|
|
|
|
* is plugged in, but hasn't yet had any buttons pressed on it.
|
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#pad2
|
2018-06-06 22:05:10 +00:00
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
pad2: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._pad2;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the third connected Gamepad.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* This will be undefined if either no pads are connected, or the browser
|
|
|
|
* has not yet issued a gamepadconnect, which can happen even if a Gamepad
|
|
|
|
* is plugged in, but hasn't yet had any buttons pressed on it.
|
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#pad3
|
2018-06-06 22:05:10 +00:00
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
pad3: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._pad3;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the fourth connected Gamepad.
|
2018-06-07 15:04:27 +00:00
|
|
|
*
|
2018-06-06 22:05:10 +00:00
|
|
|
* This will be undefined if either no pads are connected, or the browser
|
|
|
|
* has not yet issued a gamepadconnect, which can happen even if a Gamepad
|
|
|
|
* is plugged in, but hasn't yet had any buttons pressed on it.
|
|
|
|
*
|
2018-06-08 15:17:58 +00:00
|
|
|
* @name Phaser.Input.Gamepad.GamepadPlugin#pad4
|
2018-06-06 22:05:10 +00:00
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
pad4: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._pad4;
|
|
|
|
}
|
|
|
|
|
2017-09-09 02:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-06-08 15:17:58 +00:00
|
|
|
/**
|
|
|
|
* An instance of the Gamepad Plugin class, if enabled via the `input.gamepad` Scene or Game Config property.
|
|
|
|
* Use this to create access Gamepads connected to the browser and respond to gamepad buttons.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.InputPlugin#gamepad
|
|
|
|
* @type {?Phaser.Input.Gamepad.GamepadPlugin}
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
InputPluginCache.register('GamepadPlugin', GamepadPlugin, 'gamepad', 'gamepad', 'inputGamepad');
|
|
|
|
|
|
|
|
module.exports = GamepadPlugin;
|