Skip to content

Commit 7004d68

Browse files
committed
Mouse Scroll Events - device separation
- The wheel event type is now determined in by Device - The various input checking in Device have been moved to a new function
1 parent a0dcc61 commit 7004d68

2 files changed

Lines changed: 74 additions & 36 deletions

File tree

src/input/Mouse.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,19 @@ Phaser.Mouse.prototype = {
245245
window.addEventListener('mouseup', this._onMouseUpGlobal, true);
246246
this.game.canvas.addEventListener('mouseover', this._onMouseOver, true);
247247
this.game.canvas.addEventListener('mouseout', this._onMouseOut, true);
248+
}
248249

249-
// (These can probably be moved out of the cocoonJS check)
250-
// See https://developer.mozilla.org/en-US/docs/Web/Events/wheel
251-
if ('onwheel' in window || 'WindowEvent' in window)
252-
{
253-
// DOM3 Wheel Event: FF 17+, IE 9+, Chrome 31+, Safari 7+
254-
this.game.canvas.addEventListener('wheel', this._onMouseWheel, true);
255-
}
256-
else if ('onmousewheel' in window)
250+
var wheelEvent = this.game.device.wheelEvent;
251+
if (wheelEvent)
252+
{
253+
this.game.canvas.addEventListener(wheelEvent, this._onMouseWheel, true);
254+
255+
if (wheelEvent === 'mousewheel')
257256
{
258-
// Non-FF legacy: IE 6-9, Chrome 1-31, Safari 5-7.
259-
this.game.canvas.addEventListener('mousewheel', this._onMouseWheel, true);
260257
this._wheelEvent = new WheelEventProxy(-1/40, 1);
261258
}
262-
else if ('MouseScrollEvent' in window)
259+
else if (wheelEvent === 'DOMMouseScroll')
263260
{
264-
// FF prior to 17. This should probably be scrubbed.
265-
this.game.canvas.addEventListener('DOMMouseScroll', this._onMouseWheel, true);
266261
this._wheelEvent = new WheelEventProxy(1, 1);
267262
}
268263
}
@@ -563,9 +558,11 @@ Phaser.Mouse.prototype = {
563558
this.game.canvas.removeEventListener('mouseover', this._onMouseOver, true);
564559
this.game.canvas.removeEventListener('mouseout', this._onMouseOut, true);
565560

566-
this.game.canvas.removeEventListener('wheel', this._onMouseWheel, true);
567-
this.game.canvas.removeEventListener('mousewheel', this._onMouseWheel, true);
568-
this.game.canvas.removeEventListener('DOMMouseScroll', this._onMouseWheel, true);
561+
var wheelEvent = this.game.device.wheelEvent;
562+
if (wheelEvent)
563+
{
564+
this.game.canvas.removeEventListener(wheelEvent, this._onMouseWheel, true);
565+
}
569566

570567
window.removeEventListener('mouseup', this._onMouseUpGlobal, true);
571568

@@ -656,12 +653,12 @@ Object.defineProperties(WheelEventProxy.prototype, {
656653
"deltaMode": { get: function () { return this._deltaMode; } },
657654
"deltaY": {
658655
get: function () {
659-
return this._scaleFactor * (this.originalEvent.detail || this.originalEvent.wheelDelta || 0);
656+
return (this._scaleFactor * (this.originalEvent.wheelDelta || this.originalEvent.detail)) || 0;
660657
}
661658
},
662659
"deltaX": {
663660
get: function () {
664-
return this._scaleFactor * (this.originalEvent.wheelDeltaX || 0);
661+
return (this._scaleFactor * this.originalEvent.wheelDeltaX) || 0;
665662
}
666663
},
667664
"deltaZ": { value: 0 }

src/system/Device.js

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,6 @@ Phaser.Device = function (game) {
147147
*/
148148
this.worker = false;
149149

150-
/**
151-
* @property {boolean} touch - Is touch available?
152-
* @default
153-
*/
154-
this.touch = false;
155-
156-
/**
157-
* @property {boolean} mspointer - Is mspointer available?
158-
* @default
159-
*/
160-
this.mspointer = false;
161-
162150
/**
163151
* @property {boolean} css3D - Is css3D available?
164152
* @default
@@ -195,6 +183,27 @@ Phaser.Device = function (game) {
195183
*/
196184
this.quirksMode = false;
197185

186+
// Input
187+
188+
/**
189+
* @property {boolean} touch - Is touch available?
190+
* @default
191+
*/
192+
this.touch = false;
193+
194+
/**
195+
* @property {boolean} mspointer - Is mspointer available?
196+
* @default
197+
*/
198+
this.mspointer = false;
199+
200+
/**
201+
* @property {string|null} wheelType - The newest type of Wheel/Scroll event supported: 'wheel', 'mousewheel', 'DOMMouseScroll'
202+
* @default
203+
* @protected
204+
*/
205+
this.wheelEvent = null;
206+
198207
// Browser
199208

200209
/**
@@ -401,6 +410,7 @@ Phaser.Device = function (game) {
401410
this._checkCSS3D();
402411
this._checkDevice();
403412
this._checkFeatures();
413+
this._checkInput();
404414

405415
};
406416

@@ -500,7 +510,24 @@ Phaser.Device.prototype = {
500510

501511
this.worker = !!window['Worker'];
502512

503-
if ('ontouchstart' in document.documentElement || (window.navigator.maxTouchPoints && window.navigator.maxTouchPoints > 1))
513+
this.pointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
514+
515+
this.quirksMode = (document.compatMode === 'CSS1Compat') ? false : true;
516+
517+
this.getUserMedia = !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
518+
519+
},
520+
521+
/**
522+
* Checks/configures various input.
523+
*
524+
* @method Phaser.Device#checkInput
525+
* @private
526+
*/
527+
_checkInput: function () {
528+
529+
if ('ontouchstart' in document.documentElement ||
530+
(window.navigator.maxTouchPoints && window.navigator.maxTouchPoints > 1))
504531
{
505532
this.touch = true;
506533
}
@@ -510,11 +537,25 @@ Phaser.Device.prototype = {
510537
this.mspointer = true;
511538
}
512539

513-
this.pointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
514-
515-
this.quirksMode = (document.compatMode === 'CSS1Compat') ? false : true;
516-
517-
this.getUserMedia = !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
540+
if (!this.cocoonJS)
541+
{
542+
// See https://developer.mozilla.org/en-US/docs/Web/Events/wheel
543+
if ('onwheel' in window || (this.ie && 'WheelEvent' in window))
544+
{
545+
// DOM3 Wheel Event: FF 17+, IE 9+, Chrome 31+, Safari 7+
546+
this.wheelEvent = 'wheel';
547+
}
548+
else if ('onmousewheel' in window)
549+
{
550+
// Non-FF legacy: IE 6-9, Chrome 1-31, Safari 5-7.
551+
this.wheelEvent = 'mousewheel';
552+
}
553+
else if (this.firefox && 'MouseScrollEvent' in window)
554+
{
555+
// FF prior to 17. This should probably be scrubbed.
556+
this.wheelEvent = 'DOMMouseScroll';
557+
}
558+
}
518559

519560
},
520561

0 commit comments

Comments
 (0)