Phaser.InputHandler = function (sprite){ this.sprite = sprite; this.game = sprite.game; this.enabled = false ; this.checked = false ; this.priorityID = 0; this.useHandCursor = false ; this._setHandCursor = 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.snapOffsetX = 0; this.snapOffsetY = 0; this.pixelPerfectOver = false ; this.pixelPerfectClick = false ; this.pixelPerfectAlpha = 255; this.draggable = false ; this.boundsRect = null ; this.boundsSprite = null ; this.consumePointerEvent = false ; this.scaleLayer = false ; this._dragPhase = false ; this._wasEnabled = 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 ; this._wasEnabled = 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(); } } this.sprite.events.onAddedToGroup.add(this.addedToGroup, this); this.sprite.events.onRemovedFromGroup.add(this.removedFromGroup, this); this.flagged = false ; return this.sprite; } , addedToGroup: function (){ if (this._dragPhase) { return ; } if (this._wasEnabled && !this.enabled) { this.start(); } } , removedFromGroup: function (){ if (this._dragPhase) { return ; } if (this.enabled) { this._wasEnabled = true ; this.stop(); } else { this._wasEnabled = false ; } } , reset: function (){ this.enabled = false ; this.flagged = 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.sprite) { if (this._setHandCursor) { _AN_Write_cursor('cursor', this.game.canvas.style, false , "default"); this._setHandCursor = false ; } this.enabled = false ; this.game.input.interactiveItems.remove(this); this._pointerData.length = 0; this.boundsRect = null ; this.boundsSprite = null ; this.sprite = null ; } } , validForInput: function (highestID, highestRenderID, includePixelPerfect){ if (typeof includePixelPerfect === 'undefined') { includePixelPerfect = true ; } if (this.sprite.scale.x === 0 || this.sprite.scale.y === 0 || this.priorityID < this.game.input.minPriorityID) { return false ; } if (!includePixelPerfect && (this.pixelPerfectClick || this.pixelPerfectOver)) { return false ; } if (this.priorityID > highestID || (this.priorityID === highestID && this.sprite._cache[3] < highestRenderID)) { return true ; } return false ; } , isPixelPerfect: function (){ return (this.pixelPerfectClick || this.pixelPerfectOver); } , 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 (index){ if (this.enabled) { if (typeof index === 'undefined') { for (var i = 0; i < 10; i++ ){ if (this._pointerData[i].isOver) { return true ; } } } else { return this._pointerData[index].isOver; } } return false ; } , pointerOut: function (index){ if (this.enabled) { if (typeof index === 'undefined') { for (var i = 0; i < 10; i++ ){ if (this._pointerData[i].isOut) { return true ; } } } else { return this._pointerData[index].isOut; } } return false ; } , 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; } , checkPointerDown: function (pointer, fastTest){ if (!pointer.isDown || !this.enabled || !this.sprite || !this.sprite.parent || !this.sprite.visible || !this.sprite.parent.visible) { return false ; } if (this.game.input.hitTest(this.sprite, pointer, this._tempPoint)) { if (typeof fastTest === 'undefined') { fastTest = false ; } if (!fastTest && this.pixelPerfectClick) { return this.checkPixel(this._tempPoint.x, this._tempPoint.y); } else { return true ; } } return false ; } , checkPointerOver: function (pointer, fastTest){ if (!this.enabled || !this.sprite || !this.sprite.parent || !this.sprite.visible || !this.sprite.parent.visible) { return false ; } if (this.game.input.hitTest(this.sprite, pointer, this._tempPoint)) { if (typeof fastTest === 'undefined') { fastTest = false ; } if (!fastTest && this.pixelPerfectOver) { return this.checkPixel(this._tempPoint.x, this._tempPoint.y); } else { return true ; } } return false ; } , checkPixel: function (x, y, pointer){ if (this.sprite.texture.baseTexture.source) { if (x === null && y === null ) { this.game.input.getLocalPosition(this.sprite, pointer, this._tempPoint); var x = this._tempPoint.x; var y = this._tempPoint.y; } if (this.sprite.anchor.x !== 0) { x -= - this.sprite.texture.frame.width * this.sprite.anchor.x; } if (this.sprite.anchor.y !== 0) { y -= - this.sprite.texture.frame.height * this.sprite.anchor.y; } x += this.sprite.texture.frame.x; y += this.sprite.texture.frame.y; if (this.sprite.texture.trim) { x -= this.sprite.texture.trim.x; y -= this.sprite.texture.trim.y; if (x < this.sprite.texture.crop.x || x > this.sprite.texture.crop.right || y < this.sprite.texture.crop.y || y > this.sprite.texture.crop.bottom) { this._dx = x; this._dy = y; return false ; } } this._dx = x; this._dy = y; this.game.input.hitContext.clearRect(0, 0, 1, 1); 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.sprite === null || this.sprite.parent === undefined) { return ; } if (!this.enabled || !this.sprite.visible || !this.sprite.parent.visible) { this._pointerOutHandler(pointer); return false ; } if (this.draggable && this._draggedPointerID === pointer.id) { return this.updateDrag(pointer); } else if (this._pointerData[pointer.id].isOver) { 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.sprite === null ) { return ; } if (this._pointerData[pointer.id].isOver === false || pointer.dirty) { 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.canvas.style, false , "pointer"); this._setHandCursor = true ; } if (this.sprite && this.sprite.events) { this.sprite.events.onInputOver.dispatch(this.sprite, pointer); } } } , _pointerOutHandler: function (pointer){ if (this.sprite === null ) { return ; } 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.canvas.style, false , "default"); this._setHandCursor = false ; } if (this.sprite && this.sprite.events) { this.sprite.events.onInputOut.dispatch(this.sprite, pointer); } } , _touchedHandler: function (pointer){ if (this.sprite === null ) { return ; } if (this._pointerData[pointer.id].isDown === false && this._pointerData[pointer.id].isOver === true ) { if (this.pixelPerfectClick && !this.checkPixel(null , null , pointer)) { return ; } this._pointerData[pointer.id].isDown = true ; this._pointerData[pointer.id].isUp = false ; this._pointerData[pointer.id].timeDown = this.game.time.now; if (this.sprite && this.sprite.events) { this.sprite.events.onInputDown.dispatch(this.sprite, pointer); } pointer.dirty = true ; if (this.draggable && this.isDragged === false ) { this.startDrag(pointer); } if (this.bringToTop) { this.sprite.bringToTop(); } } return this.consumePointerEvent; } , _releasedHandler: function (pointer){ if (this.sprite === null ) { return ; } 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)) { if (this.sprite && this.sprite.events) { this.sprite.events.onInputUp.dispatch(this.sprite, pointer, true ); } } else { if (this.sprite && this.sprite.events) { this.sprite.events.onInputUp.dispatch(this.sprite, pointer, false ); } if (this.useHandCursor) { _AN_Write_cursor("cursor", this.game.canvas.style, false , "default"); this._setHandCursor = false ; } } pointer.dirty = true ; if (this.draggable && this.isDragged && this._draggedPointerID === pointer.id) { this.stopDrag(pointer); } } } , updateDrag: function (pointer){ if (pointer.isUp) { this.stopDrag(pointer); return false ; } var px = this.globalToLocalX(pointer.x) + this._dragPoint.x + this.dragOffset.x; var py = this.globalToLocalY(pointer.y) + this._dragPoint.y + this.dragOffset.y; if (this.sprite.fixedToCamera) { if (this.allowHorizontalDrag) { this.sprite.cameraOffset.x = px; } if (this.allowVerticalDrag) { this.sprite.cameraOffset.y = py; } if (this.boundsRect) { this.checkBoundsRect(); } if (this.boundsSprite) { this.checkBoundsSprite(); } if (this.snapOnDrag) { this.sprite.cameraOffset.x = Math.round((this.sprite.cameraOffset.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX); this.sprite.cameraOffset.y = Math.round((this.sprite.cameraOffset.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % this.snapY); } } else { if (this.allowHorizontalDrag) { this.sprite.x = px; } if (this.allowVerticalDrag) { this.sprite.y = py; } if (this.boundsRect) { this.checkBoundsRect(); } if (this.boundsSprite) { this.checkBoundsSprite(); } if (this.snapOnDrag) { this.sprite.x = Math.round((this.sprite.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX); this.sprite.y = Math.round((this.sprite.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % 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 ; } if (typeof alphaThreshold === 'undefined') { alphaThreshold = 255; } if (typeof boundsRect === 'undefined') { boundsRect = null ; } if (typeof boundsSprite === 'undefined') { boundsSprite = null ; } this._dragPoint = new Phaser.Point(); this.draggable = true ; this.bringToTop = bringToTop; this.dragOffset = new Phaser.Point(); this.dragFromCenter = lockCenter; this.pixelPerfectClick = 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.sprite.fixedToCamera) { if (this.dragFromCenter) { this.sprite.centerOn(pointer.x, pointer.y); this._dragPoint.setTo(this.sprite.cameraOffset.x - pointer.x, this.sprite.cameraOffset.y - pointer.y); } else { this._dragPoint.setTo(this.sprite.cameraOffset.x - pointer.x, this.sprite.cameraOffset.y - pointer.y); } } else { if (this.dragFromCenter) { var bounds = this.sprite.getBounds(); this.sprite.x = this.globalToLocalX(pointer.x) + (this.sprite.x - bounds.centerX); this.sprite.y = this.globalToLocalY(pointer.y) + (this.sprite.y - bounds.centerY); } this._dragPoint.setTo(this.sprite.x - this.globalToLocalX(pointer.x), this.sprite.y - this.globalToLocalY(pointer.y)); } this.updateDrag(pointer); if (this.bringToTop) { this._dragPhase = true ; this.sprite.bringToTop(); } this.sprite.events.onDragStart.dispatch(this.sprite, pointer); } , globalToLocalX: function (x){ if (this.scaleLayer) { x -= this.game.scale.grid.boundsFluid.x; x *= this.game.scale.grid.scaleFluidInversed.x; } return x; } , globalToLocalY: function (y){ if (this.scaleLayer) { y -= this.game.scale.grid.boundsFluid.y; y *= this.game.scale.grid.scaleFluidInversed.y; } return y; } , stopDrag: function (pointer){ this.isDragged = false ; this._draggedPointerID = -1; this._pointerData[pointer.id].isDragged = false ; this._dragPhase = false ; if (this.snapOnRelease) { if (this.sprite.fixedToCamera) { this.sprite.cameraOffset.x = Math.round((this.sprite.cameraOffset.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX); this.sprite.cameraOffset.y = Math.round((this.sprite.cameraOffset.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % this.snapY); } else { this.sprite.x = Math.round((this.sprite.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX); this.sprite.y = Math.round((this.sprite.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % this.snapY); } } this.sprite.events.onDragStop.dispatch(this.sprite, pointer); if (this.checkPointerOver(pointer) === false ) { this._pointerOutHandler(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, snapOffsetX, snapOffsetY){ if (typeof onDrag === 'undefined') { onDrag = true ; } if (typeof onRelease === 'undefined') { onRelease = false ; } if (typeof snapOffsetX === 'undefined') { snapOffsetX = 0; } if (typeof snapOffsetY === 'undefined') { snapOffsetY = 0; } this.snapX = snapX; this.snapY = snapY; this.snapOffsetX = snapOffsetX; this.snapOffsetY = snapOffsetY; this.snapOnDrag = onDrag; this.snapOnRelease = onRelease; } , disableSnap: function (){ this.snapOnDrag = false ; this.snapOnRelease = false ; } , checkBoundsRect: function (){ if (this.sprite.fixedToCamera) { if (this.sprite.cameraOffset.x < this.boundsRect.left) { this.sprite.cameraOffset.x = this.boundsRect.left; } else if ((this.sprite.cameraOffset.x + this.sprite.width) > this.boundsRect.right) { this.sprite.cameraOffset.x = this.boundsRect.right - this.sprite.width; } if (this.sprite.cameraOffset.y < this.boundsRect.top) { this.sprite.cameraOffset.y = this.boundsRect.top; } else if ((this.sprite.cameraOffset.y + this.sprite.height) > this.boundsRect.bottom) { this.sprite.cameraOffset.y = this.boundsRect.bottom - this.sprite.height; } } else { 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.fixedToCamera && this.boundsSprite.fixedToCamera) { if (this.sprite.cameraOffset.x < this.boundsSprite.camerOffset.x) { this.sprite.cameraOffset.x = this.boundsSprite.camerOffset.x; } else if ((this.sprite.cameraOffset.x + this.sprite.width) > (this.boundsSprite.camerOffset.x + this.boundsSprite.width)) { this.sprite.cameraOffset.x = (this.boundsSprite.camerOffset.x + this.boundsSprite.width) - this.sprite.width; } if (this.sprite.cameraOffset.y < this.boundsSprite.camerOffset.y) { this.sprite.cameraOffset.y = this.boundsSprite.camerOffset.y; } else if ((this.sprite.cameraOffset.y + this.sprite.height) > (this.boundsSprite.camerOffset.y + this.boundsSprite.height)) { this.sprite.cameraOffset.y = (this.boundsSprite.camerOffset.y + this.boundsSprite.height) - this.sprite.height; } } else { 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; } } } } ; Phaser.InputHandler.prototype.constructor = Phaser.InputHandler;