AnimatedSprite2D.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Atomic2D/StaticSprite2D.h"
  24. #ifdef ATOMIC_SPINE
  25. struct spAnimationState;
  26. struct spAnimationStateData;
  27. struct spSkeleton;
  28. #endif
  29. /// Loop mode.
  30. enum LoopMode2D
  31. {
  32. /// Default, use animation's value.
  33. LM_DEFAULT = 0,
  34. /// Force looped.
  35. LM_FORCE_LOOPED,
  36. /// Force clamped.
  37. LM_FORCE_CLAMPED
  38. };
  39. namespace Atomic
  40. {
  41. namespace Spriter
  42. {
  43. class SpriterInstance;
  44. }
  45. class AnimationSet2D;
  46. /// Animated sprite component, it uses to play animation created by Spine (http://www.esotericsoftware.com) and Spriter (http://www.brashmonkey.com/).
  47. class ATOMIC_API AnimatedSprite2D : public StaticSprite2D
  48. {
  49. ATOMIC_OBJECT(AnimatedSprite2D, StaticSprite2D);
  50. public:
  51. /// Construct.
  52. AnimatedSprite2D(Context* context);
  53. /// Destruct.
  54. virtual ~AnimatedSprite2D();
  55. /// Register object factory.
  56. static void RegisterObject(Context* context);
  57. /// Handle enabled/disabled state change.
  58. virtual void OnSetEnabled();
  59. /// Set animation set.
  60. void SetAnimationSet(AnimationSet2D* animationSet);
  61. /// Set entity name (skin name for spine, entity name for spriter).
  62. void SetEntity(const String& name);
  63. /// Set animation by name and loop mode.
  64. void SetAnimation(const String& name, LoopMode2D loopMode = LM_DEFAULT);
  65. /// Set loop mode.
  66. void SetLoopMode(LoopMode2D loopMode);
  67. /// Set speed.
  68. void SetSpeed(float speed);
  69. /// Return animation.
  70. AnimationSet2D* GetAnimationSet() const;
  71. /// Return entity name.
  72. const String& GetEntity() const { return entity_; }
  73. /// Return animation name.
  74. const String& GetAnimation() const { return animationName_; }
  75. /// Return loop mode.
  76. LoopMode2D GetLoopMode() const { return loopMode_; }
  77. /// Return speed.
  78. float GetSpeed() const { return speed_; }
  79. /// Set animation set attribute.
  80. void SetAnimationSetAttr(const ResourceRef& value);
  81. /// Return animation set attribute.
  82. ResourceRef GetAnimationSetAttr() const;
  83. /// Set animation by name.
  84. void SetAnimationAttr(const String& name);
  85. protected:
  86. /// Handle scene being assigned.
  87. virtual void OnSceneSet(Scene* scene);
  88. /// Handle update vertices.
  89. virtual void UpdateSourceBatches();
  90. /// Handle scene post update.
  91. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  92. /// Update animation.
  93. void UpdateAnimation(float timeStep);
  94. #ifdef ATOMIC_SPINE
  95. /// Handle set spine animation.
  96. void SetSpineAnimation();
  97. /// Update spine animation.
  98. void UpdateSpineAnimation(float timeStep);
  99. /// Update vertices for spine animation;
  100. void UpdateSourceBatchesSpine();
  101. #endif
  102. /// Handle set spriter animation.
  103. void SetSpriterAnimation();
  104. /// Update spriter animation.
  105. void UpdateSpriterAnimation(float timeStep);
  106. /// Update vertices for spriter animation.
  107. void UpdateSourceBatchesSpriter();
  108. /// Dispose.
  109. void Dispose();
  110. /// Speed.
  111. float speed_;
  112. /// Entity name.
  113. String entity_;
  114. /// Animation set.
  115. SharedPtr<AnimationSet2D> animationSet_;
  116. /// Animation name.
  117. String animationName_;
  118. /// Loop mode.
  119. LoopMode2D loopMode_;
  120. #ifdef ATOMIC_SPINE
  121. /// Skeleton.
  122. spSkeleton* skeleton_;
  123. /// Animation state data.
  124. spAnimationStateData* animationStateData_;
  125. /// Animation state.
  126. spAnimationState* animationState_;
  127. #endif
  128. /// Spriter instance.
  129. UniquePtr<Spriter::SpriterInstance> spriterInstance_;
  130. };
  131. }