Skip to content

Commit

Permalink
removed engine.input, removed Engine mouse events, added MouseConstra…
Browse files Browse the repository at this point in the history
…int mouse events
  • Loading branch information
liabru committed Jun 21, 2014
1 parent ca978fa commit fb5d899
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 76 deletions.
20 changes: 13 additions & 7 deletions demo/js/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
66 changes: 63 additions & 3 deletions src/constraint/MouseConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
*/

/**
Expand Down
65 changes: 0 additions & 65 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ var Engine = {};
constraintIterations: 2,
enableSleeping: false,
timeScale: 1,
input: {},
events: [],
timing: {
fps: _fps,
Expand All @@ -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',
Expand Down Expand Up @@ -167,7 +165,6 @@ var Engine = {};

// trigger events that may have occured during the step
_triggerCollisionEvents(engine);
_triggerMouseEvents(engine);

// render
Engine.render(engine);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
*
Expand Down
1 change: 0 additions & 1 deletion src/render/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fb5d899

Please sign in to comment.