AnimatedSprite2D.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. void SetAnimationSet(AnimationSet2D* animationSet);
  62. /// Set entity name (skin name for spine, entity name for spriter).
  63. void SetEntity(const String& entity);
  64. /// Set animation by name and loop mode.
  65. void SetAnimation(const String& name, LoopMode2D loopMode = LM_DEFAULT);
  66. /// Set loop mode.
  67. void SetLoopMode(LoopMode2D loopMode);
  68. /// Set speed.
  69. void SetSpeed(float speed);
  70. /// Return animation.
  71. AnimationSet2D* GetAnimationSet() const;
  72. /// Return entity name.
  73. const String& GetEntity() const { return entity_; }
  74. /// Return animation name.
  75. const String& GetAnimation() const { return animationName_; }
  76. /// Return loop mode.
  77. LoopMode2D GetLoopMode() const { return loopMode_; }
  78. /// Return speed.
  79. float GetSpeed() const { return speed_; }
  80. /// Set animation set attribute.
  81. void SetAnimationSetAttr(const ResourceRef& value);
  82. /// Return animation set attribute.
  83. ResourceRef GetAnimationSetAttr() const;
  84. /// Set animation by name.
  85. void SetAnimationAttr(const String& name);
  86. protected:
  87. /// Handle scene being assigned.
  88. void OnSceneSet(Scene* scene) override;
  89. /// Handle update vertices.
  90. void UpdateSourceBatches() override;
  91. /// Handle scene post update.
  92. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  93. /// Update animation.
  94. void UpdateAnimation(float timeStep);
  95. #ifdef URHO3D_SPINE
  96. /// Handle set spine animation.
  97. void SetSpineAnimation();
  98. /// Update spine animation.
  99. void UpdateSpineAnimation(float timeStep);
  100. /// Update vertices for spine animation.
  101. void UpdateSourceBatchesSpine();
  102. #endif
  103. /// Handle set spriter animation.
  104. void SetSpriterAnimation();
  105. /// Update spriter animation.
  106. void UpdateSpriterAnimation(float timeStep);
  107. /// Update vertices for spriter animation.
  108. void UpdateSourceBatchesSpriter();
  109. /// Dispose.
  110. void Dispose();
  111. /// Speed.
  112. float speed_;
  113. /// Entity name.
  114. String entity_;
  115. /// Animation set.
  116. SharedPtr<AnimationSet2D> animationSet_;
  117. /// Animation name.
  118. String animationName_;
  119. /// Loop mode.
  120. LoopMode2D loopMode_;
  121. #ifdef URHO3D_SPINE
  122. /// Skeleton.
  123. spSkeleton* skeleton_;
  124. /// Animation state data.
  125. spAnimationStateData* animationStateData_;
  126. /// Animation state.
  127. spAnimationState* animationState_;
  128. #endif
  129. /// Spriter instance.
  130. UniquePtr<Spriter::SpriterInstance> spriterInstance_;
  131. };
  132. }