Răsfoiți Sursa

Add AttributeAnimationInfo class, add more comment for animation class, use XMLElement::SetVariant/GetVariant save attribute animation frame value.

Aster Jian 11 ani în urmă
părinte
comite
2c18a1ca65

+ 7 - 3
Source/Engine/Scene/Animatable.cpp

@@ -298,9 +298,13 @@ void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
         return;
 
     // Set all attribute animations from the object animation
-    HashMap<String, SharedPtr<AttributeAnimation> > attributeAnimations = objectAnimation->GetAttributeAnimations();
-    for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations.Begin(); i != attributeAnimations.End(); ++i)
-        SetAttributeAnimation(i->first_, i->second_, objectAnimation->GetAttributeAnimationWrapMode(i->first_), objectAnimation->GetAttributeAnimationSpeed(i->first_));
+    const HashMap<String, SharedPtr<AttributeAnimationInfo> >& attributeAnimationInfos = objectAnimation->GetAttributeAnimationInfos();
+    for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos.Begin(); i != attributeAnimationInfos.End(); ++i)
+    {
+        const String& name = i->first_;
+        AttributeAnimationInfo* info = i->second_;
+        SetAttributeAnimation(name, info->GetAttributeAnimation(), info->GetWrapMode(), info->GetSpeed());
+    }
 }
 
 void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)

+ 1 - 1
Source/Engine/Scene/Animatable.h

@@ -33,7 +33,7 @@ class AttributeAnimation;
 class AttributeAnimationInstance;
 class ObjectAnimation;
 
-/// Base class for animatable object
+/// Base class for animatable object, an animatable object can be set animation on it's attributes, or can be set an object animation to it.
 class URHO3D_API Animatable : public Serializable
 {
     OBJECT(Animatable);

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

@@ -76,13 +76,11 @@ bool AttributeAnimation::LoadXML(const XMLElement& source)
     valueType_ = VAR_NONE;
     eventFrames_.Clear();
 
-    SetValueType(Variant::GetTypeFromName(source.GetAttribute("valuetype")));
-
     XMLElement keyFrameEleme = source.GetChild("keyframe");
     while (keyFrameEleme)
     {
         float time = keyFrameEleme.GetFloat("time");
-        Variant value(valueType_, keyFrameEleme.GetAttribute("value"));
+        Variant value = keyFrameEleme.GetVariant();
         SetKeyFrame(time, value);
 
         keyFrameEleme = keyFrameEleme.GetNext("keyframe");
@@ -104,14 +102,12 @@ bool AttributeAnimation::LoadXML(const XMLElement& source)
 
 bool AttributeAnimation::SaveXML(XMLElement& dest) const
 {
-    dest.SetAttribute("valuetype", Variant::GetTypeName(valueType_));
-
     for (unsigned i = 0; i < keyFrames_.Size(); ++i)
     {
         const AttributeKeyFrame& keyFrame = keyFrames_[i];
         XMLElement keyFrameEleme = dest.CreateChild("keyframe");
         keyFrameEleme.SetFloat("time", keyFrame.time_);
-        keyFrameEleme.SetAttribute("value", keyFrame.value_.ToString());
+        keyFrameEleme.SetVariant(keyFrame.value_);
     }
 
     for (unsigned i = 0; i < eventFrames_.Size(); ++i)

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

@@ -0,0 +1,56 @@
+//
+// Copyright (c) 2008-2014 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "Precompiled.h"
+#include "AttributeAnimation.h"
+#include "AttributeAnimationInfo.h"
+
+#include "DebugNew.h"
+
+namespace Urho3D
+{
+
+AttributeAnimationInfo::AttributeAnimationInfo(AttributeAnimation* attributeAnimation, WrapMode wrapMode, float speed) :
+    attributeAnimation_(attributeAnimation),
+    wrapMode_(wrapMode),
+    speed_(speed)
+{
+    speed_ = Max(0.0f, speed_);
+}
+
+AttributeAnimationInfo::AttributeAnimationInfo(const AttributeAnimationInfo& other) :
+    attributeAnimation_(other.attributeAnimation_),
+    wrapMode_(other.wrapMode_),
+    speed_(other.speed_)
+{
+}
+
+AttributeAnimationInfo::~AttributeAnimationInfo()
+{
+}
+
+AttributeAnimation* AttributeAnimationInfo::GetAttributeAnimation() const
+{
+    return attributeAnimation_;
+}
+
+}

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

@@ -0,0 +1,65 @@
+//
+// Copyright (c) 2008-2014 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "RefCounted.h"
+#include "ObjectAnimation.h"
+
+namespace Urho3D
+{
+
+class AttributeAnimation;
+
+/// Attribute animation info, it include attribute animation, wrap mode and animation speed.
+class URHO3D_API AttributeAnimationInfo : public RefCounted
+{
+public:
+    /// Construct.
+    AttributeAnimationInfo(AttributeAnimation* attributeAnimation, WrapMode wrapMode, float speed);
+    /// Construct.
+    AttributeAnimationInfo(const AttributeAnimationInfo& other);
+    /// Destruct.
+    ~AttributeAnimationInfo();
+
+    /// Set wrap mode.
+    void SetWrapMode(WrapMode wrapMode) { wrapMode_ = wrapMode; }
+    /// Set speed.
+    void SetSpeed(float speed) { speed_ = speed; }
+
+    /// Return attribute animation.
+    AttributeAnimation* GetAttributeAnimation() const;
+    /// Return wrap mode.
+    WrapMode GetWrapMode() const { return wrapMode_; }
+    /// Return speed.
+    float GetSpeed() const { return speed_; }
+
+protected:
+    /// Attribute animation.
+    SharedPtr<AttributeAnimation> attributeAnimation_;
+    /// Wrap mode.
+    WrapMode wrapMode_;
+    /// Animation speed.
+    float speed_;
+};
+
+}

+ 2 - 12
Source/Engine/Scene/AttributeAnimationInstance.cpp

@@ -32,23 +32,18 @@ namespace Urho3D
 {
 
 AttributeAnimationInstance::AttributeAnimationInstance(Animatable* animatable, const AttributeInfo& attributeInfo, AttributeAnimation* attributeAnimation, WrapMode wrapMode, float speed) :
+    AttributeAnimationInfo(attributeAnimation, wrapMode, speed),
     animatable_(animatable),
     attributeInfo_(attributeInfo),
-    attributeAnimation_(attributeAnimation),
-    wrapMode_(wrapMode),
-    speed_(speed),
     currentTime_(0.0f),
     lastScaledTime_(0.0f)
 {
-    speed_ = Max(0.0f, speed_);
 }
 
 AttributeAnimationInstance::AttributeAnimationInstance(const AttributeAnimationInstance& other) :
+    AttributeAnimationInfo(other),
     animatable_(other.animatable_),
     attributeInfo_(other.attributeInfo_),
-    attributeAnimation_(other.attributeAnimation_),
-    wrapMode_(other.wrapMode_),
-    speed_(other.speed_),
     currentTime_(0.0f),
     lastScaledTime_(0.0f)
 {
@@ -124,11 +119,6 @@ Animatable* AttributeAnimationInstance::GetAnimatable() const
     return animatable_;
 }
 
-AttributeAnimation* AttributeAnimationInstance::GetAttributeAnimation() const
-{
-    return attributeAnimation_;
-}
-
 float AttributeAnimationInstance::CalculateScaledTime(float currentTime, bool& finished) const
 {
     float beginTime = attributeAnimation_->GetBeginTime();

+ 5 - 23
Source/Engine/Scene/AttributeAnimationInstance.h

@@ -22,18 +22,15 @@
 
 #pragma once
 
-#include "RefCounted.h"
-#include "ObjectAnimation.h"
+#include "AttributeAnimationInfo.h"
 
 namespace Urho3D
 {
 
 class Animatable;
-class AttributeAnimation;
-struct AttributeKeyFrame;
 
-/// Attribute animation instance.
-class URHO3D_API AttributeAnimationInstance : public RefCounted
+/// Attribute animation instance, it include animation runtime information, when animation playing it will update the object's attribute value automatically.
+class URHO3D_API AttributeAnimationInstance : public AttributeAnimationInfo
 {
 public:
     /// Construct.
@@ -45,24 +42,15 @@ public:
 
     /// Update (if animaiton finished return true).
     bool Update(float timeStep);
-    /// Set wrap mode.
-    void SetWrapMode(WrapMode wrapMode) { wrapMode_ = wrapMode; }
-    /// Set speed.
-    void SetSpeed(float speed) { speed_ = speed; }
+
     /// Return animatable.
     Animatable* GetAnimatable() const;
     /// Return attribute infomation.
     const AttributeInfo& GetAttributeInfo() const { return attributeInfo_; }
-    /// Return attribute animation.
-    AttributeAnimation* GetAttributeAnimation() const;
-    /// Return wrap mode.
-    WrapMode GetWrapMode() const { return wrapMode_; }
-    /// Return speed.
-    float GetSpeed() const { return speed_; }
     /// Return current time.
     float GetCurrentTime() const { return currentTime_; }
 
-protected:
+private:
     /// Calculate scaled time.
     float CalculateScaledTime(float currentTime, bool& finished) const;
     /// Interpolation.
@@ -72,12 +60,6 @@ protected:
     WeakPtr<Animatable> animatable_;
     /// Attribute information.
     const AttributeInfo& attributeInfo_;
-    /// Attribute animation.
-    SharedPtr<AttributeAnimation> attributeAnimation_;
-    /// Wrap mode.
-    WrapMode wrapMode_;
-    /// Speed.
-    float speed_;
     /// Current time.
     float currentTime_;
     /// Last scaled time.

+ 25 - 32
Source/Engine/Scene/ObjectAnimation.cpp

@@ -22,6 +22,7 @@
 
 #include "Precompiled.h"
 #include "AttributeAnimation.h"
+#include "AttributeAnimationInfo.h"
 #include "Context.h"
 #include "ObjectAnimation.h"
 #include "XMLFile.h"
@@ -75,7 +76,7 @@ bool ObjectAnimation::Save(Serializer& dest) const
 
 bool ObjectAnimation::LoadXML(const XMLElement& source)
 {
-    attributeAnimations_.Clear();
+    attributeAnimationInfos_.Clear();
 
     XMLElement animElem;
     animElem = source.GetChild("attributeanimation");
@@ -109,16 +110,17 @@ bool ObjectAnimation::LoadXML(const XMLElement& source)
 
 bool ObjectAnimation::SaveXML(XMLElement& dest) const
 {
-    for (HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
+    for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Begin(); i != attributeAnimationInfos_.End(); ++i)
     {
         XMLElement animElem = dest.CreateChild("attributeanimation");
         animElem.SetAttribute("name", i->first_);
 
-        if (!i->second_->SaveXML(animElem))
+        const AttributeAnimationInfo* info = i->second_;
+        if (!info->GetAttributeAnimation()->SaveXML(animElem))
             return false;
 
-        animElem.SetAttribute("wrapmode", wrapModeNames[GetAttributeAnimationWrapMode(i->first_)]);
-        animElem.SetFloat("speed", GetAttributeAnimationSpeed(i->first_));
+        animElem.SetAttribute("wrapmode", wrapModeNames[info->GetWrapMode()]);
+        animElem.SetFloat("speed", info->GetSpeed());
     }
 
     return true;
@@ -130,9 +132,7 @@ void ObjectAnimation::AddAttributeAnimation(const String& name, AttributeAnimati
         return;
 
     attributeAnimation->SetObjectAnimation(this);
-    attributeAnimations_[name] = attributeAnimation;
-    attributeAnimationWrapModes_[name] = wrapMode;
-    attributeAnimationSpeeds_[name] = speed;
+    attributeAnimationInfos_[name] = new AttributeAnimationInfo(attributeAnimation, wrapMode, speed);
 }
 
 void ObjectAnimation::RemoveAttributeAnimation(const String& name)
@@ -145,48 +145,41 @@ void ObjectAnimation::RemoveAttributeAnimation(AttributeAnimation* attributeAnim
     if (!attributeAnimation)
         return;
 
-    String name;
-
-    for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
+    for (HashMap<String, SharedPtr<AttributeAnimationInfo> >::Iterator i = attributeAnimationInfos_.Begin(); i != attributeAnimationInfos_.End(); ++i)
     {
-        if (i->second_ == attributeAnimation)
+        if (i->second_->GetAttributeAnimation() == attributeAnimation)
         {
-            name = i->first_;
             attributeAnimation->SetObjectAnimation(0);
-            attributeAnimations_.Erase(i);
-            break;
+            attributeAnimationInfos_.Erase(i);
+            return;
         }
     }
-
-    if (!name.Empty())
-    {
-        attributeAnimationWrapModes_.Erase(name);
-        attributeAnimationSpeeds_.Erase(name);
-    }
 }
 
 AttributeAnimation* ObjectAnimation::GetAttributeAnimation(const String& name) const
 {
-    HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Find(name);
-    if (i != attributeAnimations_.End())
-        return i->second_;
-    return 0;
+    AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
+    return info ? info->GetAttributeAnimation() : 0;
 }
 
 WrapMode ObjectAnimation::GetAttributeAnimationWrapMode(const String& name) const
 {
-    HashMap<String, WrapMode>::ConstIterator i = attributeAnimationWrapModes_.Find(name);
-    if (i != attributeAnimationWrapModes_.End())
-        return i->second_;
-    return WM_LOOP;
+    AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
+    return info ? info->GetWrapMode() : WM_LOOP;
 }
 
 float ObjectAnimation::GetAttributeAnimationSpeed(const String& name) const
 {
-    HashMap<String, float>::ConstIterator i = attributeAnimationSpeeds_.Find(name);
-    if (i != attributeAnimationSpeeds_.End())
+    AttributeAnimationInfo* info = GetAttributeAnimationInfo(name);
+    return info ? info->GetSpeed() : 1.0f;
+}
+
+AttributeAnimationInfo* ObjectAnimation::GetAttributeAnimationInfo(const String& name) const
+{
+    HashMap<String, SharedPtr<AttributeAnimationInfo> >::ConstIterator i = attributeAnimationInfos_.Find(name);
+    if (i != attributeAnimationInfos_.End())
         return i->second_;
-    return 1.0f;
+    return 0;
 }
 
 }

+ 10 - 10
Source/Engine/Scene/ObjectAnimation.h

@@ -28,19 +28,20 @@ namespace Urho3D
 {
 
 class AttributeAnimation;
+class AttributeAnimationInfo;
 
 /// Animation wrap mode.
 enum WrapMode
 {
     /// Loop mode.
     WM_LOOP = 0,
-    /// Play once then remove.
+    /// Play once, when animation finished it will be removed.
     WM_ONCE,
     /// Clamp mode.
     WM_CLAMP,
 };
 
-/// Object animation class.
+/// Object animation class, an object animation include one or more attribute animations and theirs wrap mode and speed for an Animatable object.
 class URHO3D_API ObjectAnimation : public Resource
 {
     OBJECT(ObjectAnimation );
@@ -75,16 +76,15 @@ public:
     WrapMode GetAttributeAnimationWrapMode(const String& name) const;
     /// Return attribute animation speed by name.
     float GetAttributeAnimationSpeed(const String& name) const;
-    /// Return all attribute animations.
-    const HashMap<String, SharedPtr<AttributeAnimation> >& GetAttributeAnimations() const { return attributeAnimations_; }
+    /// Return all attribute animations infos.
+    const HashMap<String, SharedPtr<AttributeAnimationInfo> >& GetAttributeAnimationInfos() const { return attributeAnimationInfos_; }
 
 private:
-    /// Name to attribute animation mapping.
-    HashMap<String, SharedPtr<AttributeAnimation> > attributeAnimations_;
-    /// Name to attribute animation wrap mode mapping.
-    HashMap<String, WrapMode> attributeAnimationWrapModes_;
-    /// Name to attribute animation speed mapping.
-    HashMap<String, float> attributeAnimationSpeeds_;
+    /// Return attribute animation info by name.
+    AttributeAnimationInfo* GetAttributeAnimationInfo(const String& name) const;
+
+    /// Name to attribute animation info mapping.
+    HashMap<String, SharedPtr<AttributeAnimationInfo> > attributeAnimationInfos_;
 };
 
 }