Skip to content

Commit a2d1f86

Browse files
committed
Added DragEnter, DragOver and DragLeave events for drop zones.
1 parent 1c5da09 commit a2d1f86

7 files changed

Lines changed: 155 additions & 16 deletions

File tree

v3/src/input/InteractiveObject.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var InteractiveObject = function (gameObject, hitArea, hitAreaCallback)
1212
draggable: false,
1313
dropZone: false,
1414

15+
target: null,
16+
1517
hitArea: hitArea,
1618
hitAreaCallback: hitAreaCallback,
1719

v3/src/input/local/components/ProcessDragEvents.js

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var ProcessDragEvents = function (pointer, time)
1010
}
1111

1212
var i;
13+
var c;
14+
var overGameObject;
1315
var gameObject;
1416
var list;
1517
var input;
@@ -126,6 +128,17 @@ var ProcessDragEvents = function (pointer, time)
126128
// 4 = Pointer actively dragging the draglist and has moved
127129
if (pointer.dragState === 4 && pointer.justMoved)
128130
{
131+
// Let's filter out currentlyOver for dropZones only
132+
var dropZones = [];
133+
134+
for (c = 0; c < currentlyOver.length; c++)
135+
{
136+
if (currentlyOver[c].input.dropZone)
137+
{
138+
dropZones.push(currentlyOver[c]);
139+
}
140+
}
141+
129142
list = this._drag[pointer.id];
130143

131144
for (i = 0; i < list.length; i++)
@@ -140,12 +153,57 @@ var ProcessDragEvents = function (pointer, time)
140153
// input.dragX = pointer.x - input.localX;
141154
// input.dragY = pointer.y - input.localY;
142155

156+
// If this GO has a target then let's check it
157+
if (input.target)
158+
{
159+
var index = dropZones.indexOf(input.target);
160+
161+
// Got a target, are we still over it?
162+
if (index === 0)
163+
{
164+
// We're still over it, and it's still the top of the display list, phew ...
165+
this.events.dispatch(new InputEvent.DRAG_OVER(pointer, gameObject, input.target));
166+
}
167+
else if (index > 0)
168+
{
169+
// Still over it but it's no longer top of the display list (targets must always be at the top)
170+
this.events.dispatch(new InputEvent.DRAG_LEAVE(pointer, gameObject, input.target));
171+
172+
input.target = dropZones[0];
173+
174+
this.events.dispatch(new InputEvent.DRAG_ENTER(pointer, gameObject, input.target));
175+
}
176+
else
177+
{
178+
// Nope, we've moved on (or the target has!), leave the old target
179+
this.events.dispatch(new InputEvent.DRAG_LEAVE(pointer, gameObject, input.target));
180+
181+
// Anything new to replace it?
182+
// Yup!
183+
if (dropZones[0])
184+
{
185+
input.target = dropZones[0];
186+
187+
this.events.dispatch(new InputEvent.DRAG_ENTER(pointer, gameObject, input.target));
188+
}
189+
else
190+
{
191+
// Nope
192+
input.target = null;
193+
}
194+
}
195+
}
196+
else if (!input.target && dropZones[0])
197+
{
198+
input.target = dropZones[0];
199+
200+
this.events.dispatch(new InputEvent.DRAG_ENTER(pointer, gameObject, input.target));
201+
}
202+
143203
this.events.dispatch(new InputEvent.DRAG(pointer, gameObject));
144204

145205
input.onDrag(gameObject, pointer);
146206
}
147-
148-
// Check drop zones
149207
}
150208

151209
// 5 = Pointer actively dragging but has been released, notify draglist
@@ -164,9 +222,9 @@ var ProcessDragEvents = function (pointer, time)
164222
input.dragY = input.localY - gameObject.displayOriginY;
165223

166224
// Any drop zones here?
167-
for (var c = 0; c < currentlyOver.length; c++)
225+
for (c = 0; c < currentlyOver.length; c++)
168226
{
169-
var overGameObject = currentlyOver[c];
227+
overGameObject = currentlyOver[c];
170228

171229
if (overGameObject.input.dropZone)
172230
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Class = require('../../../utils/Class');
2+
var Event = require('../../../events/Event');
3+
4+
var DragEnterEvent = new Class({
5+
6+
Extends: Event,
7+
8+
initialize:
9+
10+
function DragEnterEvent (pointer, gameObject, dropZone)
11+
{
12+
Event.call(this, 'DRAG_ENTER_EVENT');
13+
14+
// The Pointer that triggered the event
15+
this.pointer = pointer;
16+
17+
// The native DOM event (MouseEvent, TouchEvent, etc)
18+
this.event = pointer.event;
19+
20+
// The Game Object the event occurred on
21+
this.gameObject = gameObject;
22+
23+
// The drop zone the game object was dropped on
24+
this.dropZone = dropZone;
25+
}
26+
27+
});
28+
29+
module.exports = DragEnterEvent;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Class = require('../../../utils/Class');
2+
var Event = require('../../../events/Event');
3+
4+
var DragLeaveEvent = new Class({
5+
6+
Extends: Event,
7+
8+
initialize:
9+
10+
function DragLeaveEvent (pointer, gameObject, dropZone)
11+
{
12+
Event.call(this, 'DRAG_LEAVE_EVENT');
13+
14+
// The Pointer that triggered the event
15+
this.pointer = pointer;
16+
17+
// The native DOM event (MouseEvent, TouchEvent, etc)
18+
this.event = pointer.event;
19+
20+
// The Game Object the event occurred on
21+
this.gameObject = gameObject;
22+
23+
// The drop zone the game object was dropped on
24+
this.dropZone = dropZone;
25+
}
26+
27+
});
28+
29+
module.exports = DragLeaveEvent;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Class = require('../../../utils/Class');
2+
var Event = require('../../../events/Event');
3+
4+
var DragOverEvent = new Class({
5+
6+
Extends: Event,
7+
8+
initialize:
9+
10+
function DragOverEvent (pointer, gameObject, dropZone)
11+
{
12+
Event.call(this, 'DRAG_OVER_EVENT');
13+
14+
// The Pointer that triggered the event
15+
this.pointer = pointer;
16+
17+
// The native DOM event (MouseEvent, TouchEvent, etc)
18+
this.event = pointer.event;
19+
20+
// The Game Object the event occurred on
21+
this.gameObject = gameObject;
22+
23+
// The drop zone the game object was dropped on
24+
this.dropZone = dropZone;
25+
}
26+
27+
});
28+
29+
module.exports = DragOverEvent;

v3/src/input/local/events/DropEvent.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ var DropEvent = new Class({
2222

2323
// The drop zone the game object was dropped on
2424
this.dropZone = dropZone;
25-
26-
// The local x/y coordinates of the event within the Game Object
27-
// this.x = pointer.x;
28-
// this.y = pointer.y;
29-
30-
// this.dragX = gameObject.input.dragX;
31-
// this.dragY = gameObject.input.dragY;
32-
33-
// The local x/y coordinates of the event within the Game Object
34-
// this.dragX = pointer.x - gameObject.input.dragX;
35-
// this.dragY = pointer.y - gameObject.input.dragY;
3625
}
3726

3827
});

v3/src/input/local/events/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
module.exports = {
44

5-
DROP: require('./DropEvent'),
65
DRAG: require('./DragEvent'),
76
DRAG_END: require('./DragEndEvent'),
7+
DRAG_ENTER: require('./DragEnterEvent'),
8+
DRAG_LEAVE: require('./DragLeaveEvent'),
9+
DRAG_OVER: require('./DragOverEvent'),
810
DRAG_START: require('./DragStartEvent'),
11+
DROP: require('./DropEvent'),
912

1013
GAME_OBJECT_DOWN: require('./GameObjectDownEvent'),
1114
GAME_OBJECT_MOVE: require('./GameObjectMoveEvent'),

0 commit comments

Comments
 (0)