AnimatedSprite2D.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Urho2D/StaticSprite2D.h"
  25. #ifdef URHO3D_SPINE
  26. struct spAnimationState;
  27. struct spAnimationStateData;
  28. struct spSkeleton;
  29. #endif
  30. namespace Urho3D
  31. {
  32. /// Loop mode.
  33. enum LoopMode2D
  34. {
  35. /// Default, use animation's value.
  36. LM_DEFAULT = 0,
  37. /// Force looped.
  38. LM_FORCE_LOOPED,
  39. /// Force clamped.
  40. LM_FORCE_CLAMPED
  41. };
  42. namespace Spriter
  43. {
  44. class SpriterInstance;
  45. }
  46. class AnimationSet2D;
  47. /// Animated sprite component, it uses to play animation created by Spine (http://www.esotericsoftware.com) and Spriter (http://www.brashmonkey.com/).
  48. class URHO3D_API AnimatedSprite2D : public StaticSprite2D
  49. {
  50. URHO3D_OBJECT(AnimatedSprite2D, StaticSprite2D);
  51. public:
  52. /// Construct.
  53. explicit AnimatedSprite2D(Context* context);
  54. /// Destruct.
  55. ~AnimatedSprite2D() override;
  56. /// Register object factory.
  57. static void RegisterObject(Context* context);
  58. /// Handle enabled/disabled state change.
  59. void OnSetEnabled() override;
  60. /// Set animation set.
  61. /// @property
  62. void SetAnimationSet(AnimationSet2D* animationSet);
  63. /// Set entity name (skin name for spine, entity name for spriter).
  64. /// @property
  65. void SetEntity(const String& entity);
  66. /// Set animation by name and loop mode.
  67. void SetAnimation(const String& name, LoopMode2D loopMode = LM_DEFAULT);
  68. /// Set loop mode.
  69. /// @property
  70. void SetLoopMode(LoopMode2D loopMode);
  71. /// Set speed.
  72. /// @property
  73. void SetSpeed(float speed);
  74. /// Return animation.
  75. /// @property
  76. AnimationSet2D* GetAnimationSet() const;
  77. /// Return entity name.
  78. /// @property
  79. const String& GetEntity() const { return entity_; }
  80. /// Return animation name.
  81. /// @property
  82. const String& GetAnimation() const { return animationName_; }
  83. /// Return loop mode.
  84. /// @property
  85. LoopMode2D GetLoopMode() const { return loopMode_; }
  86. /// Return speed.
  87. /// @property
  88. float GetSpeed() const { return speed_; }
  89. /// Set animation set attribute.
  90. void SetAnimationSetAttr(const ResourceRef& value);
  91. /// Return animation set attribute.
  92. ResourceRef GetAnimationSetAttr() const;
  93. /// Set animation by name.
  94. /// @property{set_animation}
  95. void SetAnimationAttr(const String& name);
  96. protected:
  97. /// Handle scene being assigned.
  98. void OnSceneSet(Scene* scene) override;
  99. /// Handle update vertices.
  100. void UpdateSourceBatches() override;
  101. /// Handle scene post update.
  102. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  103. /// Update animation.
  104. void UpdateAnimation(float timeStep);
  105. #ifdef URHO3D_SPINE
  106. /// Handle set spine animation.
  107. void SetSpineAnimation();
  108. /// Update spine animation.
  109. void UpdateSpineAnimation(float timeStep);
  110. /// Update vertices for spine animation.
  111. void UpdateSourceBatchesSpine();
  112. #endif
  113. /// Handle set spriter animation.
  114. void SetSpriterAnimation();
  115. /// Update spriter animation.
  116. void UpdateSpriterAnimation(float timeStep);
  117. /// Update vertices for spriter animation.
  118. void UpdateSourceBatchesSpriter();
  119. /// Dispose.
  120. void Dispose();
  121. /// Speed.
  122. float speed_;
  123. /// Entity name.
  124. String entity_;
  125. /// Animation set.
  126. SharedPtr<AnimationSet2D> animationSet_;
  127. /// Animation name.
  128. String animationName_;
  129. /// Loop mode.
  130. LoopMode2D loopMode_;
  131. #ifdef URHO3D_SPINE
  132. /// Skeleton.
  133. spSkeleton* skeleton_;
  134. /// Animation state data.
  135. spAnimationStateData* animationStateData_;
  136. /// Animation state.
  137. spAnimationState* animationState_;
  138. #endif
  139. /// Spriter instance.
  140. UniquePtr<Spriter::SpriterInstance> spriterInstance_;
  141. };
  142. }