/**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2011 Zynga 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. ****************************************************************************/ /** * Singleton that handles the loading of the sprite frames. It saves in a cache the sprite frames. * @class * @extends cc.Class * @example * // add SpriteFrames to SpriteFrameCache With File * cc.SpriteFrameCache.getInstance().addSpriteFrames(s_grossiniPlist); */ cc.SpriteFrameCache = cc.Class.extend(/** @lends cc.SpriteFrameCache# */{ _spriteFrames: null, _spriteFramesAliases: null, _loadedFileNames: null, /** * Constructor */ ctor: function () { this._spriteFrames = {}; this._spriteFramesAliases = {}; this._loadedFileNames = []; }, /** * Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames. * @param {object} dictionary * @param {cc.Texture2D} texture */ _addSpriteFramesWithDictionary: function (dictionary, texture) { var metadataDict = dictionary["metadata"] || dictionary["meta"]; var framesDict = dictionary["frames"]; var format = 0; // get the format if (metadataDict) { var tmpFormat = metadataDict["format"]; format = (tmpFormat.length <= 1) ? parseInt(tmpFormat) : tmpFormat; } // check the format if (format < 0 || format > 3) { cc.log("format is not supported for cc.SpriteFrameCache.addSpriteFramesWithDictionary"); return; } for (var key in framesDict) { var frameDict = framesDict[key]; if (frameDict) { var spriteFrame = this._spriteFrames[key]; if (spriteFrame) { continue; } if (format == 0) { var x = parseFloat(frameDict["x"]); var y = parseFloat(frameDict["y"]); var w = parseFloat(frameDict["width"]); var h = parseFloat(frameDict["height"]); var ox = parseFloat(frameDict["offsetX"]); var oy = parseFloat(frameDict["offsetY"]); var ow = parseInt(frameDict["originalWidth"]); var oh = parseInt(frameDict["originalHeight"]); // check ow/oh if (!ow || !oh) { cc.log("cocos2d: WARNING: originalWidth/Height not found on the cc.SpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist"); } // Math.abs ow/oh ow = Math.abs(ow); oh = Math.abs(oh); // create frame spriteFrame = cc.SpriteFrame.createWithTexture(texture, cc.rect(x, y, w, h), false, cc.p(ox, oy), cc.size(ow, oh)); } else if (format == 1 || format == 2) { var frame = cc.RectFromString(frameDict["frame"]); var rotated = false; // rotation if (format == 2) { rotated = frameDict["rotated"];// == "true"; } var offset = cc.PointFromString(frameDict["offset"]); var sourceSize = cc.SizeFromString(frameDict["sourceSize"]); // create frame spriteFrame = cc.SpriteFrame.createWithTexture(texture, frame, rotated, offset, sourceSize); } else if (format == 3) { // get values var spriteSize, spriteOffset, spriteSourceSize, textureRect, textureRotated; spriteSize = cc.SizeFromString(frameDict["spriteSize"]); spriteOffset = cc.PointFromString(frameDict["spriteOffset"]); spriteSourceSize = cc.SizeFromString(frameDict["spriteSourceSize"]); textureRect = cc.RectFromString(frameDict["textureRect"]); textureRotated = frameDict["textureRotated"]; // == "true"; // get aliases var aliases = frameDict["aliases"]; var frameKey = key.toString(); for (var aliasKey in aliases) { var alias = aliases[aliasKey]; if (this._spriteFramesAliases[alias]) { cc.log("cocos2d: WARNING: an alias with name " + aliasKey + " already exists"); } this._spriteFramesAliases[alias] = frameKey; } if (frameDict["spriteSize"] !== undefined) { textureRect = cc.rect(textureRect.x, textureRect.y, spriteSize.width, spriteSize.height); } //create frame spriteFrame = cc.SpriteFrame.createWithTexture(texture, textureRect, textureRotated, spriteOffset, spriteSourceSize); } else { var filename = frameDict["filename"], tmpFrame = frameDict["frame"], tmpSourceSize = frameDict["sourceSize"]; var jsonFrame = cc.rect(tmpFrame.x, tmpFrame.y, tmpFrame.w, tmpFrame.h); var jsonRotated = frameDict["rotated"]; var jsonOffset = cc.p(0, 0); var jsonSourceSize = cc.size(tmpSourceSize.w, tmpSourceSize.h); // create frame spriteFrame = cc.SpriteFrame.createWithTexture(texture, jsonFrame, jsonRotated, jsonOffset, jsonSourceSize); } if (cc.renderContextType === cc.CANVAS && spriteFrame.isRotated()) { //clip to canvas var locTexture = spriteFrame.getTexture(); if (locTexture.isLoaded()) { var tempElement = spriteFrame.getTexture().getHtmlElementObj(); tempElement = cc.cutRotateImageToCanvas(tempElement, spriteFrame.getRectInPixels()); 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)); } } // add sprite frame var keyName = (filename != null) ? filename : key; this._spriteFrames[keyName] = 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 convinient 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.
*