2016-09-29 03:21:12 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Texture consists of a source, usually an Image from the Cache, or a Canvas, and a collection
|
|
|
|
* of Frames. The Frames represent the different areas of the Texture. For example a texture atlas
|
|
|
|
* may have many Frames, one for each element within the atlas. Where-as a single image would have
|
|
|
|
* just one frame, that encompasses the whole image.
|
|
|
|
*
|
|
|
|
* Textures are managed by the global TextureManager. This is a singleton class that is
|
|
|
|
* responsible for creating and delivering Textures and their corresponding Frames to Game Objects.
|
|
|
|
*
|
|
|
|
* Sprites and other Game Objects get the texture data they need from the TextureManager.
|
|
|
|
*
|
|
|
|
* @class Phaser.Texture
|
|
|
|
* @constructor
|
|
|
|
* @param {object} source
|
|
|
|
* @param {number} scaleMode
|
|
|
|
*/
|
2016-09-30 05:07:00 +00:00
|
|
|
Phaser.Texture = function (manager, key, source)
|
2016-09-29 15:10:22 +00:00
|
|
|
{
|
2016-09-30 05:07:00 +00:00
|
|
|
this.manager = manager;
|
|
|
|
|
|
|
|
if (!Array.isArray(source))
|
|
|
|
{
|
|
|
|
source = [ source ];
|
|
|
|
}
|
|
|
|
|
2016-09-29 15:10:22 +00:00
|
|
|
this.key = key;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The source that is used to create the texture.
|
|
|
|
* Usually an Image, but can also be a Canvas.
|
|
|
|
*
|
|
|
|
* @property source
|
2016-09-30 05:07:00 +00:00
|
|
|
* @type array
|
2016-09-29 03:21:12 +00:00
|
|
|
*/
|
2016-09-30 05:07:00 +00:00
|
|
|
this.source = [];
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-09-29 15:10:22 +00:00
|
|
|
/**
|
|
|
|
* @property {object} frames - Frames
|
|
|
|
*/
|
2016-10-05 00:47:54 +00:00
|
|
|
this.frames = {};
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-09-30 05:07:00 +00:00
|
|
|
this.frameTotal = 0;
|
|
|
|
|
|
|
|
// Load the Sources
|
|
|
|
for (var i = 0; i < source.length; i++)
|
|
|
|
{
|
|
|
|
this.source.push(new Phaser.TextureSource(this, source[i]));
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Texture.prototype.constructor = Phaser.Texture;
|
|
|
|
|
|
|
|
Phaser.Texture.prototype = {
|
|
|
|
|
2016-09-30 05:07:00 +00:00
|
|
|
add: function (name, sourceIndex, x, y, width, height)
|
2016-09-29 15:10:22 +00:00
|
|
|
{
|
2016-09-30 05:07:00 +00:00
|
|
|
var frame = new Phaser.TextureFrame(this, name, sourceIndex, x, y, width, height);
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-09-29 15:10:22 +00:00
|
|
|
this.frames[name] = frame;
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-09-30 05:07:00 +00:00
|
|
|
this.frameTotal++;
|
|
|
|
|
2016-09-29 15:10:22 +00:00
|
|
|
return frame;
|
2016-09-29 03:21:12 +00:00
|
|
|
},
|
|
|
|
|
2016-09-29 15:10:22 +00:00
|
|
|
get: function (name)
|
|
|
|
{
|
2016-10-18 16:03:25 +00:00
|
|
|
if (name === undefined || name === null || this.frameTotal === 1)
|
|
|
|
{
|
|
|
|
name = '__BASE';
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-18 16:03:25 +00:00
|
|
|
var frame = this.frames[name];
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-10-18 16:03:25 +00:00
|
|
|
if (!frame)
|
|
|
|
{
|
|
|
|
console.warn('No Texture.frame found with name ' + name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return frame;
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
},
|
|
|
|
|
2016-10-25 00:40:16 +00:00
|
|
|
setTextureIndex: function (index)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.source.length; i++)
|
|
|
|
{
|
|
|
|
this.source[i].glTextureIndex = index;
|
|
|
|
|
2016-10-25 15:44:23 +00:00
|
|
|
console.log(this.source[i].image.currentSrc, 'index = ', index);
|
|
|
|
|
2016-10-25 00:40:16 +00:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
|
2016-09-29 15:10:22 +00:00
|
|
|
/**
|
|
|
|
* Destroys this base texture
|
|
|
|
*
|
|
|
|
* @method destroy
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
2016-10-18 16:03:25 +00:00
|
|
|
|
|
|
|
// Need to iterate though the TextureSources, and unload each one
|
|
|
|
// then clear out the frames
|
|
|
|
|
|
|
|
/*
|
2016-09-29 15:10:22 +00:00
|
|
|
if (this.source)
|
|
|
|
{
|
|
|
|
Phaser.CanvasPool.removeByCanvas(this.source);
|
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
2016-09-29 15:10:22 +00:00
|
|
|
this.source = null;
|
2016-10-18 16:03:25 +00:00
|
|
|
*/
|
2016-09-29 15:10:22 +00:00
|
|
|
}
|
2016-09-29 03:21:12 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that creates a base texture from the given canvas element.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method fromCanvas
|
|
|
|
* @param canvas {Canvas} The canvas element source of the texture
|
2016-10-07 02:22:59 +00:00
|
|
|
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}Phaser.scaleModes{{/crossLink}} for possible values
|
2016-09-29 03:21:12 +00:00
|
|
|
* @return {BaseTexture}
|
|
|
|
*/
|
2016-09-29 15:10:22 +00:00
|
|
|
Phaser.Texture.fromCanvas = function (canvas, scaleMode)
|
|
|
|
{
|
2016-09-29 03:21:12 +00:00
|
|
|
if (canvas.width === 0)
|
|
|
|
{
|
|
|
|
canvas.width = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canvas.height === 0)
|
|
|
|
{
|
|
|
|
canvas.height = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Phaser.Texture(canvas, scaleMode);
|
|
|
|
};
|