Skip to content

Commit 474d08f

Browse files
committed
Lots of work on making Game Objects draggable
1 parent be5961c commit 474d08f

2 files changed

Lines changed: 174 additions & 43 deletions

File tree

v3/src/input/InteractiveObject.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ var InteractiveObject = function (gameObject, hitArea, hitAreaCallback)
2525
onDown: NOOP,
2626
onUp: NOOP,
2727
onOver: NOOP,
28-
onOut: NOOP
28+
onOut: NOOP,
29+
30+
dragX: 0,
31+
dragY: 0,
32+
33+
onDragStart: NOOP,
34+
onDrag: NOOP,
35+
onDragEnd: NOOP
2936
};
3037
};
3138

v3/src/plugins/InputManager.js

Lines changed: 166 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ var InputManager = new Class({
4848
// Only those which are currently below a pointer (any pointer)
4949
this._over = [];
5050

51+
// Only Game Objects which have been flagged as draggable are added to this array
52+
this._draggable = [];
53+
5154
// Objects waiting to be inserted or removed from the active list
5255
this._pendingInsertion = [];
5356
this._pendingRemoval = [];
@@ -160,7 +163,10 @@ var InputManager = new Class({
160163

161164
this.processUpDownEvents(pointer);
162165

163-
this.processMovementEvents(pointer);
166+
if (pointer.justMoved)
167+
{
168+
this.processMovementEvents(pointer);
169+
}
164170
}
165171
},
166172

@@ -171,6 +177,8 @@ var InputManager = new Class({
171177
var justOut = [];
172178
var justOver = [];
173179
var stillOver = [];
180+
var currentlyOver = this._over;
181+
var gameObject;
174182

175183
// Returns an array of objects the pointer is over
176184

@@ -186,46 +194,47 @@ var InputManager = new Class({
186194

187195
for (i = 0; i < tested.length; i++)
188196
{
189-
var item = tested[i];
197+
gameObject = tested[i];
190198

191-
if (this._over.indexOf(item) !== -1)
199+
if (currentlyOver.indexOf(gameObject) !== -1)
192200
{
193-
stillOver.push(item);
201+
stillOver.push(gameObject);
194202
}
195203
else
196204
{
197-
justOver.push(item);
205+
justOver.push(gameObject);
198206
}
199207
}
200208

201-
this._over.forEach(function (item)
209+
for (i = 0; i < currentlyOver.length; i++)
202210
{
203-
if (tested.indexOf(item) === -1)
211+
gameObject = currentlyOver[i];
212+
213+
if (tested.indexOf(gameObject) === -1)
204214
{
205-
justOut.push(item);
215+
justOut.push(gameObject);
206216
}
207-
208-
});
209-
210-
var gameObject;
217+
}
211218

212219
// Now we can process what has happened
213220
for (i = 0; i < justOut.length; i++)
214221
{
215222
gameObject = justOut[i];
216223

217-
this.events.dispatch(new InputEvent.OUT(pointer, gameObject));
218-
219-
gameObject.input.onOut(gameObject, pointer);
224+
if (gameObject.input.enabled)
225+
{
226+
this.gameObjectOnOut(pointer, gameObject);
227+
}
220228
}
221229

222230
for (i = 0; i < justOver.length; i++)
223231
{
224232
gameObject = justOver[i];
225233

226-
this.events.dispatch(new InputEvent.OVER(pointer, gameObject));
227-
228-
gameObject.input.onOver(gameObject, pointer, gameObject.input.localX, gameObject.input.localY);
234+
if (gameObject.input.enabled)
235+
{
236+
this.gameObjectOnOver(pointer, gameObject);
237+
}
229238
}
230239

231240
// Store everything that is currently over
@@ -235,45 +244,139 @@ var InputManager = new Class({
235244
// Has it been pressed down or released in this update?
236245
processUpDownEvents: function (pointer)
237246
{
238-
var i;
239247
var gameObject;
240-
var over = this._over;
241248

242-
if (pointer.justDown)
249+
for (var i = 0; i < this._over.length; i++)
243250
{
244-
for (i = 0; i < over.length; i++)
245-
{
246-
gameObject = over[i];
247-
248-
// Maybe this should contain ALL game objects it hit in one single event?
249-
this.events.dispatch(new InputEvent.DOWN(pointer, gameObject));
251+
gameObject = this._over[i];
250252

251-
gameObject.input.onDown(gameObject, pointer, gameObject.input.localX, gameObject.input.localY);
253+
if (gameObject.input.enabled)
254+
{
255+
if (pointer.justDown)
256+
{
257+
this.gameObjectOnDown(pointer, gameObject);
258+
}
259+
else
260+
{
261+
this.gameObjectOnUp(pointer, gameObject);
262+
}
252263
}
253264
}
254-
else if (pointer.justUp)
265+
},
266+
267+
// Has it been moved in this update?
268+
processMovementEvents: function (pointer)
269+
{
270+
// Check the list of Draggable Items
271+
for (var i = 0; i < this._draggable.length; i++)
255272
{
256-
for (i = 0; i < over.length; i++)
257-
{
258-
gameObject = over[i];
273+
var gameObject = this._draggable[i];
274+
var input = gameObject.input;
259275

260-
this.events.dispatch(new InputEvent.UP(pointer, gameObject));
276+
if (!input.enabled)
277+
{
278+
continue;
279+
}
261280

262-
gameObject.input.onUp(gameObject, pointer, gameObject.input.localX, gameObject.input.localY);
281+
if (pointer.justUp && input.isDragged)
282+
{
283+
// Drag End
284+
this.gameObjectOnDragEnd(pointer, gameObject);
285+
}
286+
else if (input.isDragged)
287+
{
288+
// Drag
289+
this.gameObjectOnDrag(pointer, gameObject);
263290
}
264291
}
265292
},
266293

267-
// Has it been moved in this update?
268-
processMovementEvents: function (pointer)
294+
gameObjectOnDragStart: function (pointer, gameObject)
295+
{
296+
var input = gameObject.input;
297+
298+
input.isDragged = true;
299+
300+
input.dragX = input.localX - gameObject.displayOriginX;
301+
input.dragY = input.localY - gameObject.displayOriginY;
302+
303+
this.events.dispatch(new InputEvent.DRAG_START(pointer, gameObject));
304+
305+
gameObject.input.onDragStart(gameObject, pointer);
306+
},
307+
308+
gameObjectOnDrag: function (pointer, gameObject)
269309
{
270-
if (!pointer.justMoved)
310+
this.events.dispatch(new InputEvent.DRAG(pointer, gameObject));
311+
312+
gameObject.input.onDrag(gameObject, pointer);
313+
},
314+
315+
gameObjectOnDragEnd: function (pointer, gameObject)
316+
{
317+
var input = gameObject.input;
318+
319+
input.isDragged = false;
320+
321+
this.events.dispatch(new InputEvent.DRAG_END(pointer, gameObject));
322+
323+
gameObject.input.onDragEnd(gameObject, pointer);
324+
},
325+
326+
gameObjectOnDown: function (pointer, gameObject)
327+
{
328+
var input = gameObject.input;
329+
330+
input.isDown = true;
331+
332+
this.events.dispatch(new InputEvent.DOWN(pointer, gameObject));
333+
334+
input.onDown(gameObject, pointer, input.localX, input.localY);
335+
336+
if (input.draggable && !input.isDragged)
271337
{
272-
return;
338+
// Drag Start
339+
this.gameObjectOnDragStart(pointer, gameObject);
273340
}
341+
},
342+
343+
gameObjectOnUp: function (pointer, gameObject)
344+
{
345+
gameObject.input.isDown = false;
346+
347+
this.events.dispatch(new InputEvent.UP(pointer, gameObject));
348+
349+
gameObject.input.onUp(gameObject, pointer, gameObject.input.localX, gameObject.input.localY);
350+
},
274351

275-
// Drag?
352+
gameObjectOnOut: function (pointer, gameObject)
353+
{
354+
var input = gameObject.input;
355+
356+
input.isOver = false;
357+
358+
// Don't dispatch if we're dragging the gameObject, as the pointer often gets away from it
359+
if (!input.isDragged)
360+
{
361+
this.events.dispatch(new InputEvent.OUT(pointer, gameObject));
276362

363+
input.onOut(gameObject, pointer);
364+
}
365+
},
366+
367+
gameObjectOnOver: function (pointer, gameObject)
368+
{
369+
var input = gameObject.input;
370+
371+
input.isOver = true;
372+
373+
// Don't dispatch if we're dragging the gameObject, as the pointer often gets away from it
374+
if (!input.isDragged)
375+
{
376+
this.events.dispatch(new InputEvent.OVER(pointer, gameObject));
377+
378+
input.onOver(gameObject, pointer, input.localX, input.localY);
379+
}
277380
},
278381

279382
enable: function (gameObject, shape, callback)
@@ -482,8 +585,6 @@ var InputManager = new Class({
482585
// Drag Events
483586
// https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
484587

485-
// DRAG_START, DRAG (mouse move), DRAG_END
486-
487588
setDraggable: function (gameObjects, value)
488589
{
489590
if (value === undefined) { value = true; }
@@ -495,7 +596,29 @@ var InputManager = new Class({
495596

496597
for (var i = 0; i < gameObjects.length; i++)
497598
{
498-
gameObjects[i].input.draggable = value;
599+
var gameObject = gameObjects[i];
600+
var index = this._draggable.indexOf(gameObject);
601+
602+
if (value)
603+
{
604+
// Make draggable
605+
gameObject.input.draggable = true;
606+
607+
if (index === -1)
608+
{
609+
this._draggable.push(gameObject);
610+
}
611+
}
612+
else
613+
{
614+
// Disable drag
615+
gameObject.input.draggable = false;
616+
617+
if (index === -1)
618+
{
619+
this._draggable.splice(index, 1);
620+
}
621+
}
499622
}
500623

501624
return true;
@@ -506,6 +629,7 @@ var InputManager = new Class({
506629
{
507630
this._list = [];
508631
this._over = [];
632+
this._draggable = [];
509633
this._pendingRemoval = [];
510634
this._pendingInsertion = [];
511635
},

0 commit comments

Comments
 (0)