CCAnimationCache.h 4.8 KB

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