Skip to content

Commit 5d8a11a

Browse files
committed
Input.addMoveCallback allows you to bind as many callbacks as you like to the DOM move events (Input.setMoveCallback is now flagged as deprecated)
Input.deleteMoveCallback will remove a previously set movement event callback.
1 parent b90bcc4 commit 5d8a11a

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Version 2.0.5 - "Tanchico" - in development
9292
* Graphics.drawTriangles will draw an array of vertices to the Graphics object (thanks @codevinsky, #795)
9393
* Polygon.area will calculate the area of the Polygon (thanks @codevinsky, #795)
9494
* The Tiled JSON parser will now include Tiled polygons, ellipse and rectangle geometry objects in the resulting map data (thanks @tigermonkey, #791)
95+
* Input.addMoveCallback allows you to bind as many callbacks as you like to the DOM move events (Input.setMoveCallback is now flagged as deprecated)
96+
* Input.deleteMoveCallback will remove a previously set movement event callback.
9597

9698

9799
### New Plugins

src/input/Input.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ Phaser.Input = function (game) {
3131
*/
3232
this.hitContext = null;
3333

34+
/**
35+
* @property {array} moveCallbacks - An array of callbacks that will be fired every time the activePointer receives a move event from the DOM.
36+
*/
37+
this.moveCallbacks = [];
38+
3439
/**
3540
* @property {function} moveCallback - An optional callback that will be fired every time the activePointer receives a move event from the DOM. Set to null to disable.
3641
*/
@@ -390,14 +395,19 @@ Phaser.Input.prototype = {
390395
this.gamepad.stop();
391396
// this.gestures.stop();
392397

398+
this.moveCallbacks = [];
399+
// DEPRECATED
393400
this.moveCallback = null;
394401

395402
},
396403

397404
/**
405+
* DEPRECATED: This method will be removed in a future major point release. Please use Input.addMoveCallback instead.
406+
*
398407
* Sets a callback that is fired every time the activePointer receives a DOM move event such as a mousemove or touchmove.
399408
* It will be called every time the activePointer moves, which in a multi-touch game can be a lot of times, so this is best
400409
* to only use if you've limited input to a single pointer (i.e. mouse or touch)
410+
*
401411
* @method Phaser.Input#setMoveCallback
402412
* @param {function} callback - The callback that will be called each time the activePointer receives a DOM move event.
403413
* @param {object} callbackContext - The context in which the callback will be called.
@@ -409,6 +419,41 @@ Phaser.Input.prototype = {
409419

410420
},
411421

422+
/**
423+
* Adds a callback that is fired every time the activePointer receives a DOM move event such as a mousemove or touchmove.
424+
* It will be called every time the activePointer moves, which in a multi-touch game can be a lot of times, so this is best
425+
* to only use if you've limited input to a single pointer (i.e. mouse or touch).
426+
* The callback is added to the Phaser.Input.moveCallbacks array and should be removed with Phaser.Input.deleteMoveCallback.
427+
*
428+
* @method Phaser.Input#addMoveCallback
429+
* @param {function} callback - The callback that will be called each time the activePointer receives a DOM move event.
430+
* @param {object} callbackContext - The context in which the callback will be called.
431+
* @return {number} The index of the callback entry. Use this index when calling Input.deleteMoveCallback.
432+
*/
433+
addMoveCallback: function (callback, callbackContext) {
434+
435+
return this.moveCallbacks.push( { callback: callback, context: callbackContext }) - 1;
436+
437+
},
438+
439+
/**
440+
* Adds a callback that is fired every time the activePointer receives a DOM move event such as a mousemove or touchmove.
441+
* It will be called every time the activePointer moves, which in a multi-touch game can be a lot of times, so this is best
442+
* to only use if you've limited input to a single pointer (i.e. mouse or touch).
443+
* The callback is added to the Phaser.Input.moveCallbacks array and should be removed with Phaser.Input.deleteMoveCallback.
444+
*
445+
* @method Phaser.Input#deleteMoveCallback
446+
* @param {number} index - The index of the callback to remove.
447+
*/
448+
deleteMoveCallback: function (index) {
449+
450+
if (this.moveCallbacks[index])
451+
{
452+
this.moveCallbacks.splice(index, 1);
453+
}
454+
455+
},
456+
412457
/**
413458
* Add a new Pointer object to the Input Manager. By default Input creates 3 pointer objects: mousePointer, pointer1 and pointer2.
414459
* If you need more then use this to create a new one, up to a maximum of 10.
@@ -527,6 +572,7 @@ Phaser.Input.prototype = {
527572
this.onUp = new Phaser.Signal();
528573
this.onTap = new Phaser.Signal();
529574
this.onHold = new Phaser.Signal();
575+
this.moveCallbacks = [];
530576
}
531577

532578
this._pollCounter = 0;

src/input/Pointer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,19 @@ Phaser.Pointer.prototype = {
384384
return this;
385385
}
386386

387+
// DEPRECATED - Soon to be removed
387388
if (this.game.input.moveCallback)
388389
{
389390
this.game.input.moveCallback.call(this.game.input.moveCallbackContext, this, this.x, this.y);
390391
}
391392

393+
var i = this.game.input.moveCallbacks.length;
394+
395+
while (i--)
396+
{
397+
this.game.input.moveCallbacks[i].callback.call(this.game.input.moveCallbacks[i].context, this, this.x, this.y);
398+
}
399+
392400
// Easy out if we're dragging something and it still exists
393401
if (this.targetObject !== null && this.targetObject.isDragged === true)
394402
{

0 commit comments

Comments
 (0)