2013-08-28 06:02:55 +00:00
|
|
|
/**
|
|
|
|
* @author Mat Groves http://matgroves.com/ @Doormat23
|
|
|
|
*/
|
2013-12-23 04:19:52 +00:00
|
|
|
|
2013-11-06 04:51:23 +00:00
|
|
|
/**
|
2013-08-28 06:02:55 +00:00
|
|
|
* The interaction manager deals with mouse and touch events. Any DisplayObject can be interactive
|
2014-02-06 00:19:46 +00:00
|
|
|
* if its interactive parameter is set to true
|
2013-08-28 06:02:55 +00:00
|
|
|
* This manager also supports multitouch.
|
|
|
|
*
|
|
|
|
* @class InteractionManager
|
|
|
|
* @constructor
|
|
|
|
* @param stage {Stage} The stage to handle interactions
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager = function(stage)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
/**
|
2014-02-06 00:19:46 +00:00
|
|
|
* a reference to the stage
|
2013-12-23 04:19:52 +00:00
|
|
|
*
|
|
|
|
* @property stage
|
|
|
|
* @type Stage
|
|
|
|
*/
|
|
|
|
this.stage = stage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the mouse data
|
|
|
|
*
|
|
|
|
* @property mouse
|
|
|
|
* @type InteractionData
|
|
|
|
*/
|
|
|
|
this.mouse = new PIXI.InteractionData();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* an object that stores current touches (InteractionData) by id reference
|
|
|
|
*
|
|
|
|
* @property touchs
|
|
|
|
* @type Object
|
|
|
|
*/
|
|
|
|
this.touchs = {};
|
|
|
|
|
|
|
|
// helpers
|
|
|
|
this.tempPoint = new PIXI.Point();
|
|
|
|
|
2014-02-12 01:25:36 +00:00
|
|
|
/**
|
2014-03-31 10:04:02 +00:00
|
|
|
*
|
2014-02-12 01:25:36 +00:00
|
|
|
* @property mouseoverEnabled
|
|
|
|
* @type Boolean
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-23 04:19:52 +00:00
|
|
|
this.mouseoverEnabled = true;
|
|
|
|
|
2014-02-12 01:25:36 +00:00
|
|
|
/**
|
|
|
|
* tiny little interactiveData pool !
|
2014-03-31 10:04:02 +00:00
|
|
|
*
|
2014-02-12 01:25:36 +00:00
|
|
|
* @property pool
|
|
|
|
* @type Array
|
|
|
|
*/
|
2013-12-23 04:19:52 +00:00
|
|
|
this.pool = [];
|
|
|
|
|
2014-02-12 01:25:36 +00:00
|
|
|
/**
|
|
|
|
* An array containing all the iterative items from the our interactive tree
|
|
|
|
* @property interactiveItems
|
|
|
|
* @type Array
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
*/
|
2013-12-23 04:19:52 +00:00
|
|
|
this.interactiveItems = [];
|
2014-02-12 01:25:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Our canvas
|
|
|
|
* @property interactionDOMElement
|
|
|
|
* @type HTMLCanvasElement
|
|
|
|
* @private
|
|
|
|
*/
|
2013-12-23 04:19:52 +00:00
|
|
|
this.interactionDOMElement = null;
|
|
|
|
|
|
|
|
//this will make it so that you dont have to call bind all the time
|
|
|
|
this.onMouseMove = this.onMouseMove.bind( this );
|
|
|
|
this.onMouseDown = this.onMouseDown.bind(this);
|
|
|
|
this.onMouseOut = this.onMouseOut.bind(this);
|
|
|
|
this.onMouseUp = this.onMouseUp.bind(this);
|
|
|
|
|
|
|
|
this.onTouchStart = this.onTouchStart.bind(this);
|
|
|
|
this.onTouchEnd = this.onTouchEnd.bind(this);
|
|
|
|
this.onTouchMove = this.onTouchMove.bind(this);
|
2014-02-12 01:25:36 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
this.last = 0;
|
2014-02-06 00:19:46 +00:00
|
|
|
|
2014-02-12 01:25:36 +00:00
|
|
|
/**
|
|
|
|
* The css style of the cursor that is being used
|
|
|
|
* @property currentCursorStyle
|
|
|
|
* @type String
|
|
|
|
*
|
|
|
|
*/
|
2014-02-06 00:19:46 +00:00
|
|
|
this.currentCursorStyle = 'inherit';
|
|
|
|
|
2014-02-12 01:25:36 +00:00
|
|
|
/**
|
|
|
|
* Is set to true when the mouse is moved out of the canvas
|
|
|
|
* @property mouseOut
|
|
|
|
* @type Boolean
|
|
|
|
*
|
|
|
|
*/
|
2014-02-06 00:19:46 +00:00
|
|
|
this.mouseOut = false;
|
2013-12-23 04:19:52 +00:00
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
// constructor
|
|
|
|
PIXI.InteractionManager.prototype.constructor = PIXI.InteractionManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collects an interactive sprite recursively to have their interactions managed
|
|
|
|
*
|
|
|
|
* @method collectInteractiveSprite
|
|
|
|
* @param displayObject {DisplayObject} the displayObject to collect
|
2014-02-12 01:25:36 +00:00
|
|
|
* @param iParent {DisplayObject} the display object's parent
|
2013-08-28 06:02:55 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.collectInteractiveSprite = function(displayObject, iParent)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
var children = displayObject.children;
|
|
|
|
var length = children.length;
|
|
|
|
|
2014-02-12 01:25:36 +00:00
|
|
|
// make an interaction tree... {item.__interactiveParent}
|
2013-12-23 04:19:52 +00:00
|
|
|
for (var i = length-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
var child = children[i];
|
|
|
|
|
|
|
|
// push all interactive bits
|
2014-02-18 03:01:51 +00:00
|
|
|
if(child._interactive)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
iParent.interactiveChildren = true;
|
|
|
|
//child.__iParent = iParent;
|
|
|
|
this.interactiveItems.push(child);
|
|
|
|
|
|
|
|
if(child.children.length > 0)
|
|
|
|
{
|
|
|
|
this.collectInteractiveSprite(child, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child.__iParent = null;
|
|
|
|
|
|
|
|
if(child.children.length > 0)
|
|
|
|
{
|
|
|
|
this.collectInteractiveSprite(child, iParent);
|
|
|
|
}
|
|
|
|
}
|
2014-02-12 01:25:36 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the target for event delegation
|
|
|
|
*
|
|
|
|
* @method setTarget
|
|
|
|
* @param target {WebGLRenderer|CanvasRenderer} the renderer to bind events to
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.setTarget = function(target)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
this.target = target;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
//check if the dom element has been set. If it has don't do anything
|
|
|
|
if( this.interactionDOMElement === null ) {
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
this.setTargetDomElement( target.view );
|
|
|
|
}
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2014-03-31 10:04:02 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
};
|
2013-11-06 04:51:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-02-06 00:19:46 +00:00
|
|
|
* Sets the DOM element which will receive mouse/touch events. This is useful for when you have other DOM
|
|
|
|
* elements on top of the renderers Canvas element. With this you'll be able to delegate another DOM element
|
2013-11-06 04:51:23 +00:00
|
|
|
* to receive those events
|
|
|
|
*
|
|
|
|
* @method setTargetDomElement
|
2014-02-06 00:19:46 +00:00
|
|
|
* @param domElement {DOMElement} the DOM element which will receive mouse and touch events
|
2013-11-06 04:51:23 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.setTargetDomElement = function(domElement)
|
|
|
|
{
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
this.removeEvents();
|
2013-11-06 04:51:23 +00:00
|
|
|
|
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
if (window.navigator.msPointerEnabled)
|
|
|
|
{
|
|
|
|
// time to remove some of that zoom in ja..
|
|
|
|
domElement.style['-ms-content-zooming'] = 'none';
|
|
|
|
domElement.style['-ms-touch-action'] = 'none';
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
// DO some window specific touch!
|
|
|
|
}
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
this.interactionDOMElement = domElement;
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
domElement.addEventListener('mousemove', this.onMouseMove, true);
|
|
|
|
domElement.addEventListener('mousedown', this.onMouseDown, true);
|
|
|
|
domElement.addEventListener('mouseout', this.onMouseOut, true);
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
// aint no multi touch just yet!
|
|
|
|
domElement.addEventListener('touchstart', this.onTouchStart, true);
|
|
|
|
domElement.addEventListener('touchend', this.onTouchEnd, true);
|
|
|
|
domElement.addEventListener('touchmove', this.onTouchMove, true);
|
2014-02-06 00:19:46 +00:00
|
|
|
|
2014-02-16 14:32:16 +00:00
|
|
|
window.addEventListener('mouseup', this.onMouseUp, true);
|
2014-02-06 00:19:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PIXI.InteractionManager.prototype.removeEvents = function()
|
|
|
|
{
|
|
|
|
if(!this.interactionDOMElement)return;
|
|
|
|
|
|
|
|
this.interactionDOMElement.style['-ms-content-zooming'] = '';
|
|
|
|
this.interactionDOMElement.style['-ms-touch-action'] = '';
|
|
|
|
|
|
|
|
this.interactionDOMElement.removeEventListener('mousemove', this.onMouseMove, true);
|
|
|
|
this.interactionDOMElement.removeEventListener('mousedown', this.onMouseDown, true);
|
|
|
|
this.interactionDOMElement.removeEventListener('mouseout', this.onMouseOut, true);
|
|
|
|
|
|
|
|
// aint no multi touch just yet!
|
|
|
|
this.interactionDOMElement.removeEventListener('touchstart', this.onTouchStart, true);
|
|
|
|
this.interactionDOMElement.removeEventListener('touchend', this.onTouchEnd, true);
|
|
|
|
this.interactionDOMElement.removeEventListener('touchmove', this.onTouchMove, true);
|
|
|
|
|
|
|
|
this.interactionDOMElement = null;
|
|
|
|
|
2014-02-16 14:32:16 +00:00
|
|
|
window.removeEventListener('mouseup', this.onMouseUp, true);
|
2013-12-23 04:19:52 +00:00
|
|
|
};
|
2013-11-06 04:51:23 +00:00
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
/**
|
|
|
|
* updates the state of interactive objects
|
|
|
|
*
|
|
|
|
* @method update
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.update = function()
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
if(!this.target)return;
|
|
|
|
|
|
|
|
// frequency of 30fps??
|
|
|
|
var now = Date.now();
|
|
|
|
var diff = now - this.last;
|
2014-02-06 00:19:46 +00:00
|
|
|
diff = (diff * PIXI.INTERACTION_FREQUENCY ) / 1000;
|
2013-12-23 04:19:52 +00:00
|
|
|
if(diff < 1)return;
|
|
|
|
this.last = now;
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
// ok.. so mouse events??
|
|
|
|
// yes for now :)
|
2014-02-06 00:19:46 +00:00
|
|
|
// OPTIMISE - how often to check??
|
2013-12-23 04:19:52 +00:00
|
|
|
if(this.dirty)
|
|
|
|
{
|
|
|
|
this.dirty = false;
|
|
|
|
|
|
|
|
var len = this.interactiveItems.length;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
this.interactiveItems[i].interactiveChildren = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.interactiveItems = [];
|
|
|
|
|
|
|
|
if(this.stage.interactive)this.interactiveItems.push(this.stage);
|
|
|
|
// go through and collect all the objects that are interactive..
|
|
|
|
this.collectInteractiveSprite(this.stage, this.stage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through interactive objects!
|
|
|
|
var length = this.interactiveItems.length;
|
2014-02-06 00:19:46 +00:00
|
|
|
var cursor = 'inherit';
|
|
|
|
var over = false;
|
2013-12-23 04:19:52 +00:00
|
|
|
|
2014-03-31 10:04:02 +00:00
|
|
|
for (i = 0; i < length; i++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[i];
|
|
|
|
|
|
|
|
// OPTIMISATION - only calculate every time if the mousemove function exists..
|
|
|
|
// OK so.. does the object have any other interactive functions?
|
|
|
|
// hit-test the clip!
|
2014-02-06 00:19:46 +00:00
|
|
|
// if(item.mouseover || item.mouseout || item.buttonMode)
|
|
|
|
// {
|
|
|
|
// ok so there are some functions so lets hit test it..
|
|
|
|
item.__hit = this.hitTest(item, this.mouse);
|
|
|
|
this.mouse.target = item;
|
|
|
|
// ok so deal with interactions..
|
|
|
|
// looks like there was a hit!
|
|
|
|
if(item.__hit && !over)
|
|
|
|
{
|
|
|
|
if(item.buttonMode) cursor = item.defaultCursor;
|
2013-12-23 04:19:52 +00:00
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
if(!item.interactiveChildren)over = true;
|
2013-12-23 04:19:52 +00:00
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
if(!item.__isOver)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
2014-02-06 00:19:46 +00:00
|
|
|
if(item.mouseover)item.mouseover(this.mouse);
|
|
|
|
item.__isOver = true;
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
2014-02-06 00:19:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(item.__isOver)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
2014-02-06 00:19:46 +00:00
|
|
|
// roll out!
|
|
|
|
if(item.mouseout)item.mouseout(this.mouse);
|
|
|
|
item.__isOver = false;
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 00:19:46 +00:00
|
|
|
|
|
|
|
if( this.currentCursorStyle !== cursor )
|
|
|
|
{
|
|
|
|
this.currentCursorStyle = cursor;
|
|
|
|
this.interactionDOMElement.style.cursor = cursor;
|
|
|
|
}
|
2013-12-23 04:19:52 +00:00
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-06 00:19:46 +00:00
|
|
|
* Is called when the mouse moves across the renderer element
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
|
|
|
* @method onMouseMove
|
|
|
|
* @param event {Event} The DOM event of the mouse moving
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.onMouseMove = function(event)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
this.mouse.originalEvent = event || window.event; //IE uses window.event
|
|
|
|
// TODO optimize by not check EVERY TIME! maybe half as often? //
|
|
|
|
var rect = this.interactionDOMElement.getBoundingClientRect();
|
|
|
|
|
|
|
|
this.mouse.global.x = (event.clientX - rect.left) * (this.target.width / rect.width);
|
|
|
|
this.mouse.global.y = (event.clientY - rect.top) * ( this.target.height / rect.height);
|
|
|
|
|
|
|
|
var length = this.interactiveItems.length;
|
|
|
|
|
2014-03-31 10:04:02 +00:00
|
|
|
for (var i = 0; i < length; i++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[i];
|
|
|
|
|
|
|
|
if(item.mousemove)
|
|
|
|
{
|
|
|
|
//call the function!
|
|
|
|
item.mousemove(this.mouse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is called when the mouse button is pressed down on the renderer element
|
|
|
|
*
|
|
|
|
* @method onMouseDown
|
|
|
|
* @param event {Event} The DOM event of a mouse button being pressed down
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.onMouseDown = function(event)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
this.mouse.originalEvent = event || window.event; //IE uses window.event
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
if(PIXI.AUTO_PREVENT_DEFAULT)this.mouse.originalEvent.preventDefault();
|
|
|
|
|
|
|
|
// loop through interaction tree...
|
2013-12-23 04:19:52 +00:00
|
|
|
// hit test each item! ->
|
|
|
|
// get interactive items under point??
|
|
|
|
//stage.__i
|
|
|
|
var length = this.interactiveItems.length;
|
|
|
|
|
|
|
|
// while
|
|
|
|
// hit test
|
2014-03-31 10:04:02 +00:00
|
|
|
for (var i = 0; i < length; i++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[i];
|
|
|
|
|
|
|
|
if(item.mousedown || item.click)
|
|
|
|
{
|
|
|
|
item.__mouseIsDown = true;
|
|
|
|
item.__hit = this.hitTest(item, this.mouse);
|
|
|
|
|
|
|
|
if(item.__hit)
|
|
|
|
{
|
|
|
|
//call the function!
|
|
|
|
if(item.mousedown)item.mousedown(this.mouse);
|
|
|
|
item.__isDown = true;
|
|
|
|
|
|
|
|
// just the one!
|
|
|
|
if(!item.interactiveChildren)break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
/**
|
|
|
|
* Is called when the mouse button is moved out of the renderer element
|
|
|
|
*
|
2014-02-12 01:25:36 +00:00
|
|
|
* @method onMouseOut
|
2014-02-06 00:19:46 +00:00
|
|
|
* @param event {Event} The DOM event of a mouse button being moved out
|
2014-03-31 10:04:02 +00:00
|
|
|
* @private
|
2014-02-06 00:19:46 +00:00
|
|
|
*/
|
2013-12-23 04:19:52 +00:00
|
|
|
PIXI.InteractionManager.prototype.onMouseOut = function()
|
2013-08-28 06:02:55 +00:00
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
var length = this.interactiveItems.length;
|
|
|
|
|
|
|
|
this.interactionDOMElement.style.cursor = 'inherit';
|
|
|
|
|
2014-03-31 10:04:02 +00:00
|
|
|
for (var i = 0; i < length; i++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[i];
|
|
|
|
if(item.__isOver)
|
|
|
|
{
|
|
|
|
this.mouse.target = item;
|
|
|
|
if(item.mouseout)item.mouseout(this.mouse);
|
|
|
|
item.__isOver = false;
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 00:19:46 +00:00
|
|
|
|
|
|
|
this.mouseOut = true;
|
|
|
|
|
|
|
|
// move the mouse to an impossible position
|
|
|
|
this.mouse.global.x = -10000;
|
|
|
|
this.mouse.global.y = -10000;
|
2013-12-23 04:19:52 +00:00
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is called when the mouse button is released on the renderer element
|
|
|
|
*
|
|
|
|
* @method onMouseUp
|
|
|
|
* @param event {Event} The DOM event of a mouse button being released
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.onMouseUp = function(event)
|
|
|
|
{
|
2014-02-06 00:19:46 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
this.mouse.originalEvent = event || window.event; //IE uses window.event
|
|
|
|
|
|
|
|
var length = this.interactiveItems.length;
|
|
|
|
var up = false;
|
|
|
|
|
2014-03-31 10:04:02 +00:00
|
|
|
for (var i = 0; i < length; i++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[i];
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
item.__hit = this.hitTest(item, this.mouse);
|
2013-12-23 04:19:52 +00:00
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
if(item.__hit && !up)
|
|
|
|
{
|
|
|
|
//call the function!
|
|
|
|
if(item.mouseup)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
2014-02-06 00:19:46 +00:00
|
|
|
item.mouseup(this.mouse);
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
2014-02-06 00:19:46 +00:00
|
|
|
if(item.__isDown)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
2014-02-06 00:19:46 +00:00
|
|
|
if(item.click)item.click(this.mouse);
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
if(!item.interactiveChildren)up = true;
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
2014-02-06 00:19:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(item.__isDown)
|
|
|
|
{
|
|
|
|
if(item.mouseupoutside)item.mouseupoutside(this.mouse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item.__isDown = false;
|
|
|
|
//}
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-06 00:19:46 +00:00
|
|
|
* Tests if the current mouse coordinates hit a sprite
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
|
|
|
* @method hitTest
|
|
|
|
* @param item {DisplayObject} The displayObject to test for a hit
|
2014-02-06 00:19:46 +00:00
|
|
|
* @param interactionData {InteractionData} The interactionData object to update in the case there is a hit
|
2013-08-28 06:02:55 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.hitTest = function(item, interactionData)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
var global = interactionData.global;
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
if( !item.worldVisible )return false;
|
2013-12-23 04:19:52 +00:00
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
// temp fix for if the element is in a non visible
|
2014-03-31 10:04:02 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var isSprite = (item instanceof PIXI.Sprite),
|
|
|
|
worldTransform = item.worldTransform,
|
2014-02-06 00:19:46 +00:00
|
|
|
a00 = worldTransform.a, a01 = worldTransform.b, a02 = worldTransform.tx,
|
|
|
|
a10 = worldTransform.c, a11 = worldTransform.d, a12 = worldTransform.ty,
|
2013-12-23 04:19:52 +00:00
|
|
|
id = 1 / (a00 * a11 + a01 * -a10),
|
|
|
|
x = a11 * id * global.x + -a01 * id * global.y + (a12 * a01 - a02 * a11) * id,
|
|
|
|
y = a00 * id * global.y + -a10 * id * global.x + (-a12 * a00 + a02 * a10) * id;
|
|
|
|
|
|
|
|
interactionData.target = item;
|
|
|
|
|
|
|
|
//a sprite or display object with a hit area defined
|
|
|
|
if(item.hitArea && item.hitArea.contains) {
|
|
|
|
if(item.hitArea.contains(x, y)) {
|
|
|
|
//if(isSprite)
|
|
|
|
interactionData.target = item;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// a sprite with no hitarea defined
|
|
|
|
else if(isSprite)
|
|
|
|
{
|
|
|
|
var width = item.texture.frame.width,
|
|
|
|
height = item.texture.frame.height,
|
|
|
|
x1 = -width * item.anchor.x,
|
|
|
|
y1;
|
|
|
|
|
|
|
|
if(x > x1 && x < x1 + width)
|
|
|
|
{
|
|
|
|
y1 = -height * item.anchor.y;
|
|
|
|
|
|
|
|
if(y > y1 && y < y1 + height)
|
|
|
|
{
|
|
|
|
// set the target property if a hit is true!
|
|
|
|
interactionData.target = item;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var length = item.children.length;
|
|
|
|
|
2014-03-31 10:04:02 +00:00
|
|
|
for (var i = 0; i < length; i++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var tempItem = item.children[i];
|
|
|
|
var hit = this.hitTest(tempItem, interactionData);
|
|
|
|
if(hit)
|
|
|
|
{
|
|
|
|
// hmm.. TODO SET CORRECT TARGET?
|
|
|
|
interactionData.target = item;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-06 00:19:46 +00:00
|
|
|
* Is called when a touch is moved across the renderer element
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
|
|
|
* @method onTouchMove
|
2014-02-06 00:19:46 +00:00
|
|
|
* @param event {Event} The DOM event of a touch moving across the renderer view
|
2013-08-28 06:02:55 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.onTouchMove = function(event)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
var rect = this.interactionDOMElement.getBoundingClientRect();
|
|
|
|
var changedTouches = event.changedTouches;
|
|
|
|
var touchData;
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < changedTouches.length; i++)
|
|
|
|
{
|
|
|
|
var touchEvent = changedTouches[i];
|
|
|
|
touchData = this.touchs[touchEvent.identifier];
|
|
|
|
touchData.originalEvent = event || window.event;
|
|
|
|
|
|
|
|
// update the touch position
|
|
|
|
touchData.global.x = (touchEvent.clientX - rect.left) * (this.target.width / rect.width);
|
|
|
|
touchData.global.y = (touchEvent.clientY - rect.top) * (this.target.height / rect.height);
|
2014-02-06 00:19:46 +00:00
|
|
|
if(navigator.isCocoonJS) {
|
|
|
|
touchData.global.x = touchEvent.clientX;
|
|
|
|
touchData.global.y = touchEvent.clientY;
|
|
|
|
}
|
2013-12-23 04:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var length = this.interactiveItems.length;
|
2014-03-31 10:04:02 +00:00
|
|
|
for (i = 0; i < length; i++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[i];
|
|
|
|
if(item.touchmove)
|
|
|
|
item.touchmove(touchData);
|
|
|
|
}
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is called when a touch is started on the renderer element
|
|
|
|
*
|
|
|
|
* @method onTouchStart
|
|
|
|
* @param event {Event} The DOM event of a touch starting on the renderer view
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.onTouchStart = function(event)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
var rect = this.interactionDOMElement.getBoundingClientRect();
|
|
|
|
|
2014-02-06 00:19:46 +00:00
|
|
|
if(PIXI.AUTO_PREVENT_DEFAULT)event.preventDefault();
|
2014-03-31 10:04:02 +00:00
|
|
|
|
2013-12-23 04:19:52 +00:00
|
|
|
var changedTouches = event.changedTouches;
|
|
|
|
for (var i=0; i < changedTouches.length; i++)
|
|
|
|
{
|
|
|
|
var touchEvent = changedTouches[i];
|
|
|
|
|
|
|
|
var touchData = this.pool.pop();
|
|
|
|
if(!touchData)touchData = new PIXI.InteractionData();
|
|
|
|
|
|
|
|
touchData.originalEvent = event || window.event;
|
|
|
|
|
|
|
|
this.touchs[touchEvent.identifier] = touchData;
|
|
|
|
touchData.global.x = (touchEvent.clientX - rect.left) * (this.target.width / rect.width);
|
|
|
|
touchData.global.y = (touchEvent.clientY - rect.top) * (this.target.height / rect.height);
|
2014-02-06 00:19:46 +00:00
|
|
|
if(navigator.isCocoonJS) {
|
|
|
|
touchData.global.x = touchEvent.clientX;
|
|
|
|
touchData.global.y = touchEvent.clientY;
|
|
|
|
}
|
2013-12-23 04:19:52 +00:00
|
|
|
|
|
|
|
var length = this.interactiveItems.length;
|
|
|
|
|
2014-03-31 10:04:02 +00:00
|
|
|
for (var j = 0; j < length; j++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[j];
|
|
|
|
|
|
|
|
if(item.touchstart || item.tap)
|
|
|
|
{
|
|
|
|
item.__hit = this.hitTest(item, touchData);
|
|
|
|
|
|
|
|
if(item.__hit)
|
|
|
|
{
|
|
|
|
//call the function!
|
|
|
|
if(item.touchstart)item.touchstart(touchData);
|
|
|
|
item.__isDown = true;
|
|
|
|
item.__touchData = touchData;
|
|
|
|
|
|
|
|
if(!item.interactiveChildren)break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-08-28 06:02:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is called when a touch is ended on the renderer element
|
|
|
|
*
|
|
|
|
* @method onTouchEnd
|
|
|
|
* @param event {Event} The DOM event of a touch ending on the renderer view
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
PIXI.InteractionManager.prototype.onTouchEnd = function(event)
|
|
|
|
{
|
2013-12-23 04:19:52 +00:00
|
|
|
//this.mouse.originalEvent = event || window.event; //IE uses window.event
|
|
|
|
var rect = this.interactionDOMElement.getBoundingClientRect();
|
|
|
|
var changedTouches = event.changedTouches;
|
|
|
|
|
|
|
|
for (var i=0; i < changedTouches.length; i++)
|
|
|
|
{
|
|
|
|
var touchEvent = changedTouches[i];
|
|
|
|
var touchData = this.touchs[touchEvent.identifier];
|
|
|
|
var up = false;
|
|
|
|
touchData.global.x = (touchEvent.clientX - rect.left) * (this.target.width / rect.width);
|
|
|
|
touchData.global.y = (touchEvent.clientY - rect.top) * (this.target.height / rect.height);
|
2014-02-06 00:19:46 +00:00
|
|
|
if(navigator.isCocoonJS) {
|
|
|
|
touchData.global.x = touchEvent.clientX;
|
|
|
|
touchData.global.y = touchEvent.clientY;
|
|
|
|
}
|
2013-12-23 04:19:52 +00:00
|
|
|
|
|
|
|
var length = this.interactiveItems.length;
|
2014-03-31 10:04:02 +00:00
|
|
|
for (var j = 0; j < length; j++)
|
2013-12-23 04:19:52 +00:00
|
|
|
{
|
|
|
|
var item = this.interactiveItems[j];
|
|
|
|
var itemTouchData = item.__touchData; // <-- Here!
|
|
|
|
item.__hit = this.hitTest(item, touchData);
|
|
|
|
|
|
|
|
if(itemTouchData === touchData)
|
|
|
|
{
|
|
|
|
// so this one WAS down...
|
|
|
|
touchData.originalEvent = event || window.event;
|
|
|
|
// hitTest??
|
|
|
|
|
|
|
|
if(item.touchend || item.tap)
|
|
|
|
{
|
|
|
|
if(item.__hit && !up)
|
|
|
|
{
|
|
|
|
if(item.touchend)item.touchend(touchData);
|
|
|
|
if(item.__isDown)
|
|
|
|
{
|
|
|
|
if(item.tap)item.tap(touchData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!item.interactiveChildren)up = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(item.__isDown)
|
|
|
|
{
|
|
|
|
if(item.touchendoutside)item.touchendoutside(touchData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item.__isDown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.__touchData = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
// remove the touch..
|
|
|
|
this.pool.push(touchData);
|
|
|
|
this.touchs[touchEvent.identifier] = null;
|
|
|
|
}
|
2014-02-12 01:25:36 +00:00
|
|
|
};
|