Add mouse wheel support

This commit is contained in:
Wouter Commandeur 2014-06-29 13:45:39 +02:00
parent a0a7c02da3
commit c716709f66

View file

@ -48,6 +48,11 @@ Phaser.Mouse = function (game) {
*/
this.mouseOverCallback = null;
/**
* @property {function} mouseWheelCallback - A callback that can be fired when the mousewheel is used.
*/
this.mouseWheelCallback = null;
/**
* @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propogate fully.
*/
@ -59,6 +64,11 @@ Phaser.Mouse = function (game) {
*/
this.button = -1;
/**
* @property {number} wheelDelta - The direction of the mousewheel usage 1 for up -1 for down
*/
this.wheelDelta = 0;
/**
* @property {boolean} disabled - You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
* @default
@ -119,6 +129,12 @@ Phaser.Mouse = function (game) {
*/
this._onMouseOver = null;
/**
* @property {function} _onMouseWheel - Internal event handler reference.
* @private
*/
this._onMouseWheel = null;
};
/**
@ -187,9 +203,15 @@ Phaser.Mouse.prototype = {
return _this.onMouseOver(event);
};
this._onMouseWheel = function (event) {
return _this.onMouseWheel(event);
};
this.game.canvas.addEventListener('mousedown', this._onMouseDown, true);
this.game.canvas.addEventListener('mousemove', this._onMouseMove, true);
this.game.canvas.addEventListener('mouseup', this._onMouseUp, true);
this.game.canvas.addEventListener('mousewheel', this._onMouseWheel, true);
this.game.canvas.addEventListener('DOMMouseScroll', this._onMouseWheel, true);
if (!this.game.device.cocoonJS)
{
@ -329,6 +351,31 @@ Phaser.Mouse.prototype = {
},
/**
* The internal method that handles the mouse wheel event from the browser.
*
* @method Phaser.Mouse#onMouseWheel
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
*/
onMouseWheel: function (event) {
this.event = event;
if (this.capture)
{
event.preventDefault();
}
// reverse detail for firefox
this.wheelDelta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
if (this.mouseWheelCallback)
{
this.mouseWheelCallback.call(this.callbackContext, event);
}
},
/**
* The internal method that handles the mouse over event from the browser.
*
@ -438,6 +485,8 @@ Phaser.Mouse.prototype = {
this.game.canvas.removeEventListener('mouseup', this._onMouseUp, true);
this.game.canvas.removeEventListener('mouseover', this._onMouseOver, true);
this.game.canvas.removeEventListener('mouseout', this._onMouseOut, true);
this.game.canvas.removeEventListener('mousewheel', this._onMouseWheel, true);
this.game.canvas.removeEventListener('DOMMouseScroll', this._onMouseWheel, true);
}