2013-08-31 12:54:59 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-10-27 00:25:03 +00:00
|
|
|
* The MSPointer class handles Microsoft touch interactions with the game and the resulting Pointer objects.
|
|
|
|
*
|
2015-07-20 13:01:21 +00:00
|
|
|
* It will work only in Internet Explorer 10+ and Windows Store or Windows Phone 8 apps using JavaScript.
|
2013-08-31 12:54:59 +00:00
|
|
|
* http://msdn.microsoft.com/en-us/library/ie/hh673557(v=vs.85).aspx
|
2014-09-16 16:35:08 +00:00
|
|
|
*
|
2015-07-12 10:56:25 +00:00
|
|
|
* You should not normally access this class directly, but instead use a Phaser.Pointer object which
|
|
|
|
* normalises all game input for you including accurate button handling.
|
2015-07-07 15:58:41 +00:00
|
|
|
*
|
2015-07-20 13:01:21 +00:00
|
|
|
* Please note that at the current time of writing Phaser does not yet support chorded button interactions:
|
|
|
|
* http://www.w3.org/TR/pointerevents/#chorded-button-interactions
|
|
|
|
*
|
2014-09-16 16:35:08 +00:00
|
|
|
* @class Phaser.MSPointer
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
2013-08-31 20:50:34 +00:00
|
|
|
Phaser.MSPointer = function (game) {
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Input} input - A reference to the Phaser Input Manager.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
this.input = game.input;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-11-30 11:10:52 +00:00
|
|
|
* @property {object} callbackContext - The context under which callbacks are called (defaults to game).
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.callbackContext = this.game;
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2015-02-05 05:12:00 +00:00
|
|
|
/**
|
|
|
|
* @property {function} pointerDownCallback - A callback that can be fired on a MSPointerDown event.
|
|
|
|
*/
|
|
|
|
this.pointerDownCallback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {function} pointerMoveCallback - A callback that can be fired on a MSPointerMove event.
|
|
|
|
*/
|
|
|
|
this.pointerMoveCallback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {function} pointerUpCallback - A callback that can be fired on a MSPointerUp event.
|
|
|
|
*/
|
|
|
|
this.pointerUpCallback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} capture - If true the Pointer events will have event.preventDefault applied to them, if false they will propagate fully.
|
|
|
|
*/
|
2015-02-08 21:56:43 +00:00
|
|
|
this.capture = true;
|
2015-02-05 05:12:00 +00:00
|
|
|
|
2015-02-05 06:12:20 +00:00
|
|
|
/**
|
2015-07-12 10:47:20 +00:00
|
|
|
* This property was removed in Phaser 2.4 and should no longer be used.
|
|
|
|
* Instead please see the Pointer button properties such as `Pointer.leftButton`, `Pointer.rightButton` and so on.
|
2015-07-17 16:50:53 +00:00
|
|
|
* Or Pointer.button holds the DOM event button value if you require that.
|
2015-07-12 10:47:20 +00:00
|
|
|
* @property {number} button
|
2015-02-05 06:12:20 +00:00
|
|
|
*/
|
|
|
|
this.button = -1;
|
|
|
|
|
2015-02-05 05:12:00 +00:00
|
|
|
/**
|
|
|
|
* The browser MSPointer DOM event. Will be null if no event has ever been received.
|
|
|
|
* Access this property only inside a Pointer event handler and do not keep references to it.
|
|
|
|
* @property {MSPointerEvent|null} event
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.event = null;
|
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
/**
|
2014-10-27 00:25:03 +00:00
|
|
|
* MSPointer input will only be processed if enabled.
|
2014-10-29 04:46:33 +00:00
|
|
|
* @property {boolean} enabled
|
2014-10-27 00:25:03 +00:00
|
|
|
* @default
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
2014-10-27 00:25:03 +00:00
|
|
|
this.enabled = true;
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2013-10-02 14:05:55 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {function} _onMSPointerDown - Internal function to handle MSPointer events.
|
2013-10-02 14:05:55 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onMSPointerDown = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-02 14:05:55 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {function} _onMSPointerMove - Internal function to handle MSPointer events.
|
2013-10-02 14:05:55 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onMSPointerMove = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-02 14:05:55 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {function} _onMSPointerUp - Internal function to handle MSPointer events.
|
2013-10-02 14:05:55 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onMSPointerUp = null;
|
|
|
|
|
2015-08-20 11:47:50 +00:00
|
|
|
/**
|
|
|
|
* @property {function} _onMSPointerUpGlobal - Internal function to handle MSPointer events.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onMSPointerUpGlobal = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {function} _onMSPointerOut - Internal function to handle MSPointer events.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onMSPointerOut = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {function} _onMSPointerOver - Internal function to handle MSPointer events.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onMSPointerOver = null;
|
|
|
|
|
2013-10-02 14:05:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.MSPointer.prototype = {
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* Starts the event listeners running.
|
2013-10-02 14:05:55 +00:00
|
|
|
* @method Phaser.MSPointer#start
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
|
|
|
start: function () {
|
|
|
|
|
2014-03-19 13:22:04 +00:00
|
|
|
if (this._onMSPointerDown !== null)
|
|
|
|
{
|
|
|
|
// Avoid setting multiple listeners
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2014-08-28 02:31:47 +00:00
|
|
|
if (this.game.device.mspointer)
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
|
|
|
this._onMSPointerDown = function (event) {
|
|
|
|
return _this.onPointerDown(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
this._onMSPointerMove = function (event) {
|
|
|
|
return _this.onPointerMove(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
this._onMSPointerUp = function (event) {
|
|
|
|
return _this.onPointerUp(event);
|
|
|
|
};
|
|
|
|
|
2015-08-20 11:47:50 +00:00
|
|
|
this._onMSPointerUpGlobal = function (event) {
|
|
|
|
return _this.onPointerUpGlobal(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
this._onMSPointerOut = function (event) {
|
|
|
|
return _this.onPointerOut(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
this._onMSPointerOver = function (event) {
|
|
|
|
return _this.onPointerOver(event);
|
|
|
|
};
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
var canvas = this.game.canvas;
|
|
|
|
|
|
|
|
canvas.addEventListener('MSPointerDown', this._onMSPointerDown, false);
|
|
|
|
canvas.addEventListener('MSPointerMove', this._onMSPointerMove, false);
|
|
|
|
canvas.addEventListener('MSPointerUp', this._onMSPointerUp, false);
|
2013-08-31 20:50:34 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
// IE11+ uses non-prefix events
|
2015-08-20 11:47:50 +00:00
|
|
|
canvas.addEventListener('pointerdown', this._onMSPointerDown, false);
|
|
|
|
canvas.addEventListener('pointermove', this._onMSPointerMove, false);
|
|
|
|
canvas.addEventListener('pointerup', this._onMSPointerUp, false);
|
2013-08-31 20:50:34 +00:00
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
canvas.style['-ms-content-zooming'] = 'none';
|
|
|
|
canvas.style['-ms-touch-action'] = 'none';
|
2015-08-20 11:47:50 +00:00
|
|
|
|
|
|
|
if (!this.game.device.cocoonJS)
|
|
|
|
{
|
|
|
|
window.addEventListener('MSPointerUp', this._onMSPointerUpGlobal, true);
|
|
|
|
canvas.addEventListener('MSPointerOver', this._onMSPointerOver, true);
|
|
|
|
canvas.addEventListener('MSPointerOut', this._onMSPointerOut, true);
|
|
|
|
|
|
|
|
// IE11+ uses non-prefix events
|
|
|
|
window.addEventListener('pointerup', this._onMSPointerUpGlobal, true);
|
|
|
|
canvas.addEventListener('pointerover', this._onMSPointerOver, true);
|
|
|
|
canvas.addEventListener('pointerout', this._onMSPointerOut, true);
|
|
|
|
}
|
2013-08-31 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* The function that handles the PointerDown event.
|
2014-09-18 15:58:25 +00:00
|
|
|
*
|
2013-10-02 14:05:55 +00:00
|
|
|
* @method Phaser.MSPointer#onPointerDown
|
2014-09-18 15:58:25 +00:00
|
|
|
* @param {PointerEvent} event - The native DOM event.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-31 12:54:59 +00:00
|
|
|
onPointerDown: function (event) {
|
|
|
|
|
2015-02-05 05:12:00 +00:00
|
|
|
this.event = event;
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pointerDownCallback)
|
|
|
|
{
|
|
|
|
this.pointerDownCallback.call(this.callbackContext, event);
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
if (!this.input.enabled || !this.enabled)
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.identifier = event.pointerId;
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
if (event.pointerType === 'mouse' || event.pointerType === 0x00000004)
|
|
|
|
{
|
|
|
|
this.input.mousePointer.start(event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.input.startPointer(event);
|
|
|
|
}
|
2013-08-31 12:54:59 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* The function that handles the PointerMove event.
|
2013-10-02 14:05:55 +00:00
|
|
|
* @method Phaser.MSPointer#onPointerMove
|
2014-09-18 15:58:25 +00:00
|
|
|
* @param {PointerEvent} event - The native DOM event.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-31 12:54:59 +00:00
|
|
|
onPointerMove: function (event) {
|
|
|
|
|
2015-02-05 05:12:00 +00:00
|
|
|
this.event = event;
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pointerMoveCallback)
|
|
|
|
{
|
|
|
|
this.pointerMoveCallback.call(this.callbackContext, event);
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
if (!this.input.enabled || !this.enabled)
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.identifier = event.pointerId;
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
if (event.pointerType === 'mouse' || event.pointerType === 0x00000004)
|
|
|
|
{
|
|
|
|
this.input.mousePointer.move(event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.input.updatePointer(event);
|
|
|
|
}
|
2013-08-31 12:54:59 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* The function that handles the PointerUp event.
|
2013-10-02 14:05:55 +00:00
|
|
|
* @method Phaser.MSPointer#onPointerUp
|
2014-09-18 15:58:25 +00:00
|
|
|
* @param {PointerEvent} event - The native DOM event.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-31 12:54:59 +00:00
|
|
|
onPointerUp: function (event) {
|
|
|
|
|
2015-02-05 05:12:00 +00:00
|
|
|
this.event = event;
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pointerUpCallback)
|
|
|
|
{
|
|
|
|
this.pointerUpCallback.call(this.callbackContext, event);
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
if (!this.input.enabled || !this.enabled)
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.identifier = event.pointerId;
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
if (event.pointerType === 'mouse' || event.pointerType === 0x00000004)
|
|
|
|
{
|
|
|
|
this.input.mousePointer.stop(event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.input.stopPointer(event);
|
|
|
|
}
|
2013-08-31 12:54:59 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-08-20 11:47:50 +00:00
|
|
|
/**
|
|
|
|
* The internal method that handles the mouse up event from the window.
|
|
|
|
*
|
|
|
|
* @method Phaser.MSPointer#onPointerUpGlobal
|
|
|
|
* @param {PointerEvent} event - The native event from the browser. This gets stored in MSPointer.event.
|
|
|
|
*/
|
|
|
|
onPointerUpGlobal: function (event) {
|
|
|
|
|
|
|
|
if ((event.pointerType === 'mouse' || event.pointerType === 0x00000004) && !this.input.mousePointer.withinGame)
|
|
|
|
{
|
|
|
|
this.onPointerUp(event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var pointer = this.input.getPointerFromIdentifier(event.identifier);
|
|
|
|
|
|
|
|
if (pointer && pointer.withinGame)
|
|
|
|
{
|
|
|
|
this.onPointerUp(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal method that handles the pointer out event from the browser.
|
|
|
|
*
|
|
|
|
* @method Phaser.MSPointer#onPointerOut
|
|
|
|
* @param {PointerEvent} event - The native event from the browser. This gets stored in MSPointer.event.
|
|
|
|
*/
|
|
|
|
onPointerOut: function (event) {
|
|
|
|
|
|
|
|
this.event = event;
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.pointerType === 'mouse' || event.pointerType === 0x00000004)
|
|
|
|
{
|
|
|
|
this.input.mousePointer.withinGame = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var pointer = this.input.getPointerFromIdentifier(event.identifier);
|
|
|
|
|
|
|
|
if (pointer)
|
|
|
|
{
|
|
|
|
pointer.withinGame = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.input.mouse.mouseOutCallback)
|
|
|
|
{
|
|
|
|
this.input.mouse.mouseOutCallback.call(this.input.mouse.callbackContext, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.input.enabled || !this.enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.input.mouse.stopOnGameOut)
|
|
|
|
{
|
|
|
|
event['identifier'] = 0;
|
|
|
|
|
|
|
|
if (pointer)
|
|
|
|
{
|
|
|
|
pointer.stop(event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.input.mousePointer.stop(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal method that handles the pointer out event from the browser.
|
|
|
|
*
|
|
|
|
* @method Phaser.MSPointer#onPointerOut
|
|
|
|
* @param {PointerEvent} event - The native event from the browser. This gets stored in MSPointer.event.
|
|
|
|
*/
|
|
|
|
onPointerOver: function (event) {
|
|
|
|
|
|
|
|
this.event = event;
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.pointerType === 'mouse' || event.pointerType === 0x00000004)
|
|
|
|
{
|
|
|
|
this.input.mousePointer.withinGame = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var pointer = this.input.getPointerFromIdentifier(event.identifier);
|
|
|
|
|
|
|
|
if (pointer)
|
|
|
|
{
|
|
|
|
pointer.withinGame = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.input.mouse.mouseOverCallback)
|
|
|
|
{
|
|
|
|
this.input.mouse.mouseOverCallback.call(this.input.mouse.callbackContext, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* Stop the event listeners.
|
2013-10-02 14:05:55 +00:00
|
|
|
* @method Phaser.MSPointer#stop
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
|
|
|
stop: function () {
|
|
|
|
|
2015-07-20 13:01:21 +00:00
|
|
|
var canvas = this.game.canvas;
|
|
|
|
|
2015-09-04 21:11:50 +00:00
|
|
|
canvas.removeEventListener('MSPointerDown', this._onMSPointerDown, false);
|
|
|
|
canvas.removeEventListener('MSPointerMove', this._onMSPointerMove, false);
|
|
|
|
canvas.removeEventListener('MSPointerUp', this._onMSPointerUp, false);
|
|
|
|
|
|
|
|
// IE11+ uses non-prefix events
|
|
|
|
canvas.removeEventListener('pointerdown', this._onMSPointerDown, false);
|
|
|
|
canvas.removeEventListener('pointermove', this._onMSPointerMove, false);
|
|
|
|
canvas.removeEventListener('pointerup', this._onMSPointerUp, false);
|
|
|
|
|
|
|
|
window.removeEventListener('MSPointerUp', this._onMSPointerUpGlobal, true);
|
2015-09-02 18:36:49 +00:00
|
|
|
canvas.removeEventListener('MSPointerOver', this._onMSPointerOver, true);
|
|
|
|
canvas.removeEventListener('MSPointerOut', this._onMSPointerOut, true);
|
2015-08-20 11:47:50 +00:00
|
|
|
|
2015-09-04 21:11:50 +00:00
|
|
|
// IE11+ uses non-prefix events
|
|
|
|
window.removeEventListener('pointerup', this._onMSPointerUpGlobal, true);
|
2015-09-02 18:36:49 +00:00
|
|
|
canvas.removeEventListener('pointerover', this._onMSPointerOver, true);
|
|
|
|
canvas.removeEventListener('pointerout', this._onMSPointerOut, true);
|
2013-08-31 12:54:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.MSPointer.prototype.constructor = Phaser.MSPointer;
|