2017-06-30 14:47:51 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
|
2017-06-14 00:20:01 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
|
|
|
|
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var MouseManager = new Class({
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
function MouseManager (inputManager)
|
|
|
|
{
|
|
|
|
this.manager = inputManager;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-07-25 11:33:37 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propagate fully.
|
|
|
|
*/
|
|
|
|
this.capture = false;
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.enabled = false;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.target;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.handler;
|
|
|
|
},
|
2017-06-12 16:03:34 +00:00
|
|
|
|
|
|
|
boot: function ()
|
|
|
|
{
|
2017-06-30 14:47:51 +00:00
|
|
|
var config = this.manager.config;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
|
|
|
this.enabled = config.inputMouse;
|
|
|
|
this.target = config.inputMouseEventTarget;
|
|
|
|
|
2017-06-12 23:38:48 +00:00
|
|
|
if (!this.target)
|
|
|
|
{
|
|
|
|
this.target = this.manager.game.canvas;
|
|
|
|
}
|
|
|
|
|
2017-07-25 11:33:37 +00:00
|
|
|
if (config.disableContextMenu)
|
|
|
|
{
|
|
|
|
this.disableContextMenu();
|
|
|
|
}
|
|
|
|
|
2017-06-12 16:03:34 +00:00
|
|
|
if (this.enabled)
|
|
|
|
{
|
|
|
|
this.startListeners();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-25 11:33:37 +00:00
|
|
|
disableContextMenu: function ()
|
|
|
|
{
|
|
|
|
document.body.addEventListener('contextmenu', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-06-12 16:03:34 +00:00
|
|
|
startListeners: function ()
|
|
|
|
{
|
2017-06-14 00:20:01 +00:00
|
|
|
var queue = this.manager.queue;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-07-25 11:33:37 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2017-06-14 00:20:01 +00:00
|
|
|
var handler = function (event)
|
2017-06-12 16:03:34 +00:00
|
|
|
{
|
|
|
|
if (event.preventDefaulted)
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.push(event);
|
2017-07-25 11:33:37 +00:00
|
|
|
|
|
|
|
if (_this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2017-06-12 16:03:34 +00:00
|
|
|
};
|
|
|
|
|
2017-06-14 00:20:01 +00:00
|
|
|
this.handler = handler;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-14 00:20:01 +00:00
|
|
|
this.target.addEventListener('mousemove', handler, false);
|
|
|
|
this.target.addEventListener('mousedown', handler, false);
|
|
|
|
this.target.addEventListener('mouseup', handler, false);
|
2017-06-12 16:03:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stopListeners: function ()
|
|
|
|
{
|
2017-06-14 00:20:01 +00:00
|
|
|
this.target.removeEventListener('mousemove', this.handler);
|
|
|
|
this.target.removeEventListener('mousedown', this.handler);
|
|
|
|
this.target.removeEventListener('mouseup', this.handler);
|
2017-06-12 16:03:34 +00:00
|
|
|
}
|
2017-06-30 14:47:51 +00:00
|
|
|
|
|
|
|
});
|
2017-06-12 16:03:34 +00:00
|
|
|
|
|
|
|
module.exports = MouseManager;
|