phaser/Phaser/input/Mouse.ts

152 lines
4 KiB
TypeScript
Raw Normal View History

/// <reference path="../_definitions.ts" />
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
2013-04-18 15:49:08 +00:00
* Phaser - Mouse
*
* The Mouse class handles mouse interactions with the game and the resulting events.
2013-04-18 13:16:18 +00:00
*/
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
export class Mouse {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
constructor(game: Game) {
2013-04-12 16:19:56 +00:00
this.game = game;
this.callbackContext = this.game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-05-16 20:34:24 +00:00
/**
* Local reference to game.
* @property game
2013-05-16 20:34:24 +00:00
* @type {Phaser.Game}
**/
public game: Phaser.Game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public static LEFT_BUTTON: number = 0;
public static MIDDLE_BUTTON: number = 1;
public static RIGHT_BUTTON: number = 2;
2013-04-12 16:19:56 +00:00
2013-05-16 01:36:58 +00:00
/**
* You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
* @type {bool}
2013-05-16 01:36:58 +00:00
*/
public disabled: bool = false;
2013-05-16 01:36:58 +00:00
/**
* Custom callback useful for hooking into a 3rd party library. Will be passed the mouse event as the only parameter.
* Callbacks are fired even if this component is disabled and before the event propagation is disabled.
*/
public callbackContext;
public mouseDownCallback = null;
public mouseMoveCallback = null;
public mouseUpCallback = null;
/**
* A reference to the event handlers to allow removeEventListener support
*/
public _onMouseDown;
public _onMouseMove;
public _onMouseUp;
/**
2013-05-16 20:34:24 +00:00
* Starts the event listeners running
* @method start
*/
2013-04-18 13:16:18 +00:00
public start() {
2013-04-12 16:19:56 +00:00
if (this.game.device.android && this.game.device.chrome == false)
2013-08-02 17:32:26 +00:00
{
// Android stock browser fires mouse events even if you preventDefault on the touchStart, so ...
return;
}
this._onMouseDown = (event: MouseEvent) => this.onMouseDown(event);
this._onMouseMove = (event: MouseEvent) => this.onMouseMove(event);
this._onMouseUp = (event: MouseEvent) => this.onMouseUp(event);
this.game.stage.canvas.addEventListener('mousedown', this._onMouseDown, true);
this.game.stage.canvas.addEventListener('mousemove', this._onMouseMove, true);
this.game.stage.canvas.addEventListener('mouseup', this._onMouseUp, true);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* @param {MouseEvent} event
*/
2013-04-18 13:16:18 +00:00
public onMouseDown(event: MouseEvent) {
if (this.mouseDownCallback)
{
this.mouseDownCallback.call(this.callbackContext, event);
}
if (this.game.input.disabled || this.disabled)
2013-05-16 01:36:58 +00:00
{
return;
}
2013-05-16 20:34:24 +00:00
event['identifier'] = 0;
2013-04-12 16:19:56 +00:00
this.game.input.mousePointer.start(event);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* @param {MouseEvent} event
*/
2013-04-18 13:16:18 +00:00
public onMouseMove(event: MouseEvent) {
if (this.mouseMoveCallback)
{
this.mouseMoveCallback.call(this.callbackContext, event);
}
if (this.game.input.disabled || this.disabled)
2013-05-16 01:36:58 +00:00
{
return;
}
2013-05-16 20:34:24 +00:00
event['identifier'] = 0;
2013-04-18 13:16:18 +00:00
this.game.input.mousePointer.move(event);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
/**
* @param {MouseEvent} event
*/
2013-04-18 13:16:18 +00:00
public onMouseUp(event: MouseEvent) {
2013-04-12 16:19:56 +00:00
if (this.mouseUpCallback)
{
this.mouseUpCallback.call(this.callbackContext, event);
}
if (this.game.input.disabled || this.disabled)
2013-05-16 01:36:58 +00:00
{
return;
}
2013-05-16 20:34:24 +00:00
event['identifier'] = 0;
2013-04-12 16:19:56 +00:00
this.game.input.mousePointer.stop(event);
2013-04-12 16:19:56 +00:00
2013-05-16 20:34:24 +00:00
}
/**
* Stop the event listeners
* @method stop
*/
public stop() {
2013-04-18 13:16:18 +00:00
this.game.stage.canvas.removeEventListener('mousedown', this._onMouseDown);
this.game.stage.canvas.removeEventListener('mousemove', this._onMouseMove);
this.game.stage.canvas.removeEventListener('mouseup', this._onMouseUp);
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}