phaser/Phaser/system/input/Mouse.ts

114 lines
3 KiB
TypeScript
Raw Normal View History

2013-04-12 16:19:56 +00:00
/// <reference path="../../Game.ts" />
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
2013-04-18 13:16:18 +00:00
this._game = game;
this.start();
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-04-18 13:16:18 +00:00
private _game: Game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
private _x: number = 0;
private _y: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public button: number;
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-04-18 13:16:18 +00:00
public isDown: bool = false;
public isUp: bool = true;
public timeDown: number = 0;
public duration: number = 0;
public timeUp: number = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public start() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game.stage.canvas.addEventListener('mousedown', (event: MouseEvent) => this.onMouseDown(event), true);
this._game.stage.canvas.addEventListener('mousemove', (event: MouseEvent) => this.onMouseMove(event), true);
this._game.stage.canvas.addEventListener('mouseup', (event: MouseEvent) => this.onMouseUp(event), 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
2013-04-18 13:16:18 +00:00
public reset() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.isDown = false;
this.isUp = 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
2013-04-18 13:16:18 +00:00
public onMouseDown(event: MouseEvent) {
2013-04-18 13:16:18 +00:00
this.button = event.button;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._x = event.clientX - this._game.stage.x;
this._y = event.clientY - this._game.stage.y;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game.input.x = this._x * this._game.input.scaleX;
this._game.input.y = this._y * this._game.input.scaleY;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.isDown = true;
this.isUp = false;
this.timeDown = this._game.time.now;
2013-04-12 16:19:56 +00:00
this._game.input.onDown.dispatch(this._game.input.x, this._game.input.y, this.timeDown);
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public update() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
//this._game.input.x = this._x * this._game.input.scaleX;
//this._game.input.y = this._y * this._game.input.scaleY;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this.isDown)
{
this.duration = this._game.time.now - this.timeDown;
}
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-04-18 13:16:18 +00:00
public onMouseMove(event: MouseEvent) {
2013-04-18 13:16:18 +00:00
this.button = event.button;
this._x = event.clientX - this._game.stage.x;
this._y = event.clientY - this._game.stage.y;
this._game.input.x = this._x * this._game.input.scaleX;
this._game.input.y = this._y * this._game.input.scaleY;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
public onMouseUp(event: MouseEvent) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.button = event.button;
this.isDown = false;
this.isUp = true;
this.timeUp = this._game.time.now;
this.duration = this.timeUp - this.timeDown;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._x = event.clientX - this._game.stage.x;
this._y = event.clientY - this._game.stage.y;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game.input.x = this._x * this._game.input.scaleX;
this._game.input.y = this._y * this._game.input.scaleY;
this._game.input.onUp.dispatch(this._game.input.x, this._game.input.y, this.timeDown);
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}