Browse Source

Animation finish event.

Lasse Öörni 10 years ago
parent
commit
ac67a462ba
2 changed files with 46 additions and 0 deletions
  1. 36 0
      Source/Urho3D/Graphics/AnimationState.cpp
  2. 10 0
      Source/Urho3D/Graphics/DrawableEvents.h

+ 36 - 0
Source/Urho3D/Graphics/AnimationState.cpp

@@ -249,18 +249,54 @@ void AnimationState::AddTime(float delta)
     if (delta == 0.0f || length == 0.0f)
         return;
 
+    bool sendFinishEvent = false;
+
     float oldTime = GetTime();
     float time = oldTime + delta;
     if (looped_)
     {
         while (time >= length)
+        {
             time -= length;
+            sendFinishEvent = true;
+        }
         while (time < 0.0f)
+        {
             time += length;
+            sendFinishEvent = true;
+        }
     }
 
     SetTime(time);
 
+    if (!looped_)
+    {
+        if (delta > 0.0f && oldTime < length && GetTime() == length)
+            sendFinishEvent = true;
+        else if (delta < 0.0f && oldTime > 0.0f && GetTime() == 0.0f)
+            sendFinishEvent = true;
+    }
+
+    // Process finish event
+    if (sendFinishEvent)
+    {
+        using namespace AnimationFinished;
+
+        WeakPtr<AnimationState> self(this);
+        WeakPtr<Node> senderNode(model_ ? model_->GetNode() : node_);
+
+        VariantMap& eventData = senderNode->GetEventDataMap();
+        eventData[P_NODE] = senderNode;
+        eventData[P_ANIMATION] = animation_;
+        eventData[P_NAME] = animation_->GetAnimationName();
+        eventData[P_LOOPED] = looped_;
+
+        // Note: this may cause arbitrary deletion of animation states, including the one we are currently processing
+        senderNode->SendEvent(E_ANIMATIONFINISHED, eventData);
+        if (senderNode.Expired() || self.Expired())
+            return;
+    }
+
     // Process animation triggers
     if (animation_->GetNumTriggers())
     {

+ 10 - 0
Source/Urho3D/Graphics/DrawableEvents.h

@@ -42,6 +42,16 @@ URHO3D_EVENT(E_ANIMATIONTRIGGER, AnimationTrigger)
     URHO3D_PARAM(P_TIME, Time);                    // Float
     URHO3D_PARAM(P_DATA, Data);                    // User-defined data type
 }
+
+/// AnimatedModel animation finished or looped.
+URHO3D_EVENT(E_ANIMATIONFINISHED, AnimationFinished)
+{
+    URHO3D_PARAM(P_NODE, Node);                    // Node pointer
+    URHO3D_PARAM(P_ANIMATION, Animation);          // Animation pointer
+    URHO3D_PARAM(P_NAME, Name);                    // String
+    URHO3D_PARAM(P_LOOPED, Looped);                // Bool
+}
+
 /// Terrain geometry created.
 URHO3D_EVENT(E_TERRAINCREATED, TerrainCreated)
 {