2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-08-06 02:14:48 +00:00
|
|
|
* Phaser - Display - DynamicTexture
|
2013-04-18 15:49:08 +00:00
|
|
|
*
|
|
|
|
* A DynamicTexture can be thought of as a mini canvas into which you can draw anything.
|
|
|
|
* Game Objects can be assigned a DynamicTexture, so when they render in the world they do so
|
|
|
|
* based on the contents of the texture at the time. This allows you to create powerful effects
|
|
|
|
* once and have them replicated across as many game objects as you like.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
module Phaser.Display {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
export class DynamicTexture {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* DynamicTexture constructor
|
|
|
|
* Create a new <code>DynamicTexture</code>.
|
|
|
|
*
|
|
|
|
* @param game {Phaser.Game} Current game instance.
|
|
|
|
* @param width {number} Init width of this texture.
|
|
|
|
* @param height {number} Init height of this texture.
|
|
|
|
*/
|
2013-04-22 00:53:24 +00:00
|
|
|
constructor(game: Game, width: number, height: number) {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
this.game = game;
|
2013-08-08 18:16:47 +00:00
|
|
|
this.type = Phaser.Types.DYNAMICTEXTURE;
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.canvas = <HTMLCanvasElement> document.createElement('canvas');
|
|
|
|
this.canvas.width = width;
|
|
|
|
this.canvas.height = height;
|
|
|
|
this.context = this.canvas.getContext('2d');
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-08-06 02:14:48 +00:00
|
|
|
this.css3 = new Phaser.Display.CSS3Filters(this.canvas);
|
2013-07-19 03:57:14 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.bounds = new Rectangle(0, 0, width, height);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Reference to game.
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public game: Phaser.Game;
|
2013-05-28 20:38:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of game object.
|
|
|
|
*/
|
|
|
|
public type: number;
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
private _sx: number = 0;
|
|
|
|
private _sy: number = 0;
|
|
|
|
private _sw: number = 0;
|
|
|
|
private _sh: number = 0;
|
|
|
|
private _dx: number = 0;
|
|
|
|
private _dy: number = 0;
|
|
|
|
private _dw: number = 0;
|
|
|
|
private _dh: number = 0;
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Input / Output nodes?
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-07-19 03:57:14 +00:00
|
|
|
/**
|
|
|
|
* Controls the CSS3 Filters applied to the textures canvas object.
|
|
|
|
* Only really useful if you attach this canvas to the DOM.
|
|
|
|
* @type {Phaser.Components.CSS3Filters}
|
|
|
|
*/
|
2013-08-06 02:14:48 +00:00
|
|
|
public css3: Phaser.Display.CSS3Filters;
|
2013-07-19 03:57:14 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Bound of this texture with width and height info.
|
|
|
|
* @type {Rectangle}
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public bounds: Phaser.Rectangle;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* This class is actually a wrapper of canvas.
|
|
|
|
* @type {HTMLCanvasElement}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public canvas: HTMLCanvasElement;
|
2013-05-14 02:37:38 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Canvas context of this object.
|
|
|
|
* @type {CanvasRenderingContext2D}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public context: CanvasRenderingContext2D;
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
/**
|
|
|
|
* 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-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Get a color of a specific pixel.
|
|
|
|
* @param x {number} X position of the pixel in this texture.
|
|
|
|
* @param y {number} Y position of the pixel in this texture.
|
|
|
|
* @return {number} A native color value integer (format: 0xRRGGBB)
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public getPixel(x: number, y: number): number {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//r = imageData.data[0];
|
|
|
|
//g = imageData.data[1];
|
|
|
|
//b = imageData.data[2];
|
|
|
|
//a = imageData.data[3];
|
|
|
|
var imageData = this.context.getImageData(x, y, 1, 1);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
return Phaser.ColorUtils.getColor(imageData.data[0], imageData.data[1], imageData.data[2]);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Get a color of a specific pixel (including alpha value).
|
|
|
|
* @param x {number} X position of the pixel in this texture.
|
|
|
|
* @param y {number} Y position of the pixel in this texture.
|
|
|
|
* @return A native color value integer (format: 0xAARRGGBB)
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public getPixel32(x: number, y: number) {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var imageData = this.context.getImageData(x, y, 1, 1);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
return Phaser.ColorUtils.getColor32(imageData.data[3], imageData.data[0], imageData.data[1], imageData.data[2]);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* Get pixels in array in a specific Rectangle.
|
|
|
|
* @param rect {Rectangle} The specific Rectangle.
|
2013-05-04 12:02:12 +00:00
|
|
|
* @returns {array} CanvasPixelArray.
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public getPixels(rect: Phaser.Rectangle) {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return this.context.getImageData(rect.x, rect.y, rect.width, rect.height);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Set color of a specific pixel.
|
|
|
|
* @param x {number} X position of the target pixel.
|
|
|
|
* @param y {number} Y position of the target pixel.
|
|
|
|
* @param color {number} Native integer with color value. (format: 0xRRGGBB)
|
|
|
|
*/
|
2013-05-18 02:05:28 +00:00
|
|
|
public setPixel(x: number, y: number, color: string) {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context.fillStyle = color;
|
|
|
|
this.context.fillRect(x, y, 1, 1);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Set color (with alpha) of a specific pixel.
|
|
|
|
* @param x {number} X position of the target pixel.
|
|
|
|
* @param y {number} Y position of the target pixel.
|
|
|
|
* @param color {number} Native integer with color value. (format: 0xAARRGGBB)
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public setPixel32(x: number, y: number, color: number) {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context.fillStyle = color;
|
|
|
|
this.context.fillRect(x, y, 1, 1);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* Set image data to a specific Rectangle.
|
|
|
|
* @param rect {Rectangle} Target Rectangle.
|
2013-05-04 12:02:12 +00:00
|
|
|
* @param input {object} Source image data.
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public setPixels(rect: Phaser.Rectangle, input) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
this.context.putImageData(input, rect.x, rect.y);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-06-06 01:47:08 +00:00
|
|
|
* Fill a given Rectangle with specific color.
|
|
|
|
* @param rect {Rectangle} Target Rectangle you want to fill.
|
2013-05-04 12:02:12 +00:00
|
|
|
* @param color {number} A native number with color value. (format: 0xRRGGBB)
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public fillRect(rect: Phaser.Rectangle, color: number) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
this.context.fillStyle = color;
|
|
|
|
this.context.fillRect(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
|
2013-04-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public pasteImage(key: string, frame: number = -1, destX: number = 0, destY: number = 0, destWidth: number = null, destHeight: number = null) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
var texture = null;
|
|
|
|
var frameData;
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._sx = 0;
|
|
|
|
this._sy = 0;
|
|
|
|
this._dx = destX;
|
|
|
|
this._dy = destY;
|
|
|
|
|
|
|
|
// TODO - Load a frame from a sprite sheet, otherwise we'll draw the whole lot
|
|
|
|
if (frame > -1)
|
|
|
|
{
|
2013-05-28 20:38:37 +00:00
|
|
|
//if (this.game.cache.isSpriteSheet(key))
|
2013-04-18 13:16:18 +00:00
|
|
|
//{
|
2013-05-28 20:38:37 +00:00
|
|
|
// texture = this.game.cache.getImage(key);
|
|
|
|
//this.animations.loadFrameData(this.game.cache.getFrameData(key));
|
2013-04-18 13:16:18 +00:00
|
|
|
//}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-28 20:38:37 +00:00
|
|
|
texture = this.game.cache.getImage(key);
|
2013-04-18 13:16:18 +00:00
|
|
|
this._sw = texture.width;
|
|
|
|
this._sh = texture.height;
|
|
|
|
this._dw = texture.width;
|
|
|
|
this._dh = texture.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destWidth !== null)
|
|
|
|
{
|
|
|
|
this._dw = destWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (destHeight !== null)
|
|
|
|
{
|
|
|
|
this._dh = destHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture != null)
|
|
|
|
{
|
|
|
|
this.context.drawImage(
|
2013-05-04 12:02:12 +00:00
|
|
|
texture, // Source Image
|
|
|
|
this._sx, // Source X (location within the source image)
|
|
|
|
this._sy, // Source Y
|
|
|
|
this._sw, // Source Width
|
|
|
|
this._sh, // Source Height
|
|
|
|
this._dx, // Destination X (where on the canvas it'll be drawn)
|
|
|
|
this._dy, // Destination Y
|
|
|
|
this._dw, // Destination Width (always same as Source Width unless scaled)
|
|
|
|
this._dh // Destination Height (always same as Source Height unless scaled)
|
2013-04-18 13:16:18 +00:00
|
|
|
);
|
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-08-13 03:22:24 +00:00
|
|
|
// TODO - Add in support for: alphaBitmapData: BitmapData = null, alphaPoint: Point = null, mergeAlpha: bool = false
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Copy pixel from another DynamicTexture to this texture.
|
|
|
|
* @param sourceTexture {DynamicTexture} Source texture object.
|
2013-06-06 01:47:08 +00:00
|
|
|
* @param sourceRect {Rectangle} The specific region Rectangle to be copied to this in the source.
|
2013-05-04 12:02:12 +00:00
|
|
|
* @param destPoint {Point} Top-left point the target image data will be paste at.
|
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public copyPixels(sourceTexture: Phaser.Display.DynamicTexture, sourceRect: Phaser.Rectangle, destPoint: Phaser.Point) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
// Swap for drawImage if the sourceRect is the same size as the sourceTexture to avoid a costly getImageData call
|
2013-05-29 01:58:56 +00:00
|
|
|
if (Phaser.RectangleUtils.equals(sourceRect, this.bounds) == true)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
|
|
|
this.context.drawImage(sourceTexture.canvas, destPoint.x, destPoint.y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.context.putImageData(sourceTexture.getPixels(sourceRect), destPoint.x, destPoint.y);
|
|
|
|
}
|
|
|
|
|
2013-04-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 18:16:47 +00:00
|
|
|
public add(sprite: Phaser.Sprite) {
|
2013-08-02 17:32:26 +00:00
|
|
|
|
|
|
|
sprite.texture.canvas = this.canvas;
|
|
|
|
sprite.texture.context = this.context;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-17 05:49:43 +00:00
|
|
|
/**
|
2013-05-29 01:58:56 +00:00
|
|
|
* Given an array of Sprites it will update each of them so that their canvas/contexts reference this DynamicTexture
|
2013-05-17 05:49:43 +00:00
|
|
|
* @param objects {Array} An array of GameObjects, or objects that inherit from it such as Sprites
|
|
|
|
*/
|
2013-07-12 02:28:46 +00:00
|
|
|
public assignCanvasToGameObjects(objects) {
|
2013-05-17 05:49:43 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < objects.length; i++)
|
|
|
|
{
|
2013-07-12 02:28:46 +00:00
|
|
|
if (objects[i].texture)
|
|
|
|
{
|
|
|
|
objects[i].texture.canvas = this.canvas;
|
|
|
|
objects[i].texture.context = this.context;
|
|
|
|
}
|
2013-05-17 05:49:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Clear the whole canvas.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public clear() {
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context.clearRect(0, 0, this.bounds.width, this.bounds.height);
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-05-17 05:49:43 +00:00
|
|
|
/**
|
|
|
|
* Renders this DynamicTexture to the Stage at the given x/y coordinates
|
|
|
|
*
|
|
|
|
* @param x {number} The X coordinate to render on the stage to (given in screen coordinates, not world)
|
|
|
|
* @param y {number} The Y coordinate to render on the stage to (given in screen coordinates, not world)
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public render(x: number = 0, y: number = 0) {
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
if (this.globalCompositeOperation)
|
|
|
|
{
|
|
|
|
this.game.stage.context.save();
|
|
|
|
this.game.stage.context.globalCompositeOperation = this.globalCompositeOperation;
|
|
|
|
}
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
this.game.stage.context.drawImage(this.canvas, x, y);
|
2013-07-12 02:28:46 +00:00
|
|
|
|
|
|
|
if (this.globalCompositeOperation)
|
|
|
|
{
|
|
|
|
this.game.stage.context.restore();
|
|
|
|
}
|
|
|
|
|
2013-05-17 05:49:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get width(): number {
|
|
|
|
return this.bounds.width;
|
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get height(): number {
|
|
|
|
return this.bounds.height;
|
|
|
|
}
|
2013-04-15 21:13:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|