AnimatedSprite2D.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // Copyright (c) 2008-2014 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. #include "Precompiled.h"
  23. #include "AnimatedSprite2D.h"
  24. #include "Animation2D.h"
  25. #include "Context.h"
  26. #include "ResourceCache.h"
  27. #include "Scene.h"
  28. #include "SceneEvents.h"
  29. #include "Sprite2D.h"
  30. #include "Texture2D.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. extern const char* URHO2D_CATEGORY;
  35. const char* cycleModeNames[] =
  36. {
  37. "Loop",
  38. "Clamp",
  39. "Pingpong",
  40. 0
  41. };
  42. template<> CycleMode Variant::Get<CycleMode>() const
  43. {
  44. return (CycleMode)GetInt();
  45. }
  46. AnimatedSprite2D::AnimatedSprite2D(Context* context) :
  47. StaticSprite2D(context),
  48. speed_(1.0f),
  49. cycleMode_(CM_LOOP),
  50. animationTime_(0.0f),
  51. animationTotalTime_(1.0f)
  52. {
  53. }
  54. AnimatedSprite2D::~AnimatedSprite2D()
  55. {
  56. }
  57. void AnimatedSprite2D::RegisterObject(Context* context)
  58. {
  59. context->RegisterFactory<AnimatedSprite2D>(URHO2D_CATEGORY);
  60. ACCESSOR_ATTRIBUTE(AnimatedSprite2D, VAR_FLOAT, "Speed", GetSpeed, SetSpeed, float, 1.0f, AM_DEFAULT);
  61. ENUM_ACCESSOR_ATTRIBUTE(AnimatedSprite2D, "Cycle Mode", GetCycleMode, SetCycleMode, CycleMode, cycleModeNames, 0, AM_DEFAULT);
  62. ACCESSOR_ATTRIBUTE(AnimatedSprite2D, VAR_RESOURCEREF, "Animation", GetAnimationAttr, SetAnimationAttr, ResourceRef, ResourceRef(Animation2D::GetTypeStatic()), AM_DEFAULT);
  63. COPY_BASE_ATTRIBUTES(AnimatedSprite2D, StaticSprite2D);
  64. }
  65. void AnimatedSprite2D::OnSetEnabled()
  66. {
  67. StaticSprite2D::OnSetEnabled();
  68. Scene* scene = GetScene();
  69. if (scene)
  70. {
  71. if (IsEnabledEffective())
  72. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimatedSprite2D, HandleScenePostUpdate));
  73. else
  74. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  75. }
  76. }
  77. void AnimatedSprite2D::SetSpeed(float speed)
  78. {
  79. speed_ = speed;
  80. }
  81. void AnimatedSprite2D::SetCycleMode(CycleMode cycleMode)
  82. {
  83. cycleMode_ = cycleMode;
  84. }
  85. void AnimatedSprite2D::SetAnimation(Animation2D* animation)
  86. {
  87. animationTime_ = 0.0f;
  88. if (animation_ == animation)
  89. return;
  90. if (animation_)
  91. SetSprite(0);
  92. animation_ = animation;
  93. if (animation_)
  94. {
  95. SetSprite(animation_->GetFrameSprite(0));
  96. animationTotalTime_ = animation_->GetTotalTime();
  97. }
  98. }
  99. Animation2D* AnimatedSprite2D::GetAnimation() const
  100. {
  101. return animation_;
  102. }
  103. void AnimatedSprite2D::SetAnimationAttr(ResourceRef value)
  104. {
  105. ResourceCache* cache = GetSubsystem<ResourceCache>();
  106. SetAnimation(cache->GetResource<Animation2D>(value.name_));
  107. }
  108. Urho3D::ResourceRef AnimatedSprite2D::GetAnimationAttr() const
  109. {
  110. return GetResourceRef(animation_, Animation2D::GetTypeStatic());
  111. }
  112. void AnimatedSprite2D::OnNodeSet(Node* node)
  113. {
  114. StaticSprite2D::OnNodeSet(node);
  115. if (node)
  116. {
  117. Scene* scene = GetScene();
  118. if (scene && IsEnabledEffective())
  119. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimatedSprite2D, HandleScenePostUpdate));
  120. }
  121. }
  122. void AnimatedSprite2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  123. {
  124. using namespace ScenePostUpdate;
  125. float timeStep = eventData[P_TIMESTEP].GetFloat();
  126. animationTime_ += timeStep * speed_;
  127. if (!animation_)
  128. return;
  129. float time;
  130. switch (cycleMode_)
  131. {
  132. case CM_LOOP:
  133. time = fmodf(animationTime_, animationTotalTime_);
  134. break;
  135. case CM_CLAMP:
  136. time = Clamp(animationTime_, 0.0f, animationTotalTime_);
  137. break;
  138. case CM_PINGPONG:
  139. {
  140. float doubleTotalTime = animationTotalTime_ * 2.0f;
  141. float fract = fmodf(animationTime_, doubleTotalTime);
  142. time = (fract < animationTotalTime_) ? fract : doubleTotalTime - fract;
  143. }
  144. break;
  145. }
  146. Sprite2D* sprite = animation_->GetFrameSpriteByTime(time);
  147. if (GetSprite() != sprite)
  148. SetSprite(sprite);
  149. MarkForUpdate();
  150. }
  151. }