AnimatedSprite2D.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. MarkNetworkUpdate();
  81. }
  82. void AnimatedSprite2D::SetCycleMode(CycleMode cycleMode)
  83. {
  84. cycleMode_ = cycleMode;
  85. MarkNetworkUpdate();
  86. }
  87. void AnimatedSprite2D::SetAnimation(Animation2D* animation)
  88. {
  89. animationTime_ = 0.0f;
  90. if (animation_ == animation)
  91. return;
  92. if (animation_)
  93. SetSprite(0);
  94. animation_ = animation;
  95. if (animation_)
  96. {
  97. SetSprite(animation_->GetFrameSprite(0));
  98. animationTotalTime_ = animation_->GetTotalTime();
  99. }
  100. MarkNetworkUpdate();
  101. }
  102. Animation2D* AnimatedSprite2D::GetAnimation() const
  103. {
  104. return animation_;
  105. }
  106. void AnimatedSprite2D::SetAnimationAttr(ResourceRef value)
  107. {
  108. materialUpdatePending_ = true;
  109. ResourceCache* cache = GetSubsystem<ResourceCache>();
  110. SetAnimation(cache->GetResource<Animation2D>(value.name_));
  111. }
  112. Urho3D::ResourceRef AnimatedSprite2D::GetAnimationAttr() const
  113. {
  114. return GetResourceRef(animation_, Animation2D::GetTypeStatic());
  115. }
  116. void AnimatedSprite2D::OnNodeSet(Node* node)
  117. {
  118. StaticSprite2D::OnNodeSet(node);
  119. if (node)
  120. {
  121. Scene* scene = GetScene();
  122. if (scene && IsEnabledEffective())
  123. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimatedSprite2D, HandleScenePostUpdate));
  124. }
  125. }
  126. void AnimatedSprite2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  127. {
  128. using namespace ScenePostUpdate;
  129. float timeStep = eventData[P_TIMESTEP].GetFloat();
  130. animationTime_ += timeStep * speed_;
  131. if (!animation_)
  132. return;
  133. float time;
  134. switch (cycleMode_)
  135. {
  136. case CM_LOOP:
  137. time = fmodf(animationTime_, animationTotalTime_);
  138. break;
  139. case CM_CLAMP:
  140. time = Clamp(animationTime_, 0.0f, animationTotalTime_);
  141. break;
  142. case CM_PINGPONG:
  143. {
  144. float doubleTotalTime = animationTotalTime_ * 2.0f;
  145. float fract = fmodf(animationTime_, doubleTotalTime);
  146. time = (fract < animationTotalTime_) ? fract : doubleTotalTime - fract;
  147. }
  148. break;
  149. }
  150. Sprite2D* sprite = animation_->GetFrameSpriteByTime(time);
  151. if (GetSprite() != sprite)
  152. SetSprite(sprite);
  153. MarkForUpdate();
  154. }
  155. }