2017-09-09 02:17:13 +00:00
|
|
|
// Phaser.Input.Gamepad.GamepadManager
|
|
|
|
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Gamepad = require('./Gamepad');
|
|
|
|
|
|
|
|
// 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/
|
2017-09-11 00:28:09 +00:00
|
|
|
// http://html5gamepad.com/
|
2017-09-09 02:17:13 +00:00
|
|
|
|
|
|
|
var GamepadManager = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class GamepadManager
|
|
|
|
* @memberOf Phaser.Input.Gamepad
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Input.InputManager} inputManager - [description]
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
function GamepadManager (inputManager)
|
|
|
|
{
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Input.InputManager} manager
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.manager = inputManager;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} events
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.events = inputManager.events;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {boolean} enabled
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.enabled = false;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {null} target
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.target;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {null} handler
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.handler;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {array} gamepads
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.gamepads = [];
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* Standard FIFO queue.
|
|
|
|
*
|
|
|
|
* @property {array} queue
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.queue = [];
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#boot
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
var config = this.manager.config;
|
|
|
|
|
2018-01-25 17:03:35 +00:00
|
|
|
this.enabled = config.inputGamepad && this.manager.game.device.input.gamepads;
|
2017-09-09 02:17:13 +00:00
|
|
|
|
|
|
|
this.target = window;
|
|
|
|
|
|
|
|
if (this.enabled)
|
|
|
|
{
|
|
|
|
this.startListeners();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#startListeners
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
startListeners: function ()
|
|
|
|
{
|
|
|
|
var queue = this.queue;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
var handler = function handler (event)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-01-03 16:30:51 +00:00
|
|
|
if (event.defaultPrevented)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.push(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.handler = handler;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
var target = this.target;
|
|
|
|
|
|
|
|
target.addEventListener('gamepadconnected', handler, false);
|
|
|
|
target.addEventListener('gamepaddisconnected', handler, false);
|
2017-09-09 02:17:13 +00:00
|
|
|
|
|
|
|
// FF only for now:
|
2018-01-26 06:55:15 +00:00
|
|
|
target.addEventListener('gamepadbuttondown', handler, false);
|
|
|
|
target.addEventListener('gamepadbuttonup', handler, false);
|
|
|
|
target.addEventListener('gamepadaxismove', handler, false);
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#stopListeners
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
stopListeners: function ()
|
|
|
|
{
|
2018-01-26 06:55:15 +00:00
|
|
|
var target = this.target;
|
|
|
|
var handler = this.handler;
|
|
|
|
|
|
|
|
target.removeEventListener('gamepadconnected', handler);
|
|
|
|
target.removeEventListener('gamepaddisconnected', handler);
|
2017-09-09 02:17:13 +00:00
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
target.removeEventListener('gamepadbuttondown', handler);
|
|
|
|
target.removeEventListener('gamepadbuttonup', handler);
|
|
|
|
target.removeEventListener('gamepadaxismove', handler);
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#disconnectAll
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#addPad
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} pad - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
addPad: function (pad)
|
|
|
|
{
|
|
|
|
var gamepad = new Gamepad(this, pad.id, pad.index);
|
|
|
|
|
|
|
|
this.gamepads[pad.index] = gamepad;
|
|
|
|
|
|
|
|
return gamepad;
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#removePad
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} index - [description]
|
|
|
|
* @param {[type]} pad - [description]
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
removePad: function (index, pad)
|
|
|
|
{
|
2018-01-26 06:55:15 +00:00
|
|
|
// TODO
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#refreshPads
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} pads - [description]
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
refreshPads: function (pads)
|
|
|
|
{
|
2017-09-12 11:41:20 +00:00
|
|
|
if (!pads)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
|
|
|
this.disconnectAll();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (var i = 0; i < pads.length; i++)
|
|
|
|
{
|
|
|
|
var pad = pads[i];
|
|
|
|
|
2017-09-12 11:41:20 +00:00
|
|
|
if (!pad)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
|
|
|
// removePad?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.gamepads[pad.index] === undefined)
|
|
|
|
{
|
|
|
|
this.addPad(pad);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.gamepads[pad.index].update(pad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#getAll
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
getAll: function ()
|
|
|
|
{
|
|
|
|
var out = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < this.gamepads.length; i++)
|
|
|
|
{
|
|
|
|
if (this.gamepads[i])
|
|
|
|
{
|
|
|
|
out.push(this.gamepads[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#getPad
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} index - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
getPad: function (index)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.gamepads.length; i++)
|
|
|
|
{
|
|
|
|
if (this.gamepads[i].index === index)
|
|
|
|
{
|
|
|
|
return this.gamepads[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#update
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
update: function ()
|
|
|
|
{
|
|
|
|
if (!this.enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.refreshPads(navigator.getGamepads());
|
|
|
|
|
|
|
|
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];
|
2017-09-11 00:28:09 +00:00
|
|
|
var pad;
|
2017-09-09 02:17:13 +00:00
|
|
|
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case 'gamepadconnected':
|
2017-09-11 00:28:09 +00:00
|
|
|
|
|
|
|
pad = this.getPad(event.gamepad.index);
|
|
|
|
|
2018-01-12 17:09:09 +00:00
|
|
|
this.events.emit('connected', pad, event);
|
2017-09-11 00:28:09 +00:00
|
|
|
|
2017-09-09 02:17:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'gamepaddisconnected':
|
2017-09-11 00:28:09 +00:00
|
|
|
|
|
|
|
pad = this.getPad(event.gamepad.index);
|
|
|
|
|
2018-01-12 17:09:09 +00:00
|
|
|
this.events.emit('disconnected', pad, event);
|
2017-09-11 00:28:09 +00:00
|
|
|
|
2017-09-09 02:17:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 00:28:09 +00:00
|
|
|
},
|
|
|
|
|
2018-01-31 03:38:10 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.GamepadManager#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.stopListeners();
|
|
|
|
this.disconnectAll();
|
|
|
|
|
|
|
|
this.gamepads = [];
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* The total number of connected game pads.
|
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.GamepadManager#total
|
|
|
|
* @property {number} total
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
total: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.gamepads.length;
|
|
|
|
}
|
|
|
|
|
2017-09-09 02:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = GamepadManager;
|