Bläddra i källkod

Move CalculateScaledTime to AttributeAnimationInfo, extract GetEventFrames method.

aster 11 år sedan
förälder
incheckning
3afd10aaa6

+ 2 - 2
Source/Engine/Scene/Animatable.cpp

@@ -114,7 +114,7 @@ bool Animatable::SaveXML(XMLElement& dest) const
     for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
     for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
     {
     {
         AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
         AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
-        if (attributeAnimation->GetObjectAnimation())
+        if (attributeAnimation->GetOwner())
             continue;
             continue;
 
 
         const AttributeInfo& attr = i->second_->GetAttributeInfo();
         const AttributeInfo& attr = i->second_->GetAttributeInfo();
@@ -291,7 +291,7 @@ void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
     Vector<String> names;
     Vector<String> names;
     for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::Iterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
     for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::Iterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
     {
     {
-        if (i->second_->GetAttributeAnimation()->GetObjectAnimation() == objectAnimation)
+        if (i->second_->GetAttributeAnimation()->GetOwner() == objectAnimation)
             names.Push(i->first_);
             names.Push(i->first_);
     }
     }
 
 

+ 3 - 2
Source/Engine/Scene/AttributeAnimation.cpp

@@ -44,6 +44,7 @@ const char* interpMethodNames[] =
 
 
 AttributeAnimation::AttributeAnimation(Context* context) :
 AttributeAnimation::AttributeAnimation(Context* context) :
     Resource(context),
     Resource(context),
+    owner_(0),
     interpolationMethod_(IM_LINEAR),
     interpolationMethod_(IM_LINEAR),
     splineTension_(0.5f),
     splineTension_(0.5f),
     valueType_(VAR_NONE),
     valueType_(VAR_NONE),
@@ -175,9 +176,9 @@ void AttributeAnimation::SetValueType(VariantType valueType)
     endTime_ = -M_INFINITY;
     endTime_ = -M_INFINITY;
 }
 }
 
 
-void AttributeAnimation::SetObjectAnimation(ObjectAnimation* objectAnimation)
+void AttributeAnimation::SetOwner(void* owner)
 {
 {
-    objectAnimation_ = objectAnimation;
+    owner_ = owner;
 }
 }
 
 
 void AttributeAnimation::SetInterpolationMethod(InterpMethod method)
 void AttributeAnimation::SetInterpolationMethod(InterpMethod method)

+ 6 - 7
Source/Engine/Scene/AttributeAnimation.h

@@ -29,7 +29,6 @@ namespace Urho3D
 {
 {
 
 
 class Animatable;
 class Animatable;
-class ObjectAnimation;
 class XMLElement;
 class XMLElement;
 struct AttributeInfo;
 struct AttributeInfo;
 
 
@@ -84,8 +83,8 @@ public:
     /// Save as XML data. Return true if successful.
     /// Save as XML data. Return true if successful.
     bool SaveXML(XMLElement& dest) const;
     bool SaveXML(XMLElement& dest) const;
 
 
-    /// Set object animation.
-    void SetObjectAnimation(ObjectAnimation* objectAnimation);
+    /// Set owner.
+    void SetOwner(void* owner);
     /// Set interpolation method.
     /// Set interpolation method.
     void SetInterpolationMethod(InterpMethod method);
     void SetInterpolationMethod(InterpMethod method);
     /// Set spline tension, should be between 0.0f and 1.0f, but this is not a must.
     /// Set spline tension, should be between 0.0f and 1.0f, but this is not a must.
@@ -100,8 +99,8 @@ public:
 
 
     /// Return animation is valid.
     /// Return animation is valid.
     bool IsValid() const;
     bool IsValid() const;
-    /// Return object animation.
-    ObjectAnimation* GetObjectAnimation() const { return objectAnimation_; }
+    /// Return owner.
+    void* GetOwner() const { return owner_; }
     /// Return interpolation method.
     /// Return interpolation method.
     InterpMethod GetInterpolationMethod() const { return interpolationMethod_; }
     InterpMethod GetInterpolationMethod() const { return interpolationMethod_; }
     /// Return spline tension.
     /// Return spline tension.
@@ -129,8 +128,8 @@ protected:
     /// Return (value1 - value2) * t.
     /// Return (value1 - value2) * t.
     Variant SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const;
     Variant SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const;
 
 
-    /// Object animation.
-    WeakPtr<ObjectAnimation> objectAnimation_;
+    /// Owner.
+    void* owner_;
     /// Interpolation method.
     /// Interpolation method.
     InterpMethod interpolationMethod_;
     InterpMethod interpolationMethod_;
     /// Spline tension.
     /// Spline tension.

+ 51 - 0
Source/Engine/Scene/AttributeAnimationInfo.cpp

@@ -23,6 +23,7 @@
 #include "Precompiled.h"
 #include "Precompiled.h"
 #include "AttributeAnimation.h"
 #include "AttributeAnimation.h"
 #include "AttributeAnimationInfo.h"
 #include "AttributeAnimationInfo.h"
+#include "Log.h"
 
 
 #include "DebugNew.h"
 #include "DebugNew.h"
 
 
@@ -48,4 +49,54 @@ AttributeAnimationInfo::~AttributeAnimationInfo()
 {
 {
 }
 }
 
 
+float AttributeAnimationInfo::CalculateScaledTime(float currentTime, bool& finished) const
+{
+    float beginTime = attributeAnimation_->GetBeginTime();
+    float endTime = attributeAnimation_->GetEndTime();
+
+    switch (wrapMode_)
+    {
+    case WM_LOOP:
+        {
+            float span = endTime - beginTime;
+            float time = fmodf(currentTime - beginTime, span);
+            if (time < 0.0f)
+                time += span;
+            return beginTime + time;
+        }
+
+    case WM_ONCE:
+        finished = (currentTime >= endTime);
+        // Fallthrough
+
+    case WM_CLAMP:
+        return Clamp(currentTime, beginTime, endTime);
+
+    default:
+        LOGERROR("Unsupported attribute animation wrap mode");
+        return beginTime;
+    }
+}
+
+void AttributeAnimationInfo::GetEventFrames(float beginTime, float endTime, PODVector<const AttributeEventFrame*>& eventFrames)
+{
+    switch (wrapMode_)
+    {
+    case WM_LOOP:
+        if (beginTime < endTime)
+            attributeAnimation_->GetEventFrames(beginTime, endTime, eventFrames);
+        else
+        {
+            attributeAnimation_->GetEventFrames(beginTime, attributeAnimation_->GetEndTime(), eventFrames);
+            attributeAnimation_->GetEventFrames(attributeAnimation_->GetBeginTime(), endTime, eventFrames);
+        }
+        break;
+
+    case WM_ONCE:
+    case WM_CLAMP:
+        attributeAnimation_->GetEventFrames(beginTime, endTime, eventFrames);
+        break;
+    }
+}
+
 }
 }

+ 5 - 0
Source/Engine/Scene/AttributeAnimationInfo.h

@@ -54,6 +54,11 @@ public:
     float GetSpeed() const { return speed_; }
     float GetSpeed() const { return speed_; }
 
 
 protected:
 protected:
+    /// Calculate scaled time.
+    float CalculateScaledTime(float currentTime, bool& finished) const;
+    /// Return event frames.
+    void AttributeAnimationInfo::GetEventFrames(float beginTime, float endTime, PODVector<const AttributeEventFrame*>& eventFrames);
+
     /// Attribute animation.
     /// Attribute animation.
     SharedPtr<AttributeAnimation> attributeAnimation_;
     SharedPtr<AttributeAnimation> attributeAnimation_;
     /// Wrap mode.
     /// Wrap mode.

+ 1 - 46
Source/Engine/Scene/AttributeAnimationInstance.cpp

@@ -72,23 +72,7 @@ bool AttributeAnimationInstance::Update(float timeStep)
     if (attributeAnimation_->HasEventFrames())
     if (attributeAnimation_->HasEventFrames())
     {
     {
         PODVector<const AttributeEventFrame*> eventFrames;
         PODVector<const AttributeEventFrame*> eventFrames;
-        switch (wrapMode_)
-        {
-        case WM_LOOP:
-            if (lastScaledTime_ < scaledTime)
-                attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
-            else
-            {
-                attributeAnimation_->GetEventFrames(lastScaledTime_, attributeAnimation_->GetEndTime(), eventFrames);
-                attributeAnimation_->GetEventFrames(attributeAnimation_->GetBeginTime(), scaledTime, eventFrames);
-            }
-            break;
-
-        case WM_ONCE:
-        case WM_CLAMP:
-            attributeAnimation_->GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
-            break;
-        }
+        GetEventFrames(lastScaledTime_, scaledTime, eventFrames);
 
 
         for (unsigned i = 0; i < eventFrames.Size(); ++i)
         for (unsigned i = 0; i < eventFrames.Size(); ++i)
             animatable_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
             animatable_->SendEvent(eventFrames[i]->eventType_, const_cast<VariantMap&>(eventFrames[i]->eventData_));
@@ -99,33 +83,4 @@ bool AttributeAnimationInstance::Update(float timeStep)
     return finished;
     return finished;
 }
 }
 
 
-float AttributeAnimationInstance::CalculateScaledTime(float currentTime, bool& finished) const
-{
-    float beginTime = attributeAnimation_->GetBeginTime();
-    float endTime = attributeAnimation_->GetEndTime();
-
-    switch (wrapMode_)
-    {
-    case WM_LOOP:
-        {
-            float span = endTime - beginTime;
-            float time = fmodf(currentTime - beginTime, span);
-            if (time < 0.0f)
-                time += span;
-            return beginTime + time;
-        }
-
-    case WM_ONCE:
-        finished = (currentTime >= endTime);
-        // Fallthrough
-
-    case WM_CLAMP:
-        return Clamp(currentTime, beginTime, endTime);
-
-    default:
-        LOGERROR("Unsupported attribute animation wrap mode");
-        return beginTime;
-    }
-}
-
 }
 }

+ 0 - 3
Source/Engine/Scene/AttributeAnimationInstance.h

@@ -51,9 +51,6 @@ public:
     float GetCurrentTime() const { return currentTime_; }
     float GetCurrentTime() const { return currentTime_; }
 
 
 private:
 private:
-    /// Calculate scaled time.
-    float CalculateScaledTime(float currentTime, bool& finished) const;
-
     /// Animatable.
     /// Animatable.
     WeakPtr<Animatable> animatable_;
     WeakPtr<Animatable> animatable_;
     /// Attribute information.
     /// Attribute information.

+ 3 - 3
Source/Engine/Scene/ObjectAnimation.cpp

@@ -131,7 +131,7 @@ void ObjectAnimation::AddAttributeAnimation(const String& name, AttributeAnimati
     if (!attributeAnimation)
     if (!attributeAnimation)
         return;
         return;
 
 
-    attributeAnimation->SetObjectAnimation(this);
+    attributeAnimation->SetOwner(this);
     attributeAnimationInfos_[name] = new AttributeAnimationInfo(attributeAnimation, wrapMode, speed);
     attributeAnimationInfos_[name] = new AttributeAnimationInfo(attributeAnimation, wrapMode, speed);
 }
 }
 
 
@@ -140,7 +140,7 @@ void ObjectAnimation::RemoveAttributeAnimation(const String& name)
     HashMap<String, SharedPtr<AttributeAnimationInfo> >::Iterator i = attributeAnimationInfos_.Find(name);
     HashMap<String, SharedPtr<AttributeAnimationInfo> >::Iterator i = attributeAnimationInfos_.Find(name);
     if (i != attributeAnimationInfos_.End())
     if (i != attributeAnimationInfos_.End())
     {
     {
-        i->second_->GetAttributeAnimation()->SetObjectAnimation(0);
+        i->second_->GetAttributeAnimation()->SetOwner(0);
         attributeAnimationInfos_.Erase(i);
         attributeAnimationInfos_.Erase(i);
     }
     }
 }
 }
@@ -154,7 +154,7 @@ void ObjectAnimation::RemoveAttributeAnimation(AttributeAnimation* attributeAnim
     {
     {
         if (i->second_->GetAttributeAnimation() == attributeAnimation)
         if (i->second_->GetAttributeAnimation() == attributeAnimation)
         {
         {
-            attributeAnimation->SetObjectAnimation(0);
+            attributeAnimation->SetOwner(0);
             attributeAnimationInfos_.Erase(i);
             attributeAnimationInfos_.Erase(i);
             return;
             return;
         }
         }