XAnimatedSprite2D.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "Context.h"
  24. #include "ResourceCache.h"
  25. #include "Scene.h"
  26. #include "SceneEvents.h"
  27. #include "Sprite2D.h"
  28. #include "StaticSprite2D.h"
  29. #include "XAnimatedSprite2D.h"
  30. #include "XAnimation2D.h"
  31. #include "XAnimationSet2D.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. extern const char* URHO2D_CATEGORY;
  36. extern const char* blendModeNames[];
  37. XAnimatedSprite2D::XAnimatedSprite2D(Context* context) :
  38. Drawable(context, DRAWABLE_GEOMETRY),
  39. layer_(0),
  40. orderInLayer_(0),
  41. blendMode_(BLEND_ALPHA),
  42. speed_(1.0f),
  43. animationTime_(0.0f)
  44. {
  45. }
  46. XAnimatedSprite2D::~XAnimatedSprite2D()
  47. {
  48. }
  49. void XAnimatedSprite2D::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<XAnimatedSprite2D>(URHO2D_CATEGORY);
  52. ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_INT, "Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  53. ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_INT, "Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  54. ENUM_ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, "Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  55. ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_FLOAT, "Speed", GetSpeed, SetSpeed, float, 1.0f, AM_DEFAULT);
  56. ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_RESOURCEREF, "Animation Set", GetAnimationSetAttr, SetAnimationSetAttr, ResourceRef, ResourceRef(XAnimatedSprite2D::GetTypeStatic()), AM_DEFAULT);
  57. REF_ACCESSOR_ATTRIBUTE(XAnimatedSprite2D, VAR_STRING, "Animation", GetAnimation, SetAnimation, String, String::EMPTY, AM_DEFAULT);
  58. COPY_BASE_ATTRIBUTES(Drawable2D, Drawable);
  59. }
  60. void XAnimatedSprite2D::OnSetEnabled()
  61. {
  62. Drawable::OnSetEnabled();
  63. Scene* scene = GetScene();
  64. if (scene)
  65. {
  66. if (IsEnabledEffective())
  67. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(XAnimatedSprite2D, HandleScenePostUpdate));
  68. else
  69. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  70. }
  71. }
  72. void XAnimatedSprite2D::SetLayer(int layer)
  73. {
  74. if (layer == layer_)
  75. return;
  76. layer_ = layer;
  77. for (unsigned i = 0; i < objectNodes_.Size(); ++i)
  78. {
  79. StaticSprite2D* objectSprite = objectNodes_[i]->GetComponent<StaticSprite2D>();
  80. objectSprite->SetLayer(layer_);
  81. }
  82. }
  83. void XAnimatedSprite2D::SetOrderInLayer(int orderInLayer)
  84. {
  85. orderInLayer_ = orderInLayer;
  86. }
  87. void XAnimatedSprite2D::SetBlendMode(BlendMode blendMode)
  88. {
  89. if (blendMode == blendMode_)
  90. return;
  91. blendMode_ = blendMode;
  92. for (unsigned i = 0; i < objectNodes_.Size(); ++i)
  93. {
  94. StaticSprite2D* objectSprite = objectNodes_[i]->GetComponent<StaticSprite2D>();
  95. objectSprite->SetBlendMode(blendMode_);
  96. }
  97. }
  98. void XAnimatedSprite2D::SetSpeed(float speed)
  99. {
  100. speed_ = speed;
  101. MarkNetworkUpdate();
  102. }
  103. void XAnimatedSprite2D::SetAnimation(XAnimationSet2D* 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 XAnimatedSprite2D::SetAnimationSet(XAnimationSet2D* 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 XAnimatedSprite2D::SetAnimation(const String& name)
  123. {
  124. animationName_ = name;
  125. if (animationSet_)
  126. SetAnimation(animationSet_->GetAnimation(animationName_));
  127. }
  128. XAnimationSet2D* XAnimatedSprite2D::GetAnimationSet() const
  129. {
  130. return animationSet_;
  131. }
  132. void XAnimatedSprite2D::SetAnimationSetAttr(ResourceRef value)
  133. {
  134. ResourceCache* cache = GetSubsystem<ResourceCache>();
  135. SetAnimationSet(cache->GetResource<XAnimationSet2D>(value.name_));
  136. }
  137. Urho3D::ResourceRef XAnimatedSprite2D::GetAnimationSetAttr() const
  138. {
  139. return GetResourceRef(animationSet_, XAnimationSet2D::GetTypeStatic());
  140. }
  141. void XAnimatedSprite2D::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(XAnimatedSprite2D, HandleScenePostUpdate));
  149. }
  150. }
  151. void XAnimatedSprite2D::OnWorldBoundingBoxUpdate()
  152. {
  153. boundingBox_.Clear();
  154. worldBoundingBox_.Clear();
  155. for (unsigned i = 0; i < objectNodes_.Size(); ++i)
  156. {
  157. StaticSprite2D* ss = objectNodes_[i]->GetComponent<StaticSprite2D>();
  158. worldBoundingBox_.Merge(ss->GetWorldBoundingBox());
  159. }
  160. boundingBox_ = worldBoundingBox_.Transformed(node_->GetWorldTransform().Inverse());
  161. }
  162. void XAnimatedSprite2D::SetAnimation(XAnimation2D* animation)
  163. {
  164. if (animation == animation_)
  165. {
  166. // Reset time
  167. animationTime_ = 0.0f;
  168. return;
  169. }
  170. if (animation_)
  171. {
  172. // Remove all object nodes
  173. for (unsigned i = 0; i < objectNodes_.Size(); ++i)
  174. objectNodes_[i]->Remove();
  175. objectNodes_.Clear();
  176. }
  177. animation_ = animation;
  178. if (!animation_)
  179. return;
  180. animationTime_ = 0.0f;
  181. for (unsigned i = 0; i < animation_->GetTimelines().Size(); ++i)
  182. {
  183. SharedPtr<Node> objectNode(GetNode()->CreateChild(animation_->GetTimelines()[i].name_));
  184. StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
  185. staticSprite->SetLayer(layer_);
  186. staticSprite->SetBlendMode(blendMode_);
  187. staticSprite->SetUseHotSpot(true);
  188. objectNodes_.Push(objectNode);
  189. }
  190. UpdateAnimation(0.0f);
  191. MarkNetworkUpdate();
  192. }
  193. void XAnimatedSprite2D::UpdateAnimation(float timeStep)
  194. {
  195. if (!animation_)
  196. return;
  197. animationTime_ += timeStep * speed_;
  198. float animtationLength = animation_->GetLength();
  199. float time;
  200. if (animation_->IsLooped())
  201. {
  202. time = fmodf(animationTime_, animtationLength);
  203. if (time < 0.0f)
  204. time += animation_->GetLength();
  205. }
  206. else
  207. time = Clamp(animationTime_, 0.0f, animtationLength);
  208. const Vector<MainlineKey>& mainlineKeys = animation_->GetMainlineKeys();
  209. const MainlineKey* mainlineKey = 0;
  210. for (unsigned i = 1; i < mainlineKeys.Size(); ++i)
  211. {
  212. if (time < mainlineKeys[i].time_)
  213. {
  214. mainlineKey = &mainlineKeys[i - 1];
  215. break;
  216. }
  217. }
  218. if (!mainlineKey)
  219. mainlineKey = &mainlineKeys.Back();
  220. const Vector<Timeline>& timelines = animation_->GetTimelines();
  221. for (unsigned i = 0; i < timelines.Size(); ++i)
  222. {
  223. Node* objectNode = objectNodes_[i];
  224. const ObjectRef* objectRef = mainlineKey->GetObjectRef(i);
  225. if (!objectRef)
  226. objectNode->SetEnabled(false);
  227. else
  228. {
  229. objectNode->SetEnabled(true);
  230. StaticSprite2D* staticSprite = objectNode->GetComponent<StaticSprite2D>();
  231. staticSprite->SetOrderInLayer(orderInLayer_ + objectRef->zIndex_);
  232. const Vector<ObjectKey>& objectKeys = timelines[i].objectKeys_;
  233. for (unsigned j = 0; j < objectKeys.Size() - 1; ++j)
  234. {
  235. if (time <= objectKeys[j + 1].time_)
  236. {
  237. const ObjectKey& currKey = objectKeys[j];
  238. const ObjectKey& nextKey = objectKeys[j + 1];
  239. float t = (time - currKey.time_) / (nextKey.time_ - currKey.time_);
  240. if (t < 0.0f || t > 1.0f)
  241. {
  242. t = 0;
  243. }
  244. objectNode->SetPosition(currKey.position_.Lerp(nextKey.position_, t));
  245. float angle;
  246. if (currKey.spin_ > 0 && currKey.angle_ > nextKey.angle_)
  247. angle = Lerp(currKey.angle_, nextKey.angle_ + 360.0f, t);
  248. else if (currKey.spin_ < 0 && currKey.angle_ < nextKey.angle_)
  249. angle = Lerp(currKey.angle_, nextKey.angle_ - 360.0f, t);
  250. else
  251. angle = Lerp(currKey.angle_, nextKey.angle_, t);
  252. objectNode->SetRotation(angle);
  253. objectNode->SetScale(currKey.scale_.Lerp(nextKey.scale_, t));
  254. staticSprite->SetSprite(currKey.sprite_);
  255. staticSprite->SetHotSpot(currKey.pivot_.Lerp(nextKey.pivot_, t));
  256. float alpha_ = Lerp(currKey.alpha_, nextKey.alpha_, t);
  257. staticSprite->SetColor(Color(1.0f, 1.0f, 1.0f, alpha_));
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. MarkForUpdate();
  264. }
  265. void XAnimatedSprite2D::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  266. {
  267. using namespace ScenePostUpdate;
  268. float timeStep = eventData[P_TIMESTEP].GetFloat();
  269. UpdateAnimation(timeStep);
  270. }
  271. }