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]);
|
World.add(_engine.world, [ground, pyramid, ground2, pyramid2, rock, elastic]);
|
||||||
|
|
||||||
Events.on(_engine, 'tick', function(event) {
|
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);
|
rock = Bodies.polygon(170, 450, 7, 20, rockOptions);
|
||||||
World.add(_engine.world, rock);
|
World.add(_engine.world, rock);
|
||||||
elastic.bodyB = rock;
|
elastic.bodyB = rock;
|
||||||
|
@ -340,7 +340,7 @@
|
||||||
_sceneEvents.push(
|
_sceneEvents.push(
|
||||||
Events.on(_engine, 'beforeTick', function() {
|
Events.on(_engine, 'beforeTick', function() {
|
||||||
var world = _engine.world,
|
var world = _engine.world,
|
||||||
mouse = _engine.input.mouse,
|
mouse = _mouseConstraint.mouse,
|
||||||
render = _engine.render,
|
render = _engine.render,
|
||||||
translate;
|
translate;
|
||||||
|
|
||||||
|
@ -1085,7 +1085,7 @@
|
||||||
_sceneEvents.push(
|
_sceneEvents.push(
|
||||||
|
|
||||||
// an example of using mouse events on an engine.input.mouse
|
// 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;
|
var mousePosition = event.mouse.position;
|
||||||
console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y);
|
console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y);
|
||||||
_engine.render.options.background = 'cornsilk';
|
_engine.render.options.background = 'cornsilk';
|
||||||
|
@ -1097,7 +1097,7 @@
|
||||||
_sceneEvents.push(
|
_sceneEvents.push(
|
||||||
|
|
||||||
// an example of using mouse events on an engine.input.mouse
|
// 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;
|
var mousePosition = event.mouse.position;
|
||||||
_engine.render.options.background = "white";
|
_engine.render.options.background = "white";
|
||||||
console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y);
|
console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y);
|
||||||
|
@ -1186,7 +1186,7 @@
|
||||||
|
|
||||||
_sceneEvents.push(
|
_sceneEvents.push(
|
||||||
Events.on(_engine, 'afterRender', function() {
|
Events.on(_engine, 'afterRender', function() {
|
||||||
var mouse = _engine.input.mouse,
|
var mouse = _mouseConstraint.mouse,
|
||||||
context = _engine.render.context,
|
context = _engine.render.context,
|
||||||
bodies = Composite.allBodies(_engine.world),
|
bodies = Composite.allBodies(_engine.world),
|
||||||
startPoint = { x: 400, y: 100 },
|
startPoint = { x: 400, y: 100 },
|
||||||
|
@ -1329,6 +1329,12 @@
|
||||||
// clear all scene events
|
// clear all scene events
|
||||||
for (var i = 0; i < _sceneEvents.length; i++)
|
for (var i = 0; i < _sceneEvents.length; i++)
|
||||||
Events.off(_engine, _sceneEvents[i]);
|
Events.off(_engine, _sceneEvents[i]);
|
||||||
|
|
||||||
|
if (_mouseConstraint.events) {
|
||||||
|
for (var i = 0; i < _sceneEvents.length; i++)
|
||||||
|
Events.off(_mouseConstraint, _sceneEvents[i]);
|
||||||
|
}
|
||||||
|
|
||||||
_sceneEvents = [];
|
_sceneEvents = [];
|
||||||
|
|
||||||
// reset id pool
|
// reset id pool
|
||||||
|
@ -1338,8 +1344,8 @@
|
||||||
Common._seed = 0;
|
Common._seed = 0;
|
||||||
|
|
||||||
// reset mouse offset and scale (only required for Demo.views)
|
// reset mouse offset and scale (only required for Demo.views)
|
||||||
Mouse.setScale(_engine.input.mouse, { x: 1, y: 1 });
|
Mouse.setScale(_mouseConstraint.mouse, { x: 1, y: 1 });
|
||||||
Mouse.setOffset(_engine.input.mouse, { x: 0, y: 0 });
|
Mouse.setOffset(_mouseConstraint.mouse, { x: 0, y: 0 });
|
||||||
|
|
||||||
_engine.enableSleeping = false;
|
_engine.enableSleeping = false;
|
||||||
_engine.world.gravity.y = 1;
|
_engine.world.gravity.y = 1;
|
||||||
|
|
|
@ -22,7 +22,7 @@ var MouseConstraint = {};
|
||||||
* @return {MouseConstraint} A new MouseConstraint
|
* @return {MouseConstraint} A new MouseConstraint
|
||||||
*/
|
*/
|
||||||
MouseConstraint.create = function(engine, options) {
|
MouseConstraint.create = function(engine, options) {
|
||||||
var mouse = engine.input.mouse;
|
var mouse = (options && options.mouse) || Mouse.create(engine.render.canvas);
|
||||||
|
|
||||||
var constraint = Constraint.create({
|
var constraint = Constraint.create({
|
||||||
label: 'Mouse Constraint',
|
label: 'Mouse Constraint',
|
||||||
|
@ -50,6 +50,7 @@ var MouseConstraint = {};
|
||||||
Events.on(engine, 'tick', function(event) {
|
Events.on(engine, 'tick', function(event) {
|
||||||
var allBodies = Composite.allBodies(engine.world);
|
var allBodies = Composite.allBodies(engine.world);
|
||||||
MouseConstraint.update(mouseConstraint, allBodies);
|
MouseConstraint.update(mouseConstraint, allBodies);
|
||||||
|
_triggerEvents(mouseConstraint);
|
||||||
});
|
});
|
||||||
|
|
||||||
return 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
|
* 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
|
* @property mouse
|
||||||
* @type mouse
|
* @type mouse
|
||||||
* @default engine.input.mouse
|
* @default mouse
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -42,7 +42,6 @@ var Engine = {};
|
||||||
constraintIterations: 2,
|
constraintIterations: 2,
|
||||||
enableSleeping: false,
|
enableSleeping: false,
|
||||||
timeScale: 1,
|
timeScale: 1,
|
||||||
input: {},
|
|
||||||
events: [],
|
events: [],
|
||||||
timing: {
|
timing: {
|
||||||
fps: _fps,
|
fps: _fps,
|
||||||
|
@ -66,7 +65,6 @@ var Engine = {};
|
||||||
engine.world = World.create(engine.world);
|
engine.world = World.create(engine.world);
|
||||||
engine.pairs = Pairs.create();
|
engine.pairs = Pairs.create();
|
||||||
engine.metrics = engine.metrics || Metrics.create();
|
engine.metrics = engine.metrics || Metrics.create();
|
||||||
engine.input.mouse = engine.input.mouse || Mouse.create(engine.render.canvas);
|
|
||||||
|
|
||||||
engine.broadphase = engine.broadphase || {
|
engine.broadphase = engine.broadphase || {
|
||||||
current: 'grid',
|
current: 'grid',
|
||||||
|
@ -167,7 +165,6 @@ var Engine = {};
|
||||||
|
|
||||||
// trigger events that may have occured during the step
|
// trigger events that may have occured during the step
|
||||||
_triggerCollisionEvents(engine);
|
_triggerCollisionEvents(engine);
|
||||||
_triggerMouseEvents(engine);
|
|
||||||
|
|
||||||
// render
|
// render
|
||||||
Engine.render(engine);
|
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
|
* Triggers collision events
|
||||||
* @method _triggerMouseEvents
|
* @method _triggerMouseEvents
|
||||||
|
@ -477,36 +442,6 @@ var Engine = {};
|
||||||
* @param {} event.name The name of the event
|
* @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)
|
* 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) {
|
Render.inspector = function(inspector, context) {
|
||||||
var engine = inspector.engine,
|
var engine = inspector.engine,
|
||||||
mouse = engine.input.mouse,
|
|
||||||
selected = inspector.selected,
|
selected = inspector.selected,
|
||||||
c = context,
|
c = context,
|
||||||
render = engine.render,
|
render = engine.render,
|
||||||
|
|
Loading…
Reference in a new issue