2013-06-06 01:47:08 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
|
|
|
/// <reference path="../gameobjects/DynamicTexture.ts" />
|
|
|
|
/// <reference path="../utils/SpriteUtils.ts" />
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-05-28 08:37:32 +00:00
|
|
|
/**
|
|
|
|
* Phaser - Components - Texture
|
|
|
|
*
|
2013-06-06 01:47:08 +00:00
|
|
|
* The Texture being used to render the object (Sprite, Group background, etc). Either Image based on a DynamicTexture.
|
2013-05-28 08:37:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser.Components {
|
|
|
|
|
|
|
|
export class Texture {
|
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* Creates a new Texture component
|
|
|
|
* @param parent The object using this Texture to render.
|
2013-06-01 18:59:20 +00:00
|
|
|
* @param key An optional Game.Cache key to load an image from
|
|
|
|
*/
|
2013-06-06 01:47:08 +00:00
|
|
|
constructor(parent) {
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
this.game = parent.game;
|
2013-06-06 01:47:08 +00:00
|
|
|
this.parent = parent;
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-05-29 14:45:34 +00:00
|
|
|
this.canvas = parent.game.stage.canvas;
|
|
|
|
this.context = parent.game.stage.context;
|
2013-05-28 20:38:37 +00:00
|
|
|
this.alpha = 1;
|
|
|
|
this.flippedX = false;
|
|
|
|
this.flippedY = false;
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
this._width = 16;
|
|
|
|
this._height = 16;
|
|
|
|
|
|
|
|
this.cameraBlacklist = [];
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
/**
|
|
|
|
* Private _width - use the width getter/setter instead
|
|
|
|
*/
|
|
|
|
private _width: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private _height - use the height getter/setter instead
|
|
|
|
*/
|
|
|
|
private _height: number;
|
2013-06-01 18:59:20 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
/**
|
2013-06-01 18:59:20 +00:00
|
|
|
* Reference to Phaser.Game
|
2013-05-29 01:58:56 +00:00
|
|
|
*/
|
2013-06-01 18:59:20 +00:00
|
|
|
public game: Game;
|
2013-05-29 01:58:56 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* Reference to the parent object (Sprite, Group, etc)
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-06-06 01:47:08 +00:00
|
|
|
public parent;
|
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.
|
|
|
|
*/
|
2013-05-30 04:34:35 +00:00
|
|
|
public imageTexture = null;
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the DynamicTexture that is used as the texture for the Sprite.
|
|
|
|
* @type {DynamicTexture}
|
|
|
|
*/
|
2013-05-30 04:34:35 +00:00
|
|
|
public dynamicTexture: DynamicTexture = null;
|
2013-05-28 20:38:37 +00:00
|
|
|
|
2013-05-29 11:24:25 +00:00
|
|
|
/**
|
2013-06-01 18:59:20 +00:00
|
|
|
* The load status of the texture image.
|
2013-05-29 11:24:25 +00:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public loaded: bool = false;
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* An Array of Cameras to which this texture won't render
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
|
|
|
public cameraBlacklist: number[];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the Sprite background is opaque or not. If set to true the Sprite is filled with
|
|
|
|
* the value of Texture.backgroundColor every frame. Normally you wouldn't enable this but
|
|
|
|
* for some effects it can be handy.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public opaque: bool = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opacity of the Sprite texture where 1 is opaque (default) and 0 is fully transparent.
|
2013-05-28 20:38:37 +00:00
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public alpha: number;
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
/**
|
|
|
|
* The Background Color of the Sprite if Texture.opaque is set to true.
|
|
|
|
* Given in css color string format, i.e. 'rgb(0,0,0)' or '#ff0000'.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
public backgroundColor: string = 'rgb(255,255,255)';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2013-05-30 04:34:35 +00:00
|
|
|
/**
|
|
|
|
* Is the texture a DynamicTexture?
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
public isDynamic: bool = false;
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
/**
|
|
|
|
* Updates the texture being used to render the Sprite.
|
|
|
|
* Called automatically by SpriteUtils.loadTexture and SpriteUtils.loadDynamicTexture.
|
|
|
|
*/
|
2013-06-06 01:47:08 +00:00
|
|
|
public setTo(image = null, dynamic?: DynamicTexture = null) {
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
if (dynamic)
|
|
|
|
{
|
2013-05-30 04:34:35 +00:00
|
|
|
this.isDynamic = true;
|
|
|
|
this.dynamicTexture = dynamic;
|
|
|
|
this.texture = this.dynamicTexture.canvas;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-30 04:34:35 +00:00
|
|
|
this.isDynamic = false;
|
|
|
|
this.imageTexture = image;
|
|
|
|
this.texture = this.imageTexture;
|
2013-06-06 01:47:08 +00:00
|
|
|
this._width = image.width;
|
|
|
|
this._height = image.height;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 11:24:25 +00:00
|
|
|
this.loaded = true;
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
return this.parent;
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2013-06-06 01:47:08 +00:00
|
|
|
* @param updateBody {boolean} Update the physics body dimensions to match the newly loaded texture/frame?
|
2013-05-28 20:38:37 +00:00
|
|
|
*/
|
2013-05-31 18:28:16 +00:00
|
|
|
public loadImage(key: string, clearAnimations?: bool = true, updateBody?: bool = true) {
|
2013-05-29 01:58:56 +00:00
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
if (clearAnimations && this.parent['animations'] && this.parent['animations'].frameData !== null)
|
2013-05-29 14:45:34 +00:00
|
|
|
{
|
2013-06-06 01:47:08 +00:00
|
|
|
this.parent.animations.destroy();
|
2013-05-29 14:45:34 +00:00
|
|
|
}
|
2013-05-29 01:58:56 +00:00
|
|
|
|
2013-06-01 18:59:20 +00:00
|
|
|
if (this.game.cache.getImage(key) !== null)
|
2013-05-29 01:58:56 +00:00
|
|
|
{
|
2013-06-01 18:59:20 +00:00
|
|
|
this.setTo(this.game.cache.getImage(key), null);
|
2013-06-06 01:47:08 +00:00
|
|
|
this.cacheKey = key;
|
2013-05-29 01:58:56 +00:00
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
if (this.game.cache.isSpriteSheet(key) && this.parent['animations'])
|
2013-05-29 01:58:56 +00:00
|
|
|
{
|
2013-06-06 01:47:08 +00:00
|
|
|
this.parent.animations.loadFrameData(this.parent.game.cache.getFrameData(key));
|
2013-05-29 01:58:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-06 01:47:08 +00:00
|
|
|
if (updateBody && this.parent['body'])
|
|
|
|
{
|
|
|
|
this.parent.body.bounds.width = this.width;
|
|
|
|
this.parent.body.bounds.height = this.height;
|
|
|
|
}
|
2013-05-31 18:28:16 +00:00
|
|
|
}
|
2013-05-29 01:58:56 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-05-29 01:58:56 +00:00
|
|
|
public loadDynamicTexture(texture: DynamicTexture) {
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
if (this.parent.animations.frameData !== null)
|
2013-05-29 14:45:34 +00:00
|
|
|
{
|
2013-06-06 01:47:08 +00:00
|
|
|
this.parent.animations.destroy();
|
2013-05-29 14:45:34 +00:00
|
|
|
}
|
2013-05-29 01:58:56 +00:00
|
|
|
|
|
|
|
this.setTo(null, texture);
|
2013-06-06 01:47:08 +00:00
|
|
|
this.parent.texture.width = this.width;
|
|
|
|
this.parent.texture.height = this.height;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public set width(value: number) {
|
|
|
|
this._width = value;
|
|
|
|
}
|
2013-05-29 01:58:56 +00:00
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
public set height(value: number) {
|
|
|
|
this._height = value;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* The width of the texture. If an animation it will be the frame width, not the width of the sprite sheet.
|
|
|
|
* If using a DynamicTexture it will be the width of the dynamic texture itself.
|
2013-05-28 20:38:37 +00:00
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public get width(): number {
|
|
|
|
|
2013-05-30 04:34:35 +00:00
|
|
|
if (this.isDynamic)
|
2013-05-28 20:38:37 +00:00
|
|
|
{
|
2013-05-30 04:34:35 +00:00
|
|
|
return this.dynamicTexture.width;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-06 01:47:08 +00:00
|
|
|
return this._width;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* The height of the texture. If an animation it will be the frame height, not the height of the sprite sheet.
|
|
|
|
* If using a DynamicTexture it will be the height of the dynamic texture itself.
|
2013-05-28 20:38:37 +00:00
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public get height(): number {
|
|
|
|
|
2013-05-30 04:34:35 +00:00
|
|
|
if (this.isDynamic)
|
2013-05-28 20:38:37 +00:00
|
|
|
{
|
2013-05-30 04:34:35 +00:00
|
|
|
return this.dynamicTexture.height;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-06 01:47:08 +00:00
|
|
|
return this._height;
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-05-28 08:37:32 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
}
|
2013-05-28 08:37:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|