Merge pull request #959 from woutercommandeur/dev

Add mouse wheel support
This commit is contained in:
Richard Davey 2014-07-01 15:33:34 +01:00
commit 3c191e4cea

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;
};
/**
@ -145,6 +161,18 @@ Phaser.Mouse.MIDDLE_BUTTON = 1;
*/
Phaser.Mouse.RIGHT_BUTTON = 2;
/**
* @constant
* @type {number}
*/
Phaser.Mouse.WHEEL_UP = 1;
/**
* @constant
* @type {number}
*/
Phaser.Mouse.WHEEL_DOWN = -1;
Phaser.Mouse.prototype = {
/**
@ -187,9 +215,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 +363,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 +497,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);
}