/****************************************************************************
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 manages the Animations.
* It saves in a cache the animations. You should use this class if you want to save your animations in a cache.
*
* @class
* @extends cc.Class
*
* @example
* cc.AnimationCache.getInstance().addAnimation(animation,"animation1");
*/
cc.AnimationCache = cc.Class.extend(/** @lends cc.AnimationCache# */{
/**
* Adds a cc.Animation with a name.
* @param {cc.Animation} animation
* @param {String} name
*/
addAnimation:function (animation, name) {
this._animations[name] = animation;
},
/**
* Deletes a cc.Animation from the cache.
* @param {String} name
*/
removeAnimation:function (name) {
if (!name) {
return;
}
if (this._animations[name]) {
delete this._animations[name];
}
},
/**
*
* Returns a cc.Animation 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.
*
* @param {String} name
* @return {cc.Animation}
*/
getAnimation:function (name) {
if (this._animations[name])
return this._animations[name];
return null;
},
/**
*
* Adds an animation from an NSDictionary
* Make sure that the frames were previously loaded in the cc.SpriteFrameCache.
*
* @param {object} dictionary
* @param {String} plist
*/
_addAnimationsWithDictionary:function (dictionary,plist) {
var animations = dictionary["animations"];
if (!animations) {
cc.log("cocos2d: cc.AnimationCache: No animations were found in provided dictionary.");
return;
}
var version = 1;
var properties = dictionary["properties"];
if (properties) {
version = (properties["format"] != null) ? parseInt(properties["format"]) : version;
var spritesheets = properties["spritesheets"];
var spriteFrameCache = cc.SpriteFrameCache.getInstance();
var fileUtils = cc.FileUtils.getInstance(), path;
for (var i = 0; i < spritesheets.length; i++) {
path = fileUtils.fullPathFromRelativeFile(spritesheets[i], plist);
spriteFrameCache.addSpriteFrames(path);
}
}
switch (version) {
case 1:
this._parseVersion1(animations);
break;
case 2:
this._parseVersion2(animations);
break;
default :
cc.log("cc.AnimationCache. Invalid animation format");
break;
}
},
/**
*
* Adds an animation from a plist file.
* Make sure that the frames were previously loaded in the cc.SpriteFrameCache.
*
* @param {String} plist
*/
addAnimations:function (plist) {
if(!plist)
throw "cc.AnimationCache.addAnimations(): Invalid texture file name";
var fileUtils = cc.FileUtils.getInstance();
var path = fileUtils.fullPathForFilename(plist);
var dict = fileUtils.dictionaryWithContentsOfFileThreadSafe(path);
if(!dict){
cc.log("cc.AnimationCache.addAnimations(): File could not be found");
return;
}
this._addAnimationsWithDictionary(dict,plist);
},
_parseVersion1:function (animations) {
var frameCache = cc.SpriteFrameCache.getInstance();
for (var key in animations) {
var animationDict = animations[key];
var frameNames = animationDict["frames"];
var delay = parseFloat(animationDict["delay"]) || 0;
var animation = null;
if (!frameNames) {
cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' found in dictionary without any frames - cannot add to animation cache.");
continue;
}
var frames = [];
for (var i = 0; i < frameNames.length; i++) {
var spriteFrame = frameCache.getSpriteFrame(frameNames[i]);
if (!spriteFrame) {
cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' refers to frame '" + frameNames[i]
+ "' which is not currently in the cc.SpriteFrameCache. This frame will not be added to the animation.");
continue;
}
var animFrame = new cc.AnimationFrame();
animFrame.initWithSpriteFrame(spriteFrame, 1, null);
frames.push(animFrame);
}
if (frames.length === 0) {
cc.log("cocos2d: cc.AnimationCache: None of the frames for animation '" + key
+ "' were found in the cc.SpriteFrameCache. Animation is not being added to the Animation Cache.");
continue;
} else if (frames.length != frameNames.length) {
cc.log("cocos2d: cc.AnimationCache: An animation in your dictionary refers to a frame which is not in the cc.SpriteFrameCache." +
" Some or all of the frames for the animation '" + key + "' may be missing.");
}
animation = cc.Animation.createWithAnimationFrames(frames, delay, 1);
cc.AnimationCache.getInstance().addAnimation(animation, key);
}
},
_parseVersion2:function (animations) {
var frameCache = cc.SpriteFrameCache.getInstance();
for (var key in animations) {
var animationDict = animations[key];
var isLoop = animationDict["loop"];
var loopsTemp = parseInt(animationDict["loops"]);
var loops = isLoop ? cc.REPEAT_FOREVER : ((isNaN(loopsTemp)) ? 1 : loopsTemp);
var restoreOriginalFrame = (animationDict["restoreOriginalFrame"] && animationDict["restoreOriginalFrame"] == true) ? true : false;
var frameArray = animationDict["frames"];
if (!frameArray) {
cc.log("cocos2d: CCAnimationCache: Animation '" + key + "' found in dictionary without any frames - cannot add to animation cache.");
continue;
}
//Array of AnimationFrames
var arr = [];
for (var i = 0; i < frameArray.length; i++) {
var entry = frameArray[i];
var spriteFrameName = entry["spriteframe"];
var spriteFrame = frameCache.getSpriteFrame(spriteFrameName);
if (!spriteFrame) {
cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' refers to frame '" + spriteFrameName
+ "' which is not currently in the cc.SpriteFrameCache. This frame will not be added to the animation.");
continue;
}
var delayUnits = parseFloat(entry["delayUnits"]) || 0;
var userInfo = entry["notification"];
var animFrame = new cc.AnimationFrame();
animFrame.initWithSpriteFrame(spriteFrame, delayUnits, userInfo);
arr.push(animFrame);
}
var delayPerUnit = parseFloat(animationDict["delayPerUnit"]) || 0;
var animation = new cc.Animation();
animation.initWithAnimationFrames(arr, delayPerUnit, loops);
animation.setRestoreOriginalFrame(restoreOriginalFrame);
cc.AnimationCache.getInstance().addAnimation(animation, key);
}
},
/**
* initialize cc.AnimationCache
* @return {Boolean}
*/
init:function () {
this._animations = {};
return true;
},
_animations:null
});
/**
* Purges the cache. It releases all the cc.Animation objects and the shared instance.
*/
cc.AnimationCache.purgeSharedAnimationCache = function () {
if (cc.s_sharedAnimationCache) {
cc.s_sharedAnimationCache._animations = null;
cc.s_sharedAnimationCache = null;
}
};
/**
* Retruns ths shared instance of the Animation cache
* @return {cc.AnimationCache}
*/
cc.AnimationCache.getInstance = function () {
if (cc.s_sharedAnimationCache === null) {
cc.s_sharedAnimationCache = new cc.AnimationCache();
cc.s_sharedAnimationCache.init();
}
return cc.s_sharedAnimationCache;
};
cc.s_sharedAnimationCache = null;