mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-26 13:49:01 -05:00
removed engine.input, removed Engine mouse events, added MouseConstraint mouse events
This commit is contained in:
parent
ca978fa8c4
commit
fb5d899086
4 changed files with 76 additions and 76 deletions
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
|
@ -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)
|
||||
*
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue