2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-07-28 02:28:10 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
|
|
|
|
// https://patrickhlauke.github.io/touch/tests/results/
|
|
|
|
// https://www.html5rocks.com/en/mobile/touch/
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-06-04 12:23:34 +00:00
|
|
|
* The Touch Manager is a helper class that belongs to the Input Manager.
|
|
|
|
*
|
|
|
|
* Its role is to listen for native DOM Touch Events and then pass there onto the Input Manager for further processing.
|
|
|
|
*
|
|
|
|
* You do not need to create this class directly, the Input Manager will create an instance of it automatically.
|
2018-02-07 15:27:21 +00:00
|
|
|
*
|
|
|
|
* @class TouchManager
|
|
|
|
* @memberOf Phaser.Input.Touch
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-04 12:23:34 +00:00
|
|
|
* @param {Phaser.Input.InputManager} inputManager - A reference to the Input Manager.
|
2018-02-07 15:27:21 +00:00
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
var TouchManager = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function TouchManager (inputManager)
|
|
|
|
{
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
2018-06-04 12:23:34 +00:00
|
|
|
* A reference to the Input Manager.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Touch.TouchManager#manager
|
|
|
|
* @type {Phaser.Input.InputManager}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
this.manager = inputManager;
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* If true the DOM events will have event.preventDefault applied to them, if false they will propagate fully.
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Touch.TouchManager#capture
|
|
|
|
* @type {boolean}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @default true
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-12-27 23:52:46 +00:00
|
|
|
this.capture = true;
|
2017-07-28 02:28:10 +00:00
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
2018-06-04 12:23:34 +00:00
|
|
|
* A boolean that controls if the Touch Manager is enabled or not.
|
|
|
|
* Can be toggled on the fly.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Touch.TouchManager#enabled
|
|
|
|
* @type {boolean}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
this.enabled = false;
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
2018-06-04 12:23:34 +00:00
|
|
|
* The Touch Event target, as defined in the Game Config.
|
|
|
|
* Typically the canvas to which the game is rendering, but can be any interactive DOM element.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Touch.TouchManager#target
|
2018-06-04 12:23:34 +00:00
|
|
|
* @type {any}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
this.target;
|
|
|
|
|
2018-05-25 18:27:26 +00:00
|
|
|
inputManager.events.once('boot', this.boot, this);
|
2017-07-28 02:28:10 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
2018-06-04 12:23:34 +00:00
|
|
|
* The Touch Manager boot process.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#boot
|
2018-06-04 12:23:34 +00:00
|
|
|
* @private
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
var config = this.manager.config;
|
|
|
|
|
|
|
|
this.enabled = config.inputTouch;
|
|
|
|
this.target = config.inputTouchEventTarget;
|
2017-12-27 23:52:46 +00:00
|
|
|
this.capture = config.inputTouchCapture;
|
2017-07-28 02:28:10 +00:00
|
|
|
|
|
|
|
if (!this.target)
|
|
|
|
{
|
|
|
|
this.target = this.manager.game.canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.enabled)
|
|
|
|
{
|
|
|
|
this.startListeners();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-04 12:23:34 +00:00
|
|
|
/**
|
|
|
|
* The Touch Start Event Handler.
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#onTouchStart
|
|
|
|
* @since 3.10.0
|
|
|
|
*
|
|
|
|
* @param {TouchEVent} event - The native DOM Touch Start Event.
|
|
|
|
*/
|
2018-05-29 15:55:52 +00:00
|
|
|
onTouchStart: function (event)
|
|
|
|
{
|
|
|
|
if (event.defaultPrevented)
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-29 23:33:16 +00:00
|
|
|
this.manager.queueTouchStart(event);
|
2018-05-29 15:55:52 +00:00
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-04 12:23:34 +00:00
|
|
|
/**
|
|
|
|
* The Touch Move Event Handler.
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#onTouchMove
|
|
|
|
* @since 3.10.0
|
|
|
|
*
|
|
|
|
* @param {TouchEVent} event - The native DOM Touch Move Event.
|
|
|
|
*/
|
2018-05-29 15:55:52 +00:00
|
|
|
onTouchMove: function (event)
|
|
|
|
{
|
|
|
|
if (event.defaultPrevented)
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-29 23:33:16 +00:00
|
|
|
this.manager.queueTouchMove(event);
|
2018-05-29 15:55:52 +00:00
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-04 12:23:34 +00:00
|
|
|
/**
|
|
|
|
* The Touch End Event Handler.
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#onTouchEnd
|
|
|
|
* @since 3.10.0
|
|
|
|
*
|
|
|
|
* @param {TouchEVent} event - The native DOM Touch End Event.
|
|
|
|
*/
|
2018-05-29 15:55:52 +00:00
|
|
|
onTouchEnd: function (event)
|
|
|
|
{
|
|
|
|
if (event.defaultPrevented)
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-29 23:33:16 +00:00
|
|
|
this.manager.queueTouchEnd(event);
|
2018-05-29 15:55:52 +00:00
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
2018-06-04 12:23:34 +00:00
|
|
|
* Starts the Touch Event listeners running.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#startListeners
|
2018-06-04 12:23:34 +00:00
|
|
|
* @private
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
startListeners: function ()
|
|
|
|
{
|
2017-12-27 23:52:46 +00:00
|
|
|
var target = this.target;
|
2017-07-28 02:28:10 +00:00
|
|
|
|
2017-12-27 23:52:46 +00:00
|
|
|
var passive = { passive: true };
|
|
|
|
var nonPassive = { passive: false };
|
2017-07-28 02:28:10 +00:00
|
|
|
|
2017-12-27 23:52:46 +00:00
|
|
|
if (this.capture)
|
2017-07-28 02:28:10 +00:00
|
|
|
{
|
2018-05-29 15:55:52 +00:00
|
|
|
target.addEventListener('touchstart', this.onTouchStart.bind(this), nonPassive);
|
|
|
|
target.addEventListener('touchmove', this.onTouchMove.bind(this), nonPassive);
|
|
|
|
target.addEventListener('touchend', this.onTouchEnd.bind(this), nonPassive);
|
2017-12-27 23:52:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-29 15:55:52 +00:00
|
|
|
target.addEventListener('touchstart', this.onTouchStart.bind(this), passive);
|
|
|
|
target.addEventListener('touchmove', this.onTouchMove.bind(this), passive);
|
|
|
|
target.addEventListener('touchend', this.onTouchEnd.bind(this), passive);
|
2017-12-27 23:52:46 +00:00
|
|
|
}
|
2017-07-28 02:28:10 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
2018-06-04 12:23:34 +00:00
|
|
|
* Stops the Touch Event listeners.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#stopListeners
|
2018-06-04 12:23:34 +00:00
|
|
|
* @private
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
stopListeners: function ()
|
|
|
|
{
|
2017-12-27 23:52:46 +00:00
|
|
|
var target = this.target;
|
|
|
|
|
2018-05-29 15:55:52 +00:00
|
|
|
target.removeEventListener('touchstart', this.onTouchStart);
|
|
|
|
target.removeEventListener('touchmove', this.onTouchMove);
|
|
|
|
target.removeEventListener('touchend', this.onTouchEnd);
|
2018-01-31 03:38:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-06-04 12:23:34 +00:00
|
|
|
* Destroys this Touch Manager instance.
|
2018-01-31 03:38:10 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.stopListeners();
|
|
|
|
|
|
|
|
this.manager = null;
|
2017-07-28 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TouchManager;
|