From fb5d899086188cef0c1225e1950a6d26e1974328 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 21 Jun 2014 17:44:25 +0100 Subject: [PATCH] removed engine.input, removed Engine mouse events, added MouseConstraint mouse events --- demo/js/Demo.js | 20 ++++++---- src/constraint/MouseConstraint.js | 66 +++++++++++++++++++++++++++++-- src/core/Engine.js | 65 ------------------------------ src/render/Render.js | 1 - 4 files changed, 76 insertions(+), 76 deletions(-) diff --git a/demo/js/Demo.js b/demo/js/Demo.js index e1ffdf32..6ed3ca86 100644 --- a/demo/js/Demo.js +++ b/demo/js/Demo.js @@ -161,7 +161,7 @@ World.add(_engine.world, [ground, pyramid, ground2, pyramid2, rock, elastic]); Events.on(_engine, 'tick', function(event) { - if (_engine.input.mouse.button === -1 && (rock.position.x > 190 || rock.position.y < 430)) { + if (_mouseConstraint.mouse.button === -1 && (rock.position.x > 190 || rock.position.y < 430)) { rock = Bodies.polygon(170, 450, 7, 20, rockOptions); World.add(_engine.world, rock); elastic.bodyB = rock; @@ -340,7 +340,7 @@ _sceneEvents.push( Events.on(_engine, 'beforeTick', function() { var world = _engine.world, - mouse = _engine.input.mouse, + mouse = _mouseConstraint.mouse, render = _engine.render, translate; @@ -1085,7 +1085,7 @@ _sceneEvents.push( // an example of using mouse events on an engine.input.mouse - Events.on(_engine, 'mousedown', function(event) { + Events.on(_mouseConstraint, 'mousedown', function(event) { var mousePosition = event.mouse.position; console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y); _engine.render.options.background = 'cornsilk'; @@ -1097,7 +1097,7 @@ _sceneEvents.push( // an example of using mouse events on an engine.input.mouse - Events.on(_engine, 'mouseup', function(event) { + Events.on(_mouseConstraint, 'mouseup', function(event) { var mousePosition = event.mouse.position; _engine.render.options.background = "white"; console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y); @@ -1186,7 +1186,7 @@ _sceneEvents.push( Events.on(_engine, 'afterRender', function() { - var mouse = _engine.input.mouse, + var mouse = _mouseConstraint.mouse, context = _engine.render.context, bodies = Composite.allBodies(_engine.world), startPoint = { x: 400, y: 100 }, @@ -1329,6 +1329,12 @@ // clear all scene events for (var i = 0; i < _sceneEvents.length; i++) Events.off(_engine, _sceneEvents[i]); + + if (_mouseConstraint.events) { + for (var i = 0; i < _sceneEvents.length; i++) + Events.off(_mouseConstraint, _sceneEvents[i]); + } + _sceneEvents = []; // reset id pool @@ -1338,8 +1344,8 @@ Common._seed = 0; // reset mouse offset and scale (only required for Demo.views) - Mouse.setScale(_engine.input.mouse, { x: 1, y: 1 }); - Mouse.setOffset(_engine.input.mouse, { x: 0, y: 0 }); + Mouse.setScale(_mouseConstraint.mouse, { x: 1, y: 1 }); + Mouse.setOffset(_mouseConstraint.mouse, { x: 0, y: 0 }); _engine.enableSleeping = false; _engine.world.gravity.y = 1; diff --git a/src/constraint/MouseConstraint.js b/src/constraint/MouseConstraint.js index c7a9046c..a3303ee5 100644 --- a/src/constraint/MouseConstraint.js +++ b/src/constraint/MouseConstraint.js @@ -22,7 +22,7 @@ var MouseConstraint = {}; * @return {MouseConstraint} A new MouseConstraint */ MouseConstraint.create = function(engine, options) { - var mouse = engine.input.mouse; + var mouse = (options && options.mouse) || Mouse.create(engine.render.canvas); var constraint = Constraint.create({ label: 'Mouse Constraint', @@ -50,6 +50,7 @@ var MouseConstraint = {}; Events.on(engine, 'tick', function(event) { var allBodies = Composite.allBodies(engine.world); MouseConstraint.update(mouseConstraint, allBodies); + _triggerEvents(mouseConstraint); }); return mouseConstraint; @@ -91,6 +92,65 @@ var MouseConstraint = {}; } }; + /** + * Triggers mouse constraint events + * @method _triggerEvents + * @private + * @param {mouse} mouse + */ + var _triggerEvents = function(mouseConstraint) { + var mouse = mouseConstraint.mouse, + mouseEvents = mouse.sourceEvents; + + if (mouseEvents.mousemove) + Events.trigger(mouseConstraint, 'mousemove', { mouse: mouse }); + + if (mouseEvents.mousedown) + Events.trigger(mouseConstraint, 'mousedown', { mouse: mouse }); + + if (mouseEvents.mouseup) + Events.trigger(mouseConstraint, 'mouseup', { mouse: mouse }); + + // reset the mouse state ready for the next step + Mouse.clearSourceEvents(mouse); + }; + + /* + * + * Events Documentation + * + */ + + /** + * Fired when the mouse has moved (or a touch moves) during the last step + * + * @event mousemove + * @param {} event An event object + * @param {mouse} event.mouse The engine's mouse instance + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + + /** + * Fired when the mouse is down (or a touch has started) during the last step + * + * @event mousedown + * @param {} event An event object + * @param {mouse} event.mouse The engine's mouse instance + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + + /** + * Fired when the mouse is up (or a touch has ended) during the last step + * + * @event mouseup + * @param {} event An event object + * @param {mouse} event.mouse The engine's mouse instance + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + /* * * Properties Documentation @@ -106,11 +166,11 @@ var MouseConstraint = {}; */ /** - * The `Mouse` instance in use. + * The `Mouse` instance in use. If not supplied in `MouseConstraint.create`, one will be created. * * @property mouse * @type mouse - * @default engine.input.mouse + * @default mouse */ /** diff --git a/src/core/Engine.js b/src/core/Engine.js index 3e8550cd..952c738b 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -42,7 +42,6 @@ var Engine = {}; constraintIterations: 2, enableSleeping: false, timeScale: 1, - input: {}, events: [], timing: { fps: _fps, @@ -66,7 +65,6 @@ var Engine = {}; engine.world = World.create(engine.world); engine.pairs = Pairs.create(); engine.metrics = engine.metrics || Metrics.create(); - engine.input.mouse = engine.input.mouse || Mouse.create(engine.render.canvas); engine.broadphase = engine.broadphase || { current: 'grid', @@ -167,7 +165,6 @@ var Engine = {}; // trigger events that may have occured during the step _triggerCollisionEvents(engine); - _triggerMouseEvents(engine); // render Engine.render(engine); @@ -341,38 +338,6 @@ var Engine = {}; } }; - /** - * Triggers mouse events - * @method _triggerMouseEvents - * @private - * @param {engine} engine - */ - var _triggerMouseEvents = function(engine) { - var mouse = engine.input.mouse, - mouseEvents = mouse.sourceEvents; - - if (mouseEvents.mousemove) { - Events.trigger(engine, 'mousemove', { - mouse: mouse - }); - } - - if (mouseEvents.mousedown) { - Events.trigger(engine, 'mousedown', { - mouse: mouse - }); - } - - if (mouseEvents.mouseup) { - Events.trigger(engine, 'mouseup', { - mouse: mouse - }); - } - - // reset the mouse state ready for the next step - Mouse.clearSourceEvents(mouse); - }; - /** * Triggers collision events * @method _triggerMouseEvents @@ -477,36 +442,6 @@ var Engine = {}; * @param {} event.name The name of the event */ - /** - * Fired when the mouse has moved (or a touch moves) during the last step - * - * @event mousemove - * @param {} event An event object - * @param {mouse} event.mouse The engine's mouse instance - * @param {} event.source The source object of the event - * @param {} event.name The name of the event - */ - - /** - * Fired when the mouse is down (or a touch has started) during the last step - * - * @event mousedown - * @param {} event An event object - * @param {mouse} event.mouse The engine's mouse instance - * @param {} event.source The source object of the event - * @param {} event.name The name of the event - */ - - /** - * Fired when the mouse is up (or a touch has ended) during the last step - * - * @event mouseup - * @param {} event An event object - * @param {mouse} event.mouse The engine's mouse instance - * @param {} event.source The source object of the event - * @param {} event.name The name of the event - */ - /** * Fired after engine update, provides a list of all pairs that have started to collide in the current tick (if any) * diff --git a/src/render/Render.js b/src/render/Render.js index 6a0e3294..adb80ec0 100644 --- a/src/render/Render.js +++ b/src/render/Render.js @@ -770,7 +770,6 @@ var Render = {}; */ Render.inspector = function(inspector, context) { var engine = inspector.engine, - mouse = engine.input.mouse, selected = inspector.selected, c = context, render = engine.render,