2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-06 02:14:48 +00:00
|
|
|
* Phaser - UI - Button
|
2013-07-12 02:28:46 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-06 02:14:48 +00:00
|
|
|
module Phaser.UI {
|
2013-07-12 02:28:46 +00:00
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
export class Button extends Phaser.Sprite {
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new <code>Button</code> object.
|
|
|
|
*
|
|
|
|
* @param game {Phaser.Game} Current game instance.
|
2013-07-13 11:38:59 +00:00
|
|
|
* @param [x] {number} X position of the button.
|
|
|
|
* @param [y] {number} Y position of the button.
|
|
|
|
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this button.
|
|
|
|
* @param [callback] {function} The function to call when this button is pressed
|
|
|
|
* @param [callbackContext] {object} The context in which the callback will be called (usually 'this')
|
|
|
|
* @param [overFrame] {string|number} This is the frame or frameName that will be set when this button is in an over state. Give either a number to use a frame ID or a string for a frame name.
|
|
|
|
* @param [outFrame] {string|number} This is the frame or frameName that will be set when this button is in an out state. Give either a number to use a frame ID or a string for a frame name.
|
|
|
|
* @param [downFrame] {string|number} This is the frame or frameName that will be set when this button is in a down state. Give either a number to use a frame ID or a string for a frame name.
|
2013-07-12 02:28:46 +00:00
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
constructor(game: Phaser.Game, x: number = 0, y: number = 0, key: string = null, callback = null, callbackContext = null, overFrame = null, outFrame = null, downFrame = null) {
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
super(game, x, y, key, outFrame);
|
|
|
|
|
|
|
|
this.type = Phaser.Types.BUTTON;
|
|
|
|
|
|
|
|
if (typeof overFrame == 'string')
|
|
|
|
{
|
|
|
|
this._onOverFrameName = overFrame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._onOverFrameID = overFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof outFrame == 'string')
|
|
|
|
{
|
|
|
|
this._onOutFrameName = outFrame;
|
|
|
|
this._onUpFrameName = outFrame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._onOutFrameID = outFrame;
|
|
|
|
this._onUpFrameID = outFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof downFrame == 'string')
|
|
|
|
{
|
|
|
|
this._onDownFrameName = downFrame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._onDownFrameID = downFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are the signals the game will subscribe to
|
|
|
|
this.onInputOver = new Phaser.Signal;
|
|
|
|
this.onInputOut = new Phaser.Signal;
|
|
|
|
this.onInputDown = new Phaser.Signal;
|
|
|
|
this.onInputUp = new Phaser.Signal;
|
|
|
|
|
|
|
|
// Set a default signal for them
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
this.onInputUp.add(callback, callbackContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.input.start(0, false, true);
|
|
|
|
|
|
|
|
// Redirect the input events to here so we can handle animation updates, etc
|
|
|
|
this.events.onInputOver.add(this.onInputOverHandler, this);
|
|
|
|
this.events.onInputOut.add(this.onInputOutHandler, this);
|
|
|
|
this.events.onInputDown.add(this.onInputDownHandler, this);
|
|
|
|
this.events.onInputUp.add(this.onInputUpHandler, this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onOverFrameName = null;
|
|
|
|
private _onOutFrameName = null;
|
|
|
|
private _onDownFrameName = null;
|
|
|
|
private _onUpFrameName = null;
|
|
|
|
private _onOverFrameID = null;
|
|
|
|
private _onOutFrameID = null;
|
|
|
|
private _onDownFrameID = null;
|
|
|
|
private _onUpFrameID = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatched when a pointer moves over an Input enabled sprite.
|
|
|
|
*/
|
|
|
|
public onInputOver: Phaser.Signal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatched when a pointer moves out of an Input enabled sprite.
|
|
|
|
*/
|
|
|
|
public onInputOut: Phaser.Signal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatched when a pointer is pressed down on an Input enabled sprite.
|
|
|
|
*/
|
|
|
|
public onInputDown: Phaser.Signal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatched when a pointer is released over an Input enabled sprite
|
|
|
|
*/
|
|
|
|
public onInputUp: Phaser.Signal;
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
//public tabIndex: number;
|
2013-08-13 03:22:24 +00:00
|
|
|
//public tabEnabled: bool;
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
// ENTER or SPACE can activate this button if it has focus
|
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
private onInputOverHandler(pointer: Phaser.Pointer) {
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
if (this._onOverFrameName != null)
|
|
|
|
{
|
|
|
|
this.frameName = this._onOverFrameName;
|
|
|
|
}
|
|
|
|
else if (this._onOverFrameID != null)
|
|
|
|
{
|
|
|
|
this.frame = this._onOverFrameID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.onInputOver)
|
|
|
|
{
|
|
|
|
this.onInputOver.dispatch(this, pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
private onInputOutHandler(pointer: Phaser.Pointer) {
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
if (this._onOutFrameName != null)
|
|
|
|
{
|
|
|
|
this.frameName = this._onOutFrameName;
|
|
|
|
}
|
|
|
|
else if (this._onOutFrameID != null)
|
|
|
|
{
|
|
|
|
this.frame = this._onOutFrameID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.onInputOut)
|
|
|
|
{
|
|
|
|
this.onInputOut.dispatch(this, pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
private onInputDownHandler(pointer: Phaser.Pointer) {
|
2013-07-12 02:28:46 +00:00
|
|
|
|
2013-08-02 17:32:26 +00:00
|
|
|
//console.log('Button onInputDownHandler: ' + Date.now());
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
if (this._onDownFrameName != null)
|
|
|
|
{
|
|
|
|
this.frameName = this._onDownFrameName;
|
|
|
|
}
|
|
|
|
else if (this._onDownFrameID != null)
|
|
|
|
{
|
|
|
|
this.frame = this._onDownFrameID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.onInputDown)
|
|
|
|
{
|
|
|
|
this.onInputDown.dispatch(this, pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
private onInputUpHandler(pointer: Phaser.Pointer) {
|
2013-07-12 02:28:46 +00:00
|
|
|
|
2013-08-02 17:32:26 +00:00
|
|
|
//console.log('Button onInputUpHandler: ' + Date.now());
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
if (this._onUpFrameName != null)
|
|
|
|
{
|
|
|
|
this.frameName = this._onUpFrameName;
|
|
|
|
}
|
|
|
|
else if (this._onUpFrameID != null)
|
|
|
|
{
|
|
|
|
this.frame = this._onUpFrameID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.onInputUp)
|
|
|
|
{
|
|
|
|
this.onInputUp.dispatch(this, pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public set priorityID(value: number) {
|
|
|
|
this.input.priorityID = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get priorityID(): number {
|
|
|
|
return this.input.priorityID;
|
|
|
|
}
|
|
|
|
|
2013-08-13 03:22:24 +00:00
|
|
|
public set useHandCursor(value: bool) {
|
2013-07-12 02:28:46 +00:00
|
|
|
this.input.useHandCursor = value;
|
|
|
|
}
|
|
|
|
|
2013-08-13 03:22:24 +00:00
|
|
|
public get useHandCursor(): bool {
|
2013-07-12 02:28:46 +00:00
|
|
|
return this.input.useHandCursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|