Phaser.Mouse = function (game){ this.game = game; this.input = game.input; this.callbackContext = this.game; this.mouseDownCallback = null ; this.mouseUpCallback = null ; this.mouseOutCallback = null ; this.mouseOverCallback = null ; this.mouseWheelCallback = null ; this.capture = false ; this.button = -1; this.wheelDelta = 0; this.enabled = true ; this.locked = false ; this.stopOnGameOut = false ; this.pointerLock = new Phaser.Signal(); this.event = null ; this._onMouseDown = null ; this._onMouseMove = null ; this._onMouseUp = null ; this._onMouseOut = null ; this._onMouseOver = null ; this._onMouseWheel = null ; this._wheelEvent = null ; } ; Phaser.Mouse.NO_BUTTON = -1; Phaser.Mouse.LEFT_BUTTON = 0; Phaser.Mouse.MIDDLE_BUTTON = 1; Phaser.Mouse.RIGHT_BUTTON = 2; Phaser.Mouse.BACK_BUTTON = 3; Phaser.Mouse.FORWARD_BUTTON = 4; Phaser.Mouse.WHEEL_UP = 1; Phaser.Mouse.WHEEL_DOWN = -1; Phaser.Mouse.prototype = { start: function (){ if (this.game.device.android && this.game.device.chrome === false ) { return ; } if (this._onMouseDown !== null ) { return ; } var _this = this; this._onMouseDown = function (event){ return _this.onMouseDown(event); } ; this._onMouseMove = function (event){ return _this.onMouseMove(event); } ; this._onMouseUp = function (event){ return _this.onMouseUp(event); } ; this._onMouseUpGlobal = function (event){ return _this.onMouseUpGlobal(event); } ; this._onMouseOut = function (event){ return _this.onMouseOut(event); } ; this._onMouseOver = function (event){ return _this.onMouseOver(event); } ; this._onMouseWheel = function (event){ return _this.onMouseWheel(event); } ; var canvas = this.game.canvas; canvas.addEventListener('mousedown', this._onMouseDown, true ); canvas.addEventListener('mousemove', this._onMouseMove, true ); canvas.addEventListener('mouseup', this._onMouseUp, true ); if (!this.game.device.cocoonJS) { window.addEventListener('mouseup', this._onMouseUpGlobal, true ); canvas.addEventListener('mouseover', this._onMouseOver, true ); canvas.addEventListener('mouseout', this._onMouseOut, true ); } var wheelEvent = this.game.device.wheelEvent; if (wheelEvent) { canvas.addEventListener(wheelEvent, this._onMouseWheel, true ); if (wheelEvent === 'mousewheel') { this._wheelEvent = new WheelEventProxy(-1 / 40, 1); } else if (wheelEvent === 'DOMMouseScroll') { this._wheelEvent = new WheelEventProxy(1, 1); } } } , onMouseDown: function (event){ this.event = event; if (this.capture) { event.preventDefault(); } if (this.mouseDownCallback) { this.mouseDownCallback.call(this.callbackContext, event); } if (!this.input.enabled || !this.enabled) { return ; } event.identifier = 0; this.input.mousePointer.start(event); } , onMouseMove: function (event){ this.event = event; if (this.capture) { event.preventDefault(); } if (this.mouseMoveCallback) { this.mouseMoveCallback.call(this.callbackContext, event); } if (!this.input.enabled || !this.enabled) { return ; } event.identifier = 0; this.input.mousePointer.move(event); } , onMouseUp: function (event){ this.event = event; if (this.capture) { event.preventDefault(); } if (this.mouseUpCallback) { this.mouseUpCallback.call(this.callbackContext, event); } if (!this.input.enabled || !this.enabled) { return ; } event.identifier = 0; this.input.mousePointer.stop(event); } , onMouseUpGlobal: function (event){ if (!this.input.mousePointer.withinGame) { if (this.mouseUpCallback) { this.mouseUpCallback.call(this.callbackContext, event); } event.identifier = 0; this.input.mousePointer.stop(event); } } , onMouseOut: function (event){ this.event = event; if (this.capture) { event.preventDefault(); } this.input.mousePointer.withinGame = false ; if (this.mouseOutCallback) { this.mouseOutCallback.call(this.callbackContext, event); } if (!this.input.enabled || !this.enabled) { return ; } if (this.stopOnGameOut) { event.identifier = 0; this.input.mousePointer.stop(event); } } , onMouseOver: function (event){ this.event = event; if (this.capture) { event.preventDefault(); } this.input.mousePointer.withinGame = true ; if (this.mouseOverCallback) { this.mouseOverCallback.call(this.callbackContext, event); } } , onMouseWheel: function (event){ if (this._wheelEvent) { event = this._wheelEvent.bindEvent(event); } this.event = event; if (this.capture) { event.preventDefault(); } this.wheelDelta = Phaser.Math.clamp(- event.deltaY, -1, 1); if (this.mouseWheelCallback) { this.mouseWheelCallback.call(this.callbackContext, event); } } , requestPointerLock: function (){ if (this.game.device.pointerLock) { var element = this.game.canvas; element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock; element.requestPointerLock(); var _this = this; this._pointerLockChange = function (event){ return _this.pointerLockChange(event); } ; document.addEventListener('pointerlockchange', this._pointerLockChange, true ); document.addEventListener('mozpointerlockchange', this._pointerLockChange, true ); document.addEventListener('webkitpointerlockchange', this._pointerLockChange, true ); } } , pointerLockChange: function (event){ var element = this.game.canvas; if (document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element) { this.locked = true ; this.pointerLock.dispatch(true , event); } else { this.locked = false ; this.pointerLock.dispatch(false , event); } } , releasePointerLock: function (){ document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock; document.exitPointerLock(); document.removeEventListener('pointerlockchange', this._pointerLockChange, true ); document.removeEventListener('mozpointerlockchange', this._pointerLockChange, true ); document.removeEventListener('webkitpointerlockchange', this._pointerLockChange, true ); } , stop: function (){ var canvas = this.game.canvas; canvas.removeEventListener('mousedown', this._onMouseDown, true ); canvas.removeEventListener('mousemove', this._onMouseMove, true ); canvas.removeEventListener('mouseup', this._onMouseUp, true ); canvas.removeEventListener('mouseover', this._onMouseOver, true ); canvas.removeEventListener('mouseout', this._onMouseOut, true ); var wheelEvent = this.game.device.wheelEvent; if (wheelEvent) { canvas.removeEventListener(wheelEvent, this._onMouseWheel, true ); } window.removeEventListener('mouseup', this._onMouseUpGlobal, true ); document.removeEventListener('pointerlockchange', this._pointerLockChange, true ); document.removeEventListener('mozpointerlockchange', this._pointerLockChange, true ); document.removeEventListener('webkitpointerlockchange', this._pointerLockChange, true ); } } ; Phaser.Mouse.prototype.constructor = Phaser.Mouse; function WheelEventProxy(scaleFactor, deltaMode){ this._scaleFactor = scaleFactor; this._deltaMode = deltaMode; this.originalEvent = null ; } WheelEventProxy.prototype = { } ; WheelEventProxy.prototype.constructor = WheelEventProxy; WheelEventProxy.prototype.bindEvent = function (event){ if (!WheelEventProxy._stubsGenerated && event) { var makeBinder = function (name){ return function (){ var v = this.originalEvent[name]; return typeof v !== 'function'? v: v.bind(this.originalEvent); } ; } ; for (var prop in event){ if (!(prop in WheelEventProxy.prototype)) { Object.defineProperty(WheelEventProxy.prototype, prop, { get: makeBinder(prop)} ); } } WheelEventProxy._stubsGenerated = true ; } this.originalEvent = event; return this; } ; Object.defineProperties(WheelEventProxy.prototype, { "type": { value: "wheel"} , "deltaMode": { get: function (){ return this._deltaMode; } } , "deltaY": { get: function (){ return (this._scaleFactor * (this.originalEvent.wheelDelta || this.originalEvent.detail)) || 0; } } , "deltaX": { get: function (){ return (this._scaleFactor * this.originalEvent.wheelDeltaX) || 0; } } , "deltaZ": { value: 0} } );