AnimatedSprite2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "AnimationSet2D.h"
  26. #include "Context.h"
  27. #include "ResourceCache.h"
  28. #include "Scene.h"
  29. #include "SceneEvents.h"
  30. #include "Sprite2D.h"
  31. #include "StaticSprite2D.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. extern const char* URHO2D_CATEGORY;
  36. extern const char* blendModeNames[];
  37. AnimatedSprite2D::AnimatedSprite2D(Context* context) :
  38. Drawable(context, DRAWABLE_GEOMETRY),
  39. layer_(0),
  40. orderInLayer_(0),
  41. blendMode_(BLEND_ALPHA),
  42. speed_(1.0f),
  43. currentTime_(0.0f)
  44. {
  45. }
  46. AnimatedSprite2D::~AnimatedSprite2D()
  47. {
  48. }
  49. void AnimatedSprite2D::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<AnimatedSprite2D>(URHO2D_CATEGORY);
  52. ACCESSOR_ATTRIBUTE(AnimatedSprite2D, VAR_INT, "Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  53. ACCESSOR_ATTRIBUTE(AnimatedSprite2D, VAR_INT, "Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  54. ENUM_ACCESSOR_ATTRIBUTE(AnimatedSprite2D, "Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  55. ACCESSOR_ATTRIBUTE(AnimatedSprite2D, VAR_FLOAT, "Speed", GetSpeed, SetSpeed, float, 1.0f, AM_DEFAULT);
  56. ACCESSOR_ATTRIBUTE(AnimatedSprite2D, VAR_RESOURCEREF, "Animation Set", GetAnimationSetAttr, SetAnimationSetAttr, ResourceRef, ResourceRef(AnimatedSprite2D::GetTypeStatic()), AM_DEFAULT);
  57. REF_ACCESSOR_ATTRIBUTE(AnimatedSprite2D, VAR_STRING, "Animation", GetAnimation, SetAnimation, String, String::EMPTY, AM_DEFAULT);
  58. COPY_BASE_ATTRIBUTES(Drawable2D, Drawable);
  59. }
  60. void AnimatedSprite2D::OnSetEnabled()
  61. {
  62. Drawable::OnSetEnabled();
  63. Scene* scene = GetScene();
  64. if (scene)
  65. {
  66. if (IsEnabledEffective())
  67. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimatedSprite2D, HandleScenePostUpdate));
  68. else
  69. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  70. }
  71. }
  72. void AnimatedSprite2D::SetLayer(int layer)
  73. {
  74. if (layer == layer_)
  75. return;
  76. layer_ = layer;
  77. for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
  78. {
  79. StaticSprite2D* objectSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>();
  80. objectSprite->SetLayer(layer_);
  81. }
  82. }
  83. void AnimatedSprite2D::SetOrderInLayer(int orderInLayer)
  84. {
  85. orderInLayer_ = orderInLayer;
  86. }
  87. void AnimatedSprite2D::SetBlendMode(BlendMode blendMode)
  88. {
  89. if (blendMode == blendMode_)
  90. return;
  91. blendMode_ = blendMode;
  92. for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
  93. {
  94. StaticSprite2D* objectSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>();
  95. objectSprite->SetBlendMode(blendMode_);
  96. }
  97. }
  98. void AnimatedSprite2D::SetSpeed(float speed)
  99. {
  100. speed_ = speed;
  101. MarkNetworkUpdate();
  102. }
  103. void AnimatedSprite2D::SetAnimation(AnimationSet2D* animationSet, const String& name)
  104. {
  105. animationSet_ = animationSet;
  106. animationName_ = name;
  107. if (animationSet)
  108. SetAnimation(animationSet->GetAnimation(name));
  109. else
  110. SetAnimation(0);
  111. }
  112. void AnimatedSprite2D::SetAnimationSet(AnimationSet2D* animationSet)
  113. {
  114. if (animationSet == animationSet_)
  115. return;
  116. animationSet_ = animationSet;
  117. if (animationSet_)
  118. SetAnimation(animationSet_->GetAnimation(animationName_));
  119. else
  120. SetAnimation(0);
  121. }
  122. void AnimatedSprite2D::SetAnimation(const String& name)
  123. {
  124. animationName_ = name;
  125. if (animationSet_)
  126. SetAnimation(animationSet_->GetAnimation(animationName_));
  127. }
  128. AnimationSet2D* AnimatedSprite2D::GetAnimationSet() const
  129. {
  130. return animationSet_;
  131. }
  132. void AnimatedSprite2D::SetAnimationSetAttr(ResourceRef value)
  133. {
  134. ResourceCache* cache = GetSubsystem<ResourceCache>();
  135. SetAnimationSet(cache->GetResource<AnimationSet2D>(value.name_));
  136. }
  137. Urho3D::ResourceRef AnimatedSprite2D::GetAnimationSetAttr() const
  138. {
  139. return GetResourceRef(animationSet_, AnimationSet2D::GetTypeStatic());
  140. }
  141. void AnimatedSprite2D::OnNodeSet(Node* node)
  142. {
  143. Drawable::OnNodeSet(node);
  144. if (node)
  145. {
  146. Scene* scene = GetScene();
  147. if (scene && IsEnabledEffective())
  148. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(AnimatedSprite2D, HandleScenePostUpdate));
  149. }
  150. }
  151. void AnimatedSprite2D::OnWorldBoundingBoxUpdate()
  152. {
  153. boundingBox_.Clear();
  154. worldBoundingBox_.Clear();
  155. for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
  156. {
  157. if (!timelineNodes_[i])
  158. continue;
  159. StaticSprite2D* staticSprite = timelineNodes_[i]->GetComponent<StaticSprite2D>();
  160. worldBoundingBox_.Merge(staticSprite->GetWorldBoundingBox());
  161. }
  162. boundingBox_ = worldBoundingBox_.Transformed(node_->GetWorldTransform().Inverse());
  163. }
  164. void AnimatedSprite2D::SetAnimation(Animation2D* animation)
  165. {
  166. if (animation == animation_)
  167. {
  168. // Reset time
  169. currentTime_ = 0.0f;
  170. return;
  171. }
  172. if (animation_)
  173. {
  174. for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
  175. {
  176. if (timelineNodes_[i])
  177. timelineNodes_[i]->Remove();
  178. }
  179. timelineNodes_.Clear();
  180. }
  181. animation_ = animation;
  182. if (!animation_)
  183. return;
  184. currentTime_ = 0.0f;
  185. timelineNodes_.Resize(animation_->GetNumTimelines());
  186. timelineTransformInfos_.Resize(animation_->GetNumTimelines());
  187. for (unsigned i = 0; i < animation_->GetNumTimelines(); ++i)
  188. {
  189. const Timeline2D& timeline = animation->GetTimeline(i);
  190. // Just create sprite type node
  191. if (timeline.type_ == OT_SPRITE)
  192. {
  193. SharedPtr<Node> timelineNode(GetNode()->CreateChild(timeline.name_));
  194. StaticSprite2D* staticSprite = timelineNode->CreateComponent<StaticSprite2D>();
  195. staticSprite->SetLayer(layer_);
  196. staticSprite->SetBlendMode(blendMode_);
  197. staticSprite->SetUseHotSpot(true);
  198. timelineNodes_[i] = timelineNode;
  199. }
  200. timelineTransformInfos_[i].parent_ = timeline.parent_;
  201. }
  202. UpdateAnimation(0.0f);
  203. MarkNetworkUpdate();
  204. }
  205. void AnimatedSprite2D::UpdateAnimation(float timeStep)
  206. {
  207. if (!animation_)
  208. return;
  209. currentTime_ += timeStep * speed_;
  210. float time;
  211. float animtationLength = animation_->GetLength();
  212. if (animation_->IsLooped())
  213. {
  214. time = fmodf(currentTime_, animtationLength);
  215. if (time < 0.0f)
  216. time += animation_->GetLength();
  217. }
  218. else
  219. time = Clamp(currentTime_, 0.0f, animtationLength);
  220. // Update timeline's local transform
  221. for (unsigned i = 0; i < timelineTransformInfos_.Size(); ++i)
  222. {
  223. const Timeline2D& timeline = animation_->GetTimeline(i);
  224. const Vector<TimelineKey2D>& objectKeys = timeline.timelineKeys_;
  225. for (unsigned j = 0; j < objectKeys.Size() - 1; ++j)
  226. {
  227. if (time <= objectKeys[j + 1].time_)
  228. {
  229. const TimelineKey2D& currKey = objectKeys[j];
  230. const TimelineKey2D& nextKey = objectKeys[j + 1];
  231. float t = (time - currKey.time_) / (nextKey.time_ - currKey.time_);
  232. timelineTransformInfos_[i].localTransform_ = currKey.transform_.Lerp(nextKey.transform_, t, currKey.spin_);
  233. timelineTransformInfos_[i].worldTransformUpdated_ = false;
  234. // Update sprite's sprite and hot spot and color
  235. Node* timelineNode = timelineNodes_[i];
  236. if (timelineNode)
  237. {
  238. StaticSprite2D* staticSprite = timelineNode->GetComponent<StaticSprite2D>();
  239. staticSprite->SetSprite(currKey.sprite_);
  240. staticSprite->SetHotSpot(currKey.hotSpot_.Lerp(nextKey.hotSpot_, t));
  241. float alpha_ = Lerp(currKey.alpha_, nextKey.alpha_, t);
  242. staticSprite->SetColor(Color(1.0f, 1.0f, 1.0f, alpha_));
  243. }
  244. break;
  245. }
  246. }
  247. }
  248. // Update timeline's world transform
  249. for (unsigned i = 0; i < timelineTransformInfos_.Size(); ++i)
  250. UpateTimelineWorldTransform(i);
  251. // Get mainline key
  252. const Vector<MainlineKey2D>& mainlineKeys = animation_->GetMainlineKeys();
  253. const MainlineKey2D* mainlineKey = 0;
  254. for (unsigned i = 1; i < mainlineKeys.Size(); ++i)
  255. {
  256. if (time < mainlineKeys[i].time_)
  257. {
  258. mainlineKey = &mainlineKeys[i - 1];
  259. break;
  260. }
  261. }
  262. if (!mainlineKey)
  263. mainlineKey = &mainlineKeys.Back();
  264. // Update node's transform and sprite's z order
  265. for (unsigned i = 0; i < timelineNodes_.Size(); ++i)
  266. {
  267. Node* timelineNode = timelineNodes_[i];
  268. if (!timelineNode)
  269. continue;
  270. const Reference2D* ref = mainlineKey->GetReference(i);
  271. if (!ref)
  272. {
  273. // Disable node
  274. if (timelineNode->IsEnabled())
  275. timelineNode->SetEnabled(false);
  276. }
  277. else
  278. {
  279. // Enable node
  280. if (!timelineNode->IsEnabled())
  281. timelineNode->SetEnabled(true);
  282. // Update node's transform
  283. const Transform2D& transform = timelineTransformInfos_[i].worldTransform_;
  284. timelineNode->SetScale(transform.scale_);
  285. timelineNode->SetRotation(transform.angle_);
  286. timelineNode->SetPosition(transform.position_);
  287. // Update sprite's z order
  288. StaticSprite2D* staticSprite = timelineNode->GetComponent<StaticSprite2D>();
  289. staticSprite->SetOrderInLayer(orderInLayer_ + ref->zIndex_);
  290. }
  291. }
  292. MarkForUpdate();
  293. }
  294. void AnimatedSprite2D::UpateTimelineWorldTransform(unsigned index)
  295. {
  296. TransformInfo& info = timelineTransformInfos_[index];
  297. if (info.worldTransformUpdated_)
  298. return;
  299. if (info.parent_ == -1)
  300. {
  301. info.worldTransform_ = info.localTransform_;
  302. info.worldTransformUpdated_ = true;
  303. }
  304. else
  305. {
  306. UpateTimelineWorldTransform(info.parent_);
  307. info.worldTransform_ = timelineTransformInfos_[info.parent_].worldTransform_ * info.localTransform_;
  308. info.worldTransformUpdated_ = true;
  309. }
  310. }
  311. void AnimatedSprite2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  312. {
  313. using namespace ScenePostUpdate;
  314. float timeStep = eventData[P_TIMESTEP].GetFloat();
  315. UpdateAnimation(timeStep);
  316. }
  317. }