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/
|
|
|
|
|
|
|
|
var TouchManager = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function TouchManager (inputManager)
|
|
|
|
{
|
|
|
|
this.manager = inputManager;
|
|
|
|
|
2017-08-01 12:10:08 +00:00
|
|
|
// @property {boolean} capture - If true the DOM events will have event.preventDefault applied to them, if false they will propagate fully.
|
2017-12-27 23:52:46 +00:00
|
|
|
this.capture = true;
|
2017-07-28 02:28:10 +00:00
|
|
|
|
|
|
|
this.enabled = false;
|
|
|
|
|
|
|
|
this.target;
|
|
|
|
|
|
|
|
this.handler;
|
|
|
|
},
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-07-28 02:28:10 +00:00
|
|
|
this.handler = handler;
|
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
2017-07-28 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TouchManager;
|