Phaser.InputHandler = function (sprite){ this.sprite = sprite; this.game = sprite.game; this.enabled = false ; this.parent = null ; this.next = null ; this.prev = null ; this.last = this; this.first = this; this.priorityID = 0; this.useHandCursor = false ; this.isDragged = false ; this.allowHorizontalDrag = true ; this.allowVerticalDrag = true ; this.bringToTop = false ; this.snapOffset = null ; this.snapOnDrag = false ; this.snapOnRelease = false ; this.snapX = 0; this.snapY = 0; this.pixelPerfect = false ; this.pixelPerfectAlpha = 255; this.draggable = false ; this.boundsRect = null ; this.boundsSprite = null ; this.consumePointerEvent = false ; this._tempPoint = new Phaser.Point(); this._pointerData = [] ; this._pointerData.push({ id: 0, x: 0, y: 0, isDown: false , isUp: false , isOver: false , isOut: false , timeOver: 0, timeOut: 0, timeDown: 0, timeUp: 0, downDuration: 0, isDragged: false } ); } ; Phaser.InputHandler.prototype = { start: function (priority, useHandCursor){ priority = priority || 0; if (typeof useHandCursor == 'undefined') { useHandCursor = false ; } if (this.enabled == false ) { this.game.input.interactiveItems.add(this); this.useHandCursor = useHandCursor; this.priorityID = priority; for (var i = 0; i < 10; i++ ){ this._pointerData[i] = { id: i, x: 0, y: 0, isDown: false , isUp: false , isOver: false , isOut: false , timeOver: 0, timeOut: 0, timeDown: 0, timeUp: 0, downDuration: 0, isDragged: false } ; } this.snapOffset = new Phaser.Point(); this.enabled = true ; if (this.sprite.events && this.sprite.events.onInputOver == null ) { this.sprite.events.onInputOver = new Phaser.Signal(); this.sprite.events.onInputOut = new Phaser.Signal(); this.sprite.events.onInputDown = new Phaser.Signal(); this.sprite.events.onInputUp = new Phaser.Signal(); this.sprite.events.onDragStart = new Phaser.Signal(); this.sprite.events.onDragStop = new Phaser.Signal(); } } return this.sprite; } , reset: function (){ this.enabled = false ; for (var i = 0; i < 10; i++ ){ this._pointerData[i] = { id: i, x: 0, y: 0, isDown: false , isUp: false , isOver: false , isOut: false , timeOver: 0, timeOut: 0, timeDown: 0, timeUp: 0, downDuration: 0, isDragged: false } ; } } , stop: function (){ if (this.enabled == false ) { return ; } else { this.enabled = false ; this.game.input.interactiveItems.remove(this); } } , destroy: function (){ if (this.enabled) { this.enabled = false ; this.game.input.interactiveItems.remove(this); this.stop(); this.sprite = null ; } } , pointerX: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].x; } , pointerY: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].y; } , pointerDown: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].isDown; } , pointerUp: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].isUp; } , pointerTimeDown: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].timeDown; } , pointerTimeUp: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].timeUp; } , pointerOver: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].isOver; } , pointerOut: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].isOut; } , pointerTimeOver: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].timeOver; } , pointerTimeOut: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].timeOut; } , pointerDragged: function (pointer){ pointer = pointer || 0; return this._pointerData[pointer].isDragged; } , checkPointerOver: function (pointer){ if (this.enabled && this.sprite.visible) { this.sprite.getLocalUnmodifiedPosition(this._tempPoint, pointer.x, pointer.y); if (this._tempPoint.x >= 0 && this._tempPoint.x <= this.sprite.currentFrame.width && this._tempPoint.y >= 0 && this._tempPoint.y <= this.sprite.currentFrame.height) { if (this.pixelPerfect) { return this.checkPixel(this._tempPoint.x, this._tempPoint.y); } else { return true ; } } } return false ; } , checkPixel: function (x, y){ if (this.sprite.texture.baseTexture.source) { this.game.input.hitContext.clearRect(0, 0, 1, 1); x += this.sprite.texture.frame.x; y += this.sprite.texture.frame.y; this.game.input.hitContext.drawImage(this.sprite.texture.baseTexture.source, x, y, 1, 1, 0, 0, 1, 1); var rgb = this.game.input.hitContext.getImageData(0, 0, 1, 1); if (rgb.data[3] >= this.pixelPerfectAlpha) { return true ; } } return false ; } , update: function (pointer){ if (this.enabled == false || this.sprite.visible == false || (this.sprite.group && this.sprite.group.visible == false )) { this._pointerOutHandler(pointer); return false ; } if (this.draggable && this._draggedPointerID == pointer.id) { return this.updateDrag(pointer); } else if (this._pointerData[pointer.id].isOver == true ) { if (this.checkPointerOver(pointer)) { this._pointerData[pointer.id].x = pointer.x - this.sprite.x; this._pointerData[pointer.id].y = pointer.y - this.sprite.y; return true ; } else { this._pointerOutHandler(pointer); return false ; } } } , _pointerOverHandler: function (pointer){ if (this._pointerData[pointer.id].isOver == false ) { this._pointerData[pointer.id].isOver = true ; this._pointerData[pointer.id].isOut = false ; this._pointerData[pointer.id].timeOver = this.game.time.now; this._pointerData[pointer.id].x = pointer.x - this.sprite.x; this._pointerData[pointer.id].y = pointer.y - this.sprite.y; if (this.useHandCursor && this._pointerData[pointer.id].isDragged == false ) { _AN_Write_cursor('cursor', this.game.stage.canvas.style, false , "pointer"); } this.sprite.events.onInputOver.dispatch(this.sprite, pointer); } } , _pointerOutHandler: function (pointer){ this._pointerData[pointer.id].isOver = false ; this._pointerData[pointer.id].isOut = true ; this._pointerData[pointer.id].timeOut = this.game.time.now; if (this.useHandCursor && this._pointerData[pointer.id].isDragged == false ) { _AN_Write_cursor("cursor", this.game.stage.canvas.style, false , "default"); } if (this.sprite && this.sprite.events) { this.sprite.events.onInputOut.dispatch(this.sprite, pointer); } } , _touchedHandler: function (pointer){ if (this._pointerData[pointer.id].isDown == false && this._pointerData[pointer.id].isOver == true ) { this._pointerData[pointer.id].isDown = true ; this._pointerData[pointer.id].isUp = false ; this._pointerData[pointer.id].timeDown = this.game.time.now; this.sprite.events.onInputDown.dispatch(this.sprite, pointer); if (this.draggable && this.isDragged == false ) { this.startDrag(pointer); } if (this.bringToTop) { this.sprite.bringToTop(); } } return this.consumePointerEvent; } , _releasedHandler: function (pointer){ if (this._pointerData[pointer.id].isDown && pointer.isUp) { this._pointerData[pointer.id].isDown = false ; this._pointerData[pointer.id].isUp = true ; this._pointerData[pointer.id].timeUp = this.game.time.now; this._pointerData[pointer.id].downDuration = this._pointerData[pointer.id].timeUp - this._pointerData[pointer.id].timeDown; if (this.checkPointerOver(pointer)) { this.sprite.events.onInputUp.dispatch(this.sprite, pointer); } else { if (this.useHandCursor) { _AN_Write_cursor("cursor", this.game.stage.canvas.style, false , "default"); } } if (this.draggable && this.isDragged && this._draggedPointerID == pointer.id) { this.stopDrag(pointer); } } } , updateDrag: function (pointer){ if (pointer.isUp) { this.stopDrag(pointer); return false ; } if (this.allowHorizontalDrag) { this.sprite.x = pointer.x + this._dragPoint.x + this.dragOffset.x; } if (this.allowVerticalDrag) { this.sprite.y = pointer.y + this._dragPoint.y + this.dragOffset.y; } if (this.boundsRect) { this.checkBoundsRect(); } if (this.boundsSprite) { this.checkBoundsSprite(); } if (this.snapOnDrag) { this.sprite.x = Math.round(this.sprite.x / this.snapX) * this.snapX; this.sprite.y = Math.round(this.sprite.y / this.snapY) * this.snapY; } return true ; } , justOver: function (pointer, delay){ pointer = pointer || 0; delay = delay || 500; return (this._pointerData[pointer].isOver && this.overDuration(pointer) < delay); } , justOut: function (pointer, delay){ pointer = pointer || 0; delay = delay || 500; return (this._pointerData[pointer].isOut && (this.game.time.now - this._pointerData[pointer].timeOut < delay)); } , justPressed: function (pointer, delay){ pointer = pointer || 0; delay = delay || 500; return (this._pointerData[pointer].isDown && this.downDuration(pointer) < delay); } , justReleased: function (pointer, delay){ pointer = pointer || 0; delay = delay || 500; return (this._pointerData[pointer].isUp && (this.game.time.now - this._pointerData[pointer].timeUp < delay)); } , overDuration: function (pointer){ pointer = pointer || 0; if (this._pointerData[pointer].isOver) { return this.game.time.now - this._pointerData[pointer].timeOver; } return -1; } , downDuration: function (pointer){ pointer = pointer || 0; if (this._pointerData[pointer].isDown) { return this.game.time.now - this._pointerData[pointer].timeDown; } return -1; } , enableDrag: function (lockCenter, bringToTop, pixelPerfect, alphaThreshold, boundsRect, boundsSprite){ if (typeof lockCenter == 'undefined') { lockCenter = false ; } if (typeof bringToTop == 'undefined') { bringToTop = false ; } if (typeof pixelPerfect == 'undefined') { pixelPerfect = false ; } alphaThreshold = alphaThreshold || 255; boundsRect = boundsRect || null ; boundsSprite = boundsSprite || null ; this._dragPoint = new Phaser.Point(); this.draggable = true ; this.bringToTop = bringToTop; this.dragOffset = new Phaser.Point(); this.dragFromCenter = lockCenter; this.pixelPerfect = pixelPerfect; this.pixelPerfectAlpha = alphaThreshold; if (boundsRect) { this.boundsRect = boundsRect; } if (boundsSprite) { this.boundsSprite = boundsSprite; } } , disableDrag: function (){ if (this._pointerData) { for (var i = 0; i < 10; i++ ){ this._pointerData[i].isDragged = false ; } } this.draggable = false ; this.isDragged = false ; this._draggedPointerID = -1; } , startDrag: function (pointer){ this.isDragged = true ; this._draggedPointerID = pointer.id; this._pointerData[pointer.id].isDragged = true ; if (this.dragFromCenter) { this.sprite.centerOn(pointer.x, pointer.y); this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y); } else { this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y); } this.updateDrag(pointer); if (this.bringToTop) { this.sprite.bringToTop(); } this.sprite.events.onDragStart.dispatch(this.sprite, pointer); } , stopDrag: function (pointer){ this.isDragged = false ; this._draggedPointerID = -1; this._pointerData[pointer.id].isDragged = false ; if (this.snapOnRelease) { this.sprite.x = Math.round(this.sprite.x / this.snapX) * this.snapX; this.sprite.y = Math.round(this.sprite.y / this.snapY) * this.snapY; } this.sprite.events.onDragStop.dispatch(this.sprite, pointer); this.sprite.events.onInputUp.dispatch(this.sprite, pointer); } , setDragLock: function (allowHorizontal, allowVertical){ if (typeof allowHorizontal == 'undefined') { allowHorizontal = true ; } if (typeof allowVertical == 'undefined') { allowVertical = true ; } this.allowHorizontalDrag = allowHorizontal; this.allowVerticalDrag = allowVertical; } , enableSnap: function (snapX, snapY, onDrag, onRelease){ if (typeof onDrag == 'undefined') { onDrag = true ; } if (typeof onRelease == 'undefined') { onRelease = false ; } this.snapX = snapX; this.snapY = snapY; this.snapOnDrag = onDrag; this.snapOnRelease = onRelease; } , disableSnap: function (){ this.snapOnDrag = false ; this.snapOnRelease = false ; } , checkBoundsRect: function (){ if (this.sprite.x < this.boundsRect.left) { this.sprite.x = this.boundsRect.x; } else if ((this.sprite.x + this.sprite.width) > this.boundsRect.right) { this.sprite.x = this.boundsRect.right - this.sprite.width; } if (this.sprite.y < this.boundsRect.top) { this.sprite.y = this.boundsRect.top; } else if ((this.sprite.y + this.sprite.height) > this.boundsRect.bottom) { this.sprite.y = this.boundsRect.bottom - this.sprite.height; } } , checkBoundsSprite: function (){ if (this.sprite.x < this.boundsSprite.x) { this.sprite.x = this.boundsSprite.x; } else if ((this.sprite.x + this.sprite.width) > (this.boundsSprite.x + this.boundsSprite.width)) { this.sprite.x = (this.boundsSprite.x + this.boundsSprite.width) - this.sprite.width; } if (this.sprite.y < this.boundsSprite.y) { this.sprite.y = this.boundsSprite.y; } else if ((this.sprite.y + this.sprite.height) > (this.boundsSprite.y + this.boundsSprite.height)) { this.sprite.y = (this.boundsSprite.y + this.boundsSprite.height) - this.sprite.height; } } } ;