Starting work on the Mouse Input Manager.

This commit is contained in:
photonstorm 2017-06-12 17:03:34 +01:00
parent 8c3a857850
commit ca1b483423
6 changed files with 146 additions and 1 deletions

View file

@ -9,6 +9,7 @@ var GlobalInputManager = function (game, gameConfig)
this.gameConfig = gameConfig; this.gameConfig = gameConfig;
this.keyboard = new Keyboard(this); this.keyboard = new Keyboard(this);
this.mouse = new Mouse(this);
}; };
GlobalInputManager.prototype.constructor = GlobalInputManager; GlobalInputManager.prototype.constructor = GlobalInputManager;
@ -25,11 +26,13 @@ GlobalInputManager.prototype = {
boot: function () boot: function ()
{ {
this.keyboard.boot(); this.keyboard.boot();
this.mouse.boot();
}, },
update: function () update: function ()
{ {
this.keyboard.update(); this.keyboard.update();
this.mouse.update();
} }
}; };

View file

@ -2,6 +2,7 @@
module.exports = { module.exports = {
Keyboard: require('./keyboard') Keyboard: require('./keyboard'),
Mouse: require('./mouse')
}; };

View file

@ -0,0 +1,110 @@
var EventDispatcher = require('../../events/EventDispatcher');
var Event = require('./events');
var MouseManager = function (inputManager)
{
this.manager = inputManager;
this.enabled = false;
this.target;
this.events = new EventDispatcher();
this.mouseHandler;
// Standard FIFO queue
this.queue = [];
};
MouseManager.prototype.constructor = MouseManager;
MouseManager.prototype = {
/**
* The Boot handler is called by Phaser.Game when it first starts up.
* The renderer is available by now.
*
* @method Phaser.Input.MouseManager#boot
* @private
*/
boot: function ()
{
var config = this.manager.gameConfig;
this.enabled = config.inputMouse;
this.target = config.inputMouseEventTarget;
if (this.enabled)
{
this.startListeners();
}
},
startListeners: function ()
{
var queue = this.queue;
// var captures = this.captures;
var mouseHandler = function (event)
{
if (event.preventDefaulted)
{
// Do nothing if event already handled
return;
}
queue.push(event);
// if (captures[event.keyCode])
// {
// event.preventDefault();
// }
};
this.mouseHandler = mouseHandler;
this.target.addEventListener('mousemove', mouseHandler, false);
this.target.addEventListener('mousedown', mouseHandler, false);
this.target.addEventListener('mouseup', mouseHandler, false);
},
stopListeners: function ()
{
this.target.removeEventListener('mousemove', this.mouseHandler);
this.target.removeEventListener('mousedown', this.mouseHandler);
this.target.removeEventListener('mouseup', this.mouseHandler);
},
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
update: function ()
{
if (!this.enabled)
{
return;
}
// Clears the queue array, and also means we don't work on array data that could potentially
// be modified during the processing phase
var queue = this.queue.splice(0, this.queue.length);
// Process the event queue, dispatching all of the events that have stored up
for (var i = 0; i < queue.length; i++)
{
var event = queue[i];
if (event.type === 'mousedown')
{
this.events.dispatch(new Event.MOUSE_DOWN_EVENT(event));
}
else if (event.type === 'mouseup')
{
this.events.dispatch(new Event.MOUSE_UP_EVENT(event));
}
}
}
};
module.exports = MouseManager;

View file

@ -0,0 +1,13 @@
var Event = require('../../../events/Event');
var MouseDownEvent = function (nativeEvent)
{
Event.call(this, 'MOUSE_DOWN_EVENT');
this.data = nativeEvent;
};
MouseDownEvent.prototype = Object.create(Event.prototype);
MouseDownEvent.prototype.constructor = MouseDownEvent;
module.exports = MouseDownEvent;

View file

@ -0,0 +1,13 @@
var Event = require('../../../events/Event');
var MouseUpEvent = function (nativeEvent)
{
Event.call(this, 'MOUSE_UP_EVENT');
this.data = nativeEvent;
};
MouseUpEvent.prototype = Object.create(Event.prototype);
MouseUpEvent.prototype.constructor = MouseUpEvent;
module.exports = MouseUpEvent;

View file

@ -0,0 +1,5 @@
module.exports = {
MOUSE_DOWN_EVENT: require('./MouseDownEvent'),
MOUSE_UP_EVENT: require('./MouseUpEvent')
};