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-03-19 21:12:11 +00:00
|
|
|
/**
|
|
|
|
* @callback TouchHandler
|
|
|
|
*
|
|
|
|
* @param {TouchEvent} event - [description]
|
|
|
|
*/
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class TouchManager
|
|
|
|
* @memberOf Phaser.Input.Touch
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Input.InputManager} inputManager - [description]
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
var TouchManager = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function TouchManager (inputManager)
|
|
|
|
{
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Touch.TouchManager#target
|
|
|
|
* @type {null}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
this.target;
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Touch.TouchManager#handler
|
2018-03-27 13:59:49 +00:00
|
|
|
* @type {?TouchHandler}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
this.handler;
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#boot
|
|
|
|
* @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-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#startListeners
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-28 02:28:10 +00:00
|
|
|
startListeners: function ()
|
|
|
|
{
|
|
|
|
var queue = this.manager.queue;
|
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
|
|
|
var handler;
|
|
|
|
|
|
|
|
if (this.capture)
|
2017-07-28 02:28:10 +00:00
|
|
|
{
|
2017-12-27 23:52:46 +00:00
|
|
|
handler = function (event)
|
2017-07-28 02:28:10 +00:00
|
|
|
{
|
2018-01-03 16:30:51 +00:00
|
|
|
if (event.defaultPrevented)
|
2017-12-27 23:52:46 +00:00
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
2017-07-28 02:28:10 +00:00
|
|
|
|
2017-12-28 15:14:16 +00:00
|
|
|
// console.log('touch', event);
|
|
|
|
|
2017-12-27 23:52:46 +00:00
|
|
|
queue.push(event);
|
2017-07-28 02:28:10 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
2017-12-27 23:52:46 +00:00
|
|
|
};
|
2017-07-28 02:28:10 +00:00
|
|
|
|
2017-12-27 23:52:46 +00:00
|
|
|
target.addEventListener('touchstart', handler, nonPassive);
|
|
|
|
target.addEventListener('touchmove', handler, nonPassive);
|
|
|
|
target.addEventListener('touchend', handler, nonPassive);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handler = function (event)
|
|
|
|
{
|
2018-01-03 16:30:51 +00:00
|
|
|
if (event.defaultPrevented)
|
2017-12-27 23:52:46 +00:00
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.push(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
target.addEventListener('touchstart', handler, passive);
|
|
|
|
target.addEventListener('touchmove', handler, passive);
|
|
|
|
target.addEventListener('touchend', handler, passive);
|
|
|
|
}
|
2018-03-19 21:12:11 +00:00
|
|
|
|
2017-07-28 02:28:10 +00:00
|
|
|
this.handler = handler;
|
|
|
|
},
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Touch.TouchManager#stopListeners
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
target.removeEventListener('touchstart', this.handler);
|
|
|
|
target.removeEventListener('touchmove', this.handler);
|
|
|
|
target.removeEventListener('touchend', this.handler);
|
2018-01-31 03:38:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @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;
|