Browse Source

Send E_ANIMATIONFINISHED event also for AnimatedSprite2D animation loop/end.

Lasse Öörni 9 years ago
parent
commit
16997009c6

+ 2 - 2
Source/Urho3D/Urho2D/AnimatedSprite2D.cpp

@@ -136,7 +136,7 @@ void AnimatedSprite2D::SetAnimationSet(AnimationSet2D* animationSet)
 #endif
     if (animationSet_->GetSpriterData())
     {
-        spriterInstance_ = new Spriter::SpriterInstance(animationSet_->GetSpriterData());
+        spriterInstance_ = new Spriter::SpriterInstance(this, animationSet_->GetSpriterData());
 
         if (!animationSet_->GetSpriterData()->entities_.Empty())
         {
@@ -406,7 +406,7 @@ void AnimatedSprite2D::UpdateSourceBatchesSpine()
 void AnimatedSprite2D::SetSpriterAnimation()
 {
     if (!spriterInstance_)
-        spriterInstance_ = new Spriter::SpriterInstance(animationSet_->GetSpriterData());
+        spriterInstance_ = new Spriter::SpriterInstance(this, animationSet_->GetSpriterData());
 
     // Use entity is empty first entity
     if (entity_.Empty())

+ 29 - 3
Source/Urho3D/Urho2D/SpriterInstance2D.cpp

@@ -22,6 +22,9 @@
 
 #include "../Precompiled.h"
 
+#include "../Graphics/DrawableEvents.h"
+#include "../Scene/Component.h"
+#include "../Scene/Node.h"
 #include "../Urho2D/SpriterInstance2D.h"
 
 #include <cmath>
@@ -32,7 +35,8 @@ namespace Urho3D
 namespace Spriter
 {
 
-SpriterInstance::SpriterInstance(SpriterData* spriteData) : 
+SpriterInstance::SpriterInstance(Component* owner, SpriterData* spriteData) : 
+    owner_(owner),
     spriterData_(spriteData),
     entity_(0),
     animation_(0)
@@ -126,16 +130,38 @@ void SpriterInstance::Update(float deltaTime)
 
     Clear();
 
+    float lastTime = currentTime_;
     currentTime_ += deltaTime;
     if (currentTime_ > animation_->length_)
     {
+        bool sendFinishEvent = false;
+
         if (looping_)
         {
             currentTime_ = fmod(currentTime_, animation_->length_);
+            sendFinishEvent = true;
         }
         else
         {
             currentTime_ = animation_->length_;
+            sendFinishEvent = lastTime != currentTime_;
+        }
+
+        if (sendFinishEvent && owner_)
+        {
+            Node* senderNode = owner_->GetNode();
+            if (senderNode)
+            {
+                using namespace AnimationFinished;
+
+                VariantMap& eventData = senderNode->GetEventDataMap();
+                eventData[P_NODE] = senderNode;
+                eventData[P_ANIMATION] = animation_;
+                eventData[P_NAME] = animation_->name_;
+                eventData[P_LOOPED] = looping_;
+    
+                senderNode->SendEvent(E_ANIMATIONFINISHED, eventData);
+            }
         }
     }
 
@@ -186,7 +212,7 @@ void SpriterInstance::UpdateTimelineKeys()
         else
         {
             timelineKey->info_ = timelineKey->info_.UnmapFromParent(spatialInfo_);
-        }            
+        }
         timelineKeys_.Push(timelineKey);
     }
 
@@ -203,7 +229,7 @@ void SpriterInstance::UpdateTimelineKeys()
         {
             timelineKey->info_ = timelineKey->info_.UnmapFromParent(spatialInfo_);
         }
-            
+        
         timelineKey->zIndex_ = ref->zIndex_;
 
         timelineKeys_.Push(timelineKey);

+ 5 - 2
Source/Urho3D/Urho2D/SpriterInstance2D.h

@@ -27,6 +27,8 @@
 namespace Urho3D
 {
 
+class Component;
+
 namespace Spriter
 {
 
@@ -43,7 +45,7 @@ class SpriterInstance
 {
 public:
     /// Constructor with spriter data.
-    SpriterInstance(SpriterData* spriteData);
+    SpriterInstance(Component* owner, SpriterData* spriteData);
     /// Destructor.
     ~SpriterInstance();
 
@@ -85,6 +87,8 @@ private:
     /// Clear mainline key and timeline keys.
     void Clear();
 
+    /// Parent component.
+    Component* owner_;
     /// Spriter data.
     SpriterData* spriterData_;
     /// Current entity.
@@ -101,7 +105,6 @@ private:
     MainlineKey* mainlineKey_;
     /// Current timeline keys.
     PODVector<SpatialTimelineKey*> timelineKeys_;
-        
 };
 
 }