Phaser.Gamepad = function (game){ this.game = game; this._gamepadIndexMap = { } ; this._rawPads = [] ; this._active = false ; this.disabled = false ; this._gamepadSupportAvailable = !!navigator.webkitGetGamepads || !!navigator.webkitGamepads || (navigator.userAgent.indexOf('Firefox/') != -1) || !!navigator.getGamepads; this._prevRawGamepadTypes = [] ; this._prevTimestamps = [] ; this.callbackContext = this; this.onConnectCallback = null ; this.onDisconnectCallback = null ; this.onDownCallback = null ; this.onUpCallback = null ; this.onAxisCallback = null ; this.onFloatCallback = null ; this._ongamepadconnected = null ; this._gamepaddisconnected = null ; this._gamepads = [new Phaser.SinglePad(game, this), new Phaser.SinglePad(game, this), new Phaser.SinglePad(game, this), new Phaser.SinglePad(game, this)] ; } ; Phaser.Gamepad.prototype = { addCallbacks: function (context, callbacks){ if (typeof callbacks !== 'undefined') { this.onConnectCallback = (typeof callbacks.onConnect === 'function')? callbacks.onConnect: this.onConnectCallback; this.onDisconnectCallback = (typeof callbacks.onDisconnect === 'function')? callbacks.onDisconnect: this.onDisconnectCallback; this.onDownCallback = (typeof callbacks.onDown === 'function')? callbacks.onDown: this.onDownCallback; this.onUpCallback = (typeof callbacks.onUp === 'function')? callbacks.onUp: this.onUpCallback; this.onAxisCallback = (typeof callbacks.onAxis === 'function')? callbacks.onAxis: this.onAxisCallback; this.onFloatCallback = (typeof callbacks.onFloat === 'function')? callbacks.onFloat: this.onFloatCallback; } } , start: function (){ if (this._active) { return ; } this._active = true ; var _this = this; this._onGamepadConnected = function (event){ return _this.onGamepadConnected(event); } ; this._onGamepadDisconnected = function (event){ return _this.onGamepadDisconnected(event); } ; window.addEventListener('gamepadconnected', this._onGamepadConnected, false ); window.addEventListener('gamepaddisconnected', this._onGamepadDisconnected, false ); } , onGamepadConnected: function (event){ var newPad = event.gamepad; this._rawPads.push(newPad); this._gamepads[newPad.index].connect(newPad); } , onGamepadDisconnected: function (event){ var removedPad = event.gamepad; for (var i in this._rawPads){ if (this._rawPads[i].index === removedPad.index) { this._rawPads.splice(i, 1); } } this._gamepads[removedPad.index].disconnect(); } , update: function (){ this._pollGamepads(); this.pad1.pollStatus(); this.pad2.pollStatus(); this.pad3.pollStatus(); this.pad4.pollStatus(); } , _pollGamepads: function (){ if (navigator.getGamepads) { var rawGamepads = navigator.getGamepads(); } else if (navigator.webkitGetGamepads) { var rawGamepads = navigator.webkitGetGamepads(); } else if (navigator.webkitGamepads) { var rawGamepads = navigator.webkitGamepads(); } if (rawGamepads) { this._rawPads = [] ; var gamepadsChanged = false ; for (var i = 0; i < _AN_Read_length('length', rawGamepads); i++ ){ if (typeof rawGamepads[i] !== this._prevRawGamepadTypes[i]) { gamepadsChanged = true ; this._prevRawGamepadTypes[i] = typeof rawGamepads[i]; } if (rawGamepads[i]) { this._rawPads.push(rawGamepads[i]); } if (i === 3) { break ; } } if (gamepadsChanged) { var validConnections = { rawIndices: { } , padIndices: { } } ; var singlePad; for (var j = 0; j < _AN_Read_length('length', this._gamepads); j++ ){ singlePad = this._gamepads[j]; if (singlePad.connected) { for (var k = 0; k < _AN_Read_length('length', this._rawPads); k++ ){ if (this._rawPads[k].index === singlePad.index) { validConnections.rawIndices[singlePad.index] = true ; validConnections.padIndices[j] = true ; } } } } for (var l = 0; l < _AN_Read_length('length', this._gamepads); l++ ){ singlePad = this._gamepads[l]; if (validConnections.padIndices[l]) { continue ; } if (_AN_Read_length('length', this._rawPads) < 1) { singlePad.disconnect(); } for (var m = 0; m < _AN_Read_length('length', this._rawPads); m++ ){ if (validConnections.padIndices[l]) { break ; } var rawPad = this._rawPads[m]; if (rawPad) { if (validConnections.rawIndices[rawPad.index]) { singlePad.disconnect(); continue ; } else { singlePad.connect(rawPad); validConnections.rawIndices[rawPad.index] = true ; validConnections.padIndices[l] = true ; } } else { singlePad.disconnect(); } } } } } } , setDeadZones: function (value){ for (var i = 0; i < _AN_Read_length('length', this._gamepads); i++ ){ this._gamepads[i].deadZone = value; } } , stop: function (){ this._active = false ; window.removeEventListener('gamepadconnected', this._onGamepadConnected); window.removeEventListener('gamepaddisconnected', this._onGamepadDisconnected); } , reset: function (){ this.update(); for (var i = 0; i < _AN_Read_length('length', this._gamepads); i++ ){ this._gamepads[i].reset(); } } , justPressed: function (buttonCode, duration){ for (var i = 0; i < _AN_Read_length('length', this._gamepads); i++ ){ if (this._gamepads[i].justPressed(buttonCode, duration) === true ) { return true ; } } return false ; } , justReleased: function (buttonCode, duration){ for (var i = 0; i < _AN_Read_length('length', this._gamepads); i++ ){ if (this._gamepads[i].justReleased(buttonCode, duration) === true ) { return true ; } } return false ; } , isDown: function (buttonCode){ for (var i = 0; i < _AN_Read_length('length', this._gamepads); i++ ){ if (this._gamepads[i].isDown(buttonCode) === true ) { return true ; } } return false ; } , destroy: function (){ this.stop(); for (var i = 0; i < _AN_Read_length('length', this._gamepads); i++ ){ this._gamepads[i].destroy(); } } } ; Phaser.Gamepad.prototype.constructor = Phaser.Gamepad; Object.defineProperty(Phaser.Gamepad.prototype, "active", { get: function (){ return this._active; } } ); Object.defineProperty(Phaser.Gamepad.prototype, "supported", { get: function (){ return this._gamepadSupportAvailable; } } ); Object.defineProperty(Phaser.Gamepad.prototype, "padsConnected", { get: function (){ return _AN_Read_length("length", this._rawPads); } } ); Object.defineProperty(Phaser.Gamepad.prototype, "pad1", { get: function (){ return this._gamepads[0]; } } ); Object.defineProperty(Phaser.Gamepad.prototype, "pad2", { get: function (){ return this._gamepads[1]; } } ); Object.defineProperty(Phaser.Gamepad.prototype, "pad3", { get: function (){ return this._gamepads[2]; } } ); Object.defineProperty(Phaser.Gamepad.prototype, "pad4", { get: function (){ return this._gamepads[3]; } } ); Phaser.Gamepad.BUTTON_0 = 0; Phaser.Gamepad.BUTTON_1 = 1; Phaser.Gamepad.BUTTON_2 = 2; Phaser.Gamepad.BUTTON_3 = 3; Phaser.Gamepad.BUTTON_4 = 4; Phaser.Gamepad.BUTTON_5 = 5; Phaser.Gamepad.BUTTON_6 = 6; Phaser.Gamepad.BUTTON_7 = 7; Phaser.Gamepad.BUTTON_8 = 8; Phaser.Gamepad.BUTTON_9 = 9; Phaser.Gamepad.BUTTON_10 = 10; Phaser.Gamepad.BUTTON_11 = 11; Phaser.Gamepad.BUTTON_12 = 12; Phaser.Gamepad.BUTTON_13 = 13; Phaser.Gamepad.BUTTON_14 = 14; Phaser.Gamepad.BUTTON_15 = 15; Phaser.Gamepad.AXIS_0 = 0; Phaser.Gamepad.AXIS_1 = 1; Phaser.Gamepad.AXIS_2 = 2; Phaser.Gamepad.AXIS_3 = 3; Phaser.Gamepad.AXIS_4 = 4; Phaser.Gamepad.AXIS_5 = 5; Phaser.Gamepad.AXIS_6 = 6; Phaser.Gamepad.AXIS_7 = 7; Phaser.Gamepad.AXIS_8 = 8; Phaser.Gamepad.AXIS_9 = 9; Phaser.Gamepad.XBOX360_A = 0; Phaser.Gamepad.XBOX360_B = 1; Phaser.Gamepad.XBOX360_X = 2; Phaser.Gamepad.XBOX360_Y = 3; Phaser.Gamepad.XBOX360_LEFT_BUMPER = 4; Phaser.Gamepad.XBOX360_RIGHT_BUMPER = 5; Phaser.Gamepad.XBOX360_LEFT_TRIGGER = 6; Phaser.Gamepad.XBOX360_RIGHT_TRIGGER = 7; Phaser.Gamepad.XBOX360_BACK = 8; Phaser.Gamepad.XBOX360_START = 9; Phaser.Gamepad.XBOX360_STICK_LEFT_BUTTON = 10; Phaser.Gamepad.XBOX360_STICK_RIGHT_BUTTON = 11; Phaser.Gamepad.XBOX360_DPAD_LEFT = 14; Phaser.Gamepad.XBOX360_DPAD_RIGHT = 15; Phaser.Gamepad.XBOX360_DPAD_UP = 12; Phaser.Gamepad.XBOX360_DPAD_DOWN = 13; Phaser.Gamepad.XBOX360_STICK_LEFT_X = 0; Phaser.Gamepad.XBOX360_STICK_LEFT_Y = 1; Phaser.Gamepad.XBOX360_STICK_RIGHT_X = 2; Phaser.Gamepad.XBOX360_STICK_RIGHT_Y = 3; Phaser.Gamepad.PS3XC_X = 0; Phaser.Gamepad.PS3XC_CIRCLE = 1; Phaser.Gamepad.PS3XC_SQUARE = 2; Phaser.Gamepad.PS3XC_TRIANGLE = 3; Phaser.Gamepad.PS3XC_L1 = 4; Phaser.Gamepad.PS3XC_R1 = 5; Phaser.Gamepad.PS3XC_L2 = 6; Phaser.Gamepad.PS3XC_R2 = 7; Phaser.Gamepad.PS3XC_SELECT = 8; Phaser.Gamepad.PS3XC_START = 9; Phaser.Gamepad.PS3XC_STICK_LEFT_BUTTON = 10; Phaser.Gamepad.PS3XC_STICK_RIGHT_BUTTON = 11; Phaser.Gamepad.PS3XC_DPAD_UP = 12; Phaser.Gamepad.PS3XC_DPAD_DOWN = 13; Phaser.Gamepad.PS3XC_DPAD_LEFT = 14; Phaser.Gamepad.PS3XC_DPAD_RIGHT = 15; Phaser.Gamepad.PS3XC_STICK_LEFT_X = 0; Phaser.Gamepad.PS3XC_STICK_LEFT_Y = 1; Phaser.Gamepad.PS3XC_STICK_RIGHT_X = 2; Phaser.Gamepad.PS3XC_STICK_RIGHT_Y = 3;