@@ -68,7 +68,7 @@ Phaser.Mouse = function (game) {
68
68
this . button = - 1 ;
69
69
70
70
/**
71
- * @property {number } wheelDelta - The direction of the mousewheel usage 1 for up -1 for down
71
+ * @property {number } wheelDelta - The direction of the _last_ mousewheel usage 1 for up -1 for down
72
72
*/
73
73
this . wheelDelta = 0 ;
74
74
@@ -98,7 +98,9 @@ Phaser.Mouse = function (game) {
98
98
this . pointerLock = new Phaser . Signal ( ) ;
99
99
100
100
/**
101
- * @property {MouseEvent } event - The browser mouse DOM event. Will be set to null if no mouse event has ever been received.
101
+ * The browser mouse DOM event. Will be null if no mouse event has ever been received.
102
+ * Access this property only inside a Mouse event handler and do not keep references to it.
103
+ * @property {MouseEvent|null } event
102
104
* @default
103
105
*/
104
106
this . event = null ;
@@ -134,11 +136,18 @@ Phaser.Mouse = function (game) {
134
136
this . _onMouseOver = null ;
135
137
136
138
/**
137
- * @property {function } _onMouseWheel - Internal event handler reference.
138
- * @private
139
- */
139
+ * @property {function } _onMouseWheel - Internal event handler reference.
140
+ * @private
141
+ */
140
142
this . _onMouseWheel = null ;
141
143
144
+ /**
145
+ * Wheel proxy event object, if required. Shared for all wheel events for this mouse.
146
+ * @property {Phaser.Mouse~WheelEventProxy } _wheelEvent
147
+ * @private
148
+ */
149
+ this . _wheelEvent = null ;
150
+
142
151
} ;
143
152
144
153
/**
@@ -236,8 +245,21 @@ Phaser.Mouse.prototype = {
236
245
window . addEventListener ( 'mouseup' , this . _onMouseUpGlobal , true ) ;
237
246
this . game . canvas . addEventListener ( 'mouseover' , this . _onMouseOver , true ) ;
238
247
this . game . canvas . addEventListener ( 'mouseout' , this . _onMouseOut , true ) ;
239
- this . game . canvas . addEventListener ( 'mousewheel' , this . _onMouseWheel , true ) ;
240
- this . game . canvas . addEventListener ( 'DOMMouseScroll' , this . _onMouseWheel , true ) ;
248
+ }
249
+
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' )
256
+ {
257
+ this . _wheelEvent = new WheelEventProxy ( - 1 / 40 , 1 ) ;
258
+ }
259
+ else if ( wheelEvent === 'DOMMouseScroll' )
260
+ {
261
+ this . _wheelEvent = new WheelEventProxy ( 1 , 1 ) ;
262
+ }
241
263
}
242
264
243
265
} ,
@@ -400,10 +422,14 @@ Phaser.Mouse.prototype = {
400
422
* The internal method that handles the mouse wheel event from the browser.
401
423
*
402
424
* @method Phaser.Mouse#onMouseWheel
403
- * @param {MouseEvent } event - The native event from the browser. This gets stored in Mouse.event.
425
+ * @param {MouseEvent } event - The native event from the browser.
404
426
*/
405
427
onMouseWheel : function ( event ) {
406
428
429
+ if ( this . _wheelEvent ) {
430
+ event = this . _wheelEvent . bindEvent ( event ) ;
431
+ }
432
+
407
433
this . event = event ;
408
434
409
435
if ( this . capture )
@@ -412,7 +438,7 @@ Phaser.Mouse.prototype = {
412
438
}
413
439
414
440
// reverse detail for firefox
415
- this . wheelDelta = Math . max ( - 1 , Math . min ( 1 , ( event . wheelDelta || - event . detail ) ) ) ;
441
+ this . wheelDelta = Phaser . Math . clamp ( - event . deltaY , - 1 , 1 ) ;
416
442
417
443
if ( this . mouseWheelCallback )
418
444
{
@@ -531,8 +557,12 @@ Phaser.Mouse.prototype = {
531
557
this . game . canvas . removeEventListener ( 'mouseup' , this . _onMouseUp , true ) ;
532
558
this . game . canvas . removeEventListener ( 'mouseover' , this . _onMouseOver , true ) ;
533
559
this . game . canvas . removeEventListener ( 'mouseout' , this . _onMouseOut , true ) ;
534
- this . game . canvas . removeEventListener ( 'mousewheel' , this . _onMouseWheel , true ) ;
535
- this . game . canvas . removeEventListener ( 'DOMMouseScroll' , this . _onMouseWheel , true ) ;
560
+
561
+ var wheelEvent = this . game . device . wheelEvent ;
562
+ if ( wheelEvent )
563
+ {
564
+ this . game . canvas . removeEventListener ( wheelEvent , this . _onMouseWheel , true ) ;
565
+ }
536
566
537
567
window . removeEventListener ( 'mouseup' , this . _onMouseUpGlobal , true ) ;
538
568
@@ -563,3 +593,73 @@ Object.defineProperty(Phaser.Mouse.prototype, "disabled", {
563
593
}
564
594
565
595
} ) ;
596
+
597
+ /* jshint latedef:nofunc */
598
+ /**
599
+ * A purely internal event support class to proxy 'wheelscroll' and 'DOMMouseWheel'
600
+ * events to 'wheel'-like events.
601
+ *
602
+ * See https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel for choosing a scale and delta mode.
603
+ *
604
+ * @class Phaser.Mouse~WheelEventProxy
605
+ * @private
606
+ * @param {number } scaleFactor - Scale factor as applied to wheelDelta/wheelDeltaX or details.
607
+ * @param {integer } deltaMode - The reported delta mode.
608
+ */
609
+ function WheelEventProxy ( scaleFactor , deltaMode ) {
610
+
611
+ this . _scaleFactor = scaleFactor ;
612
+
613
+ this . _deltaMode = deltaMode ;
614
+
615
+ /**
616
+ * The original event _currently_ being proxied; the getters will follow suit.
617
+ */
618
+ this . originalEvent = null ;
619
+ }
620
+
621
+ WheelEventProxy . prototype = { } ;
622
+ WheelEventProxy . prototype . constructor = WheelEventProxy ;
623
+
624
+ WheelEventProxy . prototype . bindEvent = function ( event ) {
625
+
626
+ // Generate stubs automatically
627
+ if ( ! WheelEventProxy . _stubsGenerated && event )
628
+ {
629
+ var makeBinder = function ( name ) {
630
+ return function ( ) {
631
+ var v = this . originalEvent [ name ] ;
632
+ return typeof v !== 'function' ? v : v . bind ( this . originalEvent ) ;
633
+ } ;
634
+ } ;
635
+ for ( var prop in event ) {
636
+ if ( ! ( prop in WheelEventProxy . prototype ) )
637
+ {
638
+ Object . defineProperty ( WheelEventProxy . prototype , prop , {
639
+ get : makeBinder ( prop )
640
+ } ) ;
641
+ }
642
+ }
643
+ WheelEventProxy . _stubsGenerated = true ;
644
+ }
645
+
646
+ this . originalEvent = event ;
647
+ return this ;
648
+
649
+ } ;
650
+
651
+ Object . defineProperties ( WheelEventProxy . prototype , {
652
+ "type" : { value : "wheel" } ,
653
+ "deltaMode" : { get : function ( ) { return this . _deltaMode ; } } ,
654
+ "deltaY" : {
655
+ get : function ( ) {
656
+ return ( this . _scaleFactor * ( this . originalEvent . wheelDelta || this . originalEvent . detail ) ) || 0 ;
657
+ }
658
+ } ,
659
+ "deltaX" : {
660
+ get : function ( ) {
661
+ return ( this . _scaleFactor * this . originalEvent . wheelDeltaX ) || 0 ;
662
+ }
663
+ } ,
664
+ "deltaZ" : { value : 0 }
665
+ } ) ;
0 commit comments