var Class = require('../utils/Class'); var Frame = require('./Frame'); var TextureSource = require('./TextureSource'); var TEXTURE_MISSING_ERROR = 'Texture.frame missing: '; var Texture = new Class({ initialize: function Texture(manager, key, source, width, height){ if (!Array.isArray(source)) { source = [source] ; } this.manager = manager; this.key = key; this.source = [] ; this.dataSource = [] ; this.frames = { } ; this.customData = { } ; this.firstFrame = '__BASE'; this.frameTotal = 0; for (var i = 0; i < (_AN_Read_length('length', source)); i++ ){ this.source.push(new TextureSource(this, source[i], width, height)); } } , add: function (name, sourceIndex, x, y, width, height){ var frame = new Frame(this, name, sourceIndex, x, y, width, height); this.frames[name] = frame; if (this.frameTotal === 1) { this.firstFrame = name; } this.frameTotal++ ; return frame; } , has: function (name){ return (this.frames[name]); } , get: function (name){ if (!name) { name = this.firstFrame; } var frame = this.frames[name]; if (!frame) { console.warn(TEXTURE_MISSING_ERROR + name); frame = this.frames[this.firstFrame]; } return frame; } , getTextureSourceIndex: function (source){ for (var i = 0; i < (_AN_Read_length('length', this.source)); i++ ){ if (this.source[i] === source) { return i; } } return -1; } , 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; } , 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; } , getSourceImage: function (name){ if (name === undefined || name === null || this.frameTotal === 1) { name = '__BASE'; } var frame = this.frames[name]; if (frame) { return frame.source.image; } else { console.warn(TEXTURE_MISSING_ERROR + name); return this.frames.__BASE.source.image; } } , getDataSourceImage: function (name){ if (name === undefined || name === null || this.frameTotal === 1) { name = '__BASE'; } var frame = this.frames[name]; var idx; if (!frame) { console.warn(TEXTURE_MISSING_ERROR + name); idx = this.frames.__BASE.sourceIndex; } else { idx = frame.sourceIndex; } return this.dataSource[idx].image; } , setDataSource: function (data){ if (!Array.isArray(data)) { data = [data] ; } for (var i = 0; i < (_AN_Read_length('length', data)); i++ ){ var source = this.source[i]; this.dataSource.push(new TextureSource(this, data[i], source.width, source.height)); } } , setFilter: function (filterMode){ var i; for (i = 0; i < (_AN_Read_length('length', this.source)); i++ ){ this.source[i].setFilter(filterMode); } for (i = 0; i < (_AN_Read_length('length', this.dataSource)); i++ ){ this.dataSource[i].setFilter(filterMode); } } , destroy: function (){ var i; for (i = 0; i < (_AN_Read_length('length', this.source)); i++ ){ this.source[i].destroy(); } for (i = 0; i < (_AN_Read_length('length', this.dataSource)); i++ ){ this.dataSource[i].destroy(); } for (var frameName in this.frames){ var frame = this.frames[frameName]; frame.destroy(); } this.source = [] ; this.dataSource = [] ; this.frames = { } ; this.manager = null ; } } ); module.exports = Texture;