Skip to content

Commit b951b02

Browse files
committed
Input priority IDs working properly for drag events.
1 parent 2270da2 commit b951b02

22 files changed

Lines changed: 418 additions & 208 deletions

Phaser/components/sprite/Input.ts

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,10 @@ module Phaser.Components {
4848
*/
4949
public priorityID:number = 0;
5050

51-
public start(priority:number = 0, checkBody?:bool = false, useHandCursor?:bool = false) {
52-
53-
// Turning on
54-
if (this.enabled)
55-
{
56-
return;
57-
}
58-
else
59-
{
60-
// Register, etc
61-
this.checkBody = checkBody;
62-
this.useHandCursor = useHandCursor;
63-
64-
this._pointerData = [];
65-
66-
for (var i = 0; i < 10; i++)
67-
{
68-
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 });
69-
}
70-
71-
this.snapOffset = new Point;
72-
this.enabled = true;
73-
74-
this.game.input.addGameObject(this._sprite);
75-
}
76-
77-
}
78-
79-
public stop() {
80-
81-
// Turning off
82-
if (this.enabled == false)
83-
{
84-
return;
85-
}
86-
else
87-
{
88-
// De-register, etc
89-
this.enabled = false;
90-
this.game.input.removeGameObject(this._sprite);
91-
}
92-
93-
}
94-
9551
private _dragPoint: Point;
52+
private _draggedPointerID: number;
9653
public dragOffset: Point;
54+
public isDragged: bool = false;
9755
public dragFromCenter: bool;
9856
public dragPixelPerfect:bool = false;
9957
public dragPixelPerfectAlpha:number;
@@ -238,26 +196,62 @@ module Phaser.Components {
238196
return this._pointerData[pointer].isDragged;
239197
}
240198

199+
public start(priority:number = 0, checkBody?:bool = false, useHandCursor?:bool = false): Sprite {
200+
201+
// Turning on
202+
if (this.enabled == false)
203+
{
204+
// Register, etc
205+
this.checkBody = checkBody;
206+
this.useHandCursor = useHandCursor;
207+
this.priorityID = priority;
208+
209+
this._pointerData = [];
210+
211+
for (var i = 0; i < 10; i++)
212+
{
213+
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 });
214+
}
215+
216+
this.snapOffset = new Point;
217+
this.enabled = true;
218+
219+
this.game.input.addGameObject(this._sprite);
220+
}
221+
222+
return this._sprite;
223+
224+
}
225+
226+
public stop() {
227+
228+
// Turning off
229+
if (this.enabled == false)
230+
{
231+
return;
232+
}
233+
else
234+
{
235+
// De-register, etc
236+
this.enabled = false;
237+
this.game.input.removeGameObject(this._sprite);
238+
}
239+
240+
}
241+
241242
/**
242243
* Update
243244
*/
244-
public update(pointer: Phaser.Pointer) {
245+
public update(pointer: Phaser.Pointer): bool {
245246

246247
if (this.enabled == false)
247248
{
248-
return;
249+
return false;
249250
}
250251

251-
// If was previously touched by this Pointer, check if still is
252-
if (this._pointerData[pointer.id].isDown && pointer.isUp)
253-
{
254-
this._releasedHandler(pointer);
255-
}
256-
257-
if (this.draggable && this._pointerData[pointer.id].isDragged)
252+
if (this.draggable && this._draggedPointerID == pointer.id)
258253
{
259254
this.updateDrag(pointer);
260-
//return;
261255
}
262256

263257
if (RectangleUtils.contains(this._sprite.frameBounds, pointer.x, pointer.y))
@@ -280,6 +274,8 @@ module Phaser.Components {
280274

281275
this._sprite.events.onInputOver.dispatch(this._sprite, pointer);
282276
}
277+
278+
return true;
283279
}
284280
else
285281
{
@@ -296,6 +292,8 @@ module Phaser.Components {
296292

297293
this._sprite.events.onInputOut.dispatch(this._sprite, pointer);
298294
}
295+
296+
return false;
299297
}
300298

301299
}
@@ -307,25 +305,42 @@ module Phaser.Components {
307305
this._pointerData[pointer.id].isDown = true;
308306
this._pointerData[pointer.id].isUp = false;
309307
this._pointerData[pointer.id].timeDown = this.game.time.now;
308+
310309
this._sprite.events.onInputDown.dispatch(this._sprite, pointer);
311310

312-
// Star drag
313-
if (this.draggable)
311+
// Start drag
312+
if (this.draggable && this.isDragged == false && pointer.draggedObject == null)
314313
{
315314
this.startDrag(pointer);
316315
}
316+
317317
}
318318

319319
}
320320

321321
public _releasedHandler(pointer: Pointer) {
322322

323-
this._pointerData[pointer.id].isDown = false;
324-
this._pointerData[pointer.id].isUp = true;
325-
this._pointerData[pointer.id].timeUp = this.game.time.now;
326-
//this._pointerData[pointer.id].downDuration = this._pointerData[pointer.id].timeUp - this._pointerData[pointer.id].timeDown;
323+
// If was previously touched by this Pointer, check if still is
324+
if (this._pointerData[pointer.id].isDown && pointer.isUp)
325+
{
326+
this._pointerData[pointer.id].isDown = false;
327+
this._pointerData[pointer.id].isUp = true;
328+
this._pointerData[pointer.id].timeUp = this.game.time.now;
329+
this._pointerData[pointer.id].downDuration = this._pointerData[pointer.id].timeUp - this._pointerData[pointer.id].timeDown;
327330

328-
this._sprite.events.onInputDown.dispatch(this._sprite, pointer);
331+
this._sprite.events.onInputUp.dispatch(this._sprite, pointer);
332+
333+
// Stop drag
334+
if (this.draggable && this.isDragged && this._draggedPointerID == pointer.id)
335+
{
336+
this.stopDrag(pointer);
337+
}
338+
339+
if (this.useHandCursor)
340+
{
341+
this.game.stage.canvas.style.cursor = "default";
342+
}
343+
}
329344

330345
}
331346

@@ -478,16 +493,17 @@ module Phaser.Components {
478493
}
479494

480495
this.draggable = false;
481-
482-
//mouseStartDragCallback = null;
483-
//mouseStopDragCallback = null;
496+
this.isDragged = false;
497+
this._draggedPointerID = -1;
484498
}
485499

486500
/**
487501
* Called by Pointer when drag starts on this Sprite. Should not usually be called directly.
488502
*/
489503
public startDrag(pointer: Pointer):void
490504
{
505+
this.isDragged = true;
506+
this._draggedPointerID = pointer.id;
491507
this._pointerData[pointer.id].isDragged = true;
492508

493509
if (this.dragFromCenter)
@@ -500,20 +516,26 @@ module Phaser.Components {
500516
this._dragPoint.setTo(this._sprite.x - pointer.x, this._sprite.y - pointer.y);
501517
}
502518

519+
pointer.draggedObject = this._sprite;
520+
503521
}
504522

505523
/**
506524
* Called by Pointer when drag is stopped on this Sprite. Should not usually be called directly.
507525
*/
508526
public stopDrag(pointer: Pointer):void
509527
{
528+
this.isDragged = false;
529+
this._draggedPointerID = -1;
510530
this._pointerData[pointer.id].isDragged = false;
511531

512532
if (this.snapOnRelease)
513533
{
514534
this._sprite.x = Math.floor(this._sprite.x / this.snapX) * this.snapX;
515535
this._sprite.y = Math.floor(this._sprite.y / this.snapY) * this.snapY;
516536
}
537+
538+
pointer.draggedObject = null;
517539
}
518540

519541
/**

Phaser/input/Pointer.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ module Phaser {
252252

253253
}
254254

255-
// Sprite Drag Related
256-
257255
/**
258256
* The Game Object this Pointer is currently dragging.
259257
* @property draggedObject
@@ -369,15 +367,15 @@ module Phaser {
369367
{
370368
if (this.game.input.inputObjects[i].input.enabled)
371369
{
372-
this.game.input.inputObjects[i].input.update(this);
373-
374-
if (this.game.input.inputObjects[i].input.priorityID > _highestPriority)
370+
if (this.game.input.inputObjects[i].input.update(this) && this.game.input.inputObjects[i].input.priorityID > _highestPriority)
375371
{
376372
_highestPriority = this.game.input.inputObjects[i].input.priorityID;
377373
}
378374
}
379375
}
380376

377+
//console.log('highest priority was', _highestPriority);
378+
381379
if (this.isDown)
382380
{
383381
// Now update all objects with the highest priority ID (can be more than 1)
@@ -493,6 +491,16 @@ module Phaser {
493491
this.game.input.currentPointers--;
494492
}
495493

494+
for (var i = 0; i < this.game.input.totalTrackedObjects; i++)
495+
{
496+
if (this.game.input.inputObjects[i].input.enabled)
497+
{
498+
this.game.input.inputObjects[i].input._releasedHandler(this);
499+
}
500+
}
501+
502+
this.draggedObject = null;
503+
496504
return this;
497505

498506
}

Tests/Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,21 @@
8484
<Content Include="input\drag sprite 1.js">
8585
<DependentUpon>drag sprite 1.ts</DependentUpon>
8686
</Content>
87+
<TypeScriptCompile Include="input\drag sprite 2.ts" />
88+
<Content Include="input\drag sprite 2.js">
89+
<DependentUpon>drag sprite 2.ts</DependentUpon>
90+
</Content>
8791
<Content Include="input\over sprite 1.js">
8892
<DependentUpon>over sprite 1.ts</DependentUpon>
8993
</Content>
9094
<TypeScriptCompile Include="input\over sprite 2.ts" />
9195
<Content Include="input\over sprite 2.js">
9296
<DependentUpon>over sprite 2.ts</DependentUpon>
9397
</Content>
98+
<TypeScriptCompile Include="input\touch priority.ts" />
99+
<Content Include="input\touch priority.js">
100+
<DependentUpon>touch priority.ts</DependentUpon>
101+
</Content>
94102
<Content Include="particles\graphic emitter.js">
95103
<DependentUpon>graphic emitter.ts</DependentUpon>
96104
</Content>

Tests/assets/pics/ra_einstein.png

75.3 KB
Loading
2.7 KB
Loading
3.36 KB
Loading
1.42 KB
Loading

Tests/assets/sprites/atari400.png

2.7 KB
Loading

Tests/assets/sprites/atari800.png

2.34 KB
Loading
1.58 KB
Loading

0 commit comments

Comments
 (0)