mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-26 13:49:01 -05:00
added mouse events
This commit is contained in:
parent
9153c7b836
commit
97aef1faea
2 changed files with 140 additions and 50 deletions
|
@ -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
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue