CCAnimationCache.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CC_ANIMATION_CACHE_H__
  24. #define __CC_ANIMATION_CACHE_H__
  25. #include "base/CCRef.h"
  26. #include "base/CCMap.h"
  27. #include "base/CCValue.h"
  28. #include "2d/CCAnimation.h"
  29. #include <string>
  30. NS_CC_BEGIN
  31. class Animation;
  32. /**
  33. * @addtogroup _2d
  34. * @{
  35. */
  36. /** Singleton that manages the Animations.
  37. It saves in a cache the animations. You should use this class if you want to save your animations in a cache.
  38. Before v0.99.5, the recommend way was to save them on the Sprite. Since v0.99.5, you should use this class instead.
  39. @since v0.99.5
  40. @js cc.animationCache
  41. */
  42. class CC_DLL AnimationCache : public Ref
  43. {
  44. public:
  45. /**
  46. * @js ctor
  47. */
  48. AnimationCache();
  49. /**
  50. * @js NA
  51. * @lua NA
  52. */
  53. ~AnimationCache();
  54. /** Returns the shared instance of the Animation cache
  55. @js NA
  56. */
  57. static AnimationCache* getInstance();
  58. /** Purges the cache. It releases all the Animation objects and the shared instance.
  59. @js NA
  60. */
  61. static void destroyInstance();
  62. /** @deprecated Use getInstance() instead. */
  63. CC_DEPRECATED_ATTRIBUTE static AnimationCache* sharedAnimationCache() { return AnimationCache::getInstance(); }
  64. /** @deprecated Use destroyInstance() instead. */
  65. CC_DEPRECATED_ATTRIBUTE static void purgeSharedAnimationCache() { return AnimationCache::destroyInstance(); }
  66. bool init(void);
  67. /** Adds a Animation with a name.
  68. *
  69. * @param animation An animation.
  70. * @param name The name of animation.
  71. */
  72. void addAnimation(Animation *animation, const std::string& name);
  73. /** Deletes a Animation from the cache.
  74. *
  75. * @param name The name of animation.
  76. */
  77. void removeAnimation(const std::string& name);
  78. /** @deprecated. Use removeAnimation() instead
  79. * @js NA
  80. * @lua NA
  81. */
  82. CC_DEPRECATED_ATTRIBUTE void removeAnimationByName(const std::string& name){ removeAnimation(name);}
  83. /** Returns a Animation that was previously added.
  84. * If the name is not found it will return nil.
  85. * You should retain the returned copy if you are going to use it.
  86. *
  87. * @return A Animation that was previously added. If the name is not found it will return nil.
  88. */
  89. Animation* getAnimation(const std::string& name);
  90. /**
  91. * @deprecated. Use getAnimation() instead
  92. * @js NA
  93. * @lua NA
  94. */
  95. CC_DEPRECATED_ATTRIBUTE Animation* animationByName(const std::string& name){ return getAnimation(name); }
  96. /** Adds an animation from an NSDictionary.
  97. * Make sure that the frames were previously loaded in the SpriteFrameCache.
  98. * @param dictionary An NSDictionary.
  99. * @param plist The path of the relative file,it use to find the plist path for load SpriteFrames.
  100. * @since v1.1
  101. @js NA
  102. */
  103. void addAnimationsWithDictionary(const ValueMap& dictionary,const std::string& plist);
  104. /** Adds an animation from a plist file.
  105. * Make sure that the frames were previously loaded in the SpriteFrameCache.
  106. * @since v1.1
  107. * @js addAnimations
  108. * @lua addAnimations
  109. * @param plist An animation from a plist file.
  110. */
  111. void addAnimationsWithFile(const std::string& plist);
  112. private:
  113. void parseVersion1(const ValueMap& animations);
  114. void parseVersion2(const ValueMap& animations);
  115. private:
  116. Map<std::string, Animation*> _animations;
  117. static AnimationCache* s_sharedAnimationCache;
  118. };
  119. // end of sprite_nodes group
  120. /// @}
  121. NS_CC_END
  122. #endif // __CC_ANIMATION_CACHE_H__