2014-02-28 13:15:01 -05:00
|
|
|
/**
|
|
|
|
* _Internal Class_, not generally used outside of the engine's internals.
|
|
|
|
*
|
|
|
|
* @class Mouse
|
|
|
|
*/
|
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
var Mouse;
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
2014-02-28 13:15:01 -05:00
|
|
|
/**
|
|
|
|
* Description
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
*/
|
2014-02-19 09:15:05 -05:00
|
|
|
Mouse = function(element) {
|
|
|
|
var mouse = this;
|
|
|
|
|
|
|
|
element = element || document.body;
|
|
|
|
|
|
|
|
this.position = { x: 0, y: 0 };
|
|
|
|
this.mousedownPosition = { x: 0, y: 0 };
|
|
|
|
this.mouseupPosition = { x: 0, y: 0 };
|
|
|
|
this.button = -1;
|
2014-03-14 15:36:58 -04:00
|
|
|
|
|
|
|
this.sourceEvents = {
|
|
|
|
mousemove: null,
|
|
|
|
mousedown: null,
|
|
|
|
mouseup: null
|
|
|
|
};
|
2014-02-19 09:15:05 -05:00
|
|
|
|
|
|
|
var mousemove = function(event) {
|
|
|
|
var position = _getRelativeMousePosition(event, element),
|
|
|
|
touches = event.changedTouches;
|
|
|
|
|
|
|
|
if (touches) {
|
|
|
|
mouse.button = 0;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
mouse.position = position;
|
2014-03-14 15:36:58 -04:00
|
|
|
mouse.sourceEvents.mousemove = event;
|
2014-02-19 09:15:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var mousedown = function(event) {
|
|
|
|
var position = _getRelativeMousePosition(event, element),
|
|
|
|
touches = event.changedTouches;
|
|
|
|
|
|
|
|
if (touches) {
|
|
|
|
mouse.button = 0;
|
|
|
|
event.preventDefault();
|
|
|
|
} else {
|
|
|
|
mouse.button = event.button;
|
|
|
|
}
|
|
|
|
|
|
|
|
mouse.position = mouse.mousedownPosition = position;
|
2014-03-14 15:36:58 -04:00
|
|
|
mouse.sourceEvents.mousedown = event;
|
2014-02-19 09:15:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var mouseup = function(event) {
|
|
|
|
var position = _getRelativeMousePosition(event, element),
|
|
|
|
touches = event.changedTouches;
|
|
|
|
|
|
|
|
if (touches) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
mouse.button = -1;
|
|
|
|
mouse.position = mouse.mouseupPosition = position;
|
2014-03-14 15:36:58 -04:00
|
|
|
mouse.sourceEvents.mouseup = event;
|
2014-02-19 09:15:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
element.addEventListener('mousemove', mousemove);
|
|
|
|
element.addEventListener('mousedown', mousedown);
|
|
|
|
element.addEventListener('mouseup', mouseup);
|
|
|
|
|
|
|
|
element.addEventListener('touchmove', mousemove);
|
|
|
|
element.addEventListener('touchstart', mousedown);
|
|
|
|
element.addEventListener('touchend', mouseup);
|
|
|
|
};
|
|
|
|
|
2014-02-28 13:15:01 -05:00
|
|
|
/**
|
|
|
|
* Description
|
|
|
|
* @method create
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
* @return {mouse} A new mouse
|
|
|
|
*/
|
2014-02-19 09:15:05 -05:00
|
|
|
Mouse.create = function(element) {
|
|
|
|
return new Mouse(element);
|
|
|
|
};
|
2014-03-14 15:36:58 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
2014-02-19 09:15:05 -05:00
|
|
|
|
2014-02-28 13:15:01 -05:00
|
|
|
/**
|
|
|
|
* Description
|
|
|
|
* @method _getRelativeMousePosition
|
|
|
|
* @private
|
|
|
|
* @param {} event
|
|
|
|
* @param {} element
|
|
|
|
* @return ObjectExpression
|
|
|
|
*/
|
2014-02-19 09:15:05 -05:00
|
|
|
var _getRelativeMousePosition = function(event, element) {
|
|
|
|
var elementBounds = element.getBoundingClientRect(),
|
|
|
|
scrollX = (window.pageXOffset !== undefined) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft,
|
|
|
|
scrollY = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop,
|
|
|
|
touches = event.changedTouches,
|
|
|
|
x, y;
|
|
|
|
|
|
|
|
if (touches) {
|
|
|
|
x = touches[0].pageX - elementBounds.left - scrollX;
|
|
|
|
y = touches[0].pageY - elementBounds.top - scrollY;
|
|
|
|
} else {
|
|
|
|
x = event.pageX - elementBounds.left - scrollX;
|
|
|
|
y = event.pageY - elementBounds.top - scrollY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: x / (element.clientWidth / element.width),
|
|
|
|
y: y / (element.clientHeight / element.height)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|