2013-05-28 20:38:37 +00:00
|
|
|
/// <reference path="../../Game.ts" />
|
|
|
|
/// <reference path="../../gameobjects/DynamicTexture.ts" />
|
|
|
|
/// <reference path="../../utils/SpriteUtils.ts" />
|
|
|
|
|
2013-05-28 08:37:32 +00:00
|
|
|
/**
|
|
|
|
* Phaser - Components - Texture
|
|
|
|
*
|
2013-05-28 20:38:37 +00:00
|
|
|
* The Texture being used to render the Sprite. Either Image based on a DynamicTexture.
|
2013-05-28 08:37:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser.Components {
|
|
|
|
|
|
|
|
export class Texture {
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
constructor(parent: Sprite, key?: string = '', canvas?: HTMLCanvasElement = null, context?: CanvasRenderingContext2D = null) {
|
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this._game = parent.game;
|
2013-05-28 20:38:37 +00:00
|
|
|
this._sprite = parent;
|
|
|
|
|
|
|
|
this.canvas = canvas;
|
|
|
|
this.context = context;
|
|
|
|
this.alpha = 1;
|
|
|
|
this.flippedX = false;
|
|
|
|
this.flippedY = false;
|
|
|
|
|
|
|
|
if (key !== null)
|
|
|
|
{
|
|
|
|
this.cacheKey = key;
|
|
|
|
this.loadImage(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private _game: Game;
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
|
|
|
* Reference to the Image stored in the Game.Cache that is used as the texture for the Sprite.
|
|
|
|
*/
|
|
|
|
private _sprite: Sprite;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the Image stored in the Game.Cache that is used as the texture for the Sprite.
|
|
|
|
*/
|
|
|
|
private _imageTexture = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the DynamicTexture that is used as the texture for the Sprite.
|
|
|
|
* @type {DynamicTexture}
|
|
|
|
*/
|
|
|
|
private _dynamicTexture: DynamicTexture = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opacity of the Sprite texture where 1 is opaque and 0 is fully transparent.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public alpha: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the Canvas this Sprite renders to.
|
|
|
|
* @type {HTMLCanvasElement}
|
|
|
|
*/
|
|
|
|
public canvas: HTMLCanvasElement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the Canvas Context2D this Sprite renders to.
|
|
|
|
* @type {CanvasRenderingContext2D}
|
|
|
|
*/
|
|
|
|
public context: CanvasRenderingContext2D;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Cache key used for the Image Texture.
|
|
|
|
*/
|
|
|
|
public cacheKey: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Texture being used to render the Sprite. Either an Image Texture from the Cache or a DynamicTexture.
|
|
|
|
*/
|
|
|
|
public texture;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls if the Sprite is rendered rotated or not.
|
|
|
|
* If renderRotation is false then the object can still rotate but it will never be rendered rotated.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public renderRotation: bool = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flip the graphic horizontally (defaults to false)
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public flippedX: bool = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flip the graphic vertically (defaults to false)
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public flippedY: bool = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the texture being used to render the Sprite.
|
|
|
|
* Called automatically by SpriteUtils.loadTexture and SpriteUtils.loadDynamicTexture.
|
|
|
|
*/
|
|
|
|
public setTo(image = null, dynamic?: DynamicTexture = null): Sprite {
|
|
|
|
|
|
|
|
if (dynamic)
|
|
|
|
{
|
|
|
|
this._dynamicTexture = dynamic;
|
|
|
|
this.texture = this._dynamicTexture.canvas;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._imageTexture = image;
|
|
|
|
this.texture = this._imageTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._sprite;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new graphic from the game cache to use as the texture for this Sprite.
|
|
|
|
* The graphic can be SpriteSheet or Texture Atlas. If you need to use a DynamicTexture see loadDynamicTexture.
|
|
|
|
* @param key {string} Key of the graphic you want to load for this sprite.
|
|
|
|
* @param clearAnimations {boolean} If this Sprite has a set of animation data already loaded you can choose to keep or clear it with this boolean
|
|
|
|
* @return {Sprite} Sprite instance itself.
|
|
|
|
*/
|
2013-05-29 01:58:56 +00:00
|
|
|
public loadImage(key: string, clearAnimations: bool = true) {
|
|
|
|
|
|
|
|
//if (clearAnimations && sprite.animations.frameData !== null)
|
|
|
|
//{
|
|
|
|
// sprite.animations.destroy();
|
|
|
|
//}
|
|
|
|
|
|
|
|
if (this._game.cache.getImage(key) !== null)
|
|
|
|
{
|
|
|
|
this.setTo(this._game.cache.getImage(key), null);
|
|
|
|
|
|
|
|
if (this._game.cache.isSpriteSheet(key))
|
|
|
|
{
|
|
|
|
//sprite.animations.loadFrameData(sprite._game.cache.getFrameData(key));
|
|
|
|
//sprite.collisionMask.width = sprite.animations.currentFrame.width;
|
|
|
|
//sprite.collisionMask.height = sprite.animations.currentFrame.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._sprite.frameBounds.width = this.width;
|
|
|
|
this._sprite.frameBounds.height = this.height;
|
|
|
|
//sprite.collisionMask.width = sprite._texture.width;
|
|
|
|
//sprite.collisionMask.height = sprite._texture.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a DynamicTexture as its texture.
|
|
|
|
* @param texture {DynamicTexture} The texture object to be used by this sprite.
|
|
|
|
* @return {Sprite} Sprite instance itself.
|
|
|
|
*/
|
2013-05-29 01:58:56 +00:00
|
|
|
public loadDynamicTexture(texture: DynamicTexture) {
|
|
|
|
|
|
|
|
//if (sprite.animations.frameData !== null)
|
|
|
|
//{
|
|
|
|
// sprite.animations.destroy();
|
|
|
|
//}
|
|
|
|
|
|
|
|
this.setTo(null, texture);
|
|
|
|
this._sprite.frameBounds.width = this.width;
|
|
|
|
this._sprite.frameBounds.height = this.height;
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter only. The width of the texture.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public get width(): number {
|
|
|
|
|
|
|
|
if (this._dynamicTexture)
|
|
|
|
{
|
|
|
|
return this._dynamicTexture.width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this._imageTexture.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter only. The height of the texture.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public get height(): number {
|
|
|
|
|
|
|
|
if (this._dynamicTexture)
|
|
|
|
{
|
|
|
|
return this._dynamicTexture.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this._imageTexture.height;
|
|
|
|
}
|
2013-05-28 08:37:32 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-05-28 08:37:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|