Skip to content

Commit 98be6ad

Browse files
committed
Native support for wheel events
1 parent 62cb5c6 commit 98be6ad

3 files changed

Lines changed: 108 additions & 4 deletions

File tree

src/input/InputManager.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,22 @@ var InputManager = new Class({
756756
this.updateInputPlugins(CONST.MOUSE_UP, this.mousePointerContainer);
757757
},
758758

759+
/**
760+
* Processes a mouse wheel event, as passed in by the MouseManager.
761+
*
762+
* @method Phaser.Input.InputManager#onMouseWheel
763+
* @private
764+
* @since 3.18.0
765+
*
766+
* @param {WheelEvent} event - The native DOM Wheel event.
767+
*/
768+
onMouseWheel: function (event)
769+
{
770+
this.mousePointer.wheel(event);
771+
772+
this.updateInputPlugins(CONST.MOUSE_WHEEL, this.mousePointerContainer);
773+
},
774+
759775
/**
760776
* Checks if the given Game Object should be considered as a candidate for input or not.
761777
*

src/input/InputPlugin.js

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,7 @@ var InputPlugin = new Class({
609609
}
610610
}
611611

612-
if (pointersTotal < 3 || !pointer.wasTouch)
613-
{
614-
total += this.processOverOutEvents(pointer);
615-
}
612+
total += this.processOverOutEvents(pointer);
616613

617614
this.processDragThresholdEvent(pointer);
618615

@@ -711,6 +708,10 @@ var InputPlugin = new Class({
711708
total += this.processMoveEvents(pointer);
712709
total += this.processOverOutEvents(pointer);
713710
break;
711+
712+
case CONST.MOUSE_WHEEL:
713+
total += this.processWheelEvent(pointer);
714+
break;
714715
}
715716

716717
if (total > 0)
@@ -1433,6 +1434,73 @@ var InputPlugin = new Class({
14331434
return total;
14341435
},
14351436

1437+
/**
1438+
* An internal method that handles a mouse wheel event.
1439+
*
1440+
* @method Phaser.Input.InputPlugin#processWheelEvent
1441+
* @private
1442+
* @fires Phaser.Input.Events#GAMEOBJECT_POINTER_WHEEL
1443+
* @fires Phaser.Input.Events#GAMEOBJECT_WHEEL
1444+
* @fires Phaser.Input.Events#POINTER_WHEEL
1445+
* @since 3.18.0
1446+
*
1447+
* @param {Phaser.Input.Pointer} pointer - The pointer to check for events against.
1448+
*
1449+
* @return {integer} The total number of objects interacted with.
1450+
*/
1451+
processWheelEvent: function (pointer)
1452+
{
1453+
var total = 0;
1454+
var currentlyOver = this._temp;
1455+
1456+
var _eventData = this._eventData;
1457+
var _eventContainer = this._eventContainer;
1458+
1459+
_eventData.cancelled = false;
1460+
1461+
var aborted = false;
1462+
1463+
var dx = pointer.deltaX;
1464+
var dy = pointer.deltaY;
1465+
var dz = pointer.deltaZ;
1466+
1467+
// Go through all objects the pointer was over and fire their events / callbacks
1468+
for (var i = 0; i < currentlyOver.length; i++)
1469+
{
1470+
var gameObject = currentlyOver[i];
1471+
1472+
if (!gameObject.input)
1473+
{
1474+
continue;
1475+
}
1476+
1477+
total++;
1478+
1479+
gameObject.emit(Events.GAMEOBJECT_POINTER_WHEEL, pointer, dx, dy, dz, _eventContainer);
1480+
1481+
if (_eventData.cancelled || !gameObject.input)
1482+
{
1483+
aborted = true;
1484+
break;
1485+
}
1486+
1487+
this.emit(Events.GAMEOBJECT_WHEEL, pointer, gameObject, dx, dy, dz, _eventContainer);
1488+
1489+
if (_eventData.cancelled || !gameObject.input)
1490+
{
1491+
aborted = true;
1492+
break;
1493+
}
1494+
}
1495+
1496+
if (!aborted)
1497+
{
1498+
this.emit(Events.POINTER_WHEEL, pointer, currentlyOver, dx, dy, dz);
1499+
}
1500+
1501+
return total;
1502+
},
1503+
14361504
/**
14371505
* An internal method that handles the Pointer over events.
14381506
* This is called when a touch input hits the canvas, having previously been off of it.

src/input/mouse/MouseManager.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ var MouseManager = new Class({
160160
*/
161161
this.onMouseOut = NOOP;
162162

163+
/**
164+
* The Mouse Wheel Event handler.
165+
* This function is sent the native DOM MouseEvent.
166+
* Initially empty and bound in the `startListeners` method.
167+
*
168+
* @name Phaser.Input.Mouse.MouseManager#onMouseWheel
169+
* @type {function}
170+
* @since 3.18.0
171+
*/
172+
this.onMouseWheel = NOOP;
173+
163174
/**
164175
* Internal pointerLockChange handler.
165176
* This function is sent the native DOM MouseEvent.
@@ -364,6 +375,14 @@ var MouseManager = new Class({
364375
}
365376
};
366377

378+
this.onMouseWheel = function (event)
379+
{
380+
if (!event.defaultPrevented && _this.enabled && _this.manager && _this.manager.enabled)
381+
{
382+
_this.manager.onMouseWheel(event);
383+
}
384+
};
385+
367386
var target = this.target;
368387

369388
if (!target)
@@ -379,6 +398,7 @@ var MouseManager = new Class({
379398
target.addEventListener('mouseup', this.onMouseUp, (this.capture) ? nonPassive : passive);
380399
target.addEventListener('mouseover', this.onMouseOver, (this.capture) ? nonPassive : passive);
381400
target.addEventListener('mouseout', this.onMouseOut, (this.capture) ? nonPassive : passive);
401+
target.addEventListener('wheel', this.onMouseWheel, (this.capture) ? nonPassive : passive);
382402

383403
if (window && this.manager.game.config.inputWindowEvents)
384404
{

0 commit comments

Comments
 (0)