2013-08-06 02:14:48 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
|
|
|
/// <reference path="../utils/SpriteUtils.ts" />
|
|
|
|
/// <reference path="../utils/RectangleUtils.ts" />
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-06 02:14:48 +00:00
|
|
|
* Phaser - Components - InputHandler
|
2013-06-01 01:49:41 +00:00
|
|
|
*
|
|
|
|
* Input detection component
|
|
|
|
*/
|
|
|
|
|
2013-08-06 02:14:48 +00:00
|
|
|
module Phaser.Components {
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-08-06 02:14:48 +00:00
|
|
|
export class InputHandler {
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* Sprite Input component constructor
|
|
|
|
* @param parent The Sprite using this Input component
|
|
|
|
*/
|
2013-06-06 01:47:08 +00:00
|
|
|
constructor(parent: Phaser.Sprite) {
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
this.game = parent.game;
|
2013-07-12 02:28:46 +00:00
|
|
|
this._parent = parent;
|
2013-06-01 01:49:41 +00:00
|
|
|
this.enabled = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-01 18:59:20 +00:00
|
|
|
* Reference to Phaser.Game
|
2013-06-01 01:49:41 +00:00
|
|
|
*/
|
|
|
|
public game: Game;
|
|
|
|
|
|
|
|
/**
|
2013-07-12 02:28:46 +00:00
|
|
|
* Local private reference to its parent game object.
|
2013-06-01 01:49:41 +00:00
|
|
|
*/
|
2013-07-12 02:28:46 +00:00
|
|
|
private _parent: Phaser.Sprite;
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
private _pointerData;
|
2013-06-02 02:47:54 +00:00
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* If enabled the Input component will be updated by the parent Sprite
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2013-06-01 01:49:41 +00:00
|
|
|
public enabled: bool;
|
2013-06-01 18:59:20 +00:00
|
|
|
|
2013-06-02 02:47:54 +00:00
|
|
|
/**
|
2013-06-02 13:19:12 +00:00
|
|
|
* The PriorityID controls which Sprite receives an Input event first if they should overlap.
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public priorityID: number = 0;
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-06-13 00:55:32 +00:00
|
|
|
/**
|
|
|
|
* The index of this Input component entry in the Game.Input manager.
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public indexID: number = 0;
|
2013-06-13 00:55:32 +00:00
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
private _dragPoint: Point;
|
2013-06-03 00:22:14 +00:00
|
|
|
private _draggedPointerID: number;
|
2013-06-02 13:19:12 +00:00
|
|
|
public dragOffset: Point;
|
2013-06-03 00:22:14 +00:00
|
|
|
public isDragged: bool = false;
|
2013-06-02 13:19:12 +00:00
|
|
|
public dragFromCenter: bool;
|
2013-08-06 23:00:12 +00:00
|
|
|
public dragPixelPerfect: bool = false;
|
|
|
|
public dragPixelPerfectAlpha: number;
|
2013-06-02 13:19:12 +00:00
|
|
|
|
|
|
|
public allowHorizontalDrag: bool = true;
|
|
|
|
public allowVerticalDrag: bool = true;
|
2013-06-13 00:55:32 +00:00
|
|
|
public bringToTop: bool = false;
|
2013-06-02 13:19:12 +00:00
|
|
|
|
|
|
|
public snapOnDrag: bool = false;
|
|
|
|
public snapOnRelease: bool = false;
|
|
|
|
public snapOffset: Point;
|
|
|
|
public snapX: number = 0;
|
|
|
|
public snapY: number = 0;
|
|
|
|
|
2013-06-02 02:47:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this sprite allowed to be dragged by the mouse? true = yes, false = no
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
public draggable: bool = false;
|
|
|
|
|
|
|
|
/**
|
2013-06-02 13:19:12 +00:00
|
|
|
* A region of the game world within which the sprite is restricted during drag
|
2013-06-02 02:47:54 +00:00
|
|
|
* @default null
|
|
|
|
*/
|
|
|
|
public boundsRect: Rectangle = null;
|
|
|
|
|
|
|
|
/**
|
2013-06-02 13:19:12 +00:00
|
|
|
* An Sprite the bounds of which this sprite is restricted during drag
|
2013-06-02 02:47:54 +00:00
|
|
|
* @default null
|
|
|
|
*/
|
2013-06-06 01:47:08 +00:00
|
|
|
public boundsSprite: Phaser.Sprite = null;
|
2013-06-02 02:47:54 +00:00
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* The Input component can monitor either the physics body of the Sprite or the frameBounds
|
|
|
|
* If checkBody is set to true it will monitor the bounds of the physics body.
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2013-06-01 01:49:41 +00:00
|
|
|
public checkBody: bool;
|
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* Turn the mouse pointer into a hand image by temporarily setting the CSS style of the Game canvas
|
|
|
|
* on Input over. Only works on desktop browsers or browsers with a visible input pointer.
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
public useHandCursor: bool;
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
/**
|
|
|
|
* If this object is set to consume the pointer event then it will stop all propogation from this object on.
|
|
|
|
* For example if you had a stack of 6 sprites with the same priority IDs and one consumed the event, none of the others would receive it.
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
public consumePointerEvent: bool = false;
|
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* The x coordinate of the Input pointer, relative to the top-left of the parent Sprite.
|
2013-06-02 13:19:12 +00:00
|
|
|
* This value is only set when the pointer is over this Sprite.
|
2013-06-01 18:59:20 +00:00
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerX(pointer: number = 0): number {
|
|
|
|
return this._pointerData[pointer].x;
|
|
|
|
}
|
2013-06-01 18:59:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The y coordinate of the Input pointer, relative to the top-left of the parent Sprite
|
2013-06-02 13:19:12 +00:00
|
|
|
* This value is only set when the pointer is over this Sprite.
|
2013-06-01 18:59:20 +00:00
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerY(pointer: number = 0): number {
|
|
|
|
return this._pointerData[pointer].y;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the Pointer is touching the touchscreen, or the mouse button is held down, isDown is set to true
|
|
|
|
* @property isDown
|
|
|
|
* @type {Boolean}
|
|
|
|
**/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerDown(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].isDown;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the Pointer is not touching the touchscreen, or the mouse button is up, isUp is set to true
|
|
|
|
* @property isUp
|
|
|
|
* @type {Boolean}
|
|
|
|
**/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerUp(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].isUp;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A timestamp representing when the Pointer first touched the touchscreen.
|
|
|
|
* @property timeDown
|
|
|
|
* @type {Number}
|
|
|
|
**/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerTimeDown(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].timeDown;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A timestamp representing when the Pointer left the touchscreen.
|
|
|
|
* @property timeUp
|
|
|
|
* @type {Number}
|
|
|
|
**/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerTimeUp(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].timeUp;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the Pointer over this Sprite
|
|
|
|
* @property isOver
|
|
|
|
* @type {Boolean}
|
|
|
|
**/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerOver(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].isOver;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the Pointer outside of this Sprite
|
|
|
|
* @property isOut
|
|
|
|
* @type {Boolean}
|
|
|
|
**/
|
2013-06-02 13:19:12 +00:00
|
|
|
public pointerOut(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].isOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A timestamp representing when the Pointer first touched the touchscreen.
|
|
|
|
* @property timeDown
|
|
|
|
* @type {Number}
|
|
|
|
**/
|
|
|
|
public pointerTimeOver(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].timeOver;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
/**
|
|
|
|
* A timestamp representing when the Pointer left the touchscreen.
|
|
|
|
* @property timeUp
|
|
|
|
* @type {Number}
|
|
|
|
**/
|
|
|
|
public pointerTimeOut(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].timeOut;
|
|
|
|
}
|
2013-06-01 18:59:20 +00:00
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
/**
|
|
|
|
* Is this sprite being dragged by the mouse or not?
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
public pointerDragged(pointer: number = 0): bool {
|
|
|
|
return this._pointerData[pointer].isDragged;
|
|
|
|
}
|
2013-06-01 18:59:20 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
public start(priority: number = 0, checkBody?: bool = false, useHandCursor?: bool = false): Phaser.Sprite {
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
// Turning on
|
|
|
|
if (this.enabled == false)
|
|
|
|
{
|
2013-06-03 00:22:14 +00:00
|
|
|
// Register, etc
|
|
|
|
this.checkBody = checkBody;
|
|
|
|
this.useHandCursor = useHandCursor;
|
|
|
|
this.priorityID = priority;
|
|
|
|
|
|
|
|
this._pointerData = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
this._pointerData.push({ id: i, x: 0, y: 0, isDown: false, isUp: false, isOver: false, isOut: false, timeOver: 0, timeOut: 0, timeDown: 0, timeUp: 0, downDuration: 0, isDragged: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.snapOffset = new Point;
|
2013-08-06 23:00:12 +00:00
|
|
|
this.enabled = true;
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
this.game.input.addGameObject(this._parent);
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
// Create the signals the Input component will emit
|
2013-08-06 23:00:12 +00:00
|
|
|
if (this._parent.events.onInputOver == null)
|
|
|
|
{
|
|
|
|
this._parent.events.onInputOver = new Phaser.Signal;
|
|
|
|
this._parent.events.onInputOut = new Phaser.Signal;
|
|
|
|
this._parent.events.onInputDown = new Phaser.Signal;
|
|
|
|
this._parent.events.onInputUp = new Phaser.Signal;
|
|
|
|
this._parent.events.onDragStart = new Phaser.Signal;
|
|
|
|
this._parent.events.onDragStop = new Phaser.Signal;
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
return this._parent;
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
public reset() {
|
2013-06-03 11:03:34 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
this.enabled = false;
|
2013-06-03 11:03:34 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
this._pointerData[i] = { id: i, x: 0, y: 0, isDown: false, isUp: false, isOver: false, isOut: false, timeOver: 0, timeOut: 0, timeDown: 0, timeUp: 0, downDuration: 0, isDragged: false };
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
}
|
2013-06-03 11:03:34 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
public stop() {
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
// Turning off
|
|
|
|
if (this.enabled == false)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-03 00:22:14 +00:00
|
|
|
// De-register, etc
|
2013-08-06 23:00:12 +00:00
|
|
|
this.enabled = false;
|
|
|
|
this.game.input.removeGameObject(this.indexID);
|
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-06-13 00:55:32 +00:00
|
|
|
/**
|
|
|
|
* Clean up memory.
|
|
|
|
*/
|
|
|
|
public destroy() {
|
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
if (this.enabled)
|
|
|
|
{
|
|
|
|
this.enabled = false;
|
|
|
|
this.game.input.removeGameObject(this.indexID);
|
|
|
|
}
|
2013-06-13 00:55:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given pointer is over this Sprite. All checks are done in world coordinates.
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public checkPointerOver(pointer: Phaser.Pointer): bool {
|
|
|
|
|
|
|
|
if (this.enabled == false || this._parent.visible == false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-12 02:28:46 +00:00
|
|
|
return SpriteUtils.overlapsXY(this._parent, pointer.worldX(), pointer.worldY());
|
2013-08-06 23:00:12 +00:00
|
|
|
}
|
2013-06-03 11:03:34 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
}
|
2013-06-03 11:03:34 +00:00
|
|
|
|
2013-06-01 01:49:41 +00:00
|
|
|
/**
|
|
|
|
* Update
|
|
|
|
*/
|
2013-06-03 00:22:14 +00:00
|
|
|
public update(pointer: Phaser.Pointer): bool {
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
if (this.enabled == false || this._parent.visible == false)
|
2013-06-01 01:49:41 +00:00
|
|
|
{
|
2013-07-19 03:57:14 +00:00
|
|
|
this._pointerOutHandler(pointer);
|
2013-06-03 00:22:14 +00:00
|
|
|
return false;
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 11:03:34 +00:00
|
|
|
if (this.draggable && this._draggedPointerID == pointer.id)
|
|
|
|
{
|
|
|
|
return this.updateDrag(pointer);
|
|
|
|
}
|
|
|
|
else if (this._pointerData[pointer.id].isOver == true)
|
2013-06-01 01:49:41 +00:00
|
|
|
{
|
2013-07-12 02:28:46 +00:00
|
|
|
if (SpriteUtils.overlapsXY(this._parent, pointer.worldX(), pointer.worldY()))
|
2013-06-03 11:03:34 +00:00
|
|
|
{
|
2013-07-12 02:28:46 +00:00
|
|
|
this._pointerData[pointer.id].x = pointer.x - this._parent.x;
|
|
|
|
this._pointerData[pointer.id].y = pointer.y - this._parent.y;
|
2013-06-03 11:03:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-05 01:58:16 +00:00
|
|
|
this._pointerOutHandler(pointer);
|
2013-06-03 11:03:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public _pointerOverHandler(pointer: Pointer) {
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-06-03 11:03:34 +00:00
|
|
|
// { id: i, x: 0, y: 0, isDown: false, isUp: false, isOver: false, isOut: false, timeOver: 0, timeOut: 0, isDragged: false }
|
|
|
|
|
|
|
|
if (this._pointerData[pointer.id].isOver == false)
|
|
|
|
{
|
|
|
|
this._pointerData[pointer.id].isOver = true;
|
|
|
|
this._pointerData[pointer.id].isOut = false;
|
|
|
|
this._pointerData[pointer.id].timeOver = this.game.time.now;
|
2013-07-12 02:28:46 +00:00
|
|
|
this._pointerData[pointer.id].x = pointer.x - this._parent.x;
|
|
|
|
this._pointerData[pointer.id].y = pointer.y - this._parent.y;
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-06-03 11:03:34 +00:00
|
|
|
if (this.useHandCursor && this._pointerData[pointer.id].isDragged == false)
|
2013-06-01 01:49:41 +00:00
|
|
|
{
|
2013-06-03 11:03:34 +00:00
|
|
|
this.game.stage.canvas.style.cursor = "pointer";
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
this._parent.events.onInputOver.dispatch(this._parent, pointer);
|
2013-06-02 13:19:12 +00:00
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-06-03 11:03:34 +00:00
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-06-05 01:58:16 +00:00
|
|
|
public _pointerOutHandler(pointer: Pointer) {
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-06-03 11:03:34 +00:00
|
|
|
this._pointerData[pointer.id].isOver = false;
|
|
|
|
this._pointerData[pointer.id].isOut = true;
|
|
|
|
this._pointerData[pointer.id].timeOut = this.game.time.now;
|
|
|
|
|
|
|
|
if (this.useHandCursor && this._pointerData[pointer.id].isDragged == false)
|
|
|
|
{
|
|
|
|
this.game.stage.canvas.style.cursor = "default";
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
this._parent.events.onInputOut.dispatch(this._parent, pointer);
|
2013-06-03 11:03:34 +00:00
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 11:03:34 +00:00
|
|
|
public _touchedHandler(pointer: Pointer): bool {
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-06-02 20:41:00 +00:00
|
|
|
if (this._pointerData[pointer.id].isDown == false && this._pointerData[pointer.id].isOver == true)
|
|
|
|
{
|
|
|
|
this._pointerData[pointer.id].isDown = true;
|
|
|
|
this._pointerData[pointer.id].isUp = false;
|
|
|
|
this._pointerData[pointer.id].timeDown = this.game.time.now;
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-08-02 17:32:26 +00:00
|
|
|
//console.log('touchedHandler: ' + Date.now());
|
2013-07-12 02:28:46 +00:00
|
|
|
this._parent.events.onInputDown.dispatch(this._parent, pointer);
|
2013-06-02 20:41:00 +00:00
|
|
|
|
2013-06-03 00:22:14 +00:00
|
|
|
// Start drag
|
2013-06-03 11:03:34 +00:00
|
|
|
//if (this.draggable && this.isDragged == false && pointer.targetObject == null)
|
|
|
|
if (this.draggable && this.isDragged == false)
|
2013-06-02 20:41:00 +00:00
|
|
|
{
|
|
|
|
this.startDrag(pointer);
|
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-06-13 00:55:32 +00:00
|
|
|
if (this.bringToTop)
|
|
|
|
{
|
2013-07-28 23:43:10 +00:00
|
|
|
this._parent.bringToTop();
|
2013-08-01 21:21:03 +00:00
|
|
|
//this._parent.game.world.group.bringToTop(this._parent);
|
2013-06-13 00:55:32 +00:00
|
|
|
}
|
|
|
|
|
2013-06-02 20:41:00 +00:00
|
|
|
}
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-06-03 11:03:34 +00:00
|
|
|
// Consume the event?
|
|
|
|
return this.consumePointerEvent;
|
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public _releasedHandler(pointer: Pointer) {
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
// If was previously touched by this Pointer, check if still is AND still over this item
|
2013-06-03 00:22:14 +00:00
|
|
|
if (this._pointerData[pointer.id].isDown && pointer.isUp)
|
|
|
|
{
|
|
|
|
this._pointerData[pointer.id].isDown = false;
|
|
|
|
this._pointerData[pointer.id].isUp = true;
|
|
|
|
this._pointerData[pointer.id].timeUp = this.game.time.now;
|
|
|
|
this._pointerData[pointer.id].downDuration = this._pointerData[pointer.id].timeUp - this._pointerData[pointer.id].timeDown;
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
// Only release the InputUp signal if the pointer is still over this sprite
|
|
|
|
if (SpriteUtils.overlapsXY(this._parent, pointer.worldX(), pointer.worldY()))
|
|
|
|
{
|
2013-08-02 17:32:26 +00:00
|
|
|
//console.log('releasedHandler: ' + Date.now());
|
2013-07-12 02:28:46 +00:00
|
|
|
this._parent.events.onInputUp.dispatch(this._parent, pointer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Pointer outside the sprite? Reset the cursor
|
|
|
|
if (this.useHandCursor)
|
|
|
|
{
|
|
|
|
this.game.stage.canvas.style.cursor = "default";
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
|
|
|
// Stop drag
|
|
|
|
if (this.draggable && this.isDragged && this._draggedPointerID == pointer.id)
|
|
|
|
{
|
|
|
|
this.stopDrag(pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-06-02 02:47:54 +00:00
|
|
|
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
/**
|
2013-06-02 13:19:12 +00:00
|
|
|
* Updates the Pointer drag on this Sprite.
|
2013-06-02 02:47:54 +00:00
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
private updateDrag(pointer: Pointer): bool {
|
|
|
|
|
|
|
|
if (pointer.isUp)
|
|
|
|
{
|
|
|
|
this.stopDrag(pointer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.allowHorizontalDrag)
|
|
|
|
{
|
|
|
|
this._parent.x = pointer.x + this._dragPoint.x + this.dragOffset.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.allowVerticalDrag)
|
|
|
|
{
|
|
|
|
this._parent.y = pointer.y + this._dragPoint.y + this.dragOffset.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.boundsRect)
|
|
|
|
{
|
|
|
|
this.checkBoundsRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.boundsSprite)
|
|
|
|
{
|
|
|
|
this.checkBoundsSprite();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.snapOnDrag)
|
|
|
|
{
|
|
|
|
this._parent.x = Math.floor(this._parent.x / this.snapX) * this.snapX;
|
|
|
|
this._parent.y = Math.floor(this._parent.y / this.snapY) * this.snapY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-02 02:47:54 +00:00
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the pointer has entered the Sprite within the specified delay time (defaults to 500ms, half a second)
|
|
|
|
* @param delay The time below which the pointer is considered as just over.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2013-06-02 13:19:12 +00:00
|
|
|
public justOver(pointer: number = 0, delay?: number = 500): bool {
|
|
|
|
return (this._pointerData[pointer].isOver && this.overDuration(pointer) < delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the pointer has left the Sprite within the specified delay time (defaults to 500ms, half a second)
|
|
|
|
* @param delay The time below which the pointer is considered as just out.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
public justOut(pointer: number = 0, delay?: number = 500): bool {
|
|
|
|
return (this._pointerData[pointer].isOut && (this.game.time.now - this._pointerData[pointer].timeOut < delay));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the pointer has entered the Sprite within the specified delay time (defaults to 500ms, half a second)
|
|
|
|
* @param delay The time below which the pointer is considered as just over.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
public justPressed(pointer: number = 0, delay?: number = 500): bool {
|
|
|
|
return (this._pointerData[pointer].isDown && this.downDuration(pointer) < delay);
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the pointer has left the Sprite within the specified delay time (defaults to 500ms, half a second)
|
|
|
|
* @param delay The time below which the pointer is considered as just out.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2013-06-02 13:19:12 +00:00
|
|
|
public justReleased(pointer: number = 0, delay?: number = 500): bool {
|
|
|
|
return (this._pointerData[pointer].isUp && (this.game.time.now - this._pointerData[pointer].timeUp < delay));
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
|
|
|
* If the pointer is currently over this Sprite this returns how long it has been there for in milliseconds.
|
|
|
|
* @returns {number} The number of milliseconds the pointer has been over the Sprite, or -1 if not over.
|
|
|
|
*/
|
2013-06-02 13:19:12 +00:00
|
|
|
public overDuration(pointer: number = 0): number {
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
if (this._pointerData[pointer].isOver)
|
2013-06-01 01:49:41 +00:00
|
|
|
{
|
2013-06-02 13:19:12 +00:00
|
|
|
return this.game.time.now - this._pointerData[pointer].timeOver;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the pointer is currently over this Sprite this returns how long it has been there for in milliseconds.
|
|
|
|
* @returns {number} The number of milliseconds the pointer has been pressed down on the Sprite, or -1 if not over.
|
|
|
|
*/
|
|
|
|
public downDuration(pointer: number = 0): number {
|
|
|
|
|
|
|
|
if (this._pointerData[pointer].isDown)
|
|
|
|
{
|
|
|
|
return this.game.time.now - this._pointerData[pointer].timeDown;
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
/**
|
2013-06-02 02:47:54 +00:00
|
|
|
* Make this Sprite draggable by the mouse. You can also optionally set mouseStartDragCallback and mouseStopDragCallback
|
|
|
|
*
|
2013-06-02 13:19:12 +00:00
|
|
|
* @param lockCenter If false the Sprite will drag from where you click it minus the dragOffset. If true it will center itself to the tip of the mouse pointer.
|
2013-06-13 00:55:32 +00:00
|
|
|
* @param bringToTop If true the Sprite will be bought to the top of the rendering list in its current Group.
|
2013-06-02 02:47:54 +00:00
|
|
|
* @param pixelPerfect If true it will use a pixel perfect test to see if you clicked the Sprite. False uses the bounding box.
|
|
|
|
* @param alphaThreshold If using pixel perfect collision this specifies the alpha level from 0 to 255 above which a collision is processed (default 255)
|
|
|
|
* @param boundsRect If you want to restrict the drag of this sprite to a specific FlxRect, pass the FlxRect here, otherwise it's free to drag anywhere
|
|
|
|
* @param boundsSprite If you want to restrict the drag of this sprite to within the bounding box of another sprite, pass it here
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public enableDrag(lockCenter: bool = false, bringToTop: bool = false, pixelPerfect: bool = false, alphaThreshold: number = 255, boundsRect: Rectangle = null, boundsSprite: Phaser.Sprite = null): void {
|
2013-06-02 13:19:12 +00:00
|
|
|
this._dragPoint = new Point;
|
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
this.draggable = true;
|
|
|
|
this.bringToTop = bringToTop;
|
|
|
|
|
2013-06-02 13:19:12 +00:00
|
|
|
this.dragOffset = new Point;
|
2013-08-06 23:00:12 +00:00
|
|
|
this.dragFromCenter = lockCenter;
|
|
|
|
this.dragPixelPerfect = pixelPerfect;
|
|
|
|
this.dragPixelPerfectAlpha = alphaThreshold;
|
|
|
|
|
|
|
|
if (boundsRect)
|
|
|
|
{
|
|
|
|
this.boundsRect = boundsRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boundsSprite)
|
|
|
|
{
|
|
|
|
this.boundsSprite = boundsSprite;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-02 02:47:54 +00:00
|
|
|
* Stops this sprite from being able to be dragged. If it is currently the target of an active drag it will be stopped immediately. Also disables any set callbacks.
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public disableDrag(): void {
|
|
|
|
|
|
|
|
if (this._pointerData)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
this._pointerData[i].isDragged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.draggable = false;
|
|
|
|
this.isDragged = false;
|
|
|
|
this._draggedPointerID = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-02 13:19:12 +00:00
|
|
|
* Called by Pointer when drag starts on this Sprite. Should not usually be called directly.
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public startDrag(pointer: Pointer): void {
|
|
|
|
|
|
|
|
this.isDragged = true;
|
|
|
|
this._draggedPointerID = pointer.id;
|
2013-06-02 13:19:12 +00:00
|
|
|
this._pointerData[pointer.id].isDragged = true;
|
2013-08-06 23:00:12 +00:00
|
|
|
|
2013-06-02 20:41:00 +00:00
|
|
|
if (this.dragFromCenter)
|
|
|
|
{
|
2013-08-06 23:00:12 +00:00
|
|
|
this._parent.transform.centerOn(pointer.worldX(), pointer.worldY());
|
2013-07-12 02:28:46 +00:00
|
|
|
this._dragPoint.setTo(this._parent.x - pointer.x, this._parent.y - pointer.y);
|
2013-06-02 20:41:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-12 02:28:46 +00:00
|
|
|
this._dragPoint.setTo(this._parent.x - pointer.x, this._parent.y - pointer.y);
|
2013-06-02 20:41:00 +00:00
|
|
|
}
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-06-05 01:58:16 +00:00
|
|
|
this.updateDrag(pointer);
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-06-13 00:55:32 +00:00
|
|
|
if (this.bringToTop)
|
|
|
|
{
|
2013-07-28 23:43:10 +00:00
|
|
|
this._parent.bringToTop();
|
2013-06-13 00:55:32 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
this._parent.events.onDragStart.dispatch(this._parent, pointer);
|
2013-07-02 22:41:25 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
}
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
/**
|
2013-06-02 13:19:12 +00:00
|
|
|
* Called by Pointer when drag is stopped on this Sprite. Should not usually be called directly.
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public stopDrag(pointer: Pointer): void {
|
|
|
|
|
|
|
|
this.isDragged = false;
|
|
|
|
this._draggedPointerID = -1;
|
2013-06-02 13:19:12 +00:00
|
|
|
this._pointerData[pointer.id].isDragged = false;
|
2013-08-06 23:00:12 +00:00
|
|
|
|
|
|
|
if (this.snapOnRelease)
|
|
|
|
{
|
|
|
|
this._parent.x = Math.floor(this._parent.x / this.snapX) * this.snapX;
|
|
|
|
this._parent.y = Math.floor(this._parent.y / this.snapY) * this.snapY;
|
|
|
|
}
|
2013-06-03 00:22:14 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
this._parent.events.onDragStop.dispatch(this._parent, pointer);
|
|
|
|
this._parent.events.onInputUp.dispatch(this._parent, pointer);
|
2013-06-02 13:19:12 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-02 02:47:54 +00:00
|
|
|
* Restricts this sprite to drag movement only on the given axis. Note: If both are set to false the sprite will never move!
|
|
|
|
*
|
|
|
|
* @param allowHorizontal To enable the sprite to be dragged horizontally set to true, otherwise false
|
|
|
|
* @param allowVertical To enable the sprite to be dragged vertically set to true, otherwise false
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public setDragLock(allowHorizontal: bool = true, allowVertical: bool = true): void {
|
|
|
|
this.allowHorizontalDrag = allowHorizontal;
|
|
|
|
this.allowVerticalDrag = allowVertical;
|
|
|
|
}
|
2013-06-02 02:47:54 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
/**
|
2013-06-02 02:47:54 +00:00
|
|
|
* Make this Sprite snap to the given grid either during drag or when it's released.
|
|
|
|
* For example 16x16 as the snapX and snapY would make the sprite snap to every 16 pixels.
|
|
|
|
*
|
|
|
|
* @param snapX The width of the grid cell in pixels
|
|
|
|
* @param snapY The height of the grid cell in pixels
|
|
|
|
* @param onDrag If true the sprite will snap to the grid while being dragged
|
|
|
|
* @param onRelease If true the sprite will snap to the grid when released
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public enableSnap(snapX: number, snapY: number, onDrag: bool = true, onRelease: bool = false): void {
|
|
|
|
this.snapOnDrag = onDrag;
|
|
|
|
this.snapOnRelease = onRelease;
|
|
|
|
this.snapX = snapX;
|
|
|
|
this.snapY = snapY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-02 02:47:54 +00:00
|
|
|
* Stops the sprite from snapping to a grid during drag or release.
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
public disableSnap(): void {
|
|
|
|
this.snapOnDrag = false;
|
|
|
|
this.snapOnRelease = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-02 02:47:54 +00:00
|
|
|
* Bounds Rect check for the sprite drag
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
private checkBoundsRect(): void {
|
|
|
|
if (this._parent.x < this.boundsRect.left)
|
|
|
|
{
|
|
|
|
this._parent.x = this.boundsRect.x;
|
|
|
|
}
|
|
|
|
else if ((this._parent.x + this._parent.width) > this.boundsRect.right)
|
|
|
|
{
|
|
|
|
this._parent.x = this.boundsRect.right - this._parent.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._parent.y < this.boundsRect.top)
|
|
|
|
{
|
|
|
|
this._parent.y = this.boundsRect.top;
|
|
|
|
}
|
|
|
|
else if ((this._parent.y + this._parent.height) > this.boundsRect.bottom)
|
|
|
|
{
|
|
|
|
this._parent.y = this.boundsRect.bottom - this._parent.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-02 02:47:54 +00:00
|
|
|
* Parent Sprite Bounds check for the sprite drag
|
|
|
|
*/
|
2013-08-06 23:00:12 +00:00
|
|
|
private checkBoundsSprite(): void {
|
|
|
|
if (this._parent.x < this.boundsSprite.x)
|
|
|
|
{
|
|
|
|
this._parent.x = this.boundsSprite.x;
|
|
|
|
}
|
|
|
|
else if ((this._parent.x + this._parent.width) > (this.boundsSprite.x + this.boundsSprite.width))
|
|
|
|
{
|
|
|
|
this._parent.x = (this.boundsSprite.x + this.boundsSprite.width) - this._parent.width;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
|
2013-08-06 23:00:12 +00:00
|
|
|
if (this._parent.y < this.boundsSprite.y)
|
|
|
|
{
|
|
|
|
this._parent.y = this.boundsSprite.y;
|
|
|
|
}
|
|
|
|
else if ((this._parent.y + this._parent.height) > (this.boundsSprite.y + this.boundsSprite.height))
|
|
|
|
{
|
|
|
|
this._parent.y = (this.boundsSprite.y + this.boundsSprite.height) - this._parent.height;
|
|
|
|
}
|
2013-06-01 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|