/**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2011-2012 cocos2d-x.org Copyright (c) 2013-2014 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ // init CCSpriteFrame require("./CCSpriteFrame"); /** *
* cc.spriteFrameCache is a singleton that handles the loading of the sprite frames. It saves in a cache the sprite frames.
*
* @class spriteFrameCache
*/
cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
_CCNS_REG1 : /^\s*\{\s*([\-]?\d+[.]?\d*)\s*,\s*([\-]?\d+[.]?\d*)\s*\}\s*$/,
_CCNS_REG2 : /^\s*\{\s*\{\s*([\-]?\d+[.]?\d*)\s*,\s*([\-]?\d+[.]?\d*)\s*\}\s*,\s*\{\s*([\-]?\d+[.]?\d*)\s*,\s*([\-]?\d+[.]?\d*)\s*\}\s*\}\s*$/,
_spriteFrames: {},
_spriteFramesAliases: {},
_frameConfigCache : {},
_rectFromString : function (content) {
var result = this._CCNS_REG2.exec(content);
if(!result) return cc.rect(0, 0, 0, 0);
return cc.rect(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3]), parseFloat(result[4]));
},
_pointFromString : function (content) {
var result = this._CCNS_REG1.exec(content);
if(!result) return cc.p(0,0);
return cc.p(parseFloat(result[1]), parseFloat(result[2]));
},
_sizeFromString : function (content) {
var result = this._CCNS_REG1.exec(content);
if(!result) return cc.size(0, 0);
return cc.size(parseFloat(result[1]), parseFloat(result[2]));
},
_getFrameConfig : function(url){
var dict = cc.loader.getRes(url);
cc.assert(dict, cc._LogInfos.spriteFrameCache._getFrameConfig_2, url);
cc.loader.release(url);//release it in loader
if(dict._inited){
this._frameConfigCache[url] = dict;
return dict;
}
this._frameConfigCache[url] = this._parseFrameConfig(dict);
return this._frameConfigCache[url];
},
_getFrameConfigByJsonObject: function(url, jsonObject) {
cc.assert(jsonObject, cc._LogInfos.spriteFrameCache._getFrameConfig_2, url);
this._frameConfigCache[url] = this._parseFrameConfig(jsonObject);
return this._frameConfigCache[url];
},
_parseFrameConfig: function(dict) {
var tempFrames = dict["frames"], tempMeta = dict["metadata"] || dict["meta"];
var frames = {}, meta = {};
var format = 0;
if(tempMeta){//init meta
var tmpFormat = tempMeta["format"];
format = (tmpFormat.length <= 1) ? parseInt(tmpFormat) : tmpFormat;
meta.image = tempMeta["textureFileName"] || tempMeta["textureFileName"] || tempMeta["image"];
}
for (var key in tempFrames) {
var frameDict = tempFrames[key];
if(!frameDict) continue;
var tempFrame = {};
if (format == 0) {
tempFrame.rect = cc.rect(frameDict["x"], frameDict["y"], frameDict["width"], frameDict["height"]);
tempFrame.rotated = false;
tempFrame.offset = cc.p(frameDict["offsetX"], frameDict["offsetY"]);
var ow = frameDict["originalWidth"];
var oh = frameDict["originalHeight"];
// check ow/oh
if (!ow || !oh) {
cc.log(cc._LogInfos.spriteFrameCache._getFrameConfig);
}
// Math.abs ow/oh
ow = Math.abs(ow);
oh = Math.abs(oh);
tempFrame.size = cc.size(ow, oh);
} else if (format == 1 || format == 2) {
tempFrame.rect = this._rectFromString(frameDict["frame"]);
tempFrame.rotated = frameDict["rotated"] || false;
tempFrame.offset = this._pointFromString(frameDict["offset"]);
tempFrame.size = this._sizeFromString(frameDict["sourceSize"]);
} else if (format == 3) {
// get values
var spriteSize = this._sizeFromString(frameDict["spriteSize"]);
var textureRect = this._rectFromString(frameDict["textureRect"]);
if (spriteSize) {
textureRect = cc.rect(textureRect.x, textureRect.y, spriteSize.width, spriteSize.height);
}
tempFrame.rect = textureRect;
tempFrame.rotated = frameDict["textureRotated"] || false; // == "true";
tempFrame.offset = this._pointFromString(frameDict["spriteOffset"]);
tempFrame.size = this._sizeFromString(frameDict["spriteSourceSize"]);
tempFrame.aliases = frameDict["aliases"];
} else {
var tmpFrame = frameDict["frame"], tmpSourceSize = frameDict["sourceSize"];
key = frameDict["filename"] || key;
tempFrame.rect = cc.rect(tmpFrame["x"], tmpFrame["y"], tmpFrame["w"], tmpFrame["h"]);
tempFrame.rotated = frameDict["rotated"] || false;
tempFrame.offset = cc.p(0, 0);
tempFrame.size = cc.size(tmpSourceSize["w"], tmpSourceSize["h"]);
}
frames[key] = tempFrame;
}
return {_inited: true, frames: frames, meta: meta};
},
// Adds multiple Sprite Frames from a json object. it uses for local web view app.
_addSpriteFramesByObject: function(url, jsonObject, texture) {
cc.assert(url, cc._LogInfos.spriteFrameCache.addSpriteFrames_2);
if(!jsonObject || !jsonObject["frames"])
return;
var frameConfig = this._frameConfigCache[url] || this._getFrameConfigByJsonObject(url, jsonObject);
//this._checkConflict(frameConfig); //TODO
this._createSpriteFrames(url, frameConfig, texture);
},
_createSpriteFrames: function(url, frameConfig, texture) {
var frames = frameConfig.frames, meta = frameConfig.meta;
if(!texture){
var texturePath = cc.path.changeBasename(url, meta.image || ".png");
texture = cc.textureCache.addImage(texturePath);
}else if(texture instanceof cc.Texture2D){
//do nothing
}else if(cc.js.isString(texture)){//string
texture = cc.textureCache.addImage(texture);
}else{
cc.assert(0, cc._LogInfos.spriteFrameCache.addSpriteFrames_3);
}
//create sprite frames
var spAliases = this._spriteFramesAliases, spriteFrames = this._spriteFrames;
for (var key in frames) {
var frame = frames[key];
var spriteFrame = spriteFrames[key];
if (!spriteFrame) {
spriteFrame = new cc.SpriteFrame(texture, frame.rect, frame.rotated, frame.offset, frame.size);
var aliases = frame.aliases;
if(aliases){//set aliases
for(var i = 0, li = aliases.length; i < li; i++){
var alias = aliases[i];
if (spAliases[alias])
cc.log(cc._LogInfos.spriteFrameCache.addSpriteFrames, alias);
spAliases[alias] = key;
}
}
if (cc._renderType === cc.game.RENDER_TYPE_CANVAS && spriteFrame.isRotated()) {
//clip to canvas
var locTexture = spriteFrame.getTexture();
if (locTexture.isLoaded()) {
var tempElement = spriteFrame.getTexture().getHtmlElementObj();
tempElement = _ccsg.Sprite.CanvasRenderCmd._cutRotateImageToCanvas(tempElement, spriteFrame.getRect());
var tempTexture = new cc.Texture2D();
tempTexture.initWithElement(tempElement);
tempTexture.handleLoadedTexture();
spriteFrame.setTexture(tempTexture);
var rect = spriteFrame._rect;
spriteFrame.setRect(cc.rect(0, 0, rect.width, rect.height));
}
}
spriteFrames[key] = spriteFrame;
}
}
},
/**
*
* Adds multiple Sprite Frames from a plist or json file.
* A texture will be loaded automatically. The texture name will composed by replacing the .plist or .json suffix with .png
* If you want to use another texture, you should use the addSpriteFrames:texture method.
*
* Adds an sprite frame with a given name.
* If the name already exists, then the contents of the old name will be replaced with the new one.
*
* Purges the dictionary of loaded sprite frames.
* Call this method if you receive the "Memory Warning".
* In the short term: it will free some resources preventing your app from being killed.
* In the medium term: it will allocate more resources.
* In the long term: it will be the same.
*
* Removes multiple Sprite Frames from a plist file.
* Sprite Frames stored in this file will be removed.
* It is convinient to call this method when a specific texture needs to be removed.
*
* Removes all Sprite Frames associated with the specified textures.
* It is convenient to call this method when a specific texture needs to be removed.
*
* Returns an Sprite Frame that was previously added.
* If the name is not found it will return nil.
* You should retain the returned copy if you are going to use it.
*