New Input handler implemented.

This commit is contained in:
Richard Davey
2013-05-16 02:36:58 +01:00
parent ce7bfd1fc2
commit 3cdd2baff0
31 changed files with 3048 additions and 2029 deletions
-319
View File
@@ -1,319 +0,0 @@
/// <reference path="../../Game.ts" />
/**
* Phaser - Finger
*
* A Finger object is used by the Touch manager and represents a single finger on the touch screen.
*/
module Phaser {
export class Finger {
/**
* Constructor
* @param {Phaser.Game} game.
* @return {Phaser.Finger} This object.
*/
constructor(game: Game) {
this._game = game;
this.active = false;
}
/**
*
* @property _game
* @type {Phaser.Game}
* @private
**/
private _game: Game;
/**
* An identification number for each touch point. When a touch point becomes active, it must be assigned an identifier that is distinct from any other active touch point. While the touch point remains active, all events that refer to it must assign it the same identifier.
* @property identifier
* @type {Number}
*/
public identifier: number;
/**
*
* @property active
* @type {Boolean}
*/
public active: bool;
/**
*
* @property point
* @type {Point}
**/
public point: Point = null;
/**
*
* @property circle
* @type {Circle}
**/
public circle: Circle = null;
/**
*
* @property withinGame
* @type {Boolean}
*/
public withinGame: bool = false;
/**
* The horizontal coordinate of point relative to the viewport in pixels, excluding any scroll offset
* @property clientX
* @type {Number}
*/
public clientX: number = -1;
//
/**
* The vertical coordinate of point relative to the viewport in pixels, excluding any scroll offset
* @property clientY
* @type {Number}
*/
public clientY: number = -1;
//
/**
* The horizontal coordinate of point relative to the viewport in pixels, including any scroll offset
* @property pageX
* @type {Number}
*/
public pageX: number = -1;
/**
* The vertical coordinate of point relative to the viewport in pixels, including any scroll offset
* @property pageY
* @type {Number}
*/
public pageY: number = -1;
/**
* The horizontal coordinate of point relative to the screen in pixels
* @property screenX
* @type {Number}
*/
public screenX: number = -1;
/**
* The vertical coordinate of point relative to the screen in pixels
* @property screenY
* @type {Number}
*/
public screenY: number = -1;
/**
* The horizontal coordinate of point relative to the game element
* @property x
* @type {Number}
*/
public x: number = -1;
/**
* The vertical coordinate of point relative to the game element
* @property y
* @type {Number}
*/
public y: number = -1;
/**
* The Element on which the touch point started when it was first placed on the surface, even if the touch point has since moved outside the interactive area of that element.
* @property target
* @type {Any}
*/
public target;
/**
*
* @property isDown
* @type {Boolean}
**/
public isDown: bool = false;
/**
*
* @property isUp
* @type {Boolean}
**/
public isUp: bool = false;
/**
*
* @property timeDown
* @type {Number}
**/
public timeDown: number = 0;
/**
*
* @property duration
* @type {Number}
**/
public duration: number = 0;
/**
*
* @property timeUp
* @type {Number}
**/
public timeUp: number = 0;
/**
*
* @property justPressedRate
* @type {Number}
**/
public justPressedRate: number = 200;
/**
*
* @property justReleasedRate
* @type {Number}
**/
public justReleasedRate: number = 200;
/**
*
* @method start
* @param {Any} event
*/
public start(event) {
this.identifier = event.identifier;
this.target = event.target;
// populate geom objects
if (this.point === null)
{
this.point = new Point();
}
if (this.circle === null)
{
this.circle = new Circle(0, 0, 44);
}
this.move(event);
this.active = true;
this.withinGame = true;
this.isDown = true;
this.isUp = false;
this.timeDown = this._game.time.now;
}
/**
*
* @method move
* @param {Any} event
*/
public move(event) {
this.clientX = event.clientX;
this.clientY = event.clientY;
this.pageX = event.pageX;
this.pageY = event.pageY;
this.screenX = event.screenX;
this.screenY = event.screenY;
this.x = this.pageX - this._game.stage.offset.x;
this.y = this.pageY - this._game.stage.offset.y;
this.point.setTo(this.x, this.y);
this.circle.setTo(this.x, this.y, 44);
// Droppings history (used for gestures and motion tracking)
this.duration = this._game.time.now - this.timeDown;
}
/**
*
* @method leave
* @param {Any} event
*/
public leave(event) {
this.withinGame = false;
this.move(event);
}
/**
*
* @method stop
* @param {Any} event
*/
public stop(event) {
this.active = false;
this.withinGame = false;
this.isDown = false;
this.isUp = true;
this.timeUp = this._game.time.now;
this.duration = this.timeUp - this.timeDown;
}
/**
*
* @method justPressed
* @param {Number} [duration].
* @return {Boolean}
*/
public justPressed(duration?: number = this.justPressedRate): bool {
if (this.isDown === true && (this.timeDown + duration) > this._game.time.now)
{
return true;
}
else
{
return false;
}
}
/**
*
* @method justReleased
* @param {Number} [duration].
* @return {Boolean}
*/
public justReleased(duration?: number = this.justReleasedRate): bool {
if (this.isUp === true && (this.timeUp + duration) > this._game.time.now)
{
return true;
}
else
{
return false;
}
}
/**
* Returns a string representation of this object.
* @method toString
* @return {String} a string representation of the instance.
**/
public toString(): string {
return "[{Finger (identifer=" + this.identifier + " active=" + this.active + " duration=" + this.duration + " withinGame=" + this.withinGame + " x=" + this.x + " y=" + this.y + " clientX=" + this.clientX + " clientY=" + this.clientY + " screenX=" + this.screenX + " screenY=" + this.screenY + " pageX=" + this.pageX + " pageY=" + this.pageY + ")}]";
}
}
}
+63
View File
@@ -0,0 +1,63 @@
/// <reference path="../../Game.ts" />
/// <reference path="Pointer.ts" />
/**
* Phaser - Gestures
*
* The Gesture class monitors for gestures and dispatches the resulting signals when they occur.
* Note: Android 2.x only supports 1 touch event at once, no multi-touch
*/
module Phaser {
export class Gestures {
/**
* Constructor
* @param {Game} game.
* @return {Touch} This object.
*/
constructor(game: Game) {
this._game = game;
}
/**
* Local private reference to game.
* @property _game
* @type {Game}
* @private
**/
private _game: Game;
private _p1: Pointer;
private _p2: Pointer;
private _p3: Pointer;
private _p4: Pointer;
private _p5: Pointer;
private _p6: Pointer;
private _p7: Pointer;
private _p8: Pointer;
private _p9: Pointer;
private _p10: Pointer;
public start() {
// Local references to the Phaser.Input.pointer objects
this._p1 = this._game.input.pointer1;
this._p2 = this._game.input.pointer2;
this._p3 = this._game.input.pointer3;
this._p4 = this._game.input.pointer4;
this._p5 = this._game.input.pointer5;
this._p6 = this._game.input.pointer6;
this._p7 = this._game.input.pointer7;
this._p8 = this._game.input.pointer8;
this._p9 = this._game.input.pointer9;
this._p10 = this._game.input.pointer10;
}
}
}
+583 -34
View File
@@ -1,11 +1,14 @@
/// <reference path="../../Game.ts" />
/// <reference path="../../Signal.ts" />
/// <reference path="Pointer.ts" />
/// <reference path="MSPointer.ts" />
/// <reference path="Gestures.ts" />
/**
* Phaser - Input
*
* A game specific Input manager that looks after the mouse, keyboard and touch objects. This is updated by the core game loop.
* A game specific Input manager that looks after the mouse, keyboard and touch objects.
* This is updated by the core game loop.
*/
module Phaser {
@@ -16,53 +19,83 @@ module Phaser {
this._game = game;
this.pointer1 = new Pointer(this._game, 1);
this.pointer2 = new Pointer(this._game, 2);
this.pointer3 = new Pointer(this._game, 3);
this.pointer4 = new Pointer(this._game, 4);
this.pointer5 = new Pointer(this._game, 5);
this.pointer6 = new Pointer(this._game, 6);
this.pointer7 = new Pointer(this._game, 7);
this.pointer8 = new Pointer(this._game, 8);
this.pointer9 = new Pointer(this._game, 9);
this.pointer10 = new Pointer(this._game, 10);
this.mouse = new Mouse(this._game);
this.keyboard = new Keyboard(this._game);
this.touch = new Touch(this._game);
this.mspointer = new MSPointer(this._game);
this.gestures = new Gestures(this._game);
this.onDown = new Phaser.Signal();
this.onUp = new Phaser.Signal();
}
/**
* Local private reference to game.
*/
private _game: Game;
/**
*
* @type {Mouse}
*/
* You can disable all Input by setting Input.disabled = true. While set all new input related events will be ignored.
* If you need to disable just one type of input, for example mouse, use Input.mouse.disabled = true instead
* @type {Boolean}
*/
public disabled: bool = false;
/**
* Phaser.Mouse handler
* @type {Mouse}
*/
public mouse: Mouse;
/**
*
* @type {Keyboard}
*/
* Phaser.Keyboard handler
* @type {Keyboard}
*/
public keyboard: Keyboard;
/**
*
* @type {Touch}
*/
* Phaser.Touch handler
* @type {Touch}
*/
public touch: Touch;
/**
*
* @type {MSPointer}
*/
* Phaser.MSPointer handler
* @type {MSPointer}
*/
public mspointer: MSPointer;
/**
*
* @type {Number}
*/
public x: number = 0;
* Phaser.Gestures handler
* @type {Gestures}
*/
public gestures: Gestures;
/**
*
* @type {Number}
*/
public y: number = 0;
* X coordinate of the most recent Pointer event
* @type {Number}
* @private
*/
private _x: number = 0;
/**
* X coordinate of the most recent Pointer event
* @type {Number}
* @private
*/
private _y: number = 0;
/**
*
@@ -89,27 +122,162 @@ module Phaser {
public worldY: number = 0;
/**
*
* @type {Phaser.Signal}
*/
* The maximum number of Pointers allowed to be active at any one time.
* For lots of games it's useful to set this to 1
* @type {Number}
*/
public maxPointers: number = 10;
/**
* A Signal dispatched when a mouse/Pointer object is pressed
* @type {Phaser.Signal}
*/
public onDown: Phaser.Signal;
/**
*
* @type {Phaser.Signal}
*/
* A Signal dispatched when a mouse/Pointer object is released
* @type {Phaser.Signal}
*/
public onUp: Phaser.Signal;
/**
* A Signal dispatched when a Pointer object (including the mouse) is tapped: pressed and released quickly
* @type {Phaser.Signal}
*/
public onTap: Phaser.Signal;
/**
* A Signal dispatched when a Pointer object (including the mouse) is double tapped: pressed and released quickly twice in succession
* @type {Phaser.Signal}
*/
public onDoubleTap: Phaser.Signal;
/**
* A Signal dispatched when a Pointer object (including the mouse) is held down
* @type {Phaser.Signal}
*/
public onHold: Phaser.Signal;
/**
* A Pointer object
* @property pointer1
* @type {Pointer}
**/
public pointer1: Pointer;
/**
* A Pointer object
* @property pointer2
* @type {Pointer}
**/
public pointer2: Pointer;
/**
* A Pointer object
* @property pointer3
* @type {Pointer}
**/
public pointer3: Pointer;
/**
* A Pointer object
* @property pointer4
* @type {Pointer}
**/
public pointer4: Pointer;
/**
* A Pointer object
* @property pointer5
* @type {Pointer}
**/
public pointer5: Pointer;
/**
* A Pointer object
* @property pointer6
* @type {Pointer}
**/
public pointer6: Pointer;
/**
* A Pointer object
* @property pointer7
* @type {Pointer}
**/
public pointer7: Pointer;
/**
* A Pointer object
* @property pointer8
* @type {Pointer}
**/
public pointer8: Pointer;
/**
* A Pointer object
* @property pointer9
* @type {Pointer}
**/
public pointer9: Pointer;
/**
* A Pointer object
* @property pointer10
* @type {Pointer}
**/
public pointer10: Pointer;
/**
* The screen X coordinate
* @property x
* @type {Number}
**/
public get x(): number {
return this._x;
}
public set x(value: number) {
this._x = Math.round(value);
}
/**
* The screen Y coordinate
* @property y
* @type {Number}
**/
public get y(): number {
return this._y;
}
public set y(value: number) {
this._y = Math.round(value);
}
public start() {
this.mouse.start();
this.keyboard.start();
this.touch.start();
this.mspointer.start();
this.gestures.start();
}
public update() {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
this.worldX = this._game.camera.worldView.x + this.x;
this.worldY = this._game.camera.worldView.y + this.y;
//this.worldX = this._game.camera.worldView.x + this.x;
//this.worldY = this._game.camera.worldView.y + this.y;
this.mouse.update();
this.touch.update();
}
@@ -117,7 +285,360 @@ module Phaser {
this.mouse.reset();
this.keyboard.reset();
this.touch.reset();
this.pointer1.reset();
this.pointer2.reset();
this.pointer3.reset();
this.pointer4.reset();
this.pointer5.reset();
this.pointer6.reset();
this.pointer7.reset();
this.pointer8.reset();
this.pointer9.reset();
this.pointer10.reset();
this.onDown = new Phaser.Signal();
this.onUp = new Phaser.Signal();
}
/**
* Get the total number of inactive Pointers
* @method totalInactivePointers
* @return {Number} The number of Pointers currently inactive
**/
public get totalInactivePointers(): number {
return 10 - this.totalActivePointers;
}
/**
* Get the total number of active Pointers
* @method totalActivePointers
* @return {Number} The number of Pointers currently active
**/
public get totalActivePointers(): number {
var result: number = 0;
if (this.pointer1.active == true)
{
result++;
}
else if (this.pointer2.active == true)
{
result++;
}
else if (this.pointer3.active == true)
{
result++;
}
else if (this.pointer4.active == true)
{
result++;
}
else if (this.pointer5.active == true)
{
result++;
}
else if (this.pointer6.active == true)
{
result++;
}
else if (this.pointer7.active == true)
{
result++;
}
else if (this.pointer8.active == true)
{
result++;
}
else if (this.pointer9.active == true)
{
result++;
}
else if (this.pointer10.active == true)
{
result++;
}
return result;
}
/**
* Find the first free Pointer object and start it, passing in the event data.
* @method startPointer
* @param {Any} event The event data from the Touch event
* @return {Pointer} The Pointer object that was started or null if no Pointer object is available
**/
public startPointer(event):Pointer {
if (this.maxPointers < 10 && this.totalActivePointers == this.maxPointers)
{
return null;
}
// Unrolled for speed
if (this.pointer1.active == false)
{
return this.pointer1.start(event);
}
else if (this.pointer2.active == false)
{
return this.pointer2.start(event);
}
else if (this.pointer3.active == false)
{
return this.pointer3.start(event);
}
else if (this.pointer4.active == false)
{
return this.pointer4.start(event);
}
else if (this.pointer5.active == false)
{
return this.pointer5.start(event);
}
else if (this.pointer6.active == false)
{
return this.pointer6.start(event);
}
else if (this.pointer7.active == false)
{
return this.pointer7.start(event);
}
else if (this.pointer8.active == false)
{
return this.pointer8.start(event);
}
else if (this.pointer9.active == false)
{
return this.pointer9.start(event);
}
else if (this.pointer10.active == false)
{
return this.pointer10.start(event);
}
return null;
}
/**
* Updates the matching Pointer object, passing in the event data.
* @method updatePointer
* @param {Any} event The event data from the Touch event
* @return {Pointer} The Pointer object that was updated or null if no Pointer object is available
**/
public updatePointer(event):Pointer {
// Unrolled for speed
if (this.pointer1.active == true && this.pointer1.identifier == event.identifier)
{
return this.pointer1.move(event);
}
else if (this.pointer2.active == true && this.pointer2.identifier == event.identifier)
{
return this.pointer2.move(event);
}
else if (this.pointer3.active == true && this.pointer3.identifier == event.identifier)
{
return this.pointer3.move(event);
}
else if (this.pointer4.active == true && this.pointer4.identifier == event.identifier)
{
return this.pointer4.move(event);
}
else if (this.pointer5.active == true && this.pointer5.identifier == event.identifier)
{
return this.pointer5.move(event);
}
else if (this.pointer6.active == true && this.pointer6.identifier == event.identifier)
{
return this.pointer6.move(event);
}
else if (this.pointer7.active == true && this.pointer7.identifier == event.identifier)
{
return this.pointer7.move(event);
}
else if (this.pointer8.active == true && this.pointer8.identifier == event.identifier)
{
return this.pointer8.move(event);
}
else if (this.pointer9.active == true && this.pointer9.identifier == event.identifier)
{
return this.pointer9.move(event);
}
else if (this.pointer10.active == true && this.pointer10.identifier == event.identifier)
{
return this.pointer10.move(event);
}
return null;
}
/**
* Stops the matching Pointer object, passing in the event data.
* @method stopPointer
* @param {Any} event The event data from the Touch event
* @return {Pointer} The Pointer object that was stopped or null if no Pointer object is available
**/
public stopPointer(event):Pointer {
// Unrolled for speed
if (this.pointer1.active == true && this.pointer1.identifier == event.identifier)
{
return this.pointer1.stop(event);
}
else if (this.pointer2.active == true && this.pointer2.identifier == event.identifier)
{
return this.pointer2.stop(event);
}
else if (this.pointer3.active == true && this.pointer3.identifier == event.identifier)
{
return this.pointer3.stop(event);
}
else if (this.pointer4.active == true && this.pointer4.identifier == event.identifier)
{
return this.pointer4.stop(event);
}
else if (this.pointer5.active == true && this.pointer5.identifier == event.identifier)
{
return this.pointer5.stop(event);
}
else if (this.pointer6.active == true && this.pointer6.identifier == event.identifier)
{
return this.pointer6.stop(event);
}
else if (this.pointer7.active == true && this.pointer7.identifier == event.identifier)
{
return this.pointer7.stop(event);
}
else if (this.pointer8.active == true && this.pointer8.identifier == event.identifier)
{
return this.pointer8.stop(event);
}
else if (this.pointer9.active == true && this.pointer9.identifier == event.identifier)
{
return this.pointer9.stop(event);
}
else if (this.pointer10.active == true && this.pointer10.identifier == event.identifier)
{
return this.pointer10.stop(event);
}
return null;
}
/**
* Get the next Pointer object whos active property matches the given state
* @method getPointer
* @param {Boolean} state The state the Pointer should be in (false for inactive, true for active)
* @return {Pointer} A Pointer object or null if no Pointer object matches the requested state.
**/
public getPointer(state: bool = false): Pointer {
// Unrolled for speed
if (this.pointer1.active == state)
{
return this.pointer1;
}
else if (this.pointer2.active == state)
{
return this.pointer2;
}
else if (this.pointer3.active == state)
{
return this.pointer3;
}
else if (this.pointer4.active == state)
{
return this.pointer4;
}
else if (this.pointer5.active == state)
{
return this.pointer5;
}
else if (this.pointer6.active == state)
{
return this.pointer6;
}
else if (this.pointer7.active == state)
{
return this.pointer7;
}
else if (this.pointer8.active == state)
{
return this.pointer8;
}
else if (this.pointer9.active == state)
{
return this.pointer9;
}
else if (this.pointer10.active == state)
{
return this.pointer10;
}
return null;
}
/**
* Get the Pointer object whos identified property matches the given identifier value
* @method getPointerFromIdentifier
* @param {Number} identifier The Pointer.identifier value to search for
* @return {Pointer} A Pointer object or null if no Pointer object matches the requested identifier.
**/
public getPointerFromIdentifier(identifier: number): Pointer {
// Unrolled for speed
if (this.pointer1.identifier == identifier)
{
return this.pointer1;
}
else if (this.pointer2.identifier == identifier)
{
return this.pointer2;
}
else if (this.pointer3.identifier == identifier)
{
return this.pointer3;
}
else if (this.pointer4.identifier == identifier)
{
return this.pointer4;
}
else if (this.pointer5.identifier == identifier)
{
return this.pointer5;
}
else if (this.pointer6.identifier == identifier)
{
return this.pointer6;
}
else if (this.pointer7.identifier == identifier)
{
return this.pointer7;
}
else if (this.pointer8.identifier == identifier)
{
return this.pointer8;
}
else if (this.pointer9.identifier == identifier)
{
return this.pointer9;
}
else if (this.pointer10.identifier == identifier)
{
return this.pointer10;
}
return null;
}
@@ -155,6 +676,34 @@ module Phaser {
}
/**
*
* @method calculateDistance
* @param {Finger} finger1
* @param {Finger} finger2
**/
//public calculateDistance(finger1: Finger, finger2: Finger) {
//}
/**
*
* @method calculateAngle
* @param {Finger} finger1
* @param {Finger} finger2
**/
//public calculateAngle(finger1: Finger, finger2: Finger) {
//}
/**
*
* @method checkOverlap
* @param {Finger} finger1
* @param {Finger} finger2
**/
//public checkOverlap(finger1: Finger, finger2: Finger) {
//}
}
}
+16 -1
View File
@@ -15,7 +15,6 @@ module Phaser {
constructor(game: Game) {
this._game = game;
this.start();
}
@@ -23,6 +22,12 @@ module Phaser {
private _keys = {};
private _capture = {};
/**
* You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
* @type {Boolean}
*/
public disabled: bool = false;
public start() {
document.body.addEventListener('keydown', (event: KeyboardEvent) => this.onKeyDown(event), false);
@@ -69,6 +74,11 @@ module Phaser {
*/
public onKeyDown(event: KeyboardEvent) {
if (this._game.input.disabled || this.disabled)
{
return;
}
if (this._capture[event.keyCode])
{
event.preventDefault();
@@ -91,6 +101,11 @@ module Phaser {
*/
public onKeyUp(event: KeyboardEvent) {
if (this._game.input.disabled || this.disabled)
{
return;
}
if (this._capture[event.keyCode])
{
event.preventDefault();
+29 -232
View File
@@ -1,15 +1,12 @@
/// <reference path="../../Game.ts" />
/// <reference path="Finger.ts" />
/// <reference path="Pointer.ts" />
/**
* Phaser - MSPointer
*
* The MSPointer class handles touch interactions with the game and the resulting Finger objects.
* The MSPointer class handles touch interactions with the game and the resulting Pointer objects.
* It will work only in Internet Explorer 10 and Windows Store or Windows Phone 8 apps using JavaScript.
* http://msdn.microsoft.com/en-us/library/ie/hh673557(v=vs.85).aspx
*
*
* @todo Gestures (pinch, zoom, swipe)
*/
module Phaser {
@@ -25,24 +22,6 @@ module Phaser {
this._game = game;
this.finger1 = new Finger(this._game);
this.finger2 = new Finger(this._game);
this.finger3 = new Finger(this._game);
this.finger4 = new Finger(this._game);
this.finger5 = new Finger(this._game);
this.finger6 = new Finger(this._game);
this.finger7 = new Finger(this._game);
this.finger8 = new Finger(this._game);
this.finger9 = new Finger(this._game);
this.finger10 = new Finger(this._game);
this._fingers = [this.finger1, this.finger2, this.finger3, this.finger4, this.finger5, this.finger6, this.finger7, this.finger8, this.finger9, this.finger10];
this.touchDown = new Signal();
this.touchUp = new Signal();
this.start();
}
/**
@@ -53,121 +32,11 @@ module Phaser {
**/
private _game: Game;
/**
*
* @property x
* @type Number
**/
public x: number = 0;
/**
*
* @property y
* @type Number
**/
public y: number = 0;
/**
*
* @property _fingers
* @type Array
* @private
**/
private _fingers: Finger[];
/**
*
* @property finger1
* @type Finger
**/
public finger1: Finger;
/**
*
* @property finger2
* @type Finger
**/
public finger2: Finger;
/**
*
* @property finger3
* @type Finger
**/
public finger3: Finger;
/**
*
* @property finger4
* @type Finger
**/
public finger4: Finger;
/**
*
* @property finger5
* @type Finger
**/
public finger5: Finger;
/**
*
* @property finger6
* @type Finger
**/
public finger6: Finger;
/**
*
* @property finger7
* @type Finger
**/
public finger7: Finger;
/**
*
* @property finger8
* @type Finger
**/
public finger8: Finger;
/**
*
* @property finger9
* @type Finger
**/
public finger9: Finger;
/**
*
* @property finger10
* @type Finger
**/
public finger10: Finger;
/**
*
* @property latestFinger
* @type Finger
**/
public latestFinger: Finger;
/**
*
* @property isDown
* @type Boolean
**/
public isDown: bool = false;
/**
*
* @property isUp
* @type Boolean
**/
public isUp: bool = true;
public touchDown: Signal;
public touchUp: Signal;
/**
* You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
* @type {Boolean}
*/
public disabled: bool = false;
/**
*
@@ -175,7 +44,7 @@ module Phaser {
*/
public start() {
if (navigator.msMaxTouchPoints)
if (this._game.device.mspointer == true)
{
this._game.stage.canvas.addEventListener('MSPointerDown', (event) => this.onPointerDown(event), false);
this._game.stage.canvas.addEventListener('MSPointerMove', (event) => this.onPointerMove(event), false);
@@ -191,25 +60,16 @@ module Phaser {
**/
private onPointerDown(event) {
event.preventDefault();
for (var f = 0; f < this._fingers.length; f++)
if (this._game.input.disabled || this.disabled)
{
if (this._fingers[f].active === false)
{
event.identifier = event.pointerId;
this._fingers[f].start(event);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
this.touchDown.dispatch(this._fingers[f].x, this._fingers[f].y, this._fingers[f].timeDown, this._fingers[f].timeUp, this._fingers[f].duration);
this._game.input.onDown.dispatch(this._game.input.x, this._game.input.y, this._fingers[f].timeDown);
this.isDown = true;
this.isUp = false;
break;
}
return;
}
event.preventDefault();
event.identifier = event.pointerId;
this._game.input.startPointer(event);
}
/**
@@ -219,22 +79,16 @@ module Phaser {
**/
private onPointerMove(event) {
event.preventDefault();
for (var f = 0; f < this._fingers.length; f++)
if (this._game.input.disabled || this.disabled)
{
if (this._fingers[f].identifier === event.pointerId &&
this._fingers[f].active === true)
{
event.identifier = event.pointerId;
this._fingers[f].move(event);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
break;
}
return;
}
event.preventDefault();
event.identifier = event.pointerId;
this._game.input.updatePointer(event);
}
/**
@@ -244,61 +98,15 @@ module Phaser {
**/
private onPointerUp(event) {
event.preventDefault();
for (var f = 0; f < this._fingers.length; f++)
if (this._game.input.disabled || this.disabled)
{
if (this._fingers[f].identifier === event.pointerId)
{
event.identifier = event.pointerId;
this._fingers[f].stop(event);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
this.touchUp.dispatch(this._fingers[f].x, this._fingers[f].y, this._fingers[f].timeDown, this._fingers[f].timeUp, this._fingers[f].duration);
this._game.input.onUp.dispatch(this._game.input.x, this._game.input.y, this._fingers[f].timeUp);
this.isDown = false;
this.isUp = true;
break;
}
return;
}
}
/**
*
* @method calculateDistance
* @param {Finger} finger1
* @param {Finger} finger2
**/
public calculateDistance(finger1: Finger, finger2: Finger) {
}
/**
*
* @method calculateAngle
* @param {Finger} finger1
* @param {Finger} finger2
**/
public calculateAngle(finger1: Finger, finger2: Finger) {
}
/**
*
* @method checkOverlap
* @param {Finger} finger1
* @param {Finger} finger2
**/
public checkOverlap(finger1: Finger, finger2: Finger) {
}
/**
*
* @method update
*/
public update() {
event.preventDefault();
event.identifier = event.pointerId;
this._game.input.stopPointer(event);
}
@@ -310,17 +118,6 @@ module Phaser {
}
/**
*
* @method reset
**/
public reset() {
this.isDown = false;
this.isUp = false;
}
}
}
+25 -1
View File
@@ -13,7 +13,6 @@ module Phaser {
constructor(game: Game) {
this._game = game;
this.start();
}
@@ -28,22 +27,32 @@ module Phaser {
public static MIDDLE_BUTTON: number = 1;
public static RIGHT_BUTTON: number = 2;
/**
* You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
* @type {Boolean}
*/
public disabled: bool = false;
/**
* @type {Boolean}
*/
public isDown: bool = false;
/**
* @type {Boolean}
*/
public isUp: bool = true;
/**
* @type {Number}
*/
public timeDown: number = 0;
/**
* @type {Number}
*/
public duration: number = 0;
/**
* @type {Number}
*/
@@ -69,6 +78,11 @@ module Phaser {
*/
public onMouseDown(event: MouseEvent) {
if (this._game.input.disabled || this.disabled)
{
return;
}
this.button = event.button;
this._x = event.clientX - this._game.stage.x;
@@ -102,6 +116,11 @@ module Phaser {
*/
public onMouseMove(event: MouseEvent) {
if (this._game.input.disabled || this.disabled)
{
return;
}
this.button = event.button;
this._x = event.clientX - this._game.stage.x;
@@ -117,6 +136,11 @@ module Phaser {
*/
public onMouseUp(event: MouseEvent) {
if (this._game.input.disabled || this.disabled)
{
return;
}
this.button = event.button;
this.isDown = false;
this.isUp = true;
+430
View File
@@ -0,0 +1,430 @@
/// <reference path="../../Game.ts" />
/**
* Phaser - Pointer
*
* A Pointer object is used by the Touch and MSPoint managers and represents a single finger on the touch screen.
*/
module Phaser {
export class Pointer {
/**
* Constructor
* @param {Phaser.Game} game.
* @return {Phaser.Pointer} This object.
*/
constructor(game: Game, id: number) {
this._game = game;
this.id = id;
this.active = false;
this.pointA = new Point();
this.pointB = new Point();
this.circle = new Circle(0, 0, 44);
}
/**
* Local private reference to game.
* @property _game
* @type {Phaser.Game}
* @private
**/
private _game: Game;
/**
* The Pointer ID (a number between 1 and 10)
* @property id
* @type {Number}
*/
public id: number;
/**
* An identification number for each touch point.
* When a touch point becomes active, it is assigned an identifier that is distinct from any other active touch point.
* While the touch point remains active, all events that refer to it are assigned the same identifier.
* @property identifier
* @type {Number}
*/
public identifier: number;
/**
* Is this Pointer active or not? An active Pointer is one that is in contact with the touch screen.
* @property active
* @type {Boolean}
*/
public active: bool;
/**
* A Point object representing the x/y screen coordinates of the Pointer.
* @property pointA
* @type {Point}
**/
public pointA: Point = null;
/**
* A Point object representing the x/y screen coordinates of the Pointer.
* @property pointB
* @type {Point}
**/
public pointB: Point = null;
/**
* A Circle object centered on the x/y screen coordinates of the Pointer.
* Default size of 44px (Apple's recommended "finger tip" size)
* @property circle
* @type {Circle}
**/
public circle: Circle = null;
/**
*
* @property withinGame
* @type {Boolean}
*/
public withinGame: bool = false;
/**
* The horizontal coordinate of point relative to the viewport in pixels, excluding any scroll offset
* @property clientX
* @type {Number}
*/
public clientX: number = -1;
/**
* The vertical coordinate of point relative to the viewport in pixels, excluding any scroll offset
* @property clientY
* @type {Number}
*/
public clientY: number = -1;
/**
* The horizontal coordinate of point relative to the viewport in pixels, including any scroll offset
* @property pageX
* @type {Number}
*/
public pageX: number = -1;
/**
* The vertical coordinate of point relative to the viewport in pixels, including any scroll offset
* @property pageY
* @type {Number}
*/
public pageY: number = -1;
/**
* The horizontal coordinate of point relative to the screen in pixels
* @property screenX
* @type {Number}
*/
public screenX: number = -1;
/**
* The vertical coordinate of point relative to the screen in pixels
* @property screenY
* @type {Number}
*/
public screenY: number = -1;
/**
* The horizontal coordinate of point relative to the game element
* @property x
* @type {Number}
*/
public x: number = -1;
/**
* The vertical coordinate of point relative to the game element
* @property y
* @type {Number}
*/
public y: number = -1;
/**
* The Element on which the touch point started when it was first placed on the surface, even if the touch point has since moved outside the interactive area of that element.
* @property target
* @type {Any}
*/
public target;
/**
* If the Pointer is touching the touchscreen isDown is set to true
* @property isDown
* @type {Boolean}
**/
public isDown: bool = false;
/**
* If the Pointer is not touching the touchscreen isUp is set to true
* @property isUp
* @type {Boolean}
**/
public isUp: bool = true;
/**
* A timestamp representing when the Pointer first touched the touchscreen.
* @property timeDown
* @type {Number}
**/
public timeDown: number = 0;
/**
* A timestamp representing when the Pointer left the touchscreen.
* @property timeUp
* @type {Number}
**/
public timeUp: number = 0;
/**
* The number of milliseconds below which the Pointer is considered justPressed
* @property justPressedRate
* @type {Number}
**/
public justPressedRate: number = 200;
/**
* The number of milliseconds below which the Pointer is considered justReleased
* @property justReleasedRate
* @type {Number}
**/
public justReleasedRate: number = 200;
/**
* The total number of times this Pointer has been touched to the touchscreen
* @property totalTouches
* @type {Number}
**/
public totalTouches: number = 0;
/**
* How long the Pointer has been depressed on the touchscreen.
* @property duration
* @type {Number}
**/
public get duration(): number {
return this._game.time.now - this.timeDown;
}
/**
* Gets the X value of this Pointer in world coordinate space
* @param {Camera} [camera]
*/
public getWorldX(camera?: Camera = this._game.camera) {
return camera.worldView.x + this.x;
}
/**
* Gets the Y value of this Pointer in world coordinate space
* @param {Camera} [camera]
*/
public getWorldY(camera?: Camera = this._game.camera) {
return camera.worldView.y + this.y;
}
/**
* Called when the Pointer is pressed onto the touchscreen
* @method start
* @param {Any} event
*/
public start(event): Pointer {
this.identifier = event.identifier;
this.target = event.target;
this.move(event);
this.pointA.setTo(this.x, this.y);
this.active = true;
this.withinGame = true;
this.isDown = true;
this.isUp = false;
this.timeDown = this._game.time.now;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
this._game.input.onDown.dispatch(this);
this.totalTouches++;
return this;
}
/**
* Called when the Pointer is moved on the touchscreen
* @method move
* @param {Any} event
*/
public move(event): Pointer {
this.clientX = event.clientX;
this.clientY = event.clientY;
this.pageX = event.pageX;
this.pageY = event.pageY;
this.screenX = event.screenX;
this.screenY = event.screenY;
this.x = this.pageX - this._game.stage.offset.x;
this.y = this.pageY - this._game.stage.offset.y;
this.pointB.setTo(this.x, this.y);
this.circle.setTo(this.x, this.y, 44);
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
// Droppings history (used for gestures and motion tracking)
return this;
}
/**
* Called when the Pointer leaves the target area
* @method leave
* @param {Any} event
*/
public leave(event) {
this.withinGame = false;
this.move(event);
}
/**
* Called when the Pointer leaves the touchscreen
* @method stop
* @param {Any} event
*/
public stop(event): Pointer {
this.active = false;
this.withinGame = false;
this.isDown = false;
this.isUp = true;
this.timeUp = this._game.time.now;
this._game.input.onUp.dispatch(this);
return this;
}
/**
* The Pointer is considered justPressed if the time it was pressed onto the touchscreen is less than justPressedRate
* @method justPressed
* @param {Number} [duration].
* @return {Boolean}
*/
public justPressed(duration?: number = this.justPressedRate): bool {
if (this.isDown === true && (this.timeDown + duration) > this._game.time.now)
{
return true;
}
else
{
return false;
}
}
/**
* The Pointer is considered justReleased if the time it left the touchscreen is less than justReleasedRate
* @method justReleased
* @param {Number} [duration].
* @return {Boolean}
*/
public justReleased(duration?: number = this.justReleasedRate): bool {
if (this.isUp === true && (this.timeUp + duration) > this._game.time.now)
{
return true;
}
else
{
return false;
}
}
public reset() {
this.active = false;
this.identifier = null;
this.isDown = false;
this.isUp = true;
this.totalTouches = 0;
}
/**
* Renders the Pointer.circle object onto the stage in green if down or red if up.
* @method renderDebug
*/
public renderDebug(hideIfUp: bool = false) {
if (hideIfUp == true && this.isUp == true)
{
return;
}
this._game.stage.context.beginPath();
this._game.stage.context.arc(this.x, this.y, this.circle.radius * 2, 0, Math.PI * 2);
if (this.active)
{
this._game.stage.context.fillStyle = 'rgb(0,255,0)';
this._game.stage.context.strokeStyle = 'rgb(0,255,0)';
}
else
{
this._game.stage.context.fillStyle = 'rgb(100,0,0)';
this._game.stage.context.strokeStyle = 'rgb(100,0,0)';
}
this._game.stage.context.fill();
this._game.stage.context.closePath();
// Render the points
this._game.stage.context.beginPath();
this._game.stage.context.moveTo(this.pointA.x, this.pointA.y);
this._game.stage.context.lineTo(this.pointB.x, this.pointB.y);
this._game.stage.context.lineWidth = 2;
this._game.stage.context.stroke();
this._game.stage.context.closePath();
// Render the text
this._game.stage.context.fillStyle = 'rgb(255,255,255)';
this._game.stage.context.font = 'Arial 16px';
this._game.stage.context.fillText('ID: ' + this.id + " Active: " + this.active, this.x, this.y - 100);
this._game.stage.context.fillText('Screen X: ' + this.x + " Screen Y: " + this.y, this.x, this.y - 80);
this._game.stage.context.fillText('Duration: ' + this.duration + " ms", this.x, this.y - 60);
}
/**
* Returns a string representation of this object.
* @method toString
* @return {String} a string representation of the instance.
**/
public toString(): string {
return "[{Pointer (id=" + this.id + " identifer=" + this.identifier + " active=" + this.active + " duration=" + this.duration + " withinGame=" + this.withinGame + " x=" + this.x + " y=" + this.y + " clientX=" + this.clientX + " clientY=" + this.clientY + " screenX=" + this.screenX + " screenY=" + this.screenY + " pageX=" + this.pageX + " pageY=" + this.pageY + ")}]";
}
}
}
+30 -270
View File
@@ -1,20 +1,14 @@
/// <reference path="../../Game.ts" />
/// <reference path="Finger.ts" />
/// <reference path="Pointer.ts" />
/**
* Phaser - Touch
*
* The Touch class handles touch interactions with the game and the resulting Finger objects.
* The Touch class handles touch interactions with the game and the resulting Pointer objects.
* http://www.w3.org/TR/touch-events/
* https://developer.mozilla.org/en-US/docs/DOM/TouchList
* http://www.html5rocks.com/en/mobile/touchandmouse/
* Note: Android 2.x only supports 1 touch event at once, no multi-touch
*
* @todo Try and resolve update lag in Chrome/Android
* Gestures (pinch, zoom, swipe)
* GameObject Touch
* Touch point within GameObject
* Input Zones (mouse and touch) - lock entities within them + axis aligned drags
*/
module Phaser {
@@ -30,24 +24,6 @@ module Phaser {
this._game = game;
this.finger1 = new Finger(this._game);
this.finger2 = new Finger(this._game);
this.finger3 = new Finger(this._game);
this.finger4 = new Finger(this._game);
this.finger5 = new Finger(this._game);
this.finger6 = new Finger(this._game);
this.finger7 = new Finger(this._game);
this.finger8 = new Finger(this._game);
this.finger9 = new Finger(this._game);
this.finger10 = new Finger(this._game);
this._fingers = [this.finger1, this.finger2, this.finger3, this.finger4, this.finger5, this.finger6, this.finger7, this.finger8, this.finger9, this.finger10];
this.touchDown = new Signal();
this.touchUp = new Signal();
this.start();
}
/**
@@ -59,120 +35,10 @@ module Phaser {
private _game: Game;
/**
*
* @property x
* @type {Number}
**/
public x: number = 0;
/**
*
* @property y
* @type {Number}
**/
public y: number = 0;
/**
*
* @property _fingers
* @type {Array}
* @private
**/
private _fingers: Finger[];
/**
*
* @property finger1
* @type {Finger}
**/
public finger1: Finger;
/**
*
* @property finger2
* @type {Finger}
**/
public finger2: Finger;
/**
*
* @property finger3
* @type {Finger}
**/
public finger3: Finger;
/**
*
* @property finger4
* @type {Finger}
**/
public finger4: Finger;
/**
*
* @property finger5
* @type {Finger}
**/
public finger5: Finger;
/**
*
* @property finger6
* @type {Finger}
**/
public finger6: Finger;
/**
*
* @property finger7
* @type {Finger}
**/
public finger7: Finger;
/**
*
* @property finger8
* @type {Finger}
**/
public finger8: Finger;
/**
*
* @property finger9
* @type {Finger}
**/
public finger9: Finger;
/**
*
* @property finger10
* @type {Finger}
**/
public finger10: Finger;
/**
*
* @property latestFinger
* @type {Finger}
**/
public latestFinger: Finger;
/**
*
* @property isDown
* You can disable all Input by setting disabled = true. While set all new input related events will be ignored.
* @type {Boolean}
**/
public isDown: bool = false;
/**
*
* @property isUp
* @type {Boolean}
**/
public isUp: bool = true;
public touchDown: Signal;
public touchUp: Signal;
*/
public disabled: bool = false;
/**
*
@@ -209,91 +75,71 @@ module Phaser {
**/
private onTouchStart(event) {
event.preventDefault();
if (this._game.input.disabled || this.disabled)
{
return;
}
// A list of all the touch points that BECAME active with the current event
// https://developer.mozilla.org/en-US/docs/DOM/TouchList
event.preventDefault();
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
for (var i = 0; i < event.changedTouches.length; i++)
{
for (var f = 0; f < this._fingers.length; f++)
{
if (this._fingers[f].active === false)
{
this._fingers[f].start(event.changedTouches[i]);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
this.touchDown.dispatch(this._fingers[f].x, this._fingers[f].y, this._fingers[f].timeDown, this._fingers[f].timeUp, this._fingers[f].duration);
this._game.input.onDown.dispatch(this._game.input.x, this._game.input.y, this._fingers[f].timeDown);
this.isDown = true;
this.isUp = false;
break;
}
}
this._game.input.startPointer(event.changedTouches[i]);
}
}
/**
* Doesn't appear to be supported by most browsers yet
* Touch cancel - touches that were disrupted (perhaps by moving into a plugin or browser chrome)
* Occurs for example on iOS when you put down 4 fingers and the app selector UI appears
* @method onTouchCancel
* @param {Any} event
**/
private onTouchCancel(event) {
if (this._game.input.disabled || this.disabled)
{
return;
}
event.preventDefault();
// Touch cancel - touches that were disrupted (perhaps by moving into a plugin or browser chrome)
// http://www.w3.org/TR/touch-events/#dfn-touchcancel
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
for (var i = 0; i < event.changedTouches.length; i++)
{
for (var f = 0; f < this._fingers.length; f++)
{
if (this._fingers[f].identifier === event.changedTouches[i].identifier)
{
this._fingers[f].stop(event.changedTouches[i]);
break;
}
}
this._game.input.stopPointer(event.changedTouches[i]);
}
}
/**
* For touch enter and leave its a list of the touch points that have entered or left the target
* Doesn't appear to be supported by most browsers yet
* @method onTouchEnter
* @param {Any} event
**/
private onTouchEnter(event) {
if (this._game.input.disabled || this.disabled)
{
return;
}
event.preventDefault();
// For touch enter and leave its a list of the touch points that have entered or left the target
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
for (var i = 0; i < event.changedTouches.length; i++)
{
for (var f = 0; f < this._fingers.length; f++)
{
if (this._fingers[f].active === false)
{
this._fingers[f].start(event.changedTouches[i]);
break;
}
}
console.log('touch enter');
}
}
/**
* For touch enter and leave its a list of the touch points that have entered or left the target
* Doesn't appear to be supported by most browsers yet
* @method onTouchLeave
* @param {Any} event
@@ -302,19 +148,9 @@ module Phaser {
event.preventDefault();
// For touch enter and leave its a list of the touch points that have entered or left the target
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
for (var i = 0; i < event.changedTouches.length; i++)
{
for (var f = 0; f < this._fingers.length; f++)
{
if (this._fingers[f].identifier === event.changedTouches[i].identifier)
{
this._fingers[f].leave(event.changedTouches[i]);
break;
}
}
console.log('touch leave');
}
}
@@ -328,23 +164,9 @@ module Phaser {
event.preventDefault();
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
for (var i = 0; i < event.changedTouches.length; i++)
{
for (var f = 0; f < this._fingers.length; f++)
{
if (this._fingers[f].identifier === event.changedTouches[i].identifier)
{
this._fingers[f].move(event.changedTouches[i]);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
break;
}
}
this._game.input.updatePointer(event.changedTouches[i]);
}
}
@@ -364,60 +186,9 @@ module Phaser {
// event.changedTouches = the touches that CHANGED in this event, not the total number of them
for (var i = 0; i < event.changedTouches.length; i++)
{
for (var f = 0; f < this._fingers.length; f++)
{
if (this._fingers[f].identifier === event.changedTouches[i].identifier)
{
this._fingers[f].stop(event.changedTouches[i]);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
this.touchUp.dispatch(this._fingers[f].x, this._fingers[f].y, this._fingers[f].timeDown, this._fingers[f].timeUp, this._fingers[f].duration);
this._game.input.onUp.dispatch(this._game.input.x, this._game.input.y, this._fingers[f].timeUp);
this.isDown = false;
this.isUp = true;
break;
}
}
this._game.input.stopPointer(event.changedTouches[i]);
}
}
/**
*
* @method calculateDistance
* @param {Finger} finger1
* @param {Finger} finger2
**/
public calculateDistance(finger1: Finger, finger2: Finger) {
}
/**
*
* @method calculateAngle
* @param {Finger} finger1
* @param {Finger} finger2
**/
public calculateAngle(finger1: Finger, finger2: Finger) {
}
/**
*
* @method checkOverlap
* @param {Finger} finger1
* @param {Finger} finger2
**/
public checkOverlap(finger1: Finger, finger2: Finger) {
}
/**
*
* @method update
*/
public update() {
}
/**
@@ -435,17 +206,6 @@ module Phaser {
}
/**
*
* @method reset
**/
public reset() {
this.isDown = false;
this.isUp = false;
}
}
}