/*global Quintus:false */ Quintus.Touch = function(Q) { if(Q._isUndefined(Quintus.Sprites)) { throw "Quintus.Touch requires Quintus.Sprites Module"; } var hasTouch = !!('ontouchstart' in window); var touchStage = [0]; var touchType = 0; Q.Evented.extend("TouchSystem",{ init: function() { var touchSystem = this; this.boundTouch = function(e) { touchSystem.touch(e); }; this.boundDrag = function(e) { touchSystem.drag(e); }; this.boundEnd = function(e) { touchSystem.touchEnd(e); }; Q.el.addEventListener('touchstart',this.boundTouch); Q.el.addEventListener('mousedown',this.boundTouch); Q.el.addEventListener('touchmove',this.boundDrag); Q.el.addEventListener('mousemove',this.boundDrag); Q.el.addEventListener('touchend',this.boundEnd); Q.el.addEventListener('mouseup',this.boundEnd); Q.el.addEventListener('touchcancel',this.boundEnd); this.touchPos = new Q.Evented(); this.touchPos.grid = {}; this.touchPos.p = { w:1, h:1, cx: 0, cy: 0 }; this.activeTouches = {}; this.touchedObjects = {}; }, destroy: function() { Q.el.removeEventListener('touchstart',this.boundTouch); Q.el.removeEventListener('mousedown',this.boundTouch); Q.el.removeEventListener('touchmove',this.boundDrag); Q.el.removeEventListener('mousemove',this.boundDrag); Q.el.removeEventListener('touchend',this.boundEnd); Q.el.removeEventListener('mouseup',this.boundEnd); Q.el.removeEventListener('touchcancel',this.boundEnd); }, normalizeTouch: function(touch,stage) { var canvasPosX = touch.offsetX, canvasPosY = touch.offsetY; if(Q._isUndefined(canvasPosX) || Q._isUndefined(canvasPosY)) { canvasPosX = touch.layerX; canvasPosY = touch.layerY; } if(Q._isUndefined(canvasPosX) || Q._isUndefined(canvasPosY)) { if(Q.touch.offsetX === void 0) { Q.touch.offsetX = 0; Q.touch.offsetY = 0; var el = Q.el; do { Q.touch.offsetX += el.offsetLeft; Q.touch.offsetY += el.offsetTop; } while(el = el.offsetParent); } canvasPosX = touch.pageX - Q.touch.offsetX; canvasPosY = touch.pageY - Q.touch.offsetY; } this.touchPos.p.ox = this.touchPos.p.px = canvasPosX / Q.cssWidth * Q.width; this.touchPos.p.oy = this.touchPos.p.py = canvasPosY / Q.cssHeight * Q.height; if(stage.viewport) { this.touchPos.p.px /= stage.viewport.scale; this.touchPos.p.py /= stage.viewport.scale; this.touchPos.p.px += stage.viewport.x; this.touchPos.p.py += stage.viewport.y; } this.touchPos.p.x = this.touchPos.p.px; this.touchPos.p.y = this.touchPos.p.py; this.touchPos.obj = null; return this.touchPos; }, touch: function(e) { var touches = e.changedTouches || [ e ]; for(var i=0;i