2017-07-04 12:23:58 +00:00
|
|
|
var Class = require('../utils/Class');
|
2016-12-06 16:18:28 +00:00
|
|
|
var Frame = require('./Frame');
|
|
|
|
var TextureSource = require('./TextureSource');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
var Texture = new Class({
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
initialize:
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
function Texture (manager, key, source, width, height)
|
|
|
|
{
|
|
|
|
this.manager = manager;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
if (!Array.isArray(source))
|
|
|
|
{
|
|
|
|
source = [ source ];
|
|
|
|
}
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
this.key = key;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
/**
|
|
|
|
* The source that is used to create the texture.
|
|
|
|
* Usually an Image, but can also be a Canvas.
|
|
|
|
*/
|
|
|
|
this.source = [];
|
2017-03-27 22:38:27 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
this.frames = {};
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-01-19 18:23:25 +00:00
|
|
|
// Any additional data that was set in the source JSON (if any), or any extra data you'd like to store relating to this texture
|
|
|
|
this.customData = {};
|
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
this.firstFrame = '__BASE';
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
this.frameTotal = 0;
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
// Load the Sources
|
|
|
|
for (var i = 0; i < source.length; i++)
|
|
|
|
{
|
|
|
|
this.source.push(new TextureSource(this, source[i], width, height));
|
|
|
|
}
|
|
|
|
},
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
add: function (name, sourceIndex, x, y, width, height)
|
|
|
|
{
|
|
|
|
var frame = new Frame(this, name, sourceIndex, x, y, width, height);
|
|
|
|
|
|
|
|
this.frames[name] = frame;
|
|
|
|
|
2017-03-27 22:38:27 +00:00
|
|
|
// Set the first frame of the Texture (other than __BASE)
|
|
|
|
// This is used to ensure we don't spam the display with entire
|
|
|
|
// atlases of sprite sheets, but instead just the first frame of them
|
|
|
|
// should the dev incorrectly specify the frame index
|
|
|
|
if (this.frameTotal === 1)
|
|
|
|
{
|
|
|
|
this.firstFrame = name;
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
this.frameTotal++;
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
},
|
|
|
|
|
2017-04-05 16:04:55 +00:00
|
|
|
has: function (name)
|
|
|
|
{
|
|
|
|
return (this.frames[name]);
|
|
|
|
},
|
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
get: function (name)
|
|
|
|
{
|
2018-01-20 16:35:29 +00:00
|
|
|
if (name === undefined || name === null || typeof name !== 'string')
|
2016-12-06 16:18:28 +00:00
|
|
|
{
|
2017-03-27 22:38:27 +00:00
|
|
|
name = (this.frameTotal === 1) ? '__BASE' : this.firstFrame;
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var frame = this.frames[name];
|
|
|
|
|
|
|
|
if (!frame)
|
|
|
|
{
|
|
|
|
console.warn('No Texture.frame found with name ' + name);
|
2016-12-07 10:58:35 +00:00
|
|
|
|
|
|
|
return this.frames['__BASE'];
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-18 12:45:11 +00:00
|
|
|
getTextureSourceIndex: function (source)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.source.length; i++)
|
|
|
|
{
|
|
|
|
if (this.source[i] === source)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
// source = TextureSource object
|
|
|
|
getFramesFromTextureSource: function (sourceIndex)
|
|
|
|
{
|
|
|
|
var out = [];
|
|
|
|
|
|
|
|
for (var frameName in this.frames)
|
|
|
|
{
|
|
|
|
if (frameName === '__BASE')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var frame = this.frames[frameName];
|
|
|
|
|
|
|
|
if (frame.sourceIndex === sourceIndex)
|
|
|
|
{
|
|
|
|
out.push(frame.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
|
2017-04-28 03:30:32 +00:00
|
|
|
getFrameNames: function (includeBase)
|
|
|
|
{
|
|
|
|
if (includeBase === undefined) { includeBase = false; }
|
|
|
|
|
|
|
|
var out = Object.keys(this.frames);
|
|
|
|
|
|
|
|
if (!includeBase)
|
|
|
|
{
|
|
|
|
var idx = out.indexOf('__BASE');
|
|
|
|
|
|
|
|
if (idx !== -1)
|
|
|
|
{
|
|
|
|
out.splice(idx, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
|
2017-03-14 16:37:32 +00:00
|
|
|
getSourceImage: function (name)
|
|
|
|
{
|
|
|
|
if (name === undefined || name === null || this.frameTotal === 1)
|
|
|
|
{
|
|
|
|
name = '__BASE';
|
|
|
|
}
|
|
|
|
|
|
|
|
var frame = this.frames[name];
|
|
|
|
|
|
|
|
if (!frame)
|
|
|
|
{
|
|
|
|
console.warn('No Texture.frame found with name ' + name);
|
|
|
|
|
|
|
|
return this.frames['__BASE'].source.image;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return frame.source.image;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-20 01:16:45 +00:00
|
|
|
setFilter: function (filterMode)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.source.length; i++)
|
|
|
|
{
|
|
|
|
this.source[i].setFilter(filterMode);
|
|
|
|
}
|
2017-07-04 12:23:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
2016-12-06 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
});
|
2016-12-06 16:18:28 +00:00
|
|
|
|
|
|
|
module.exports = Texture;
|