From 97aef1faea5eb6c4f643d813afbd7dc5189cab34 Mon Sep 17 00:00:00 2001 From: liabru Date: Fri, 14 Mar 2014 19:36:58 +0000 Subject: [PATCH] added mouse events --- src/core/Engine.js | 169 ++++++++++++++++++++++++++++++++------------- src/core/Mouse.js | 21 +++++- 2 files changed, 140 insertions(+), 50 deletions(-) diff --git a/src/core/Engine.js b/src/core/Engine.js index a8e5119..1af5494 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -172,55 +172,9 @@ var Engine = {}; // update Engine.update(engine, delta, correction); - var pairs = engine.pairs; - - /** - * Fired after engine update, provides a list of all pairs that have started to collide in the current tick (if any) - * - * @event collisionStart - * @param {} event An event object - * @param {} event.pairs List of affected pairs - * @param {DOMHighResTimeStamp} event.timestamp The timestamp of the current tick - * @param {} event.source The source object of the event - * @param {} event.name The name of the event - */ - if (pairs.collisionStart.length > 0) { - Events.trigger(engine, 'collisionStart', { - pairs: pairs.collisionStart - }); - } - - /** - * Fired after engine update, provides a list of all pairs that are colliding in the current tick (if any) - * - * @event collisionActive - * @param {} event An event object - * @param {} event.pairs List of affected pairs - * @param {DOMHighResTimeStamp} event.timestamp The timestamp of the current tick - * @param {} event.source The source object of the event - * @param {} event.name The name of the event - */ - if (pairs.collisionActive.length > 0) { - Events.trigger(engine, 'collisionActive', { - pairs: pairs.collisionActive - }); - } - - /** - * Fired after engine update, provides a list of all pairs that have ended collision in the current tick (if any) - * - * @event collisionEnd - * @param {} event An event object - * @param {} event.pairs List of affected pairs - * @param {DOMHighResTimeStamp} event.timestamp The timestamp of the current tick - * @param {} event.source The source object of the event - * @param {} event.name The name of the event - */ - if (pairs.collisionEnd.length > 0) { - Events.trigger(engine, 'collisionEnd', { - pairs: pairs.collisionEnd - }); - } + // trigger events that may have occured during the step + _triggerCollisionEvents(engine); + _triggerMouseEvents(engine); /** * Fired after engine update and all collision events @@ -393,4 +347,121 @@ var Engine = {}; } }; + /** + * Triggers mouse events + * @method _triggerMouseEvents + * @private + * @param {engine} engine + */ + var _triggerMouseEvents = function(engine) { + var mouse = engine.input.mouse, + mouseEvents = mouse.sourceEvents; + + /** + * Fired when the mouse has moved (or a touch moves) during the last step + * + * @event mousemove + * @param {} event An event object + * @param {MouseEvent} event.mouseEvent The native MouseEvent from the browser + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + if (mouseEvents.mousemove) { + Events.trigger(mouse, 'mousemove', { + mouseEvent: mouseEvents.mousemove + }); + } + + /** + * Fired when the mouse is down (or a touch has started) during the last step + * + * @event mousedown + * @param {} event An event object + * @param {MouseEvent} event.mouseEvent The native MouseEvent from the browser + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + if (mouseEvents.mousedown) { + Events.trigger(mouse, 'mousedown', { + mouseEvent: mouseEvents.mousedown + }); + } + + /** + * Fired when the mouse is up (or a touch has ended) during the last step + * + * @event mouseup + * @param {} event An event object + * @param {MouseEvent} event.mouseEvent The native MouseEvent from the browser + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + if (mouseEvents.mouseup) { + Events.trigger(mouse, 'mouseup', { + mouseEvent: mouseEvents.mouseup + }); + } + + // reset the mouse state ready for the next step + Mouse.clearSourceEvents(mouse); + }; + + /** + * Triggers collision events + * @method _triggerMouseEvents + * @private + * @param {engine} engine + */ + var _triggerCollisionEvents = function(engine) { + var pairs = engine.pairs; + + /** + * Fired after engine update, provides a list of all pairs that have started to collide in the current tick (if any) + * + * @event collisionStart + * @param {} event An event object + * @param {} event.pairs List of affected pairs + * @param {DOMHighResTimeStamp} event.timestamp The timestamp of the current tick + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + if (pairs.collisionStart.length > 0) { + Events.trigger(engine, 'collisionStart', { + pairs: pairs.collisionStart + }); + } + + /** + * Fired after engine update, provides a list of all pairs that are colliding in the current tick (if any) + * + * @event collisionActive + * @param {} event An event object + * @param {} event.pairs List of affected pairs + * @param {DOMHighResTimeStamp} event.timestamp The timestamp of the current tick + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + if (pairs.collisionActive.length > 0) { + Events.trigger(engine, 'collisionActive', { + pairs: pairs.collisionActive + }); + } + + /** + * Fired after engine update, provides a list of all pairs that have ended collision in the current tick (if any) + * + * @event collisionEnd + * @param {} event An event object + * @param {} event.pairs List of affected pairs + * @param {DOMHighResTimeStamp} event.timestamp The timestamp of the current tick + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + if (pairs.collisionEnd.length > 0) { + Events.trigger(engine, 'collisionEnd', { + pairs: pairs.collisionEnd + }); + } + }; + })(); \ No newline at end of file diff --git a/src/core/Mouse.js b/src/core/Mouse.js index 094b276..e226c7e 100644 --- a/src/core/Mouse.js +++ b/src/core/Mouse.js @@ -21,6 +21,12 @@ var Mouse; this.mousedownPosition = { x: 0, y: 0 }; this.mouseupPosition = { x: 0, y: 0 }; this.button = -1; + + this.sourceEvents = { + mousemove: null, + mousedown: null, + mouseup: null + }; var mousemove = function(event) { var position = _getRelativeMousePosition(event, element), @@ -32,6 +38,7 @@ var Mouse; } mouse.position = position; + mouse.sourceEvents.mousemove = event; }; var mousedown = function(event) { @@ -46,6 +53,7 @@ var Mouse; } mouse.position = mouse.mousedownPosition = position; + mouse.sourceEvents.mousedown = event; }; var mouseup = function(event) { @@ -57,8 +65,8 @@ var Mouse; } mouse.button = -1; - mouse.position = mouse.mouseupPosition = position; + mouse.sourceEvents.mouseup = event; }; element.addEventListener('mousemove', mousemove); @@ -79,6 +87,17 @@ var Mouse; Mouse.create = function(element) { return new Mouse(element); }; + + /** + * Clears all captured source events + * @method create + * @param {mouse} mouse + */ + Mouse.clearSourceEvents = function(mouse) { + mouse.sourceEvents.mousemove = null; + mouse.sourceEvents.mousedown = null; + mouse.sourceEvents.mouseup = null; + }; /** * Description