Added phaser build and hellophaser sample
This commit is contained in:
584
src/input/Gamepad.js
Normal file
584
src/input/Gamepad.js
Normal file
@@ -0,0 +1,584 @@
|
||||
/**
|
||||
* @author @karlmacklin <tacklemcclean@gmail.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Gamepad class handles looking after gamepad input for your game.
|
||||
* Remember to call gamepad.start(); expecting input!
|
||||
*
|
||||
* HTML5 GAMEPAD API SUPPORT IS AT AN EXPERIMENTAL STAGE!
|
||||
* At moment of writing this (end of 2013) only Chrome supports parts of it out of the box. Firefox supports it
|
||||
* via prefs flags (about:config, search gamepad). The browsers map the same controllers differently.
|
||||
* This class has constants for Windows 7 Chrome mapping of XBOX 360 controller.
|
||||
*
|
||||
* @class Phaser.Gamepad
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
Phaser.Gamepad = function (game) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {Array<Phaser.SinglePad>} _gamepads - The four Phaser Gamepads.
|
||||
* @private
|
||||
*/
|
||||
this._gamepads = [
|
||||
new Phaser.SinglePad(game, this),
|
||||
new Phaser.SinglePad(game, this),
|
||||
new Phaser.SinglePad(game, this),
|
||||
new Phaser.SinglePad(game, this)
|
||||
];
|
||||
|
||||
/**
|
||||
* @property {Object} _gamepadIndexMap - Maps the browsers gamepad indices to our Phaser Gamepads
|
||||
* @private
|
||||
*/
|
||||
this._gamepadIndexMap = {};
|
||||
|
||||
/**
|
||||
* @property {Array} _rawPads - The raw state of the gamepads from the browser
|
||||
* @private
|
||||
*/
|
||||
this._rawPads = [];
|
||||
|
||||
/**
|
||||
* @property {boolean} _active - Private flag for whether or not the API is polled
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._active = false;
|
||||
|
||||
/**
|
||||
* You can disable all Gamepad Input by setting disabled to true. While true all new input related events will be ignored.
|
||||
* @property {boolean} disabled - The disabled state of the Gamepad.
|
||||
* @default
|
||||
*/
|
||||
this.disabled = false;
|
||||
|
||||
/**
|
||||
* Whether or not gamepads are supported in the current browser. Note that as of Dec. 2013 this check is actually not accurate at all due to poor implementation.
|
||||
* @property {boolean} _gamepadSupportAvailable - Are gamepads supported in this browser or not?
|
||||
* @private
|
||||
*/
|
||||
this._gamepadSupportAvailable = !!navigator.webkitGetGamepads || !!navigator.webkitGamepads || (navigator.userAgent.indexOf('Firefox/') != -1) || !!navigator.getGamepads;
|
||||
|
||||
/**
|
||||
* Used to check for differences between earlier polls and current state of gamepads.
|
||||
* @property {Array} _prevRawGamepadTypes
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._prevRawGamepadTypes = [];
|
||||
|
||||
/**
|
||||
* Used to check for differences between earlier polls and current state of gamepads.
|
||||
* @property {Array} _prevTimestamps
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._prevTimestamps = [];
|
||||
|
||||
/**
|
||||
* @property {Object} callbackContext - The context under which the callbacks are run.
|
||||
*/
|
||||
this.callbackContext = this;
|
||||
|
||||
/**
|
||||
* @property {function} onConnectCallback - This callback is invoked every time any gamepad is connected
|
||||
*/
|
||||
this.onConnectCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onDisconnectCallback - This callback is invoked every time any gamepad is disconnected
|
||||
*/
|
||||
this.onDisconnectCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onDownCallback - This callback is invoked every time any gamepad button is pressed down.
|
||||
*/
|
||||
this.onDownCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onUpCallback - This callback is invoked every time any gamepad button is released.
|
||||
*/
|
||||
this.onUpCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onAxisCallback - This callback is invoked every time any gamepad axis is changed.
|
||||
*/
|
||||
this.onAxisCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onFloatCallback - This callback is invoked every time any gamepad button is changed to a value where value > 0 and value < 1.
|
||||
*/
|
||||
this.onFloatCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} _ongamepadconnected - Private callback for Firefox gamepad connection handling
|
||||
* @private
|
||||
*/
|
||||
this._ongamepadconnected = null;
|
||||
|
||||
/**
|
||||
* @property {function} _gamepaddisconnected - Private callback for Firefox gamepad connection handling
|
||||
* @private
|
||||
*/
|
||||
this._gamepaddisconnected = null;
|
||||
};
|
||||
|
||||
Phaser.Gamepad.prototype = {
|
||||
|
||||
/**
|
||||
* Add callbacks to the main Gamepad handler to handle connect/disconnect/button down/button up/axis change/float value buttons
|
||||
* @method Phaser.Gamepad#addCallbacks
|
||||
* @param {Object} context - The context under which the callbacks are run.
|
||||
* @param {Object} callbacks - Object that takes six different callback methods:
|
||||
* onConnectCallback, onDisconnectCallback, onDownCallback, onUpCallback, onAxisCallback, onFloatCallback
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Starts the Gamepad event handling.
|
||||
* This MUST be called manually before Phaser will start polling the Gamepad API.
|
||||
*
|
||||
* @method Phaser.Gamepad#start
|
||||
*/
|
||||
start: function () {
|
||||
|
||||
if (this._active)
|
||||
{
|
||||
// Avoid setting multiple listeners
|
||||
return;
|
||||
}
|
||||
|
||||
this._active = true;
|
||||
|
||||
var _this = this;
|
||||
|
||||
this._ongamepadconnected = function(event) {
|
||||
var newPad = event.gamepad;
|
||||
_this._rawPads.push(newPad);
|
||||
_this._gamepads[newPad.index].connect(newPad);
|
||||
};
|
||||
|
||||
window.addEventListener('gamepadconnected', this._ongamepadconnected, false);
|
||||
|
||||
this._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();
|
||||
};
|
||||
|
||||
window.addEventListener('gamepaddisconnected', this._ongamepaddisconnected, false);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Main gamepad update loop. Should not be called manually.
|
||||
* @method Phaser.Gamepad#update
|
||||
* @private
|
||||
*/
|
||||
update: function () {
|
||||
|
||||
this._pollGamepads();
|
||||
|
||||
for (var i = 0; i < this._gamepads.length; i++)
|
||||
{
|
||||
if (this._gamepads[i]._connected)
|
||||
{
|
||||
this._gamepads[i].pollStatus();
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Updating connected gamepads (for Google Chrome).
|
||||
* Should not be called manually.
|
||||
* @method Phaser.Gamepad#_pollGamepads
|
||||
* @private
|
||||
*/
|
||||
_pollGamepads: function () {
|
||||
|
||||
var rawGamepads = navigator.getGamepads || (navigator.webkitGetGamepads && navigator.webkitGetGamepads()) || navigator.webkitGamepads;
|
||||
|
||||
if (rawGamepads)
|
||||
{
|
||||
this._rawPads = [];
|
||||
|
||||
var gamepadsChanged = false;
|
||||
|
||||
for (var i = 0; i < rawGamepads.length; i++)
|
||||
{
|
||||
if (typeof rawGamepads[i] !== this._prevRawGamepadTypes[i])
|
||||
{
|
||||
gamepadsChanged = true;
|
||||
this._prevRawGamepadTypes[i] = typeof rawGamepads[i];
|
||||
}
|
||||
|
||||
if (rawGamepads[i])
|
||||
{
|
||||
this._rawPads.push(rawGamepads[i]);
|
||||
}
|
||||
|
||||
// Support max 4 pads at the moment
|
||||
if (i === 3)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (gamepadsChanged)
|
||||
{
|
||||
var validConnections = { rawIndices: {}, padIndices: {} };
|
||||
var singlePad;
|
||||
|
||||
for (var j = 0; j < this._gamepads.length; j++)
|
||||
{
|
||||
singlePad = this._gamepads[j];
|
||||
|
||||
if (singlePad.connected)
|
||||
{
|
||||
for (var k = 0; k < this._rawPads.length; k++)
|
||||
{
|
||||
if (this._rawPads[k].index === singlePad.index)
|
||||
{
|
||||
validConnections.rawIndices[singlePad.index] = true;
|
||||
validConnections.padIndices[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var l = 0; l < this._gamepads.length; l++)
|
||||
{
|
||||
singlePad = this._gamepads[l];
|
||||
|
||||
if (validConnections.padIndices[l])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this._rawPads.length < 1)
|
||||
{
|
||||
singlePad.disconnect();
|
||||
}
|
||||
|
||||
for (var m = 0; m < this._rawPads.length; 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the deadZone variable for all four gamepads
|
||||
* @method Phaser.Gamepad#setDeadZones
|
||||
*/
|
||||
setDeadZones: function (value) {
|
||||
|
||||
for (var i = 0; i < this._gamepads.length; i++)
|
||||
{
|
||||
this._gamepads[i].deadZone = value;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stops the Gamepad event handling.
|
||||
*
|
||||
* @method Phaser.Gamepad#stop
|
||||
*/
|
||||
stop: function () {
|
||||
|
||||
this._active = false;
|
||||
|
||||
window.removeEventListener('gamepadconnected', this._ongamepadconnected);
|
||||
window.removeEventListener('gamepaddisconnected', this._ongamepaddisconnected);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Reset all buttons/axes of all gamepads
|
||||
* @method Phaser.Gamepad#reset
|
||||
*/
|
||||
reset: function () {
|
||||
|
||||
this.update();
|
||||
|
||||
for (var i = 0; i < this._gamepads.length; i++)
|
||||
{
|
||||
this._gamepads[i].reset();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just pressed" state of a button from ANY gamepad connected. Just pressed is considered true if the button was pressed down within the duration given (default 250ms).
|
||||
* @method Phaser.Gamepad#justPressed
|
||||
* @param {number} buttonCode - The buttonCode of the button to check for.
|
||||
* @param {number} [duration=250] - The duration below which the button is considered as being just pressed.
|
||||
* @return {boolean} True if the button is just pressed otherwise false.
|
||||
*/
|
||||
justPressed: function (buttonCode, duration) {
|
||||
|
||||
for (var i = 0; i < this._gamepads.length; i++)
|
||||
{
|
||||
if (this._gamepads[i].justPressed(buttonCode, duration) === true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just released" state of a button from ANY gamepad connected. Just released is considered as being true if the button was released within the duration given (default 250ms).
|
||||
* @method Phaser.Gamepad#justPressed
|
||||
* @param {number} buttonCode - The buttonCode of the button to check for.
|
||||
* @param {number} [duration=250] - The duration below which the button is considered as being just released.
|
||||
* @return {boolean} True if the button is just released otherwise false.
|
||||
*/
|
||||
justReleased: function (buttonCode, duration) {
|
||||
|
||||
for (var i = 0; i < this._gamepads.length; i++)
|
||||
{
|
||||
if (this._gamepads[i].justReleased(buttonCode, duration) === true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true if the button is currently pressed down, on ANY gamepad.
|
||||
* @method Phaser.Gamepad#isDown
|
||||
* @param {number} buttonCode - The buttonCode of the button to check for.
|
||||
* @return {boolean} True if a button is currently down.
|
||||
*/
|
||||
isDown: function (buttonCode) {
|
||||
|
||||
for (var i = 0; i < this._gamepads.length; i++)
|
||||
{
|
||||
if (this._gamepads[i].isDown(buttonCode) === true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Gamepad.prototype.constructor = Phaser.Gamepad;
|
||||
|
||||
/**
|
||||
* If the gamepad input is active or not - if not active it should not be updated from Input.js
|
||||
* @name Phaser.Gamepad#active
|
||||
* @property {boolean} active - If the gamepad input is active or not.
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Gamepad.prototype, "active", {
|
||||
|
||||
get: function () {
|
||||
return this._active;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Whether or not gamepads are supported in current browser.
|
||||
* @name Phaser.Gamepad#supported
|
||||
* @property {boolean} supported - Whether or not gamepads are supported in current browser.
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Gamepad.prototype, "supported", {
|
||||
|
||||
get: function () {
|
||||
return this._gamepadSupportAvailable;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* How many live gamepads are currently connected.
|
||||
* @name Phaser.Gamepad#padsConnected
|
||||
* @property {boolean} padsConnected - How many live gamepads are currently connected.
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Gamepad.prototype, "padsConnected", {
|
||||
|
||||
get: function () {
|
||||
return this._rawPads.length;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Gamepad #1
|
||||
* @name Phaser.Gamepad#pad1
|
||||
* @property {boolean} pad1 - Gamepad #1;
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Gamepad.prototype, "pad1", {
|
||||
|
||||
get: function () {
|
||||
return this._gamepads[0];
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Gamepad #2
|
||||
* @name Phaser.Gamepad#pad2
|
||||
* @property {boolean} pad2 - Gamepad #2
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Gamepad.prototype, "pad2", {
|
||||
|
||||
get: function () {
|
||||
return this._gamepads[1];
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Gamepad #3
|
||||
* @name Phaser.Gamepad#pad3
|
||||
* @property {boolean} pad3 - Gamepad #3
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Gamepad.prototype, "pad3", {
|
||||
|
||||
get: function () {
|
||||
return this._gamepads[2];
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Gamepad #4
|
||||
* @name Phaser.Gamepad#pad4
|
||||
* @property {boolean} pad4 - Gamepad #4
|
||||
* @readonly
|
||||
*/
|
||||
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;
|
||||
|
||||
// Below mapping applies to XBOX 360 Wired and Wireless controller on Google Chrome (tested on Windows 7).
|
||||
// - Firefox uses different map! Separate amount of buttons and axes. DPAD = axis and not a button.
|
||||
// In other words - discrepancies when using gamepads.
|
||||
|
||||
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;
|
||||
175
src/input/GamepadButton.js
Normal file
175
src/input/GamepadButton.js
Normal file
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* @author @karlmacklin <tacklemcclean@gmail.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class Phaser.GamepadButton
|
||||
* @classdesc If you need more fine-grained control over the handling of specific buttons you can create and use Phaser.GamepadButton objects.
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - Current game instance.
|
||||
* @param {number} buttoncode - The button code this GamepadButton is responsible for.
|
||||
*/
|
||||
Phaser.GamepadButton = function (game, buttoncode) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {boolean} isDown - The "down" state of the button.
|
||||
* @default
|
||||
*/
|
||||
this.isDown = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} isUp - The "up" state of the button.
|
||||
* @default
|
||||
*/
|
||||
this.isUp = true;
|
||||
|
||||
/**
|
||||
* @property {number} timeDown - The timestamp when the button was last pressed down.
|
||||
* @default
|
||||
*/
|
||||
this.timeDown = 0;
|
||||
|
||||
/**
|
||||
* If the button is down this value holds the duration of that button press and is constantly updated.
|
||||
* If the button is up it holds the duration of the previous down session.
|
||||
* @property {number} duration - The number of milliseconds this button has been held down for.
|
||||
* @default
|
||||
*/
|
||||
this.duration = 0;
|
||||
|
||||
/**
|
||||
* @property {number} timeUp - The timestamp when the button was last released.
|
||||
* @default
|
||||
*/
|
||||
this.timeUp = 0;
|
||||
|
||||
/**
|
||||
* @property {number} repeats - If a button is held down this holds down the number of times the button has 'repeated'.
|
||||
* @default
|
||||
*/
|
||||
this.repeats = 0;
|
||||
|
||||
/**
|
||||
* @property {number} value - Button value. Mainly useful for checking analog buttons (like shoulder triggers)
|
||||
* @default
|
||||
*/
|
||||
this.value = 0;
|
||||
|
||||
/**
|
||||
* @property {number} buttonCode - The buttoncode of this button.
|
||||
*/
|
||||
this.buttonCode = buttoncode;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onDown - This Signal is dispatched every time this GamepadButton is pressed down. It is only dispatched once (until the button is released again).
|
||||
*/
|
||||
this.onDown = new Phaser.Signal();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onUp - This Signal is dispatched every time this GamepadButton is pressed down. It is only dispatched once (until the button is released again).
|
||||
*/
|
||||
this.onUp = new Phaser.Signal();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onFloat - This Signal is dispatched every time this GamepadButton changes floating value (between (but not exactly) 0 and 1)
|
||||
*/
|
||||
this.onFloat = new Phaser.Signal();
|
||||
|
||||
};
|
||||
|
||||
Phaser.GamepadButton.prototype = {
|
||||
|
||||
/**
|
||||
* Called automatically by Phaser.SinglePad.
|
||||
* @method Phaser.GamepadButton#processButtonDown
|
||||
* @param {Object} value - Button value
|
||||
* @protected
|
||||
*/
|
||||
processButtonDown: function (value) {
|
||||
|
||||
if (this.isDown)
|
||||
{
|
||||
this.duration = this.game.time.now - this.timeDown;
|
||||
this.repeats++;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.isDown = true;
|
||||
this.isUp = false;
|
||||
this.timeDown = this.game.time.now;
|
||||
this.duration = 0;
|
||||
this.repeats = 0;
|
||||
this.value = value;
|
||||
|
||||
this.onDown.dispatch(this, value);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by Phaser.SinglePad.
|
||||
* @method Phaser.GamepadButton#processButtonUp
|
||||
* @param {Object} value - Button value
|
||||
* @protected
|
||||
*/
|
||||
processButtonUp: function (value) {
|
||||
|
||||
this.isDown = false;
|
||||
this.isUp = true;
|
||||
this.timeUp = this.game.time.now;
|
||||
this.value = value;
|
||||
|
||||
this.onUp.dispatch(this, value);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by Phaser.Gamepad.
|
||||
* @method Phaser.GamepadButton#processButtonFloat
|
||||
* @param {Object} value - Button value
|
||||
* @protected
|
||||
*/
|
||||
processButtonFloat: function (value) {
|
||||
|
||||
this.value = value;
|
||||
this.onFloat.dispatch(this, value);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just pressed" state of this button. Just pressed is considered true if the button was pressed down within the duration given (default 250ms).
|
||||
* @method Phaser.GamepadButton#justPressed
|
||||
* @param {number} [duration=250] - The duration below which the button is considered as being just pressed.
|
||||
* @return {boolean} True if the button is just pressed otherwise false.
|
||||
*/
|
||||
justPressed: function (duration) {
|
||||
|
||||
if (typeof duration === "undefined") { duration = 250; }
|
||||
|
||||
return (this.isDown && this.duration < duration);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just released" state of this button. Just released is considered as being true if the button was released within the duration given (default 250ms).
|
||||
* @method Phaser.GamepadButton#justPressed
|
||||
* @param {number} [duration=250] - The duration below which the button is considered as being just released.
|
||||
* @return {boolean} True if the button is just pressed otherwise false.
|
||||
*/
|
||||
justReleased: function (duration) {
|
||||
|
||||
if (typeof duration === "undefined") { duration = 250; }
|
||||
|
||||
return (this.isDown === false && (this.game.time.now - this.timeUp < duration));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.GamepadButton.prototype.constructor = Phaser.GamepadButton;
|
||||
1003
src/input/Input.js
Normal file
1003
src/input/Input.js
Normal file
File diff suppressed because it is too large
Load Diff
1410
src/input/InputHandler.js
Normal file
1410
src/input/InputHandler.js
Normal file
File diff suppressed because it is too large
Load Diff
248
src/input/Key.js
Normal file
248
src/input/Key.js
Normal file
@@ -0,0 +1,248 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class Phaser.Key
|
||||
* @classdesc If you need more fine-grained control over the handling of specific keys you can create and use Phaser.Key objects.
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - Current game instance.
|
||||
* @param {number} keycode - The key code this Key is responsible for.
|
||||
*/
|
||||
Phaser.Key = function (game, keycode) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {boolean} enabled - An enabled key processes its update and dispatches events. You can toggle this at run-time to disable a key without deleting it.
|
||||
* @default
|
||||
*/
|
||||
this.enabled = true;
|
||||
|
||||
/**
|
||||
* @property {object} event - Stores the most recent DOM event.
|
||||
* @readonly
|
||||
*/
|
||||
this.event = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} isDown - The "down" state of the key.
|
||||
* @default
|
||||
*/
|
||||
this.isDown = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} isUp - The "up" state of the key.
|
||||
* @default
|
||||
*/
|
||||
this.isUp = true;
|
||||
|
||||
/**
|
||||
* @property {boolean} altKey - The down state of the ALT key, if pressed at the same time as this key.
|
||||
* @default
|
||||
*/
|
||||
this.altKey = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} ctrlKey - The down state of the CTRL key, if pressed at the same time as this key.
|
||||
* @default
|
||||
*/
|
||||
this.ctrlKey = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} shiftKey - The down state of the SHIFT key, if pressed at the same time as this key.
|
||||
* @default
|
||||
*/
|
||||
this.shiftKey = false;
|
||||
|
||||
/**
|
||||
* @property {number} timeDown - The timestamp when the key was last pressed down. This is based on Game.time.now.
|
||||
*/
|
||||
this.timeDown = 0;
|
||||
|
||||
/**
|
||||
* If the key is down this value holds the duration of that key press and is constantly updated.
|
||||
* If the key is up it holds the duration of the previous down session.
|
||||
* @property {number} duration - The number of milliseconds this key has been held down for.
|
||||
* @default
|
||||
*/
|
||||
this.duration = 0;
|
||||
|
||||
/**
|
||||
* @property {number} timeUp - The timestamp when the key was last released. This is based on Game.time.now.
|
||||
* @default
|
||||
*/
|
||||
this.timeUp = -2500;
|
||||
|
||||
/**
|
||||
* @property {number} repeats - If a key is held down this holds down the number of times the key has 'repeated'.
|
||||
* @default
|
||||
*/
|
||||
this.repeats = 0;
|
||||
|
||||
/**
|
||||
* @property {number} keyCode - The keycode of this key.
|
||||
*/
|
||||
this.keyCode = keycode;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onDown - This Signal is dispatched every time this Key is pressed down. It is only dispatched once (until the key is released again).
|
||||
*/
|
||||
this.onDown = new Phaser.Signal();
|
||||
|
||||
/**
|
||||
* @property {function} onHoldCallback - A callback that is called while this Key is held down. Warning: Depending on refresh rate that could be 60+ times per second.
|
||||
*/
|
||||
this.onHoldCallback = null;
|
||||
|
||||
/**
|
||||
* @property {object} onHoldContext - The context under which the onHoldCallback will be called.
|
||||
*/
|
||||
this.onHoldContext = null;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onUp - This Signal is dispatched every time this Key is pressed down. It is only dispatched once (until the key is released again).
|
||||
*/
|
||||
this.onUp = new Phaser.Signal();
|
||||
|
||||
};
|
||||
|
||||
Phaser.Key.prototype = {
|
||||
|
||||
update: function () {
|
||||
|
||||
if (!this.enabled) { return; }
|
||||
|
||||
if (this.isDown)
|
||||
{
|
||||
this.duration = this.game.time.now - this.timeDown;
|
||||
this.repeats++;
|
||||
|
||||
if (this.onHoldCallback)
|
||||
{
|
||||
this.onHoldCallback.call(this.onHoldContext, this);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by Phaser.Keyboard.
|
||||
* @method Phaser.Key#processKeyDown
|
||||
* @param {KeyboardEvent} event.
|
||||
* @protected
|
||||
*/
|
||||
processKeyDown: function (event) {
|
||||
|
||||
if (!this.enabled) { return; }
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.isDown)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.altKey = event.altKey;
|
||||
this.ctrlKey = event.ctrlKey;
|
||||
this.shiftKey = event.shiftKey;
|
||||
|
||||
this.isDown = true;
|
||||
this.isUp = false;
|
||||
this.timeDown = this.game.time.now;
|
||||
this.duration = 0;
|
||||
this.repeats = 0;
|
||||
|
||||
this.onDown.dispatch(this);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by Phaser.Keyboard.
|
||||
* @method Phaser.Key#processKeyUp
|
||||
* @param {KeyboardEvent} event.
|
||||
* @protected
|
||||
*/
|
||||
processKeyUp: function (event) {
|
||||
|
||||
if (!this.enabled) { return; }
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.isUp)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.isDown = false;
|
||||
this.isUp = true;
|
||||
this.timeUp = this.game.time.now;
|
||||
this.duration = this.game.time.now - this.timeDown;
|
||||
|
||||
this.onUp.dispatch(this);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Resets the state of this Key. This sets isDown to false, isUp to true, resets the time to be the current time and clears any callbacks
|
||||
* associated with the onDown and onUp events and nulls the onHoldCallback if set.
|
||||
*
|
||||
* @method Phaser.Key#reset
|
||||
* @param {boolean} [hard=true] - A soft reset won't reset any events or callbacks that are bound to this Key. A hard reset will.
|
||||
*/
|
||||
reset: function (hard) {
|
||||
|
||||
if (typeof hard === 'undefined') { hard = true; }
|
||||
|
||||
this.isDown = false;
|
||||
this.isUp = true;
|
||||
this.timeUp = this.game.time.now;
|
||||
this.duration = this.game.time.now - this.timeDown;
|
||||
this.enabled = true;
|
||||
|
||||
if (hard)
|
||||
{
|
||||
this.onDown.removeAll();
|
||||
this.onUp.removeAll();
|
||||
this.onHoldCallback = null;
|
||||
this.onHoldContext = null;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just pressed" state of the Key. Just pressed is considered true if the key was pressed down within the duration given (default 250ms)
|
||||
* @method Phaser.Key#justPressed
|
||||
* @param {number} [duration=50] - The duration below which the key is considered as being just pressed.
|
||||
* @return {boolean} True if the key is just pressed otherwise false.
|
||||
*/
|
||||
justPressed: function (duration) {
|
||||
|
||||
if (typeof duration === "undefined") { duration = 50; }
|
||||
|
||||
return (this.isDown && this.duration < duration);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just released" state of the Key. Just released is considered as being true if the key was released within the duration given (default 250ms)
|
||||
* @method Phaser.Key#justReleased
|
||||
* @param {number} [duration=50] - The duration below which the key is considered as being just released.
|
||||
* @return {boolean} True if the key is just released otherwise false.
|
||||
*/
|
||||
justReleased: function (duration) {
|
||||
|
||||
if (typeof duration === "undefined") { duration = 50; }
|
||||
|
||||
return (!this.isDown && ((this.game.time.now - this.timeUp) < duration));
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Key.prototype.constructor = Phaser.Key;
|
||||
549
src/input/Keyboard.js
Normal file
549
src/input/Keyboard.js
Normal file
@@ -0,0 +1,549 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Keyboard class handles looking after keyboard input for your game.
|
||||
* It will recognise and respond to key presses and dispatch the required events.
|
||||
* Please be aware that lots of keyboards are unable to process certain combinations of keys due to hardware
|
||||
* limitations known as ghosting. Full details here: http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/
|
||||
*
|
||||
* @class Phaser.Keyboard
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
Phaser.Keyboard = function (game) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* You can disable all Keyboard Input by setting disabled to true. While true all new input related events will be ignored.
|
||||
* @property {boolean} disabled - The disabled state of the Keyboard.
|
||||
* @default
|
||||
*/
|
||||
this.disabled = false;
|
||||
|
||||
/**
|
||||
* @property {Object} event - The most recent DOM event. This is updated every time a new key is pressed or released.
|
||||
*/
|
||||
this.event = null;
|
||||
|
||||
/**
|
||||
* @property {Object} callbackContext - The context under which the callbacks are run.
|
||||
*/
|
||||
this.callbackContext = this;
|
||||
|
||||
/**
|
||||
* @property {function} onDownCallback - This callback is invoked every time a key is pressed down.
|
||||
*/
|
||||
this.onDownCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onUpCallback - This callback is invoked every time a key is released.
|
||||
*/
|
||||
this.onUpCallback = null;
|
||||
|
||||
/**
|
||||
* @property {array<Phaser.Key>} _keys - The array the Phaser.Key objects are stored in.
|
||||
* @private
|
||||
*/
|
||||
this._keys = [];
|
||||
|
||||
/**
|
||||
* @property {array} _capture - The array the key capture values are stored in.
|
||||
* @private
|
||||
*/
|
||||
this._capture = [];
|
||||
|
||||
/**
|
||||
* @property {function} _onKeyDown
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._onKeyDown = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onKeyUp
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._onKeyUp = null;
|
||||
|
||||
/**
|
||||
* @property {number} _i - Internal cache var
|
||||
* @private
|
||||
*/
|
||||
this._i = 0;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Keyboard.prototype = {
|
||||
|
||||
/**
|
||||
* Add callbacks to the Keyboard handler so that each time a key is pressed down or released the callbacks are activated.
|
||||
*
|
||||
* @method Phaser.Keyboard#addCallbacks
|
||||
* @param {Object} context - The context under which the callbacks are run.
|
||||
* @param {function} onDown - This callback is invoked every time a key is pressed down.
|
||||
* @param {function} [onUp=null] - This callback is invoked every time a key is released.
|
||||
*/
|
||||
addCallbacks: function (context, onDown, onUp) {
|
||||
|
||||
this.callbackContext = context;
|
||||
this.onDownCallback = onDown;
|
||||
|
||||
if (typeof onUp !== 'undefined')
|
||||
{
|
||||
this.onUpCallback = onUp;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If you need more fine-grained control over a Key you can create a new Phaser.Key object via this method.
|
||||
* The Key object can then be polled, have events attached to it, etc.
|
||||
*
|
||||
* @method Phaser.Keyboard#addKey
|
||||
* @param {number} keycode - The keycode of the key, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
||||
* @return {Phaser.Key} The Key object which you can store locally and reference directly.
|
||||
*/
|
||||
addKey: function (keycode) {
|
||||
|
||||
if (!this._keys[keycode])
|
||||
{
|
||||
this._keys[keycode] = new Phaser.Key(this.game, keycode);
|
||||
|
||||
this.addKeyCapture(keycode);
|
||||
}
|
||||
|
||||
return this._keys[keycode];
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes a Key object from the Keyboard manager.
|
||||
*
|
||||
* @method Phaser.Keyboard#removeKey
|
||||
* @param {number} keycode - The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
||||
*/
|
||||
removeKey: function (keycode) {
|
||||
|
||||
if (this._keys[keycode])
|
||||
{
|
||||
this._keys[keycode] = null;
|
||||
|
||||
this.removeKeyCapture(keycode);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right.
|
||||
*
|
||||
* @method Phaser.Keyboard#createCursorKeys
|
||||
* @return {object} An object containing properties: up, down, left and right. Which can be polled like any other Phaser.Key object.
|
||||
*/
|
||||
createCursorKeys: function () {
|
||||
|
||||
return {
|
||||
up: this.addKey(Phaser.Keyboard.UP),
|
||||
down: this.addKey(Phaser.Keyboard.DOWN),
|
||||
left: this.addKey(Phaser.Keyboard.LEFT),
|
||||
right: this.addKey(Phaser.Keyboard.RIGHT)
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Starts the Keyboard event listeners running (keydown and keyup). They are attached to the window.
|
||||
* This is called automatically by Phaser.Input and should not normally be invoked directly.
|
||||
*
|
||||
* @method Phaser.Keyboard#start
|
||||
*/
|
||||
start: function () {
|
||||
|
||||
if (this._onKeyDown !== null)
|
||||
{
|
||||
// Avoid setting multiple listeners
|
||||
return;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
this._onKeyDown = function (event) {
|
||||
return _this.processKeyDown(event);
|
||||
};
|
||||
|
||||
this._onKeyUp = function (event) {
|
||||
return _this.processKeyUp(event);
|
||||
};
|
||||
|
||||
window.addEventListener('keydown', this._onKeyDown, false);
|
||||
window.addEventListener('keyup', this._onKeyUp, false);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stops the Keyboard event listeners from running (keydown and keyup). They are removed from the window.
|
||||
*
|
||||
* @method Phaser.Keyboard#stop
|
||||
*/
|
||||
stop: function () {
|
||||
|
||||
window.removeEventListener('keydown', this._onKeyDown);
|
||||
window.removeEventListener('keyup', this._onKeyUp);
|
||||
|
||||
this._onKeyDown = null;
|
||||
this._onKeyUp = null;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stops the Keyboard event listeners from running (keydown and keyup). They are removed from the window.
|
||||
* Also clears all key captures and currently created Key objects.
|
||||
*
|
||||
* @method Phaser.Keyboard#destroy
|
||||
*/
|
||||
destroy: function () {
|
||||
|
||||
this.stop();
|
||||
|
||||
this.clearCaptures();
|
||||
|
||||
this._keys.length = 0;
|
||||
this._i = 0;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* By default when a key is pressed Phaser will not stop the event from propagating up to the browser.
|
||||
* There are some keys this can be annoying for, like the arrow keys or space bar, which make the browser window scroll.
|
||||
* You can use addKeyCapture to consume the keyboard event for specific keys so it doesn't bubble up to the the browser.
|
||||
* Pass in either a single keycode or an array/hash of keycodes.
|
||||
*
|
||||
* @method Phaser.Keyboard#addKeyCapture
|
||||
* @param {Any} keycode - Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].
|
||||
*/
|
||||
addKeyCapture: function (keycode) {
|
||||
|
||||
if (typeof keycode === 'object')
|
||||
{
|
||||
for (var key in keycode)
|
||||
{
|
||||
this._capture[keycode[key]] = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this._capture[keycode] = true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes an existing key capture.
|
||||
*
|
||||
* @method Phaser.Keyboard#removeKeyCapture
|
||||
* @param {number} keycode
|
||||
*/
|
||||
removeKeyCapture: function (keycode) {
|
||||
|
||||
delete this._capture[keycode];
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Clear all set key captures.
|
||||
*
|
||||
* @method Phaser.Keyboard#clearCaptures
|
||||
*/
|
||||
clearCaptures: function () {
|
||||
|
||||
this._capture = {};
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates all currently defined keys.
|
||||
*
|
||||
* @method Phaser.Keyboard#update
|
||||
*/
|
||||
update: function () {
|
||||
|
||||
this._i = this._keys.length;
|
||||
|
||||
while (this._i--)
|
||||
{
|
||||
if (this._keys[this._i])
|
||||
{
|
||||
this._keys[this._i].update();
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Process the keydown event.
|
||||
*
|
||||
* @method Phaser.Keyboard#processKeyDown
|
||||
* @param {KeyboardEvent} event
|
||||
* @protected
|
||||
*/
|
||||
processKeyDown: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// The event is being captured but another hotkey may need it
|
||||
if (this._capture[event.keyCode])
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
if (this.onDownCallback)
|
||||
{
|
||||
this.onDownCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (!this._keys[event.keyCode])
|
||||
{
|
||||
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
|
||||
}
|
||||
|
||||
this._keys[event.keyCode].processKeyDown(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Process the keyup event.
|
||||
*
|
||||
* @method Phaser.Keyboard#processKeyUp
|
||||
* @param {KeyboardEvent} event
|
||||
* @protected
|
||||
*/
|
||||
processKeyUp: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._capture[event.keyCode])
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
if (this.onUpCallback)
|
||||
{
|
||||
this.onUpCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (!this._keys[event.keyCode])
|
||||
{
|
||||
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
|
||||
}
|
||||
|
||||
this._keys[event.keyCode].processKeyUp(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Resets all Keys.
|
||||
*
|
||||
* @method Phaser.Keyboard#reset
|
||||
* @param {boolean} [hard=true] - A soft reset won't reset any events or callbacks that are bound to the Keys. A hard reset will.
|
||||
*/
|
||||
reset: function (hard) {
|
||||
|
||||
if (typeof hard === 'undefined') { hard = true; }
|
||||
|
||||
this.event = null;
|
||||
|
||||
var i = this._keys.length;
|
||||
|
||||
while (i--)
|
||||
{
|
||||
if (this._keys[i])
|
||||
{
|
||||
this._keys[i].reset(hard);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just pressed" state of the key. Just pressed is considered true if the key was pressed down within the duration given (default 250ms)
|
||||
*
|
||||
* @method Phaser.Keyboard#justPressed
|
||||
* @param {number} keycode - The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
||||
* @param {number} [duration=50] - The duration below which the key is considered as being just pressed.
|
||||
* @return {boolean} True if the key is just pressed otherwise false.
|
||||
*/
|
||||
justPressed: function (keycode, duration) {
|
||||
|
||||
if (typeof duration === 'undefined') { duration = 50; }
|
||||
|
||||
if (this._keys[keycode])
|
||||
{
|
||||
return this._keys[keycode].justPressed(duration);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just released" state of the Key. Just released is considered as being true if the key was released within the duration given (default 250ms)
|
||||
*
|
||||
* @method Phaser.Keyboard#justReleased
|
||||
* @param {number} keycode - The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
||||
* @param {number} [duration=50] - The duration below which the key is considered as being just released.
|
||||
* @return {boolean} True if the key is just released otherwise false.
|
||||
*/
|
||||
justReleased: function (keycode, duration) {
|
||||
|
||||
if (typeof duration === 'undefined') { duration = 50; }
|
||||
|
||||
if (this._keys[keycode])
|
||||
{
|
||||
return this._keys[keycode].justReleased(duration);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true of the key is currently pressed down. Note that it can only detect key presses on the web browser.
|
||||
*
|
||||
* @method Phaser.Keyboard#isDown
|
||||
* @param {number} keycode - The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
||||
* @return {boolean} True if the key is currently down.
|
||||
*/
|
||||
isDown: function (keycode) {
|
||||
|
||||
if (this._keys[keycode])
|
||||
{
|
||||
return this._keys[keycode].isDown;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Keyboard.prototype.constructor = Phaser.Keyboard;
|
||||
|
||||
Phaser.Keyboard.A = "A".charCodeAt(0);
|
||||
Phaser.Keyboard.B = "B".charCodeAt(0);
|
||||
Phaser.Keyboard.C = "C".charCodeAt(0);
|
||||
Phaser.Keyboard.D = "D".charCodeAt(0);
|
||||
Phaser.Keyboard.E = "E".charCodeAt(0);
|
||||
Phaser.Keyboard.F = "F".charCodeAt(0);
|
||||
Phaser.Keyboard.G = "G".charCodeAt(0);
|
||||
Phaser.Keyboard.H = "H".charCodeAt(0);
|
||||
Phaser.Keyboard.I = "I".charCodeAt(0);
|
||||
Phaser.Keyboard.J = "J".charCodeAt(0);
|
||||
Phaser.Keyboard.K = "K".charCodeAt(0);
|
||||
Phaser.Keyboard.L = "L".charCodeAt(0);
|
||||
Phaser.Keyboard.M = "M".charCodeAt(0);
|
||||
Phaser.Keyboard.N = "N".charCodeAt(0);
|
||||
Phaser.Keyboard.O = "O".charCodeAt(0);
|
||||
Phaser.Keyboard.P = "P".charCodeAt(0);
|
||||
Phaser.Keyboard.Q = "Q".charCodeAt(0);
|
||||
Phaser.Keyboard.R = "R".charCodeAt(0);
|
||||
Phaser.Keyboard.S = "S".charCodeAt(0);
|
||||
Phaser.Keyboard.T = "T".charCodeAt(0);
|
||||
Phaser.Keyboard.U = "U".charCodeAt(0);
|
||||
Phaser.Keyboard.V = "V".charCodeAt(0);
|
||||
Phaser.Keyboard.W = "W".charCodeAt(0);
|
||||
Phaser.Keyboard.X = "X".charCodeAt(0);
|
||||
Phaser.Keyboard.Y = "Y".charCodeAt(0);
|
||||
Phaser.Keyboard.Z = "Z".charCodeAt(0);
|
||||
Phaser.Keyboard.ZERO = "0".charCodeAt(0);
|
||||
Phaser.Keyboard.ONE = "1".charCodeAt(0);
|
||||
Phaser.Keyboard.TWO = "2".charCodeAt(0);
|
||||
Phaser.Keyboard.THREE = "3".charCodeAt(0);
|
||||
Phaser.Keyboard.FOUR = "4".charCodeAt(0);
|
||||
Phaser.Keyboard.FIVE = "5".charCodeAt(0);
|
||||
Phaser.Keyboard.SIX = "6".charCodeAt(0);
|
||||
Phaser.Keyboard.SEVEN = "7".charCodeAt(0);
|
||||
Phaser.Keyboard.EIGHT = "8".charCodeAt(0);
|
||||
Phaser.Keyboard.NINE = "9".charCodeAt(0);
|
||||
Phaser.Keyboard.NUMPAD_0 = 96;
|
||||
Phaser.Keyboard.NUMPAD_1 = 97;
|
||||
Phaser.Keyboard.NUMPAD_2 = 98;
|
||||
Phaser.Keyboard.NUMPAD_3 = 99;
|
||||
Phaser.Keyboard.NUMPAD_4 = 100;
|
||||
Phaser.Keyboard.NUMPAD_5 = 101;
|
||||
Phaser.Keyboard.NUMPAD_6 = 102;
|
||||
Phaser.Keyboard.NUMPAD_7 = 103;
|
||||
Phaser.Keyboard.NUMPAD_8 = 104;
|
||||
Phaser.Keyboard.NUMPAD_9 = 105;
|
||||
Phaser.Keyboard.NUMPAD_MULTIPLY = 106;
|
||||
Phaser.Keyboard.NUMPAD_ADD = 107;
|
||||
Phaser.Keyboard.NUMPAD_ENTER = 108;
|
||||
Phaser.Keyboard.NUMPAD_SUBTRACT = 109;
|
||||
Phaser.Keyboard.NUMPAD_DECIMAL = 110;
|
||||
Phaser.Keyboard.NUMPAD_DIVIDE = 111;
|
||||
Phaser.Keyboard.F1 = 112;
|
||||
Phaser.Keyboard.F2 = 113;
|
||||
Phaser.Keyboard.F3 = 114;
|
||||
Phaser.Keyboard.F4 = 115;
|
||||
Phaser.Keyboard.F5 = 116;
|
||||
Phaser.Keyboard.F6 = 117;
|
||||
Phaser.Keyboard.F7 = 118;
|
||||
Phaser.Keyboard.F8 = 119;
|
||||
Phaser.Keyboard.F9 = 120;
|
||||
Phaser.Keyboard.F10 = 121;
|
||||
Phaser.Keyboard.F11 = 122;
|
||||
Phaser.Keyboard.F12 = 123;
|
||||
Phaser.Keyboard.F13 = 124;
|
||||
Phaser.Keyboard.F14 = 125;
|
||||
Phaser.Keyboard.F15 = 126;
|
||||
Phaser.Keyboard.COLON = 186;
|
||||
Phaser.Keyboard.EQUALS = 187;
|
||||
Phaser.Keyboard.UNDERSCORE = 189;
|
||||
Phaser.Keyboard.QUESTION_MARK = 191;
|
||||
Phaser.Keyboard.TILDE = 192;
|
||||
Phaser.Keyboard.OPEN_BRACKET = 219;
|
||||
Phaser.Keyboard.BACKWARD_SLASH = 220;
|
||||
Phaser.Keyboard.CLOSED_BRACKET = 221;
|
||||
Phaser.Keyboard.QUOTES = 222;
|
||||
Phaser.Keyboard.BACKSPACE = 8;
|
||||
Phaser.Keyboard.TAB = 9;
|
||||
Phaser.Keyboard.CLEAR = 12;
|
||||
Phaser.Keyboard.ENTER = 13;
|
||||
Phaser.Keyboard.SHIFT = 16;
|
||||
Phaser.Keyboard.CONTROL = 17;
|
||||
Phaser.Keyboard.ALT = 18;
|
||||
Phaser.Keyboard.CAPS_LOCK = 20;
|
||||
Phaser.Keyboard.ESC = 27;
|
||||
Phaser.Keyboard.SPACEBAR = 32;
|
||||
Phaser.Keyboard.PAGE_UP = 33;
|
||||
Phaser.Keyboard.PAGE_DOWN = 34;
|
||||
Phaser.Keyboard.END = 35;
|
||||
Phaser.Keyboard.HOME = 36;
|
||||
Phaser.Keyboard.LEFT = 37;
|
||||
Phaser.Keyboard.UP = 38;
|
||||
Phaser.Keyboard.RIGHT = 39;
|
||||
Phaser.Keyboard.DOWN = 40;
|
||||
Phaser.Keyboard.INSERT = 45;
|
||||
Phaser.Keyboard.DELETE = 46;
|
||||
Phaser.Keyboard.HELP = 47;
|
||||
Phaser.Keyboard.NUM_LOCK = 144;
|
||||
176
src/input/MSPointer.js
Normal file
176
src/input/MSPointer.js
Normal file
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Phaser - MSPointer constructor.
|
||||
*
|
||||
* @class Phaser.MSPointer
|
||||
* @classdesc The MSPointer class handles touch interactions with the game and the resulting Pointer objects.
|
||||
* It will work only in Internet Explorer 10 and Windows Store or Windows Phone 8 apps using JavaScript.
|
||||
* http://msdn.microsoft.com/en-us/library/ie/hh673557(v=vs.85).aspx
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
Phaser.MSPointer = function (game) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {Object} callbackContext - The context under which callbacks are called (defaults to game).
|
||||
*/
|
||||
this.callbackContext = this.game;
|
||||
|
||||
/**
|
||||
* You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
|
||||
* @property {boolean} disabled
|
||||
*/
|
||||
this.disabled = false;
|
||||
|
||||
/**
|
||||
* @property {function} _onMSPointerDown - Internal function to handle MSPointer events.
|
||||
* @private
|
||||
*/
|
||||
this._onMSPointerDown = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onMSPointerMove - Internal function to handle MSPointer events.
|
||||
* @private
|
||||
*/
|
||||
this._onMSPointerMove = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onMSPointerUp - Internal function to handle MSPointer events.
|
||||
* @private
|
||||
*/
|
||||
this._onMSPointerUp = null;
|
||||
|
||||
};
|
||||
|
||||
Phaser.MSPointer.prototype = {
|
||||
|
||||
/**
|
||||
* Starts the event listeners running.
|
||||
* @method Phaser.MSPointer#start
|
||||
*/
|
||||
start: function () {
|
||||
|
||||
if (this._onMSPointerDown !== null)
|
||||
{
|
||||
// Avoid setting multiple listeners
|
||||
return;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
if (this.game.device.mspointer === true)
|
||||
{
|
||||
this._onMSPointerDown = function (event) {
|
||||
return _this.onPointerDown(event);
|
||||
};
|
||||
|
||||
this._onMSPointerMove = function (event) {
|
||||
return _this.onPointerMove(event);
|
||||
};
|
||||
|
||||
this._onMSPointerUp = function (event) {
|
||||
return _this.onPointerUp(event);
|
||||
};
|
||||
|
||||
this.game.renderer.view.addEventListener('MSPointerDown', this._onMSPointerDown, false);
|
||||
this.game.renderer.view.addEventListener('MSPointerMove', this._onMSPointerMove, false);
|
||||
this.game.renderer.view.addEventListener('MSPointerUp', this._onMSPointerUp, false);
|
||||
|
||||
// IE11+ uses non-prefix events
|
||||
this.game.renderer.view.addEventListener('pointerDown', this._onMSPointerDown, false);
|
||||
this.game.renderer.view.addEventListener('pointerMove', this._onMSPointerMove, false);
|
||||
this.game.renderer.view.addEventListener('pointerUp', this._onMSPointerUp, false);
|
||||
|
||||
this.game.renderer.view.style['-ms-content-zooming'] = 'none';
|
||||
this.game.renderer.view.style['-ms-touch-action'] = 'none';
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The function that handles the PointerDown event.
|
||||
* @method Phaser.MSPointer#onPointerDown
|
||||
* @param {PointerEvent} event
|
||||
*/
|
||||
onPointerDown: function (event) {
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
event.identifier = event.pointerId;
|
||||
|
||||
this.game.input.startPointer(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The function that handles the PointerMove event.
|
||||
* @method Phaser.MSPointer#onPointerMove
|
||||
* @param {PointerEvent } event
|
||||
*/
|
||||
onPointerMove: function (event) {
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
event.identifier = event.pointerId;
|
||||
|
||||
this.game.input.updatePointer(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The function that handles the PointerUp event.
|
||||
* @method Phaser.MSPointer#onPointerUp
|
||||
* @param {PointerEvent} event
|
||||
*/
|
||||
onPointerUp: function (event) {
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
event.identifier = event.pointerId;
|
||||
|
||||
this.game.input.stopPointer(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop the event listeners.
|
||||
* @method Phaser.MSPointer#stop
|
||||
*/
|
||||
stop: function () {
|
||||
|
||||
this.game.canvas.removeEventListener('MSPointerDown', this._onMSPointerDown);
|
||||
this.game.canvas.removeEventListener('MSPointerMove', this._onMSPointerMove);
|
||||
this.game.canvas.removeEventListener('MSPointerUp', this._onMSPointerUp);
|
||||
|
||||
this.game.canvas.removeEventListener('pointerDown', this._onMSPointerDown);
|
||||
this.game.canvas.removeEventListener('pointerMove', this._onMSPointerMove);
|
||||
this.game.canvas.removeEventListener('pointerUp', this._onMSPointerUp);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.MSPointer.prototype.constructor = Phaser.MSPointer;
|
||||
442
src/input/Mouse.js
Normal file
442
src/input/Mouse.js
Normal file
@@ -0,0 +1,442 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Phaser.Mouse is responsible for handling all aspects of mouse interaction with the browser. It captures and processes mouse events.
|
||||
*
|
||||
* @class Phaser.Mouse
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
Phaser.Mouse = function (game) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {Object} callbackContext - The context under which callbacks are called.
|
||||
*/
|
||||
this.callbackContext = this.game;
|
||||
|
||||
/**
|
||||
* @property {function} mouseDownCallback - A callback that can be fired when the mouse is pressed down.
|
||||
*/
|
||||
this.mouseDownCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} mouseMoveCallback - A callback that can be fired when the mouse is moved while pressed down.
|
||||
*/
|
||||
this.mouseMoveCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} mouseUpCallback - A callback that can be fired when the mouse is released from a pressed down state.
|
||||
*/
|
||||
this.mouseUpCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} mouseOutCallback - A callback that can be fired when the mouse is no longer over the game canvas.
|
||||
*/
|
||||
this.mouseOutCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} mouseOverCallback - A callback that can be fired when the mouse enters the game canvas (usually after a mouseout).
|
||||
*/
|
||||
this.mouseOverCallback = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propogate fully.
|
||||
*/
|
||||
this.capture = false;
|
||||
|
||||
/**
|
||||
* @property {number} button- The type of click, either: Phaser.Mouse.NO_BUTTON, Phaser.Mouse.LEFT_BUTTON, Phaser.Mouse.MIDDLE_BUTTON or Phaser.Mouse.RIGHT_BUTTON.
|
||||
* @default
|
||||
*/
|
||||
this.button = -1;
|
||||
|
||||
/**
|
||||
* @property {boolean} disabled - You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
|
||||
* @default
|
||||
*/
|
||||
this.disabled = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} locked - If the mouse has been Pointer Locked successfully this will be set to true.
|
||||
* @default
|
||||
*/
|
||||
this.locked = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} stopOnGameOut - If true Pointer.stop will be called if the mouse leaves the game canvas.
|
||||
* @default
|
||||
*/
|
||||
this.stopOnGameOut = false;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} pointerLock - This event is dispatched when the browser enters or leaves pointer lock state.
|
||||
* @default
|
||||
*/
|
||||
this.pointerLock = new Phaser.Signal();
|
||||
|
||||
/**
|
||||
* @property {MouseEvent} event - The browser mouse DOM event. Will be set to null if no mouse event has ever been received.
|
||||
* @default
|
||||
*/
|
||||
this.event = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onMouseDown - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onMouseDown = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onMouseMove - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onMouseMove = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onMouseUp - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onMouseUp = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onMouseOut - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onMouseOut = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onMouseOver - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onMouseOver = null;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @type {number}
|
||||
*/
|
||||
Phaser.Mouse.NO_BUTTON = -1;
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @type {number}
|
||||
*/
|
||||
Phaser.Mouse.LEFT_BUTTON = 0;
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @type {number}
|
||||
*/
|
||||
Phaser.Mouse.MIDDLE_BUTTON = 1;
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @type {number}
|
||||
*/
|
||||
Phaser.Mouse.RIGHT_BUTTON = 2;
|
||||
|
||||
Phaser.Mouse.prototype = {
|
||||
|
||||
/**
|
||||
* Starts the event listeners running.
|
||||
* @method Phaser.Mouse#start
|
||||
*/
|
||||
start: function () {
|
||||
|
||||
if (this.game.device.android && this.game.device.chrome === false)
|
||||
{
|
||||
// Android stock browser fires mouse events even if you preventDefault on the touchStart, so ...
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._onMouseDown !== null)
|
||||
{
|
||||
// Avoid setting multiple listeners
|
||||
return;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
this._onMouseDown = function (event) {
|
||||
return _this.onMouseDown(event);
|
||||
};
|
||||
|
||||
this._onMouseMove = function (event) {
|
||||
return _this.onMouseMove(event);
|
||||
};
|
||||
|
||||
this._onMouseUp = function (event) {
|
||||
return _this.onMouseUp(event);
|
||||
};
|
||||
|
||||
this._onMouseOut = function (event) {
|
||||
return _this.onMouseOut(event);
|
||||
};
|
||||
|
||||
this._onMouseOver = function (event) {
|
||||
return _this.onMouseOver(event);
|
||||
};
|
||||
|
||||
this.game.canvas.addEventListener('mousedown', this._onMouseDown, true);
|
||||
this.game.canvas.addEventListener('mousemove', this._onMouseMove, true);
|
||||
this.game.canvas.addEventListener('mouseup', this._onMouseUp, true);
|
||||
this.game.canvas.addEventListener('mouseover', this._onMouseOver, true);
|
||||
this.game.canvas.addEventListener('mouseout', this._onMouseOut, true);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The internal method that handles the mouse down event from the browser.
|
||||
* @method Phaser.Mouse#onMouseDown
|
||||
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
|
||||
*/
|
||||
onMouseDown: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.capture)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
this.button = event.button;
|
||||
|
||||
if (this.mouseDownCallback)
|
||||
{
|
||||
this.mouseDownCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
event['identifier'] = 0;
|
||||
|
||||
this.game.input.mousePointer.start(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The internal method that handles the mouse move event from the browser.
|
||||
* @method Phaser.Mouse#onMouseMove
|
||||
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
|
||||
*/
|
||||
onMouseMove: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.capture)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
if (this.mouseMoveCallback)
|
||||
{
|
||||
this.mouseMoveCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
event['identifier'] = 0;
|
||||
|
||||
this.game.input.mousePointer.move(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The internal method that handles the mouse up event from the browser.
|
||||
* @method Phaser.Mouse#onMouseUp
|
||||
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
|
||||
*/
|
||||
onMouseUp: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.capture)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
this.button = Phaser.Mouse.NO_BUTTON;
|
||||
|
||||
if (this.mouseUpCallback)
|
||||
{
|
||||
this.mouseUpCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
event['identifier'] = 0;
|
||||
|
||||
this.game.input.mousePointer.stop(event);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The internal method that handles the mouse out event from the browser.
|
||||
*
|
||||
* @method Phaser.Mouse#onMouseOut
|
||||
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
|
||||
*/
|
||||
onMouseOut: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.capture)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
if (this.mouseOutCallback)
|
||||
{
|
||||
this.mouseOutCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.game.input.mousePointer.withinGame = false;
|
||||
|
||||
if (this.stopOnGameOut)
|
||||
{
|
||||
event['identifier'] = 0;
|
||||
|
||||
this.game.input.mousePointer.stop(event);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The internal method that handles the mouse over event from the browser.
|
||||
*
|
||||
* @method Phaser.Mouse#onMouseOver
|
||||
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
|
||||
*/
|
||||
onMouseOver: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.capture)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
if (this.mouseOverCallback)
|
||||
{
|
||||
this.mouseOverCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.game.input.mousePointer.withinGame = true;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If the browser supports it you can request that the pointer be locked to the browser window.
|
||||
* This is classically known as 'FPS controls', where the pointer can't leave the browser until the user presses an exit key.
|
||||
* If the browser successfully enters a locked state the event Phaser.Mouse.pointerLock will be dispatched and the first parameter will be 'true'.
|
||||
* @method Phaser.Mouse#requestPointerLock
|
||||
*/
|
||||
requestPointerLock: function () {
|
||||
|
||||
if (this.game.device.pointerLock)
|
||||
{
|
||||
var element = this.game.canvas;
|
||||
|
||||
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
|
||||
|
||||
element.requestPointerLock();
|
||||
|
||||
var _this = this;
|
||||
|
||||
this._pointerLockChange = function (event) {
|
||||
return _this.pointerLockChange(event);
|
||||
};
|
||||
|
||||
document.addEventListener('pointerlockchange', this._pointerLockChange, true);
|
||||
document.addEventListener('mozpointerlockchange', this._pointerLockChange, true);
|
||||
document.addEventListener('webkitpointerlockchange', this._pointerLockChange, true);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal pointerLockChange handler.
|
||||
* @method Phaser.Mouse#pointerLockChange
|
||||
* @param {pointerlockchange} event - The native event from the browser. This gets stored in Mouse.event.
|
||||
*/
|
||||
pointerLockChange: function (event) {
|
||||
|
||||
var element = this.game.canvas;
|
||||
|
||||
if (document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element)
|
||||
{
|
||||
// Pointer was successfully locked
|
||||
this.locked = true;
|
||||
this.pointerLock.dispatch(true, event);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Pointer was unlocked
|
||||
this.locked = false;
|
||||
this.pointerLock.dispatch(false, event);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal release pointer lock handler.
|
||||
* @method Phaser.Mouse#releasePointerLock
|
||||
*/
|
||||
releasePointerLock: function () {
|
||||
|
||||
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
|
||||
|
||||
document.exitPointerLock();
|
||||
|
||||
document.removeEventListener('pointerlockchange', this._pointerLockChange, true);
|
||||
document.removeEventListener('mozpointerlockchange', this._pointerLockChange, true);
|
||||
document.removeEventListener('webkitpointerlockchange', this._pointerLockChange, true);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop the event listeners.
|
||||
* @method Phaser.Mouse#stop
|
||||
*/
|
||||
stop: function () {
|
||||
|
||||
this.game.canvas.removeEventListener('mousedown', this._onMouseDown, true);
|
||||
this.game.canvas.removeEventListener('mousemove', this._onMouseMove, true);
|
||||
this.game.canvas.removeEventListener('mouseup', this._onMouseUp, true);
|
||||
this.game.canvas.removeEventListener('mouseover', this._onMouseOver, true);
|
||||
this.game.canvas.removeEventListener('mouseout', this._onMouseOut, true);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Mouse.prototype.constructor = Phaser.Mouse;
|
||||
720
src/input/Pointer.js
Normal file
720
src/input/Pointer.js
Normal file
@@ -0,0 +1,720 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Phaser - Pointer constructor.
|
||||
*
|
||||
* @class Phaser.Pointer
|
||||
* @classdesc A Pointer object is used by the Mouse, Touch and MSPoint managers and represents a single finger on the touch screen.
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
* @param {number} id - The ID of the Pointer object within the game. Each game can have up to 10 active pointers.
|
||||
*/
|
||||
Phaser.Pointer = function (game, id) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {number} id - The ID of the Pointer object within the game. Each game can have up to 10 active pointers.
|
||||
*/
|
||||
this.id = id;
|
||||
|
||||
/**
|
||||
* @property {number} type - The const type of this object.
|
||||
* @readonly
|
||||
*/
|
||||
this.type = Phaser.POINTER;
|
||||
|
||||
/**
|
||||
* @property {boolean} exists - A Pointer object that exists is allowed to be checked for physics collisions and overlaps.
|
||||
* @default
|
||||
*/
|
||||
this.exists = true;
|
||||
|
||||
/**
|
||||
* @property {number} identifier - The identifier property of the Pointer as set by the DOM event when this Pointer is started.
|
||||
* @default
|
||||
*/
|
||||
this.identifier = 0;
|
||||
|
||||
/**
|
||||
* @property {number} pointerId - The pointerId property of the Pointer as set by the DOM event when this Pointer is started. The browser can and will recycle this value.
|
||||
* @default
|
||||
*/
|
||||
this.pointerId = null;
|
||||
|
||||
/**
|
||||
* @property {any} target - The target property of the Pointer as set by the DOM event when this Pointer is started.
|
||||
* @default
|
||||
*/
|
||||
this.target = null;
|
||||
|
||||
/**
|
||||
* @property {any} button - The button property of the Pointer as set by the DOM event when this Pointer is started.
|
||||
* @default
|
||||
*/
|
||||
this.button = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} _holdSent - Local private variable to store the status of dispatching a hold event.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._holdSent = false;
|
||||
|
||||
/**
|
||||
* @property {array} _history - Local private variable storing the short-term history of pointer movements.
|
||||
* @private
|
||||
*/
|
||||
this._history = [];
|
||||
|
||||
/**
|
||||
* @property {number} _nextDrop - Local private variable storing the time at which the next history drop should occur.
|
||||
* @private
|
||||
*/
|
||||
this._nextDrop = 0;
|
||||
|
||||
/**
|
||||
* @property {boolean} _stateReset - Monitor events outside of a state reset loop.
|
||||
* @private
|
||||
*/
|
||||
this._stateReset = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} withinGame - true if the Pointer is over the game canvas, otherwise false.
|
||||
*/
|
||||
this.withinGame = false;
|
||||
|
||||
/**
|
||||
* @property {number} clientX - The horizontal coordinate of the Pointer within the application's client area at which the event occurred (as opposed to the coordinates within the page).
|
||||
*/
|
||||
this.clientX = -1;
|
||||
|
||||
/**
|
||||
* @property {number} clientY - The vertical coordinate of the Pointer within the application's client area at which the event occurred (as opposed to the coordinates within the page).
|
||||
*/
|
||||
this.clientY = -1;
|
||||
|
||||
/**
|
||||
* @property {number} pageX - The horizontal coordinate of the Pointer relative to whole document.
|
||||
*/
|
||||
this.pageX = -1;
|
||||
|
||||
/**
|
||||
* @property {number} pageY - The vertical coordinate of the Pointer relative to whole document.
|
||||
*/
|
||||
this.pageY = -1;
|
||||
|
||||
/**
|
||||
* @property {number} screenX - The horizontal coordinate of the Pointer relative to the screen.
|
||||
*/
|
||||
this.screenX = -1;
|
||||
|
||||
/**
|
||||
* @property {number} screenY - The vertical coordinate of the Pointer relative to the screen.
|
||||
*/
|
||||
this.screenY = -1;
|
||||
|
||||
/**
|
||||
* @property {number} rawMovementX - The horizontal raw relative movement of the Pointer in pixels since last event.
|
||||
* @default
|
||||
*/
|
||||
this.rawMovementX = 0;
|
||||
|
||||
/**
|
||||
* @property {number} rawMovementY - The vertical raw relative movement of the Pointer in pixels since last event.
|
||||
* @default
|
||||
*/
|
||||
this.rawMovementY = 0;
|
||||
|
||||
/**
|
||||
* @property {number} movementX - The horizontal processed relative movement of the Pointer in pixels since last event.
|
||||
* @default
|
||||
*/
|
||||
this.movementX = 0;
|
||||
|
||||
/**
|
||||
* @property {number} movementY - The vertical processed relative movement of the Pointer in pixels since last event.
|
||||
* @default
|
||||
*/
|
||||
this.movementY = 0;
|
||||
|
||||
/**
|
||||
* @property {number} x - The horizontal coordinate of the Pointer. This value is automatically scaled based on the game scale.
|
||||
* @default
|
||||
*/
|
||||
this.x = -1;
|
||||
|
||||
/**
|
||||
* @property {number} y - The vertical coordinate of the Pointer. This value is automatically scaled based on the game scale.
|
||||
* @default
|
||||
*/
|
||||
this.y = -1;
|
||||
|
||||
/**
|
||||
* @property {boolean} isMouse - If the Pointer is a mouse this is true, otherwise false.
|
||||
* @default
|
||||
*/
|
||||
this.isMouse = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} isDown - If the Pointer is touching the touchscreen, or the mouse button is held down, isDown is set to true.
|
||||
* @default
|
||||
*/
|
||||
this.isDown = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} isUp - If the Pointer is not touching the touchscreen, or the mouse button is up, isUp is set to true.
|
||||
* @default
|
||||
*/
|
||||
this.isUp = true;
|
||||
|
||||
/**
|
||||
* @property {number} timeDown - A timestamp representing when the Pointer first touched the touchscreen.
|
||||
* @default
|
||||
*/
|
||||
this.timeDown = 0;
|
||||
|
||||
/**
|
||||
* @property {number} timeUp - A timestamp representing when the Pointer left the touchscreen.
|
||||
* @default
|
||||
*/
|
||||
this.timeUp = 0;
|
||||
|
||||
/**
|
||||
* @property {number} previousTapTime - A timestamp representing when the Pointer was last tapped or clicked.
|
||||
* @default
|
||||
*/
|
||||
this.previousTapTime = 0;
|
||||
|
||||
/**
|
||||
* @property {number} totalTouches - The total number of times this Pointer has been touched to the touchscreen.
|
||||
* @default
|
||||
*/
|
||||
this.totalTouches = 0;
|
||||
|
||||
/**
|
||||
* @property {number} msSinceLastClick - The number of milliseconds since the last click or touch event.
|
||||
* @default
|
||||
*/
|
||||
this.msSinceLastClick = Number.MAX_VALUE;
|
||||
|
||||
/**
|
||||
* @property {any} targetObject - The Game Object this Pointer is currently over / touching / dragging.
|
||||
* @default
|
||||
*/
|
||||
this.targetObject = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} active - An active pointer is one that is currently pressed down on the display. A Mouse is always active.
|
||||
* @default
|
||||
*/
|
||||
this.active = false;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} position - A Phaser.Point object containing the current x/y values of the pointer on the display.
|
||||
*/
|
||||
this.position = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} positionDown - A Phaser.Point object containing the x/y values of the pointer when it was last in a down state on the display.
|
||||
*/
|
||||
this.positionDown = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} positionUp - A Phaser.Point object containing the x/y values of the pointer when it was last released.
|
||||
*/
|
||||
this.positionUp = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* A Phaser.Circle that is centered on the x/y coordinates of this pointer, useful for hit detection.
|
||||
* The Circle size is 44px (Apples recommended "finger tip" size).
|
||||
* @property {Phaser.Circle} circle
|
||||
*/
|
||||
this.circle = new Phaser.Circle(0, 0, 44);
|
||||
|
||||
if (id === 0)
|
||||
{
|
||||
this.isMouse = true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Pointer.prototype = {
|
||||
|
||||
/**
|
||||
* Called when the Pointer is pressed onto the touchscreen.
|
||||
* @method Phaser.Pointer#start
|
||||
* @param {any} event - The DOM event from the browser.
|
||||
*/
|
||||
start: function (event) {
|
||||
|
||||
if (event['pointerId'])
|
||||
{
|
||||
this.pointerId = event.pointerId;
|
||||
}
|
||||
|
||||
this.identifier = event.identifier;
|
||||
this.target = event.target;
|
||||
|
||||
if (typeof event.button !== 'undefined')
|
||||
{
|
||||
this.button = event.button;
|
||||
}
|
||||
|
||||
this._history = [];
|
||||
this.active = true;
|
||||
this.withinGame = true;
|
||||
this.isDown = true;
|
||||
this.isUp = false;
|
||||
|
||||
// Work out how long it has been since the last click
|
||||
this.msSinceLastClick = this.game.time.now - this.timeDown;
|
||||
this.timeDown = this.game.time.now;
|
||||
this._holdSent = false;
|
||||
|
||||
// This sets the x/y and other local values
|
||||
this.move(event, true);
|
||||
|
||||
// x and y are the old values here?
|
||||
this.positionDown.setTo(this.x, this.y);
|
||||
|
||||
if (this.game.input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers === 0))
|
||||
{
|
||||
this.game.input.x = this.x;
|
||||
this.game.input.y = this.y;
|
||||
this.game.input.position.setTo(this.x, this.y);
|
||||
this.game.input.onDown.dispatch(this, event);
|
||||
this.game.input.resetSpeed(this.x, this.y);
|
||||
}
|
||||
|
||||
this._stateReset = false;
|
||||
this.totalTouches++;
|
||||
|
||||
if (!this.isMouse)
|
||||
{
|
||||
this.game.input.currentPointers++;
|
||||
}
|
||||
|
||||
if (this.targetObject !== null)
|
||||
{
|
||||
this.targetObject._touchedHandler(this);
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called by the Input Manager.
|
||||
* @method Phaser.Pointer#update
|
||||
*/
|
||||
update: function () {
|
||||
|
||||
if (this.active)
|
||||
{
|
||||
if (this._holdSent === false && this.duration >= this.game.input.holdRate)
|
||||
{
|
||||
if (this.game.input.multiInputOverride == Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers === 0))
|
||||
{
|
||||
this.game.input.onHold.dispatch(this);
|
||||
}
|
||||
|
||||
this._holdSent = true;
|
||||
}
|
||||
|
||||
// Update the droppings history
|
||||
if (this.game.input.recordPointerHistory && this.game.time.now >= this._nextDrop)
|
||||
{
|
||||
this._nextDrop = this.game.time.now + this.game.input.recordRate;
|
||||
|
||||
this._history.push({
|
||||
x: this.position.x,
|
||||
y: this.position.y
|
||||
});
|
||||
|
||||
if (this._history.length > this.game.input.recordLimit)
|
||||
{
|
||||
this._history.shift();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called when the Pointer is moved.
|
||||
* @method Phaser.Pointer#move
|
||||
* @param {MouseEvent|PointerEvent|TouchEvent} event - The event passed up from the input handler.
|
||||
* @param {boolean} [fromClick=false] - Was this called from the click event?
|
||||
*/
|
||||
move: function (event, fromClick) {
|
||||
|
||||
if (this.game.input.pollLocked)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof fromClick === 'undefined') { fromClick = false; }
|
||||
|
||||
if (typeof event.button !== 'undefined')
|
||||
{
|
||||
this.button = event.button;
|
||||
}
|
||||
|
||||
this.clientX = event.clientX;
|
||||
this.clientY = event.clientY;
|
||||
|
||||
this.pageX = event.pageX;
|
||||
this.pageY = event.pageY;
|
||||
|
||||
this.screenX = event.screenX;
|
||||
this.screenY = event.screenY;
|
||||
|
||||
if (this.isMouse && this.game.input.mouse.locked && !fromClick)
|
||||
{
|
||||
this.rawMovementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
|
||||
this.rawMovementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
|
||||
|
||||
this.movementX += this.rawMovementX;
|
||||
this.movementY += this.rawMovementY;
|
||||
}
|
||||
|
||||
this.x = (this.pageX - this.game.stage.offset.x) * this.game.input.scale.x;
|
||||
this.y = (this.pageY - this.game.stage.offset.y) * this.game.input.scale.y;
|
||||
|
||||
this.position.setTo(this.x, this.y);
|
||||
this.circle.x = this.x;
|
||||
this.circle.y = this.y;
|
||||
|
||||
if (this.game.input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers === 0))
|
||||
{
|
||||
this.game.input.activePointer = this;
|
||||
this.game.input.x = this.x;
|
||||
this.game.input.y = this.y;
|
||||
this.game.input.position.setTo(this.game.input.x, this.game.input.y);
|
||||
this.game.input.circle.x = this.game.input.x;
|
||||
this.game.input.circle.y = this.game.input.y;
|
||||
}
|
||||
|
||||
this.withinGame = this.game.scale.bounds.contains(this.pageX, this.pageY);
|
||||
|
||||
// If the game is paused we don't process any target objects or callbacks
|
||||
if (this.game.paused)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
// DEPRECATED - Soon to be removed
|
||||
if (this.game.input.moveCallback)
|
||||
{
|
||||
this.game.input.moveCallback.call(this.game.input.moveCallbackContext, this, this.x, this.y);
|
||||
}
|
||||
|
||||
var i = this.game.input.moveCallbacks.length;
|
||||
|
||||
while (i--)
|
||||
{
|
||||
this.game.input.moveCallbacks[i].callback.call(this.game.input.moveCallbacks[i].context, this, this.x, this.y);
|
||||
}
|
||||
|
||||
// Easy out if we're dragging something and it still exists
|
||||
if (this.targetObject !== null && this.targetObject.isDragged === true)
|
||||
{
|
||||
if (this.targetObject.update(this) === false)
|
||||
{
|
||||
this.targetObject = null;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// Work out which object is on the top
|
||||
this._highestRenderOrderID = Number.MAX_SAFE_INTEGER;
|
||||
this._highestRenderObject = null;
|
||||
this._highestInputPriorityID = -1;
|
||||
|
||||
// Run through the list
|
||||
if (this.game.input.interactiveItems.total > 0)
|
||||
{
|
||||
var currentNode = this.game.input.interactiveItems.first;
|
||||
|
||||
do
|
||||
{
|
||||
// If the object is using pixelPerfect checks, or has a higher InputManager.PriorityID OR if the priority ID is the same as the current highest AND it has a higher renderOrderID, then set it to the top
|
||||
if (currentNode && currentNode.validForInput(this._highestInputPriorityID, this._highestRenderOrderID))
|
||||
{
|
||||
if ((!fromClick && currentNode.checkPointerOver(this)) || (fromClick && currentNode.checkPointerDown(this)))
|
||||
{
|
||||
this._highestRenderOrderID = currentNode.sprite._cache[3]; // renderOrderID
|
||||
this._highestInputPriorityID = currentNode.priorityID;
|
||||
this._highestRenderObject = currentNode;
|
||||
}
|
||||
}
|
||||
currentNode = this.game.input.interactiveItems.next;
|
||||
}
|
||||
while (currentNode !== null);
|
||||
}
|
||||
|
||||
if (this._highestRenderObject === null)
|
||||
{
|
||||
// The pointer isn't currently over anything, check if we've got a lingering previous target
|
||||
if (this.targetObject)
|
||||
{
|
||||
// console.log("The pointer isn't currently over anything, check if we've got a lingering previous target");
|
||||
this.targetObject._pointerOutHandler(this);
|
||||
this.targetObject = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.targetObject === null)
|
||||
{
|
||||
// And now set the new one
|
||||
// console.log('And now set the new one');
|
||||
this.targetObject = this._highestRenderObject;
|
||||
this._highestRenderObject._pointerOverHandler(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// We've got a target from the last update
|
||||
// console.log("We've got a target from the last update");
|
||||
if (this.targetObject === this._highestRenderObject)
|
||||
{
|
||||
// Same target as before, so update it
|
||||
// console.log("Same target as before, so update it");
|
||||
if (this._highestRenderObject.update(this) === false)
|
||||
{
|
||||
this.targetObject = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// The target has changed, so tell the old one we've left it
|
||||
// console.log("The target has changed, so tell the old one we've left it");
|
||||
this.targetObject._pointerOutHandler(this);
|
||||
|
||||
// And now set the new one
|
||||
this.targetObject = this._highestRenderObject;
|
||||
this.targetObject._pointerOverHandler(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called when the Pointer leaves the target area.
|
||||
*
|
||||
* @method Phaser.Pointer#leave
|
||||
* @param {MouseEvent|PointerEvent|TouchEvent} event - The event passed up from the input handler.
|
||||
*/
|
||||
leave: function (event) {
|
||||
|
||||
this.withinGame = false;
|
||||
this.move(event, false);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called when the Pointer leaves the touchscreen.
|
||||
*
|
||||
* @method Phaser.Pointer#stop
|
||||
* @param {MouseEvent|PointerEvent|TouchEvent} event - The event passed up from the input handler.
|
||||
*/
|
||||
stop: function (event) {
|
||||
|
||||
if (this._stateReset)
|
||||
{
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
this.timeUp = this.game.time.now;
|
||||
|
||||
if (this.game.input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers === 0))
|
||||
{
|
||||
this.game.input.onUp.dispatch(this, event);
|
||||
|
||||
// Was it a tap?
|
||||
if (this.duration >= 0 && this.duration <= this.game.input.tapRate)
|
||||
{
|
||||
// Was it a double-tap?
|
||||
if (this.timeUp - this.previousTapTime < this.game.input.doubleTapRate)
|
||||
{
|
||||
// Yes, let's dispatch the signal then with the 2nd parameter set to true
|
||||
this.game.input.onTap.dispatch(this, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Wasn't a double-tap, so dispatch a single tap signal
|
||||
this.game.input.onTap.dispatch(this, false);
|
||||
}
|
||||
|
||||
this.previousTapTime = this.timeUp;
|
||||
}
|
||||
}
|
||||
|
||||
// Mouse is always active
|
||||
if (this.id > 0)
|
||||
{
|
||||
this.active = false;
|
||||
}
|
||||
|
||||
this.withinGame = false;
|
||||
this.isDown = false;
|
||||
this.isUp = true;
|
||||
this.pointerId = null;
|
||||
this.identifier = null;
|
||||
|
||||
this.positionUp.setTo(this.x, this.y);
|
||||
|
||||
if (this.isMouse === false)
|
||||
{
|
||||
this.game.input.currentPointers--;
|
||||
}
|
||||
|
||||
this.game.input.interactiveItems.callAll('_releasedHandler', this);
|
||||
|
||||
this.targetObject = null;
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The Pointer is considered justPressed if the time it was pressed onto the touchscreen or clicked is less than justPressedRate.
|
||||
* Note that calling justPressed doesn't reset the pressed status of the Pointer, it will return `true` for as long as the duration is valid.
|
||||
* If you wish to check if the Pointer was pressed down just once then see the Sprite.events.onInputDown event.
|
||||
* @method Phaser.Pointer#justPressed
|
||||
* @param {number} [duration] - The time to check against. If none given it will use InputManager.justPressedRate.
|
||||
* @return {boolean} true if the Pointer was pressed down within the duration given.
|
||||
*/
|
||||
justPressed: function (duration) {
|
||||
|
||||
duration = duration || this.game.input.justPressedRate;
|
||||
|
||||
return (this.isDown === true && (this.timeDown + duration) > this.game.time.now);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The Pointer is considered justReleased if the time it left the touchscreen is less than justReleasedRate.
|
||||
* Note that calling justReleased doesn't reset the pressed status of the Pointer, it will return `true` for as long as the duration is valid.
|
||||
* If you wish to check if the Pointer was released just once then see the Sprite.events.onInputUp event.
|
||||
* @method Phaser.Pointer#justReleased
|
||||
* @param {number} [duration] - The time to check against. If none given it will use InputManager.justReleasedRate.
|
||||
* @return {boolean} true if the Pointer was released within the duration given.
|
||||
*/
|
||||
justReleased: function (duration) {
|
||||
|
||||
duration = duration || this.game.input.justReleasedRate;
|
||||
|
||||
return (this.isUp === true && (this.timeUp + duration) > this.game.time.now);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Resets the Pointer properties. Called by InputManager.reset when you perform a State change.
|
||||
* @method Phaser.Pointer#reset
|
||||
*/
|
||||
reset: function () {
|
||||
|
||||
if (this.isMouse === false)
|
||||
{
|
||||
this.active = false;
|
||||
}
|
||||
|
||||
this.pointerId = null;
|
||||
this.identifier = null;
|
||||
this.isDown = false;
|
||||
this.isUp = true;
|
||||
this.totalTouches = 0;
|
||||
this._holdSent = false;
|
||||
this._history.length = 0;
|
||||
this._stateReset = true;
|
||||
|
||||
if (this.targetObject)
|
||||
{
|
||||
this.targetObject._releasedHandler(this);
|
||||
}
|
||||
|
||||
this.targetObject = null;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Resets the movementX and movementY properties. Use in your update handler after retrieving the values.
|
||||
* @method Phaser.Pointer#resetMovement
|
||||
*/
|
||||
resetMovement: function() {
|
||||
|
||||
this.movementX = 0;
|
||||
this.movementY = 0;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Pointer.prototype.constructor = Phaser.Pointer;
|
||||
|
||||
/**
|
||||
* How long the Pointer has been depressed on the touchscreen. If not currently down it returns -1.
|
||||
* @name Phaser.Pointer#duration
|
||||
* @property {number} duration - How long the Pointer has been depressed on the touchscreen. If not currently down it returns -1.
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Pointer.prototype, "duration", {
|
||||
|
||||
get: function () {
|
||||
|
||||
if (this.isUp)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return this.game.time.now - this.timeDown;
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the X value of this Pointer in world coordinates based on the world camera.
|
||||
* @name Phaser.Pointer#worldX
|
||||
* @property {number} duration - The X value of this Pointer in world coordinates based on the world camera.
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Pointer.prototype, "worldX", {
|
||||
|
||||
get: function () {
|
||||
|
||||
return this.game.world.camera.x + this.x;
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the Y value of this Pointer in world coordinates based on the world camera.
|
||||
* @name Phaser.Pointer#worldY
|
||||
* @property {number} duration - The Y value of this Pointer in world coordinates based on the world camera.
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.Pointer.prototype, "worldY", {
|
||||
|
||||
get: function () {
|
||||
|
||||
return this.game.world.camera.y + this.y;
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
569
src/input/SinglePad.js
Normal file
569
src/input/SinglePad.js
Normal file
@@ -0,0 +1,569 @@
|
||||
/**
|
||||
* @author @karlmacklin <tacklemcclean@gmail.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class Phaser.SinglePad
|
||||
* @classdesc A single Phaser Gamepad
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - Current game instance.
|
||||
* @param {Object} padParent - The parent Phaser.Gamepad object (all gamepads reside under this)
|
||||
*/
|
||||
Phaser.SinglePad = function (game, padParent) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Gamepad} padParent - Main Phaser Gamepad object
|
||||
*/
|
||||
this._padParent = padParent;
|
||||
|
||||
/**
|
||||
* @property {number} index - The gamepad index as per browsers data
|
||||
* @default
|
||||
*/
|
||||
this._index = null;
|
||||
|
||||
/**
|
||||
* @property {Object} _rawPad - The 'raw' gamepad data.
|
||||
* @private
|
||||
*/
|
||||
this._rawPad = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} _connected - Is this pad connected or not.
|
||||
* @private
|
||||
*/
|
||||
this._connected = false;
|
||||
|
||||
/**
|
||||
* @property {number} _prevTimestamp - Used to check for differences between earlier polls and current state of gamepads.
|
||||
* @private
|
||||
*/
|
||||
this._prevTimestamp = null;
|
||||
|
||||
/**
|
||||
* @property {Array} _rawButtons - The 'raw' button state.
|
||||
* @private
|
||||
*/
|
||||
this._rawButtons = [];
|
||||
|
||||
/**
|
||||
* @property {Array} _buttons - Current Phaser state of the buttons.
|
||||
* @private
|
||||
*/
|
||||
this._buttons = [];
|
||||
|
||||
/**
|
||||
* @property {Array} _axes - Current axes state.
|
||||
* @private
|
||||
*/
|
||||
this._axes = [];
|
||||
|
||||
/**
|
||||
* @property {Array} _hotkeys - Hotkey buttons.
|
||||
* @private
|
||||
*/
|
||||
this._hotkeys = [];
|
||||
|
||||
/**
|
||||
* @property {Object} callbackContext - The context under which the callbacks are run.
|
||||
*/
|
||||
this.callbackContext = this;
|
||||
|
||||
/**
|
||||
* @property {function} onConnectCallback - This callback is invoked every time this gamepad is connected
|
||||
*/
|
||||
this.onConnectCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onDisconnectCallback - This callback is invoked every time this gamepad is disconnected
|
||||
*/
|
||||
this.onDisconnectCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onDownCallback - This callback is invoked every time a button is pressed down.
|
||||
*/
|
||||
this.onDownCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onUpCallback - This callback is invoked every time a gamepad button is released.
|
||||
*/
|
||||
this.onUpCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onAxisCallback - This callback is invoked every time an axis is changed.
|
||||
*/
|
||||
this.onAxisCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} onFloatCallback - This callback is invoked every time a button is changed to a value where value > 0 and value < 1.
|
||||
*/
|
||||
this.onFloatCallback = null;
|
||||
|
||||
/**
|
||||
* @property {number} deadZone - Dead zone for axis feedback - within this value you won't trigger updates.
|
||||
*/
|
||||
this.deadZone = 0.26;
|
||||
|
||||
};
|
||||
|
||||
Phaser.SinglePad.prototype = {
|
||||
|
||||
/**
|
||||
* Add callbacks to the this Gamepad to handle connect/disconnect/button down/button up/axis change/float value buttons
|
||||
* @method Phaser.SinglePad#addCallbacks
|
||||
* @param {Object} context - The context under which the callbacks are run.
|
||||
* @param {Object} callbacks - Object that takes six different callbak methods:
|
||||
* onConnectCallback, onDisconnectCallback, onDownCallback, onUpCallback, onAxisCallback, onFloatCallback
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If you need more fine-grained control over a Key you can create a new Phaser.Key object via this method.
|
||||
* The Key object can then be polled, have events attached to it, etc.
|
||||
*
|
||||
* @method Phaser.SinglePad#addButton
|
||||
* @param {number} buttonCode - The buttonCode of the button, i.e. Phaser.Gamepad.BUTTON_0 or Phaser.Gamepad.BUTTON_1
|
||||
* @return {Phaser.GamepadButton} The GamepadButton object which you can store locally and reference directly.
|
||||
*/
|
||||
addButton: function (buttonCode) {
|
||||
|
||||
this._hotkeys[buttonCode] = new Phaser.GamepadButton(this.game, buttonCode);
|
||||
return this._hotkeys[buttonCode];
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Main update function, should be called by Phaser.Gamepad
|
||||
* @method Phaser.SinglePad#pollStatus
|
||||
*/
|
||||
pollStatus: function () {
|
||||
|
||||
if (this._rawPad.timestamp && (this._rawPad.timestamp == this._prevTimestamp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < this._rawPad.buttons.length; i += 1)
|
||||
{
|
||||
var buttonValue = this._rawPad.buttons[i];
|
||||
|
||||
if (this._rawButtons[i] !== buttonValue)
|
||||
{
|
||||
if (buttonValue === 1)
|
||||
{
|
||||
this.processButtonDown(i, buttonValue);
|
||||
}
|
||||
else if (buttonValue === 0)
|
||||
{
|
||||
this.processButtonUp(i, buttonValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.processButtonFloat(i, buttonValue);
|
||||
}
|
||||
|
||||
this._rawButtons[i] = buttonValue;
|
||||
}
|
||||
}
|
||||
|
||||
var axes = this._rawPad.axes;
|
||||
|
||||
for (var j = 0; j < axes.length; j += 1)
|
||||
{
|
||||
var axis = axes[j];
|
||||
|
||||
if (axis > 0 && axis > this.deadZone || axis < 0 && axis < -this.deadZone)
|
||||
{
|
||||
this.processAxisChange({axis: j, value: axis});
|
||||
}
|
||||
else
|
||||
{
|
||||
this.processAxisChange({axis: j, value: 0});
|
||||
}
|
||||
}
|
||||
|
||||
this._prevTimestamp = this._rawPad.timestamp;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gamepad connect function, should be called by Phaser.Gamepad
|
||||
* @method Phaser.SinglePad#connect
|
||||
* @param {Object} rawPad - The raw gamepad object
|
||||
*/
|
||||
connect: function (rawPad) {
|
||||
|
||||
var triggerCallback = !this._connected;
|
||||
|
||||
this._index = rawPad.index;
|
||||
this._connected = true;
|
||||
this._rawPad = rawPad;
|
||||
this._rawButtons = rawPad.buttons;
|
||||
this._axes = rawPad.axes;
|
||||
|
||||
if (triggerCallback && this._padParent.onConnectCallback)
|
||||
{
|
||||
this._padParent.onConnectCallback.call(this._padParent.callbackContext, this._index);
|
||||
}
|
||||
|
||||
if (triggerCallback && this.onConnectCallback)
|
||||
{
|
||||
this.onConnectCallback.call(this.callbackContext);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gamepad disconnect function, should be called by Phaser.Gamepad
|
||||
* @method Phaser.SinglePad#disconnect
|
||||
*/
|
||||
disconnect: function () {
|
||||
|
||||
var triggerCallback = this._connected;
|
||||
this._connected = false;
|
||||
this._rawPad = undefined;
|
||||
this._rawButtons = [];
|
||||
this._buttons = [];
|
||||
var disconnectingIndex = this._index;
|
||||
this._index = null;
|
||||
|
||||
if (triggerCallback && this._padParent.onDisconnectCallback)
|
||||
{
|
||||
this._padParent.onDisconnectCallback.call(this._padParent.callbackContext, disconnectingIndex);
|
||||
}
|
||||
|
||||
if (triggerCallback && this.onDisconnectCallback)
|
||||
{
|
||||
this.onDisconnectCallback.call(this.callbackContext);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Handles changes in axis
|
||||
* @method Phaser.SinglePad#processAxisChange
|
||||
* @param {Object} axisState - State of the relevant axis
|
||||
*/
|
||||
processAxisChange: function (axisState) {
|
||||
|
||||
if (this.game.input.disabled || this.game.input.gamepad.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._axes[axisState.axis] === axisState.value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this._axes[axisState.axis] = axisState.value;
|
||||
|
||||
if (this._padParent.onAxisCallback)
|
||||
{
|
||||
this._padParent.onAxisCallback.call(this._padParent.callbackContext, axisState, this._index);
|
||||
}
|
||||
|
||||
if (this.onAxisCallback)
|
||||
{
|
||||
this.onAxisCallback.call(this.callbackContext, axisState);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Handles button down press
|
||||
* @method Phaser.SinglePad#processButtonDown
|
||||
* @param {number} buttonCode - Which buttonCode of this button
|
||||
* @param {Object} value - Button value
|
||||
*/
|
||||
processButtonDown: function (buttonCode, value) {
|
||||
|
||||
if (this.game.input.disabled || this.game.input.gamepad.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._padParent.onDownCallback)
|
||||
{
|
||||
this._padParent.onDownCallback.call(this._padParent.callbackContext, buttonCode, value, this._index);
|
||||
}
|
||||
|
||||
if (this.onDownCallback)
|
||||
{
|
||||
this.onDownCallback.call(this.callbackContext, buttonCode, value);
|
||||
}
|
||||
|
||||
if (this._buttons[buttonCode] && this._buttons[buttonCode].isDown)
|
||||
{
|
||||
// Key already down and still down, so update
|
||||
this._buttons[buttonCode].duration = this.game.time.now - this._buttons[buttonCode].timeDown;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!this._buttons[buttonCode])
|
||||
{
|
||||
// Not used this button before, so register it
|
||||
this._buttons[buttonCode] = {
|
||||
isDown: true,
|
||||
timeDown: this.game.time.now,
|
||||
timeUp: 0,
|
||||
duration: 0,
|
||||
value: value
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
// Button used before but freshly down
|
||||
this._buttons[buttonCode].isDown = true;
|
||||
this._buttons[buttonCode].timeDown = this.game.time.now;
|
||||
this._buttons[buttonCode].duration = 0;
|
||||
this._buttons[buttonCode].value = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (this._hotkeys[buttonCode])
|
||||
{
|
||||
this._hotkeys[buttonCode].processButtonDown(value);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Handles button release
|
||||
* @method Phaser.SinglePad#processButtonUp
|
||||
* @param {number} buttonCode - Which buttonCode of this button
|
||||
* @param {Object} value - Button value
|
||||
*/
|
||||
processButtonUp: function (buttonCode, value) {
|
||||
|
||||
if (this.game.input.disabled || this.game.input.gamepad.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._padParent.onUpCallback)
|
||||
{
|
||||
this._padParent.onUpCallback.call(this._padParent.callbackContext, buttonCode, value, this._index);
|
||||
}
|
||||
|
||||
if (this.onUpCallback)
|
||||
{
|
||||
this.onUpCallback.call(this.callbackContext, buttonCode, value);
|
||||
}
|
||||
|
||||
if (this._hotkeys[buttonCode])
|
||||
{
|
||||
this._hotkeys[buttonCode].processButtonUp(value);
|
||||
}
|
||||
|
||||
if (this._buttons[buttonCode])
|
||||
{
|
||||
this._buttons[buttonCode].isDown = false;
|
||||
this._buttons[buttonCode].timeUp = this.game.time.now;
|
||||
this._buttons[buttonCode].value = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not used this button before, so register it
|
||||
this._buttons[buttonCode] = {
|
||||
isDown: false,
|
||||
timeDown: this.game.time.now,
|
||||
timeUp: this.game.time.now,
|
||||
duration: 0,
|
||||
value: value
|
||||
};
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Handles buttons with floating values (like analog buttons that acts almost like an axis but still registers like a button)
|
||||
* @method Phaser.SinglePad#processButtonFloat
|
||||
* @param {number} buttonCode - Which buttonCode of this button
|
||||
* @param {Object} value - Button value (will range somewhere between 0 and 1, but not specifically 0 or 1.
|
||||
*/
|
||||
processButtonFloat: function (buttonCode, value) {
|
||||
|
||||
if (this.game.input.disabled || this.game.input.gamepad.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._padParent.onFloatCallback)
|
||||
{
|
||||
this._padParent.onFloatCallback.call(this._padParent.callbackContext, buttonCode, value, this._index);
|
||||
}
|
||||
|
||||
if (this.onFloatCallback)
|
||||
{
|
||||
this.onFloatCallback.call(this.callbackContext, buttonCode, value);
|
||||
}
|
||||
|
||||
if (!this._buttons[buttonCode])
|
||||
{
|
||||
// Not used this button before, so register it
|
||||
this._buttons[buttonCode] = { value: value };
|
||||
}
|
||||
else
|
||||
{
|
||||
// Button used before but freshly down
|
||||
this._buttons[buttonCode].value = value;
|
||||
}
|
||||
|
||||
if (this._hotkeys[buttonCode])
|
||||
{
|
||||
this._hotkeys[buttonCode].processButtonFloat(value);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns value of requested axis
|
||||
* @method Phaser.SinglePad#axis
|
||||
* @param {number} axisCode - The index of the axis to check
|
||||
* @return {number} Axis value if available otherwise false
|
||||
*/
|
||||
axis: function (axisCode) {
|
||||
|
||||
if (this._axes[axisCode])
|
||||
{
|
||||
return this._axes[axisCode];
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true if the button is currently pressed down.
|
||||
* @method Phaser.SinglePad#isDown
|
||||
* @param {number} buttonCode - The buttonCode of the key to check.
|
||||
* @return {boolean} True if the key is currently down.
|
||||
*/
|
||||
isDown: function (buttonCode) {
|
||||
|
||||
if (this._buttons[buttonCode])
|
||||
{
|
||||
return this._buttons[buttonCode].isDown;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just released" state of a button from this gamepad. Just released is considered as being true if the button was released within the duration given (default 250ms).
|
||||
* @method Phaser.SinglePad#justReleased
|
||||
* @param {number} buttonCode - The buttonCode of the button to check for.
|
||||
* @param {number} [duration=250] - The duration below which the button is considered as being just released.
|
||||
* @return {boolean} True if the button is just released otherwise false.
|
||||
*/
|
||||
justReleased: function (buttonCode, duration) {
|
||||
|
||||
if (typeof duration === "undefined") { duration = 250; }
|
||||
|
||||
return (this._buttons[buttonCode] && this._buttons[buttonCode].isDown === false && (this.game.time.now - this._buttons[buttonCode].timeUp < duration));
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the "just pressed" state of a button from this gamepad. Just pressed is considered true if the button was pressed down within the duration given (default 250ms).
|
||||
* @method Phaser.SinglePad#justPressed
|
||||
* @param {number} buttonCode - The buttonCode of the button to check for.
|
||||
* @param {number} [duration=250] - The duration below which the button is considered as being just pressed.
|
||||
* @return {boolean} True if the button is just pressed otherwise false.
|
||||
*/
|
||||
justPressed: function (buttonCode, duration) {
|
||||
|
||||
if (typeof duration === "undefined") { duration = 250; }
|
||||
|
||||
return (this._buttons[buttonCode] && this._buttons[buttonCode].isDown && this._buttons[buttonCode].duration < duration);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the value of a gamepad button. Intended mainly for cases when you have floating button values, for example
|
||||
* analog trigger buttons on the XBOX 360 controller
|
||||
* @method Phaser.SinglePad#buttonValue
|
||||
* @param {number} buttonCode - The buttonCode of the button to check.
|
||||
* @return {boolean} Button value if available otherwise false.
|
||||
*/
|
||||
buttonValue: function (buttonCode) {
|
||||
|
||||
if (this._buttons[buttonCode])
|
||||
{
|
||||
return this._buttons[buttonCode].value;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Reset all buttons/axes of this gamepad
|
||||
* @method Phaser.SinglePad#reset
|
||||
*/
|
||||
reset: function () {
|
||||
|
||||
for (var i = 0; i < this._buttons.length; i++)
|
||||
{
|
||||
this._buttons[i] = 0;
|
||||
}
|
||||
|
||||
for (var j = 0; j < this._axes.length; j++)
|
||||
{
|
||||
this._axes[j] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.SinglePad.prototype.constructor = Phaser.SinglePad;
|
||||
|
||||
/**
|
||||
* Whether or not this particular gamepad is connected or not.
|
||||
* @name Phaser.SinglePad#connected
|
||||
* @property {boolean} connected - Whether or not this particular gamepad is connected or not.
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.SinglePad.prototype, "connected", {
|
||||
|
||||
get: function () {
|
||||
return this._connected;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Gamepad index as per browser data
|
||||
* @name Phaser.SinglePad#index
|
||||
* @property {number} index - The gamepad index, used to identify specific gamepads in the browser
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(Phaser.SinglePad.prototype, "index", {
|
||||
|
||||
get: function () {
|
||||
return this._index;
|
||||
}
|
||||
|
||||
});
|
||||
379
src/input/Touch.js
Normal file
379
src/input/Touch.js
Normal file
@@ -0,0 +1,379 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Phaser.Touch handles touch events with your game. Note: Android 2.x only supports 1 touch event at once, no multi-touch.
|
||||
*
|
||||
* @class Phaser.Touch
|
||||
* @classdesc The Touch class handles touch interactions with the game and the resulting Pointer objects.
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
Phaser.Touch = function (game) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {boolean} disabled - You can disable all Touch events by setting disabled = true. While set all new touch events will be ignored.
|
||||
* @return {boolean}
|
||||
*/
|
||||
this.disabled = false;
|
||||
|
||||
/**
|
||||
* @property {Object} callbackContext - The context under which callbacks are called.
|
||||
*/
|
||||
this.callbackContext = this.game;
|
||||
|
||||
/**
|
||||
* @property {function} touchStartCallback - A callback that can be fired on a touchStart event.
|
||||
*/
|
||||
this.touchStartCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} touchMoveCallback - A callback that can be fired on a touchMove event.
|
||||
*/
|
||||
this.touchMoveCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} touchEndCallback - A callback that can be fired on a touchEnd event.
|
||||
*/
|
||||
this.touchEndCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} touchEnterCallback - A callback that can be fired on a touchEnter event.
|
||||
*/
|
||||
this.touchEnterCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} touchLeaveCallback - A callback that can be fired on a touchLeave event.
|
||||
*/
|
||||
this.touchLeaveCallback = null;
|
||||
|
||||
/**
|
||||
* @property {function} touchCancelCallback - A callback that can be fired on a touchCancel event.
|
||||
*/
|
||||
this.touchCancelCallback = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} preventDefault - If true the TouchEvent will have prevent.default called on it.
|
||||
* @default
|
||||
*/
|
||||
this.preventDefault = true;
|
||||
|
||||
/**
|
||||
* @property {TouchEvent} event - The browser touch DOM event. Will be set to null if no touch event has ever been received.
|
||||
* @default
|
||||
*/
|
||||
this.event = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onTouchStart - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onTouchStart = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onTouchMove - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onTouchMove = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onTouchEnd - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onTouchEnd = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onTouchEnter - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onTouchEnter = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onTouchLeave - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onTouchLeave = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onTouchCancel - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onTouchCancel = null;
|
||||
|
||||
/**
|
||||
* @property {function} _onTouchMove - Internal event handler reference.
|
||||
* @private
|
||||
*/
|
||||
this._onTouchMove = null;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Touch.prototype = {
|
||||
|
||||
/**
|
||||
* Starts the event listeners running.
|
||||
* @method Phaser.Touch#start
|
||||
*/
|
||||
start: function () {
|
||||
|
||||
if (this._onTouchStart !== null)
|
||||
{
|
||||
// Avoid setting multiple listeners
|
||||
return;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
if (this.game.device.touch)
|
||||
{
|
||||
this._onTouchStart = function (event) {
|
||||
return _this.onTouchStart(event);
|
||||
};
|
||||
|
||||
this._onTouchMove = function (event) {
|
||||
return _this.onTouchMove(event);
|
||||
};
|
||||
|
||||
this._onTouchEnd = function (event) {
|
||||
return _this.onTouchEnd(event);
|
||||
};
|
||||
|
||||
this._onTouchEnter = function (event) {
|
||||
return _this.onTouchEnter(event);
|
||||
};
|
||||
|
||||
this._onTouchLeave = function (event) {
|
||||
return _this.onTouchLeave(event);
|
||||
};
|
||||
|
||||
this._onTouchCancel = function (event) {
|
||||
return _this.onTouchCancel(event);
|
||||
};
|
||||
|
||||
this.game.canvas.addEventListener('touchstart', this._onTouchStart, false);
|
||||
this.game.canvas.addEventListener('touchmove', this._onTouchMove, false);
|
||||
this.game.canvas.addEventListener('touchend', this._onTouchEnd, false);
|
||||
this.game.canvas.addEventListener('touchenter', this._onTouchEnter, false);
|
||||
this.game.canvas.addEventListener('touchleave', this._onTouchLeave, false);
|
||||
this.game.canvas.addEventListener('touchcancel', this._onTouchCancel, false);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Consumes all touchmove events on the document (only enable this if you know you need it!).
|
||||
* @method Phaser.Touch#consumeTouchMove
|
||||
*/
|
||||
consumeDocumentTouches: function () {
|
||||
|
||||
this._documentTouchMove = function (event) {
|
||||
event.preventDefault();
|
||||
};
|
||||
|
||||
document.addEventListener('touchmove', this._documentTouchMove, false);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The internal method that handles the touchstart event from the browser.
|
||||
* @method Phaser.Touch#onTouchStart
|
||||
* @param {TouchEvent} event - The native event from the browser. This gets stored in Touch.event.
|
||||
*/
|
||||
onTouchStart: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.touchStartCallback)
|
||||
{
|
||||
this.touchStartCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.preventDefault)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
|
||||
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element
|
||||
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
|
||||
for (var i = 0; i < event.changedTouches.length; i++)
|
||||
{
|
||||
this.game.input.startPointer(event.changedTouches[i]);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Touch cancel - touches that were disrupted (perhaps by moving into a plugin or browser chrome).
|
||||
* Occurs for example on iOS when you put down 4 fingers and the app selector UI appears.
|
||||
* @method Phaser.Touch#onTouchCancel
|
||||
* @param {TouchEvent} event - The native event from the browser. This gets stored in Touch.event.
|
||||
*/
|
||||
onTouchCancel: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.touchCancelCallback)
|
||||
{
|
||||
this.touchCancelCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.preventDefault)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
// Touch cancel - touches that were disrupted (perhaps by moving into a plugin or browser chrome)
|
||||
// http://www.w3.org/TR/touch-events/#dfn-touchcancel
|
||||
for (var i = 0; i < event.changedTouches.length; i++)
|
||||
{
|
||||
this.game.input.stopPointer(event.changedTouches[i]);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* For touch enter and leave its a list of the touch points that have entered or left the target.
|
||||
* Doesn't appear to be supported by most browsers on a canvas element yet.
|
||||
* @method Phaser.Touch#onTouchEnter
|
||||
* @param {TouchEvent} event - The native event from the browser. This gets stored in Touch.event.
|
||||
*/
|
||||
onTouchEnter: function (event) {
|
||||
|
||||
console.log('touch enter', event);
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.touchEnterCallback)
|
||||
{
|
||||
this.touchEnterCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.game.input.disabled || this.disabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.preventDefault)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* For touch enter and leave its a list of the touch points that have entered or left the target.
|
||||
* Doesn't appear to be supported by most browsers on a canvas element yet.
|
||||
* @method Phaser.Touch#onTouchLeave
|
||||
* @param {TouchEvent} event - The native event from the browser. This gets stored in Touch.event.
|
||||
*/
|
||||
onTouchLeave: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.touchLeaveCallback)
|
||||
{
|
||||
this.touchLeaveCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.preventDefault)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The handler for the touchmove events.
|
||||
* @method Phaser.Touch#onTouchMove
|
||||
* @param {TouchEvent} event - The native event from the browser. This gets stored in Touch.event.
|
||||
*/
|
||||
onTouchMove: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.touchMoveCallback)
|
||||
{
|
||||
this.touchMoveCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.preventDefault)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
for (var i = 0; i < event.changedTouches.length; i++)
|
||||
{
|
||||
this.game.input.updatePointer(event.changedTouches[i]);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The handler for the touchend events.
|
||||
* @method Phaser.Touch#onTouchEnd
|
||||
* @param {TouchEvent} event - The native event from the browser. This gets stored in Touch.event.
|
||||
*/
|
||||
onTouchEnd: function (event) {
|
||||
|
||||
this.event = event;
|
||||
|
||||
if (this.touchEndCallback)
|
||||
{
|
||||
this.touchEndCallback.call(this.callbackContext, event);
|
||||
}
|
||||
|
||||
if (this.preventDefault)
|
||||
{
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
// For touch end its a list of the touch points that have been removed from the surface
|
||||
// https://developer.mozilla.org/en-US/docs/DOM/TouchList
|
||||
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
|
||||
for (var i = 0; i < event.changedTouches.length; i++)
|
||||
{
|
||||
this.game.input.stopPointer(event.changedTouches[i]);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop the event listeners.
|
||||
* @method Phaser.Touch#stop
|
||||
*/
|
||||
stop: function () {
|
||||
|
||||
if (this.game.device.touch)
|
||||
{
|
||||
this.game.canvas.removeEventListener('touchstart', this._onTouchStart);
|
||||
this.game.canvas.removeEventListener('touchmove', this._onTouchMove);
|
||||
this.game.canvas.removeEventListener('touchend', this._onTouchEnd);
|
||||
this.game.canvas.removeEventListener('touchenter', this._onTouchEnter);
|
||||
this.game.canvas.removeEventListener('touchleave', this._onTouchLeave);
|
||||
this.game.canvas.removeEventListener('touchcancel', this._onTouchCancel);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Touch.prototype.constructor = Phaser.Touch;
|
||||
Reference in New Issue
Block a user