Added the new Button game object and various other input and tilemap fixes.

This commit is contained in:
Richard Davey
2013-07-12 03:28:46 +01:00
parent c81cf0c882
commit dcce99ec60
42 changed files with 1898 additions and 363 deletions
+211
View File
@@ -0,0 +1,211 @@
/// <reference path="../Game.ts" />
/// <reference path="../math/Vec2.ts" />
/// <reference path="../geom/Rectangle.ts" />
/// <reference path="../components/animation/AnimationManager.ts" />
/// <reference path="../components/Texture.ts" />
/// <reference path="../components/Transform.ts" />
/// <reference path="../components/sprite/Input.ts" />
/// <reference path="../components/sprite/Events.ts" />
/**
* Phaser - Button
*/
module Phaser {
export class Button extends Sprite {
/**
* Create a new <code>Button</code> object.
*
* @param game {Phaser.Game} Current game instance.
* @param [x] {number} the initial x position of the button.
* @param [y] {number} the initial y position of the button.
* @param [key] {string} Key of the graphic you want to load for this button.
*/
constructor(game: Game, x?: number = 0, y?: number = 0, key?: string = null, callback? = null, callbackContext? = null, overFrame? = null, outFrame? = null, downFrame? = null) {
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);
// By default we'll position it using screen space, not world space.
this.transform.scrollFactor.setTo(0, 0);
}
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;
//public tabEnabled: bool;
// ENTER or SPACE can activate this button if it has focus
private onInputOverHandler(pointer:Phaser.Pointer) {
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);
}
}
private onInputOutHandler(pointer:Phaser.Pointer) {
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);
}
}
private onInputDownHandler(pointer:Phaser.Pointer) {
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);
}
}
private onInputUpHandler(pointer:Phaser.Pointer) {
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;
}
public set useHandCursor(value: bool) {
this.input.useHandCursor = value;
}
public get useHandCursor(): bool {
return this.input.useHandCursor;
}
}
}
+27 -3
View File
@@ -77,6 +77,14 @@ module Phaser {
*/
public context: CanvasRenderingContext2D;
/**
* You can set a globalCompositeOperation that will be applied before the render method is called on this Sprite.
* This is useful if you wish to apply an effect like 'lighten'.
* If this value is set it will call a canvas context save and restore before and after the render pass, so use it sparingly.
* Set to null to disable.
*/
public globalCompositeOperation: string = null;
/**
* Get a color of a specific pixel.
* @param x {number} X position of the pixel in this texture.
@@ -252,12 +260,15 @@ module Phaser {
* Given an array of Sprites it will update each of them so that their canvas/contexts reference this DynamicTexture
* @param objects {Array} An array of GameObjects, or objects that inherit from it such as Sprites
*/
public assignCanvasToGameObjects(objects: IGameObject[]) {
public assignCanvasToGameObjects(objects) {
for (var i = 0; i < objects.length; i++)
{
objects[i].texture.canvas = this.canvas;
objects[i].texture.context = this.context;
if (objects[i].texture)
{
objects[i].texture.canvas = this.canvas;
objects[i].texture.context = this.context;
}
}
}
@@ -278,7 +289,20 @@ module Phaser {
* @param y {number} The Y coordinate to render on the stage to (given in screen coordinates, not world)
*/
public render(x?: number = 0, y?: number = 0) {
if (this.globalCompositeOperation)
{
this.game.stage.context.save();
this.game.stage.context.globalCompositeOperation = this.globalCompositeOperation;
}
this.game.stage.context.drawImage(this.canvas, x, y);
if (this.globalCompositeOperation)
{
this.game.stage.context.restore();
}
}
public get width(): number {
+18
View File
@@ -3,6 +3,7 @@
/// <reference path="../gameobjects/Emitter.ts" />
/// <reference path="../gameobjects/Particle.ts" />
/// <reference path="../gameobjects/Sprite.ts" />
/// <reference path="../gameobjects/Button.ts" />
/// <reference path="../gameobjects/ScrollZone.ts" />
/// <reference path="../gameobjects/DynamicTexture.ts" />
/// <reference path="../gameobjects/Tilemap.ts" />
@@ -62,6 +63,23 @@ module Phaser {
// return <GeomSprite> this._world.group.add(new GeomSprite(this._game, x, y));
//}
/**
* Create a new Button game object.
*
* @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.
* @returns {Button} The newly created button object.
*/
public button(x?: number = 0, y?: number = 0, key?: string = null, callback? = null, callbackContext? = null, overFrame? = null, outFrame? = null, downFrame? = null): Button {
return <Button> this._world.group.add(new Button(this._game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame));
}
/**
* Create a new Sprite with specific position and sprite sheet key.
*
+15
View File
@@ -54,6 +54,21 @@ module Phaser {
*/
visible: bool;
/**
* The animation manager component
*/
animations: Phaser.Components.AnimationManager;
/**
* Associated events
*/
events;
/**
* The input component
*/
input;
/**
* The texture used to render.
*/
+15 -6
View File
@@ -42,9 +42,9 @@ module Phaser {
this.group = null;
this.name = '';
this.events = new Phaser.Components.Sprite.Events(this);
this.animations = new Phaser.Components.AnimationManager(this);
this.input = new Phaser.Components.Sprite.Input(this);
this.events = new Phaser.Components.Sprite.Events(this);
this.texture = new Phaser.Components.Texture(this);
this.transform = new Phaser.Components.Transform(this);
@@ -224,17 +224,26 @@ module Phaser {
*/
public scale: Phaser.Vec2;
/**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
public alpha: number;
/**
* The origin of the Sprite around which rotation and positioning takes place.
* This is a reference to Sprite.transform.origin
*/
public origin: Phaser.Vec2;
/**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
public set alpha(value: number) {
this.texture.alpha = value;
}
/**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
public get alpha(): number {
return this.texture.alpha;
}
/**
* Set the animation frame by frame number.
*/
+3
View File
@@ -206,6 +206,9 @@ module Phaser {
public postUpdate() {
}
public destroy() {
}
/**
* Parset csv map data and generate tiles.
* @param data {string} CSV map data.