Skip to content

Commit 730c84e

Browse files
committed
Draggable Game Objects would not work if you had multiple Scenes running in parallel, with draggable objects in both of them. Only the top-most Scene would work fully. Items in the bottom Scene would never finish their drag cycle, causing them to get stuck. Fix phaserjs#4249
1 parent 9a70589 commit 730c84e

2 files changed

Lines changed: 78 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ one set of bindings ever created, which makes things a lot cleaner.
119119
* The Input Plugin has a new event you can listen to: `pointerupoutside`, which is triggered whenever the mouse or a pointer is released while outside of the Game canvas. Listen to it with `this.input.on('pointerupoutside')` from within a Scene.
120120
* `Pointer.downElement` is a new property that holds the target of the DOM Event that triggered when the Pointer was pressed down. If this is within the game, this will be the game canvas element.
121121
* `Pointer.upElement` is a new property that holds the target of the DOM Event that triggered when the Pointer was released. If this is within the game, this will be the game canvas element.
122+
* The `Pointer.dragState` property has been removed. This is no longer used internally as it has to be tracked per Scene, not on a global level.
123+
* `InputPlugin.setDragState` is a new internal method that sets the drag state for the given Pointer.
124+
* `InputPlugin.getDragState` is a new internal method that gets the drag state for the given Pointer.
125+
* Draggable Game Objects would not work if you had multiple Scenes running in parallel, with draggable objects in both of them. Only the top-most Scene would work fully. Items in the bottom Scene would never finish their drag cycle, causing them to get stuck. Fix #4249 (thanks @probt)
122126

123127
### New Features
124128

src/input/InputPlugin.js

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@ var InputPlugin = new Class({
324324
*/
325325
this._drag = { 0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [] };
326326

327+
/**
328+
* A array containing the dragStates, for this Scene, index by the Pointer ID.
329+
*
330+
* @name Phaser.Input.InputPlugin#_dragState
331+
* @type {integer[]}
332+
* @private
333+
* @since 3.16.0
334+
*/
335+
this._dragState = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
336+
327337
/**
328338
* A list of all Interactive Objects currently considered as being 'over' by any pointer, indexed by pointer ID.
329339
*
@@ -818,6 +828,53 @@ var InputPlugin = new Class({
818828
return total;
819829
},
820830

831+
/**
832+
* Returns the drag state of the given Pointer for this Input Plugin.
833+
*
834+
* The state will be one of the following:
835+
*
836+
* 0 = Not dragging anything
837+
* 1 = Primary button down and objects below, so collect a draglist
838+
* 2 = Pointer being checked if meets drag criteria
839+
* 3 = Pointer meets criteria, notify the draglist
840+
* 4 = Pointer actively dragging the draglist and has moved
841+
* 5 = Pointer actively dragging but has been released, notify draglist
842+
*
843+
* @method Phaser.Input.InputPlugin#getDragState
844+
* @since 3.16.0
845+
*
846+
* @param {Phaser.Input.Pointer} pointer - The Pointer to get the drag state for.
847+
*
848+
* @return {integer} The drag state of the given Pointer.
849+
*/
850+
getDragState: function (pointer)
851+
{
852+
return this._dragState[pointer.id];
853+
},
854+
855+
/**
856+
* Sets the drag state of the given Pointer for this Input Plugin.
857+
*
858+
* The state must be one of the following values:
859+
*
860+
* 0 = Not dragging anything
861+
* 1 = Primary button down and objects below, so collect a draglist
862+
* 2 = Pointer being checked if meets drag criteria
863+
* 3 = Pointer meets criteria, notify the draglist
864+
* 4 = Pointer actively dragging the draglist and has moved
865+
* 5 = Pointer actively dragging but has been released, notify draglist
866+
*
867+
* @method Phaser.Input.InputPlugin#setDragState
868+
* @since 3.16.0
869+
*
870+
* @param {Phaser.Input.Pointer} pointer - The Pointer to set the drag state for.
871+
* @param {integer} state - The drag state value. An integer between 0 and 5.
872+
*/
873+
setDragState: function (pointer, state)
874+
{
875+
this._dragState[pointer.id] = state;
876+
},
877+
821878
/**
822879
* An internal method that handles the Pointer drag events.
823880
*
@@ -865,19 +922,19 @@ var InputPlugin = new Class({
865922
// 4 = Pointer actively dragging the draglist and has moved
866923
// 5 = Pointer actively dragging but has been released, notify draglist
867924

868-
if (pointer.dragState === 0 && pointer.primaryDown && pointer.justDown && currentlyOver.length > 0)
925+
if (this.getDragState(pointer) === 0 && pointer.primaryDown && pointer.justDown && currentlyOver.length > 0)
869926
{
870-
pointer.dragState = 1;
927+
this.setDragState(pointer, 1);
871928
}
872-
else if (pointer.dragState > 0 && !pointer.primaryDown && pointer.justUp)
929+
else if (this.getDragState(pointer) > 0 && !pointer.primaryDown && pointer.justUp)
873930
{
874-
pointer.dragState = 5;
931+
this.setDragState(pointer, 5);
875932
}
876933

877934
// Process the various drag states
878935

879936
// 1 = Primary button down and objects below, so collect a draglist
880-
if (pointer.dragState === 1)
937+
if (this.getDragState(pointer) === 1)
881938
{
882939
// Get draggable objects, sort them, pick the top (or all) and store them somewhere
883940
var draglist = [];
@@ -894,7 +951,7 @@ var InputPlugin = new Class({
894951

895952
if (draglist.length === 0)
896953
{
897-
pointer.dragState = 0;
954+
this.setDragState(pointer, 0);
898955

899956
return 0;
900957
}
@@ -914,35 +971,35 @@ var InputPlugin = new Class({
914971
if (this.dragDistanceThreshold === 0 && this.dragTimeThreshold === 0)
915972
{
916973
// No drag criteria, so snap immediately to mode 3
917-
pointer.dragState = 3;
974+
this.setDragState(pointer, 3);
918975
}
919976
else
920977
{
921978
// Check the distance / time
922-
pointer.dragState = 2;
979+
this.setDragState(pointer, 2);
923980
}
924981
}
925982

926983
// 2 = Pointer being checked if meets drag criteria
927-
if (pointer.dragState === 2)
984+
if (this.getDragState(pointer) === 2)
928985
{
929986
// Has it moved far enough to be considered a drag?
930987
if (this.dragDistanceThreshold > 0 && DistanceBetween(pointer.x, pointer.y, pointer.downX, pointer.downY) >= this.dragDistanceThreshold)
931988
{
932989
// Alrighty, we've got a drag going on ...
933-
pointer.dragState = 3;
990+
this.setDragState(pointer, 3);
934991
}
935992

936993
// Held down long enough to be considered a drag?
937994
if (this.dragTimeThreshold > 0 && (time >= pointer.downTime + this.dragTimeThreshold))
938995
{
939996
// Alrighty, we've got a drag going on ...
940-
pointer.dragState = 3;
997+
this.setDragState(pointer, 3);
941998
}
942999
}
9431000

9441001
// 3 = Pointer meets criteria and is freshly down, notify the draglist
945-
if (pointer.dragState === 3)
1002+
if (this.getDragState(pointer) === 3)
9461003
{
9471004
list = this._drag[pointer.id];
9481005

@@ -965,13 +1022,13 @@ var InputPlugin = new Class({
9651022
this.emit('dragstart', pointer, gameObject);
9661023
}
9671024

968-
pointer.dragState = 4;
1025+
this.setDragState(pointer, 4);
9691026

9701027
return list.length;
9711028
}
9721029

9731030
// 4 = Pointer actively dragging the draglist and has moved
974-
if (pointer.dragState === 4 && pointer.justMoved && !pointer.justUp)
1031+
if (this.getDragState(pointer) === 4 && pointer.justMoved && !pointer.justUp)
9751032
{
9761033
var dropZones = this._tempZones;
9771034

@@ -1054,7 +1111,7 @@ var InputPlugin = new Class({
10541111
}
10551112

10561113
// 5 = Pointer was actively dragging but has been released, notify draglist
1057-
if (pointer.dragState === 5)
1114+
if (this.getDragState(pointer) === 5)
10581115
{
10591116
list = this._drag[pointer.id];
10601117

@@ -1092,7 +1149,7 @@ var InputPlugin = new Class({
10921149
}
10931150
}
10941151

1095-
pointer.dragState = 0;
1152+
this.setDragState(pointer, 0);
10961153

10971154
list.splice(0);
10981155
}
@@ -2228,6 +2285,7 @@ var InputPlugin = new Class({
22282285
this._draggable.length = 0;
22292286
this._pendingRemoval.length = 0;
22302287
this._pendingInsertion.length = 0;
2288+
this._dragState.length = 0;
22312289

22322290
for (var i = 0; i < 10; i++)
22332291
{

0 commit comments

Comments
 (0)