Phaser.Input = function (game){ this.game = game; this.hitCanvas = null ; this.hitContext = null ; this.moveCallbacks = [] ; this.pollRate = 0; this.enabled = true ; this.multiInputOverride = Phaser.Input.MOUSE_TOUCH_COMBINE; this.position = null ; this.speed = null ; this.circle = null ; this.scale = null ; this.maxPointers = -1; this.tapRate = 200; this.doubleTapRate = 300; this.holdRate = 2000; this.justPressedRate = 200; this.justReleasedRate = 200; this.recordPointerHistory = false ; this.recordRate = 100; this.recordLimit = 100; this.pointer1 = null ; this.pointer2 = null ; this.pointer3 = null ; this.pointer4 = null ; this.pointer5 = null ; this.pointer6 = null ; this.pointer7 = null ; this.pointer8 = null ; this.pointer9 = null ; this.pointer10 = null ; this.pointers = [] ; this.activePointer = null ; this.mousePointer = null ; this.mouse = null ; this.keyboard = null ; this.touch = null ; this.mspointer = null ; this.gamepad = null ; this.resetLocked = false ; this.onDown = null ; this.onUp = null ; this.onTap = null ; this.onHold = null ; this.minPriorityID = 0; this.interactiveItems = new Phaser.ArraySet(); this._localPoint = new Phaser.Point(); this._pollCounter = 0; this._oldPosition = null ; this._x = 0; this._y = 0; } ; Phaser.Input.MOUSE_OVERRIDES_TOUCH = 0; Phaser.Input.TOUCH_OVERRIDES_MOUSE = 1; Phaser.Input.MOUSE_TOUCH_COMBINE = 2; Phaser.Input.MAX_POINTERS = 10; Phaser.Input.prototype = { boot: function (){ this.mousePointer = new Phaser.Pointer(this.game, 0); this.addPointer(); this.addPointer(); this.mouse = new Phaser.Mouse(this.game); this.touch = new Phaser.Touch(this.game); this.mspointer = new Phaser.MSPointer(this.game); if (Phaser.Keyboard) { this.keyboard = new Phaser.Keyboard(this.game); } if (Phaser.Gamepad) { this.gamepad = new Phaser.Gamepad(this.game); } this.onDown = new Phaser.Signal(); this.onUp = new Phaser.Signal(); this.onTap = new Phaser.Signal(); this.onHold = new Phaser.Signal(); this.scale = new Phaser.Point(1, 1); this.speed = new Phaser.Point(); this.position = new Phaser.Point(); this._oldPosition = new Phaser.Point(); this.circle = new Phaser.Circle(0, 0, 44); this.activePointer = this.mousePointer; this.hitCanvas = _AN_Call_createelement('createElement', document, 'canvas'); this.hitCanvas.width = 1; this.hitCanvas.height = 1; this.hitContext = this.hitCanvas.getContext('2d'); this.mouse.start(); this.touch.start(); this.mspointer.start(); this.mousePointer.active = true ; if (this.keyboard) { this.keyboard.start(); } var _this = this; this._onClickTrampoline = function (event){ _this.onClickTrampoline(event); } ; this.game.canvas.addEventListener('click', this._onClickTrampoline, false ); } , destroy: function (){ this.mouse.stop(); this.touch.stop(); this.mspointer.stop(); if (this.keyboard) { this.keyboard.stop(); } if (this.gamepad) { this.gamepad.stop(); } this.moveCallbacks = [] ; this.game.canvas.removeEventListener('click', this._onClickTrampoline); } , addMoveCallback: function (callback, context){ this.moveCallbacks.push({ callback: callback, context: context} ); } , deleteMoveCallback: function (callback, context){ var i = _AN_Read_length('length', this.moveCallbacks); while (i-- ){ if (this.moveCallbacks[i].callback === callback && this.moveCallbacks[i].context === context) { this.moveCallbacks.splice(i, 1); return ; } } } , addPointer: function (){ if (_AN_Read_length('length', this.pointers) >= Phaser.Input.MAX_POINTERS) { console.warn("Phaser.Input.addPointer: Maximum limit of " + Phaser.Input.MAX_POINTERS + " pointers reached."); return null ; } var id = _AN_Read_length("length", this.pointers) + 1; var pointer = new Phaser.Pointer(this.game, id); this.pointers.push(pointer); this['pointer' + id] = pointer; return pointer; } , update: function (){ if (this.keyboard) { this.keyboard.update(); } if (this.pollRate > 0 && this._pollCounter < this.pollRate) { this._pollCounter++ ; return ; } this.speed.x = this.position.x - this._oldPosition.x; this.speed.y = this.position.y - this._oldPosition.y; this._oldPosition.copyFrom(this.position); this.mousePointer.update(); if (this.gamepad && this.gamepad.active) { this.gamepad.update(); } for (var i = 0; i < _AN_Read_length('length', this.pointers); i++ ){ this.pointers[i].update(); } this._pollCounter = 0; } , reset: function (hard){ if (!this.game.isBooted || this.resetLocked) { return ; } if (hard === undefined) { hard = false ; } this.mousePointer.reset(); if (this.keyboard) { this.keyboard.reset(hard); } if (this.gamepad) { this.gamepad.reset(); } for (var i = 0; i < _AN_Read_length('length', this.pointers); i++ ){ this.pointers[i].reset(); } if (this.game.canvas.style.cursor !== 'none') { _AN_Write_cursor('cursor', this.game.canvas.style, false , 'inherit'); } if (hard) { this.onDown.dispose(); this.onUp.dispose(); this.onTap.dispose(); this.onHold.dispose(); this.onDown = new Phaser.Signal(); this.onUp = new Phaser.Signal(); this.onTap = new Phaser.Signal(); this.onHold = new Phaser.Signal(); this.moveCallbacks = [] ; } this._pollCounter = 0; } , resetSpeed: function (x, y){ this._oldPosition.setTo(x, y); this.speed.setTo(0, 0); } , startPointer: function (event){ if (this.maxPointers >= 0 && this.countActivePointers(this.maxPointers) >= this.maxPointers) { return null ; } if (!this.pointer1.active) { return this.pointer1.start(event); } if (!this.pointer2.active) { return this.pointer2.start(event); } for (var i = 2; i < _AN_Read_length('length', this.pointers); i++ ){ var pointer = this.pointers[i]; if (!pointer.active) { return pointer.start(event); } } return null ; } , updatePointer: function (event){ if (this.pointer1.active && this.pointer1.identifier === event.identifier) { return this.pointer1.move(event); } if (this.pointer2.active && this.pointer2.identifier === event.identifier) { return this.pointer2.move(event); } for (var i = 2; i < _AN_Read_length('length', this.pointers); i++ ){ var pointer = this.pointers[i]; if (pointer.active && pointer.identifier === event.identifier) { return pointer.move(event); } } return null ; } , stopPointer: function (event){ if (this.pointer1.active && this.pointer1.identifier === event.identifier) { return this.pointer1.stop(event); } if (this.pointer2.active && this.pointer2.identifier === event.identifier) { return this.pointer2.stop(event); } for (var i = 2; i < _AN_Read_length('length', this.pointers); i++ ){ var pointer = this.pointers[i]; if (pointer.active && pointer.identifier === event.identifier) { return pointer.stop(event); } } return null ; } , countActivePointers: function (limit){ if (limit === undefined) { limit = _AN_Read_length('length', this.pointers); } var count = limit; for (var i = 0; i < _AN_Read_length('length', this.pointers) && count > 0; i++ ){ var pointer = this.pointers[i]; if (pointer.active) { count-- ; } } return (limit - count); } , getPointer: function (isActive){ if (isActive === undefined) { isActive = false ; } for (var i = 0; i < _AN_Read_length('length', this.pointers); i++ ){ var pointer = this.pointers[i]; if (pointer.active === isActive) { return pointer; } } return null ; } , getPointerFromIdentifier: function (identifier){ for (var i = 0; i < _AN_Read_length('length', this.pointers); i++ ){ var pointer = this.pointers[i]; if (pointer.identifier === identifier) { return pointer; } } return null ; } , getPointerFromId: function (pointerId){ for (var i = 0; i < _AN_Read_length('length', this.pointers); i++ ){ var pointer = this.pointers[i]; if (pointer.pointerId === pointerId) { return pointer; } } return null ; } , getLocalPosition: function (displayObject, pointer, output){ if (output === undefined) { output = new Phaser.Point(); } var wt = displayObject.worldTransform; var id = 1 / (wt.a * wt.d + wt.c * - wt.b); return output.setTo(wt.d * id * pointer.x + - wt.c * id * pointer.y + (wt.ty * wt.c - wt.tx * wt.d) * id, wt.a * id * pointer.y + - wt.b * id * pointer.x + (- wt.ty * wt.a + wt.tx * wt.b) * id); } , hitTest: function (displayObject, pointer, localPoint){ if (!displayObject.worldVisible) { return false ; } this.getLocalPosition(displayObject, pointer, this._localPoint); localPoint.copyFrom(this._localPoint); if (displayObject.hitArea && displayObject.hitArea.contains) { return (displayObject.hitArea.contains(this._localPoint.x, this._localPoint.y)); } else if (displayObject instanceof Phaser.TileSprite) { var width = displayObject.width; var height = displayObject.height; var x1 = - width * displayObject.anchor.x; if (this._localPoint.x >= x1 && this._localPoint.x < x1 + width) { var y1 = - height * displayObject.anchor.y; if (this._localPoint.y >= y1 && this._localPoint.y < y1 + height) { return true ; } } } else if (displayObject instanceof PIXI.Sprite) { var width = displayObject.texture.frame.width; var height = displayObject.texture.frame.height; var x1 = - width * displayObject.anchor.x; if (this._localPoint.x >= x1 && this._localPoint.x < x1 + width) { var y1 = - height * displayObject.anchor.y; if (this._localPoint.y >= y1 && this._localPoint.y < y1 + height) { return true ; } } } else if (displayObject instanceof Phaser.Graphics) { for (var i = 0; i < _AN_Read_length('length', displayObject.graphicsData); i++ ){ var data = displayObject.graphicsData[i]; if (!data.fill) { continue ; } if (data.shape && data.shape.contains(this._localPoint.x, this._localPoint.y)) { return true ; } } } for (var i = 0, len = _AN_Read_length('length', displayObject.children); i < len; i++ ){ if (this.hitTest(displayObject.children[i], pointer, localPoint)) { return true ; } } return false ; } , onClickTrampoline: function (){ this.activePointer.processClickTrampolines(); } } ; Phaser.Input.prototype.constructor = Phaser.Input; Object.defineProperty(Phaser.Input.prototype, "x", { get: function (){ return this._x; } , set: function (value){ this._x = Math.floor(value); } } ); Object.defineProperty(Phaser.Input.prototype, "y", { get: function (){ return this._y; } , set: function (value){ this._y = Math.floor(value); } } ); Object.defineProperty(Phaser.Input.prototype, "pollLocked", { get: function (){ return (this.pollRate > 0 && this._pollCounter < this.pollRate); } } ); Object.defineProperty(Phaser.Input.prototype, "totalInactivePointers", { get: function (){ return _AN_Read_length("length", this.pointers) - this.countActivePointers(); } } ); Object.defineProperty(Phaser.Input.prototype, "totalActivePointers", { get: function (){ return this.countActivePointers(); } } ); Object.defineProperty(Phaser.Input.prototype, "worldX", { get: function (){ return this.game.camera.view.x + this.x; } } ); Object.defineProperty(Phaser.Input.prototype, "worldY", { get: function (){ return this.game.camera.view.y + this.y; } } );