From ca1b4834236bb7c7f11eba23c7a875586d7e001c Mon Sep 17 00:00:00 2001 From: photonstorm Date: Mon, 12 Jun 2017 17:03:34 +0100 Subject: [PATCH] Starting work on the Mouse Input Manager. --- v3/src/input/GlobalInputManager.js | 3 + v3/src/input/index.js | 3 +- v3/src/input/mouse/MouseManager.js | 110 ++++++++++++++++++++ v3/src/input/mouse/events/MouseDownEvent.js | 13 +++ v3/src/input/mouse/events/MouseUpEvent.js | 13 +++ v3/src/input/mouse/events/index.js | 5 + 6 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 v3/src/input/mouse/MouseManager.js create mode 100644 v3/src/input/mouse/events/MouseDownEvent.js create mode 100644 v3/src/input/mouse/events/MouseUpEvent.js create mode 100644 v3/src/input/mouse/events/index.js diff --git a/v3/src/input/GlobalInputManager.js b/v3/src/input/GlobalInputManager.js index af0e4beb4..81b96ddc8 100644 --- a/v3/src/input/GlobalInputManager.js +++ b/v3/src/input/GlobalInputManager.js @@ -9,6 +9,7 @@ var GlobalInputManager = function (game, gameConfig) this.gameConfig = gameConfig; this.keyboard = new Keyboard(this); + this.mouse = new Mouse(this); }; GlobalInputManager.prototype.constructor = GlobalInputManager; @@ -25,11 +26,13 @@ GlobalInputManager.prototype = { boot: function () { this.keyboard.boot(); + this.mouse.boot(); }, update: function () { this.keyboard.update(); + this.mouse.update(); } }; diff --git a/v3/src/input/index.js b/v3/src/input/index.js index b719b927d..a4bf2cb45 100644 --- a/v3/src/input/index.js +++ b/v3/src/input/index.js @@ -2,6 +2,7 @@ module.exports = { - Keyboard: require('./keyboard') + Keyboard: require('./keyboard'), + Mouse: require('./mouse') }; diff --git a/v3/src/input/mouse/MouseManager.js b/v3/src/input/mouse/MouseManager.js new file mode 100644 index 000000000..4406f6b87 --- /dev/null +++ b/v3/src/input/mouse/MouseManager.js @@ -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; diff --git a/v3/src/input/mouse/events/MouseDownEvent.js b/v3/src/input/mouse/events/MouseDownEvent.js new file mode 100644 index 000000000..2b331e275 --- /dev/null +++ b/v3/src/input/mouse/events/MouseDownEvent.js @@ -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; diff --git a/v3/src/input/mouse/events/MouseUpEvent.js b/v3/src/input/mouse/events/MouseUpEvent.js new file mode 100644 index 000000000..f51bc0d6b --- /dev/null +++ b/v3/src/input/mouse/events/MouseUpEvent.js @@ -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; diff --git a/v3/src/input/mouse/events/index.js b/v3/src/input/mouse/events/index.js new file mode 100644 index 000000000..08b37fc8e --- /dev/null +++ b/v3/src/input/mouse/events/index.js @@ -0,0 +1,5 @@ + +module.exports = { + MOUSE_DOWN_EVENT: require('./MouseDownEvent'), + MOUSE_UP_EVENT: require('./MouseUpEvent') +};