diff --git a/Phaser/components/sprite/Input.ts b/Phaser/components/sprite/Input.ts index af05c0b43..bfe6c39d8 100644 --- a/Phaser/components/sprite/Input.ts +++ b/Phaser/components/sprite/Input.ts @@ -48,52 +48,10 @@ module Phaser.Components { */ public priorityID:number = 0; - public start(priority:number = 0, checkBody?:bool = false, useHandCursor?:bool = false) { - - // Turning on - if (this.enabled) - { - return; - } - else - { - // Register, etc - this.checkBody = checkBody; - this.useHandCursor = useHandCursor; - - this._pointerData = []; - - for (var i = 0; i < 10; i++) - { - this._pointerData.push({ 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 Point; - this.enabled = true; - - this.game.input.addGameObject(this._sprite); - } - - } - - public stop() { - - // Turning off - if (this.enabled == false) - { - return; - } - else - { - // De-register, etc - this.enabled = false; - this.game.input.removeGameObject(this._sprite); - } - - } - private _dragPoint: Point; + private _draggedPointerID: number; public dragOffset: Point; + public isDragged: bool = false; public dragFromCenter: bool; public dragPixelPerfect:bool = false; public dragPixelPerfectAlpha:number; @@ -238,26 +196,62 @@ module Phaser.Components { return this._pointerData[pointer].isDragged; } + public start(priority:number = 0, checkBody?:bool = false, useHandCursor?:bool = false): Sprite { + + // Turning on + if (this.enabled == false) + { + // Register, etc + this.checkBody = checkBody; + this.useHandCursor = useHandCursor; + this.priorityID = priority; + + this._pointerData = []; + + for (var i = 0; i < 10; i++) + { + this._pointerData.push({ 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 Point; + this.enabled = true; + + this.game.input.addGameObject(this._sprite); + } + + return this._sprite; + + } + + public stop() { + + // Turning off + if (this.enabled == false) + { + return; + } + else + { + // De-register, etc + this.enabled = false; + this.game.input.removeGameObject(this._sprite); + } + + } + /** * Update */ - public update(pointer: Phaser.Pointer) { + public update(pointer: Phaser.Pointer): bool { if (this.enabled == false) { - return; + return false; } - // If was previously touched by this Pointer, check if still is - if (this._pointerData[pointer.id].isDown && pointer.isUp) - { - this._releasedHandler(pointer); - } - - if (this.draggable && this._pointerData[pointer.id].isDragged) + if (this.draggable && this._draggedPointerID == pointer.id) { this.updateDrag(pointer); - //return; } if (RectangleUtils.contains(this._sprite.frameBounds, pointer.x, pointer.y)) @@ -280,6 +274,8 @@ module Phaser.Components { this._sprite.events.onInputOver.dispatch(this._sprite, pointer); } + + return true; } else { @@ -296,6 +292,8 @@ module Phaser.Components { this._sprite.events.onInputOut.dispatch(this._sprite, pointer); } + + return false; } } @@ -307,25 +305,42 @@ module Phaser.Components { 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); - // Star drag - if (this.draggable) + // Start drag + if (this.draggable && this.isDragged == false && pointer.draggedObject == null) { this.startDrag(pointer); } + } } public _releasedHandler(pointer: Pointer) { - 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 was previously touched by this Pointer, check if still is + 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; - this._sprite.events.onInputDown.dispatch(this._sprite, pointer); + this._sprite.events.onInputUp.dispatch(this._sprite, pointer); + + // Stop drag + if (this.draggable && this.isDragged && this._draggedPointerID == pointer.id) + { + this.stopDrag(pointer); + } + + if (this.useHandCursor) + { + this.game.stage.canvas.style.cursor = "default"; + } + } } @@ -478,9 +493,8 @@ module Phaser.Components { } this.draggable = false; - - //mouseStartDragCallback = null; - //mouseStopDragCallback = null; + this.isDragged = false; + this._draggedPointerID = -1; } /** @@ -488,6 +502,8 @@ module Phaser.Components { */ public startDrag(pointer: Pointer):void { + this.isDragged = true; + this._draggedPointerID = pointer.id; this._pointerData[pointer.id].isDragged = true; if (this.dragFromCenter) @@ -500,6 +516,8 @@ module Phaser.Components { this._dragPoint.setTo(this._sprite.x - pointer.x, this._sprite.y - pointer.y); } + pointer.draggedObject = this._sprite; + } /** @@ -507,6 +525,8 @@ module Phaser.Components { */ public stopDrag(pointer: Pointer):void { + this.isDragged = false; + this._draggedPointerID = -1; this._pointerData[pointer.id].isDragged = false; if (this.snapOnRelease) @@ -514,6 +534,8 @@ module Phaser.Components { this._sprite.x = Math.floor(this._sprite.x / this.snapX) * this.snapX; this._sprite.y = Math.floor(this._sprite.y / this.snapY) * this.snapY; } + + pointer.draggedObject = null; } /** diff --git a/Phaser/input/Pointer.ts b/Phaser/input/Pointer.ts index 62d94a761..e69998c9b 100644 --- a/Phaser/input/Pointer.ts +++ b/Phaser/input/Pointer.ts @@ -252,8 +252,6 @@ module Phaser { } - // Sprite Drag Related - /** * The Game Object this Pointer is currently dragging. * @property draggedObject @@ -369,15 +367,15 @@ module Phaser { { if (this.game.input.inputObjects[i].input.enabled) { - this.game.input.inputObjects[i].input.update(this); - - if (this.game.input.inputObjects[i].input.priorityID > _highestPriority) + if (this.game.input.inputObjects[i].input.update(this) && this.game.input.inputObjects[i].input.priorityID > _highestPriority) { _highestPriority = this.game.input.inputObjects[i].input.priorityID; } } } + //console.log('highest priority was', _highestPriority); + if (this.isDown) { // Now update all objects with the highest priority ID (can be more than 1) @@ -493,6 +491,16 @@ module Phaser { this.game.input.currentPointers--; } + for (var i = 0; i < this.game.input.totalTrackedObjects; i++) + { + if (this.game.input.inputObjects[i].input.enabled) + { + this.game.input.inputObjects[i].input._releasedHandler(this); + } + } + + this.draggedObject = null; + return this; } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 2f5b117c7..67aec3152 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -84,6 +84,10 @@ drag sprite 1.ts + + + drag sprite 2.ts + over sprite 1.ts @@ -91,6 +95,10 @@ over sprite 2.ts + + + touch priority.ts + graphic emitter.ts diff --git a/Tests/assets/pics/ra_einstein.png b/Tests/assets/pics/ra_einstein.png new file mode 100644 index 000000000..becfefb9f Binary files /dev/null and b/Tests/assets/pics/ra_einstein.png differ diff --git a/Tests/assets/pics/shadow_of_the_beast2_karamoon.png b/Tests/assets/pics/shadow_of_the_beast2_karamoon.png new file mode 100644 index 000000000..56bcc8692 Binary files /dev/null and b/Tests/assets/pics/shadow_of_the_beast2_karamoon.png differ diff --git a/Tests/assets/pics/shadow_of_the_beast2_other_world.png b/Tests/assets/pics/shadow_of_the_beast2_other_world.png new file mode 100644 index 000000000..1f6698262 Binary files /dev/null and b/Tests/assets/pics/shadow_of_the_beast2_other_world.png differ diff --git a/Tests/assets/sprites/atari1200xl.png b/Tests/assets/sprites/atari1200xl.png new file mode 100644 index 000000000..40c272708 Binary files /dev/null and b/Tests/assets/sprites/atari1200xl.png differ diff --git a/Tests/assets/sprites/atari400.png b/Tests/assets/sprites/atari400.png new file mode 100644 index 000000000..08e103705 Binary files /dev/null and b/Tests/assets/sprites/atari400.png differ diff --git a/Tests/assets/sprites/atari800.png b/Tests/assets/sprites/atari800.png new file mode 100644 index 000000000..e60d0940b Binary files /dev/null and b/Tests/assets/sprites/atari800.png differ diff --git a/Tests/assets/sprites/darkwing_crazy.png b/Tests/assets/sprites/darkwing_crazy.png new file mode 100644 index 000000000..d9ed92042 Binary files /dev/null and b/Tests/assets/sprites/darkwing_crazy.png differ diff --git a/Tests/assets/sprites/diamond.png b/Tests/assets/sprites/diamond.png new file mode 100644 index 000000000..5ad023b4e Binary files /dev/null and b/Tests/assets/sprites/diamond.png differ diff --git a/Tests/assets/sprites/firstaid.png b/Tests/assets/sprites/firstaid.png new file mode 100644 index 000000000..672059ee6 Binary files /dev/null and b/Tests/assets/sprites/firstaid.png differ diff --git a/Tests/assets/sprites/ra_dont_crack_under_pressure.png b/Tests/assets/sprites/ra_dont_crack_under_pressure.png new file mode 100644 index 000000000..3eacc510b Binary files /dev/null and b/Tests/assets/sprites/ra_dont_crack_under_pressure.png differ diff --git a/Tests/input/drag sprite 1.js b/Tests/input/drag sprite 1.js index d326bd96c..fceb4e571 100644 --- a/Tests/input/drag sprite 1.js +++ b/Tests/input/drag sprite 1.js @@ -9,12 +9,13 @@ var sprite; function create() { sprite = game.add.sprite(200, 200, 'sprite'); - // Enable Input detection + // Enable Input detection. Sprites have this disabled by default, + // so you have to start it if you want to interact with them. sprite.input.start(0, false, true); + // This allows you to drag the sprite. The parameter controls if you drag from the position you touched it (false) + // or if it will snap to the center (true) sprite.input.enableDrag(true); - sprite.input.allowVerticalDrag = false; - //sprite.input.dragOffset.setTo(0, 50); - } + } function render() { game.input.renderDebugInfo(32, 32); sprite.input.renderDebugInfo(300, 32); diff --git a/Tests/input/drag sprite 1.ts b/Tests/input/drag sprite 1.ts index be1808083..f9e8ce320 100644 --- a/Tests/input/drag sprite 1.ts +++ b/Tests/input/drag sprite 1.ts @@ -18,14 +18,14 @@ sprite = game.add.sprite(200, 200, 'sprite'); - // Enable Input detection + // Enable Input detection. Sprites have this disabled by default, + // so you have to start it if you want to interact with them. sprite.input.start(0, false, true); + // This allows you to drag the sprite. The parameter controls if you drag from the position you touched it (false) + // or if it will snap to the center (true) sprite.input.enableDrag(true); - //sprite.input.allowVerticalDrag = false; - //sprite.input.dragOffset.setTo(0, 50); - } function render() { diff --git a/Tests/input/drag sprite 2.js b/Tests/input/drag sprite 2.js new file mode 100644 index 000000000..75d6b5039 --- /dev/null +++ b/Tests/input/drag sprite 2.js @@ -0,0 +1,22 @@ +/// +(function () { + var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render); + function init() { + // Using Phasers asset loader we load up a PNG from the assets folder + game.loader.addImageFile('sprite', 'assets/sprites/atari800.png'); + game.loader.load(); + } + var sprite; + function create() { + sprite = game.add.sprite(200, 200, 'sprite'); + sprite.input.start(0, false, true); + sprite.input.enableDrag(true); + // The drag offset allows us to position the sprite relative to the pointer (+ lock) position + // In this case it will be positioned -100px above the pointer + sprite.input.dragOffset.y = -100; + } + function render() { + game.input.renderDebugInfo(32, 32); + sprite.input.renderDebugInfo(300, 32); + } +})(); diff --git a/Tests/input/drag sprite 2.ts b/Tests/input/drag sprite 2.ts new file mode 100644 index 000000000..8729219ef --- /dev/null +++ b/Tests/input/drag sprite 2.ts @@ -0,0 +1,38 @@ +/// + +(function () { + + var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render); + + function init() { + + // Using Phasers asset loader we load up a PNG from the assets folder + game.loader.addImageFile('sprite', 'assets/sprites/atari800.png'); + game.loader.load(); + + } + + var sprite: Phaser.Sprite; + + function create() { + + sprite = game.add.sprite(200, 200, 'sprite'); + + sprite.input.start(0, false, true); + + sprite.input.enableDrag(true); + + // The drag offset allows us to position the sprite relative to the pointer (+ lock) position + // In this case it will be positioned -100px above the pointer + sprite.input.dragOffset.y = -100; + + } + + function render() { + + game.input.renderDebugInfo(32, 32); + sprite.input.renderDebugInfo(300, 32); + + } + +})(); diff --git a/Tests/input/touch priority.js b/Tests/input/touch priority.js new file mode 100644 index 000000000..dc45981cc --- /dev/null +++ b/Tests/input/touch priority.js @@ -0,0 +1,28 @@ +/// +(function () { + var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render); + function init() { + // Using Phasers asset loader we load up a PNG from the assets folder + game.loader.addImageFile('atari1', 'assets/sprites/atari130xe.png'); + game.loader.addImageFile('atari2', 'assets/sprites/atari800xl.png'); + game.loader.addImageFile('sonic', 'assets/sprites/sonic_havok_sanity.png'); + game.loader.load(); + } + var atari1; + var atari2; + var sonic; + function create() { + atari1 = game.add.sprite(100, 100, 'atari1'); + atari2 = game.add.sprite(300, 200, 'atari2'); + sonic = game.add.sprite(400, 300, 'sonic'); + atari1.input.start(0, false, true); + atari2.input.start(1, false, true); + sonic.input.start(2, false, true); + atari1.input.enableDrag(); + atari2.input.enableDrag(); + sonic.input.enableDrag(); + } + function render() { + game.input.renderDebugInfo(32, 32); + } +})(); diff --git a/Tests/input/touch priority.ts b/Tests/input/touch priority.ts new file mode 100644 index 000000000..6c6d75b36 --- /dev/null +++ b/Tests/input/touch priority.ts @@ -0,0 +1,41 @@ +/// + +(function () { + + var game = new Phaser.Game(this, 'game', 800, 600, init, create, null, render); + + function init() { + + // Using Phasers asset loader we load up a PNG from the assets folder + game.loader.addImageFile('atari1', 'assets/sprites/atari130xe.png'); + game.loader.addImageFile('atari2', 'assets/sprites/atari800xl.png'); + game.loader.addImageFile('sonic', 'assets/sprites/sonic_havok_sanity.png'); + game.loader.load(); + + } + + var atari1: Phaser.Sprite; + var atari2: Phaser.Sprite; + var sonic: Phaser.Sprite; + + function create() { + + atari1 = game.add.sprite(100, 100, 'atari1'); + atari2 = game.add.sprite(300, 200, 'atari2'); + sonic = game.add.sprite(400, 300, 'sonic'); + + atari1.input.start(0, false, true); + atari2.input.start(1, false, true); + sonic.input.start(2, false, true); + + atari1.input.enableDrag(); + atari2.input.enableDrag(); + sonic.input.enableDrag(); + + } + + function render() { + game.input.renderDebugInfo(32, 32); + } + +})(); diff --git a/Tests/phaser.js b/Tests/phaser.js index 50b388442..379d16ac9 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -5583,6 +5583,7 @@ var Phaser; * The PriorityID controls which Sprite receives an Input event first if they should overlap. */ this.priorityID = 0; + this.isDragged = false; this.dragPixelPerfect = false; this.allowHorizontalDrag = true; this.allowVerticalDrag = true; @@ -5609,50 +5610,6 @@ var Phaser; this._sprite = parent; this.enabled = false; } - Input.prototype.start = function (priority, checkBody, useHandCursor) { - if (typeof priority === "undefined") { priority = 0; } - if (typeof checkBody === "undefined") { checkBody = false; } - if (typeof useHandCursor === "undefined") { useHandCursor = false; } - // Turning on - if(this.enabled) { - return; - } else { - // Register, etc - this.checkBody = checkBody; - this.useHandCursor = useHandCursor; - this._pointerData = []; - for(var i = 0; i < 10; i++) { - this._pointerData.push({ - 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.game.input.addGameObject(this._sprite); - } - }; - Input.prototype.stop = function () { - // Turning off - if(this.enabled == false) { - return; - } else { - // De-register, etc - this.enabled = false; - this.game.input.removeGameObject(this._sprite); - } - }; Input.prototype.pointerX = /** * The x coordinate of the Input pointer, relative to the top-left of the parent Sprite. * This value is only set when the pointer is over this Sprite. @@ -5751,21 +5708,60 @@ var Phaser; if (typeof pointer === "undefined") { pointer = 0; } return this._pointerData[pointer].isDragged; }; + Input.prototype.start = function (priority, checkBody, useHandCursor) { + if (typeof priority === "undefined") { priority = 0; } + if (typeof checkBody === "undefined") { checkBody = false; } + if (typeof useHandCursor === "undefined") { useHandCursor = false; } + // Turning on + if(this.enabled == false) { + // Register, etc + this.checkBody = checkBody; + this.useHandCursor = useHandCursor; + this.priorityID = priority; + this._pointerData = []; + for(var i = 0; i < 10; i++) { + this._pointerData.push({ + 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.game.input.addGameObject(this._sprite); + } + return this._sprite; + }; + Input.prototype.stop = function () { + // Turning off + if(this.enabled == false) { + return; + } else { + // De-register, etc + this.enabled = false; + this.game.input.removeGameObject(this._sprite); + } + }; Input.prototype.update = /** * Update */ function (pointer) { if(this.enabled == false) { - return; + return false; } - // If was previously touched by this Pointer, check if still is - if(this._pointerData[pointer.id].isDown && pointer.isUp) { - this._releasedHandler(pointer); - } - if(this.draggable && this._pointerData[pointer.id].isDragged) { + if(this.draggable && this._draggedPointerID == pointer.id) { this.updateDrag(pointer); - //return; - } + } if(Phaser.RectangleUtils.contains(this._sprite.frameBounds, pointer.x, pointer.y)) { // { id: i, x: 0, y: 0, isDown: false, isUp: false, isOver: false, isOut: false, timeOver: 0, timeOut: 0, isDragged: false } this._pointerData[pointer.id].x = pointer.x - this._sprite.x; @@ -5779,6 +5775,7 @@ var Phaser; } this._sprite.events.onInputOver.dispatch(this._sprite, pointer); } + return true; } else { if(this._pointerData[pointer.id].isOver) { this._pointerData[pointer.id].isOver = false; @@ -5789,6 +5786,7 @@ var Phaser; } this._sprite.events.onInputOut.dispatch(this._sprite, pointer); } + return false; } }; Input.prototype._touchedHandler = function (pointer) { @@ -5797,18 +5795,28 @@ var Phaser; this._pointerData[pointer.id].isUp = false; this._pointerData[pointer.id].timeDown = this.game.time.now; this._sprite.events.onInputDown.dispatch(this._sprite, pointer); - // Star drag - if(this.draggable) { + // Start drag + if(this.draggable && this.isDragged == false && pointer.draggedObject == null) { this.startDrag(pointer); } } }; Input.prototype._releasedHandler = function (pointer) { - 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; - this._sprite.events.onInputDown.dispatch(this._sprite, pointer); + // If was previously touched by this Pointer, check if still is + 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; + this._sprite.events.onInputUp.dispatch(this._sprite, pointer); + // Stop drag + if(this.draggable && this.isDragged && this._draggedPointerID == pointer.id) { + this.stopDrag(pointer); + } + if(this.useHandCursor) { + this.game.stage.canvas.style.cursor = "default"; + } + } }; Input.prototype.updateDrag = /** * Updates the Pointer drag on this Sprite. @@ -5935,13 +5943,15 @@ var Phaser; } } this.draggable = false; - //mouseStartDragCallback = null; - //mouseStopDragCallback = null; - }; + this.isDragged = false; + this._draggedPointerID = -1; + }; Input.prototype.startDrag = /** * Called by Pointer when drag starts on this Sprite. Should not usually be called directly. */ function (pointer) { + this.isDragged = true; + this._draggedPointerID = pointer.id; this._pointerData[pointer.id].isDragged = true; if(this.dragFromCenter) { // Move the sprite to the middle of the pointer @@ -5949,16 +5959,20 @@ var Phaser; } else { this._dragPoint.setTo(this._sprite.x - pointer.x, this._sprite.y - pointer.y); } + pointer.draggedObject = this._sprite; }; Input.prototype.stopDrag = /** * Called by Pointer when drag is stopped on this Sprite. Should not usually be called directly. */ function (pointer) { + this.isDragged = false; + this._draggedPointerID = -1; this._pointerData[pointer.id].isDragged = false; if(this.snapOnRelease) { this._sprite.x = Math.floor(this._sprite.x / this.snapX) * this.snapX; this._sprite.y = Math.floor(this._sprite.y / this.snapY) * this.snapY; } + pointer.draggedObject = null; }; Input.prototype.setDragLock = /** * Restricts this sprite to drag movement only on the given axis. Note: If both are set to false the sprite will never move! @@ -13525,12 +13539,12 @@ var Phaser; var _highestPriority = 0; for(var i = 0; i < this.game.input.totalTrackedObjects; i++) { if(this.game.input.inputObjects[i].input.enabled) { - this.game.input.inputObjects[i].input.update(this); - if(this.game.input.inputObjects[i].input.priorityID > _highestPriority) { + if(this.game.input.inputObjects[i].input.update(this) && this.game.input.inputObjects[i].input.priorityID > _highestPriority) { _highestPriority = this.game.input.inputObjects[i].input.priorityID; } } } + //console.log('highest priority was', _highestPriority); if(this.isDown) { // Now update all objects with the highest priority ID (can be more than 1) for(var i = 0; i < this.game.input.totalTrackedObjects; i++) { @@ -13611,6 +13625,12 @@ var Phaser; if(this.isMouse == false) { this.game.input.currentPointers--; } + for(var i = 0; i < this.game.input.totalTrackedObjects; i++) { + if(this.game.input.inputObjects[i].input.enabled) { + this.game.input.inputObjects[i].input._releasedHandler(this); + } + } + this.draggedObject = null; return this; }; Pointer.prototype.justPressed = /** diff --git a/build/phaser.d.ts b/build/phaser.d.ts index 83980c536..6771c6bbc 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -3271,10 +3271,10 @@ module Phaser.Components { * The PriorityID controls which Sprite receives an Input event first if they should overlap. */ public priorityID: number; - public start(priority?: number, checkBody?: bool, useHandCursor?: bool): void; - public stop(): void; private _dragPoint; + private _draggedPointerID; public dragOffset: Point; + public isDragged: bool; public dragFromCenter: bool; public dragPixelPerfect: bool; public dragPixelPerfectAlpha: number; @@ -3377,10 +3377,12 @@ module Phaser.Components { * @default false */ public pointerDragged(pointer?: number): bool; + public start(priority?: number, checkBody?: bool, useHandCursor?: bool): Sprite; + public stop(): void; /** * Update */ - public update(pointer: Pointer): void; + public update(pointer: Pointer): bool; public _touchedHandler(pointer: Pointer): void; public _releasedHandler(pointer: Pointer): void; /** diff --git a/build/phaser.js b/build/phaser.js index 50b388442..379d16ac9 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -5583,6 +5583,7 @@ var Phaser; * The PriorityID controls which Sprite receives an Input event first if they should overlap. */ this.priorityID = 0; + this.isDragged = false; this.dragPixelPerfect = false; this.allowHorizontalDrag = true; this.allowVerticalDrag = true; @@ -5609,50 +5610,6 @@ var Phaser; this._sprite = parent; this.enabled = false; } - Input.prototype.start = function (priority, checkBody, useHandCursor) { - if (typeof priority === "undefined") { priority = 0; } - if (typeof checkBody === "undefined") { checkBody = false; } - if (typeof useHandCursor === "undefined") { useHandCursor = false; } - // Turning on - if(this.enabled) { - return; - } else { - // Register, etc - this.checkBody = checkBody; - this.useHandCursor = useHandCursor; - this._pointerData = []; - for(var i = 0; i < 10; i++) { - this._pointerData.push({ - 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.game.input.addGameObject(this._sprite); - } - }; - Input.prototype.stop = function () { - // Turning off - if(this.enabled == false) { - return; - } else { - // De-register, etc - this.enabled = false; - this.game.input.removeGameObject(this._sprite); - } - }; Input.prototype.pointerX = /** * The x coordinate of the Input pointer, relative to the top-left of the parent Sprite. * This value is only set when the pointer is over this Sprite. @@ -5751,21 +5708,60 @@ var Phaser; if (typeof pointer === "undefined") { pointer = 0; } return this._pointerData[pointer].isDragged; }; + Input.prototype.start = function (priority, checkBody, useHandCursor) { + if (typeof priority === "undefined") { priority = 0; } + if (typeof checkBody === "undefined") { checkBody = false; } + if (typeof useHandCursor === "undefined") { useHandCursor = false; } + // Turning on + if(this.enabled == false) { + // Register, etc + this.checkBody = checkBody; + this.useHandCursor = useHandCursor; + this.priorityID = priority; + this._pointerData = []; + for(var i = 0; i < 10; i++) { + this._pointerData.push({ + 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.game.input.addGameObject(this._sprite); + } + return this._sprite; + }; + Input.prototype.stop = function () { + // Turning off + if(this.enabled == false) { + return; + } else { + // De-register, etc + this.enabled = false; + this.game.input.removeGameObject(this._sprite); + } + }; Input.prototype.update = /** * Update */ function (pointer) { if(this.enabled == false) { - return; + return false; } - // If was previously touched by this Pointer, check if still is - if(this._pointerData[pointer.id].isDown && pointer.isUp) { - this._releasedHandler(pointer); - } - if(this.draggable && this._pointerData[pointer.id].isDragged) { + if(this.draggable && this._draggedPointerID == pointer.id) { this.updateDrag(pointer); - //return; - } + } if(Phaser.RectangleUtils.contains(this._sprite.frameBounds, pointer.x, pointer.y)) { // { id: i, x: 0, y: 0, isDown: false, isUp: false, isOver: false, isOut: false, timeOver: 0, timeOut: 0, isDragged: false } this._pointerData[pointer.id].x = pointer.x - this._sprite.x; @@ -5779,6 +5775,7 @@ var Phaser; } this._sprite.events.onInputOver.dispatch(this._sprite, pointer); } + return true; } else { if(this._pointerData[pointer.id].isOver) { this._pointerData[pointer.id].isOver = false; @@ -5789,6 +5786,7 @@ var Phaser; } this._sprite.events.onInputOut.dispatch(this._sprite, pointer); } + return false; } }; Input.prototype._touchedHandler = function (pointer) { @@ -5797,18 +5795,28 @@ var Phaser; this._pointerData[pointer.id].isUp = false; this._pointerData[pointer.id].timeDown = this.game.time.now; this._sprite.events.onInputDown.dispatch(this._sprite, pointer); - // Star drag - if(this.draggable) { + // Start drag + if(this.draggable && this.isDragged == false && pointer.draggedObject == null) { this.startDrag(pointer); } } }; Input.prototype._releasedHandler = function (pointer) { - 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; - this._sprite.events.onInputDown.dispatch(this._sprite, pointer); + // If was previously touched by this Pointer, check if still is + 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; + this._sprite.events.onInputUp.dispatch(this._sprite, pointer); + // Stop drag + if(this.draggable && this.isDragged && this._draggedPointerID == pointer.id) { + this.stopDrag(pointer); + } + if(this.useHandCursor) { + this.game.stage.canvas.style.cursor = "default"; + } + } }; Input.prototype.updateDrag = /** * Updates the Pointer drag on this Sprite. @@ -5935,13 +5943,15 @@ var Phaser; } } this.draggable = false; - //mouseStartDragCallback = null; - //mouseStopDragCallback = null; - }; + this.isDragged = false; + this._draggedPointerID = -1; + }; Input.prototype.startDrag = /** * Called by Pointer when drag starts on this Sprite. Should not usually be called directly. */ function (pointer) { + this.isDragged = true; + this._draggedPointerID = pointer.id; this._pointerData[pointer.id].isDragged = true; if(this.dragFromCenter) { // Move the sprite to the middle of the pointer @@ -5949,16 +5959,20 @@ var Phaser; } else { this._dragPoint.setTo(this._sprite.x - pointer.x, this._sprite.y - pointer.y); } + pointer.draggedObject = this._sprite; }; Input.prototype.stopDrag = /** * Called by Pointer when drag is stopped on this Sprite. Should not usually be called directly. */ function (pointer) { + this.isDragged = false; + this._draggedPointerID = -1; this._pointerData[pointer.id].isDragged = false; if(this.snapOnRelease) { this._sprite.x = Math.floor(this._sprite.x / this.snapX) * this.snapX; this._sprite.y = Math.floor(this._sprite.y / this.snapY) * this.snapY; } + pointer.draggedObject = null; }; Input.prototype.setDragLock = /** * Restricts this sprite to drag movement only on the given axis. Note: If both are set to false the sprite will never move! @@ -13525,12 +13539,12 @@ var Phaser; var _highestPriority = 0; for(var i = 0; i < this.game.input.totalTrackedObjects; i++) { if(this.game.input.inputObjects[i].input.enabled) { - this.game.input.inputObjects[i].input.update(this); - if(this.game.input.inputObjects[i].input.priorityID > _highestPriority) { + if(this.game.input.inputObjects[i].input.update(this) && this.game.input.inputObjects[i].input.priorityID > _highestPriority) { _highestPriority = this.game.input.inputObjects[i].input.priorityID; } } } + //console.log('highest priority was', _highestPriority); if(this.isDown) { // Now update all objects with the highest priority ID (can be more than 1) for(var i = 0; i < this.game.input.totalTrackedObjects; i++) { @@ -13611,6 +13625,12 @@ var Phaser; if(this.isMouse == false) { this.game.input.currentPointers--; } + for(var i = 0; i < this.game.input.totalTrackedObjects; i++) { + if(this.game.input.inputObjects[i].input.enabled) { + this.game.input.inputObjects[i].input._releasedHandler(this); + } + } + this.draggedObject = null; return this; }; Pointer.prototype.justPressed = /**