Browse Source

Remove child object animation, add load and save function.

Aster Jian 11 years ago
parent
commit
48c3868a79

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

@@ -28,6 +28,7 @@
 #include "Log.h"
 #include "ObjectAnimation.h"
 #include "ResourceCache.h"
+#include "XMLElement.h"
 
 #include "DebugNew.h"
 
@@ -49,6 +50,132 @@ void Animatable::RegisterObject(Context* context)
     ACCESSOR_ATTRIBUTE(Animatable, VAR_RESOURCEREF, "Object Animation", GetObjectAnimationAttr, SetObjectAnimationAttr, ResourceRef, ResourceRef(ObjectAnimation::GetTypeStatic()), AM_DEFAULT);
 }
 
+bool Animatable::Load(Deserializer& source, bool setInstanceDefault)
+{
+    if (!Serializable::Load(source, setInstanceDefault))
+        return false;
+
+    bool hasObjectAnimation = source.ReadBool();
+    if (hasObjectAnimation)
+    {
+        SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
+        if (!objectAnimation->Load(source))
+            return false;
+        SetObjectAnimation(objectAnimation);
+    }
+
+    unsigned count = source.ReadUInt();
+    for (unsigned i = 0; i < count; ++i)
+    {
+        String name = source.ReadString();
+        SharedPtr<AttributeAnimation> attributeAnimation(new AttributeAnimation(context_));
+        if (!attributeAnimation->Load(source))
+            return false;
+
+        SetAttributeAnimation(name, attributeAnimation);
+    }
+
+    return true;
+}
+
+bool Animatable::Save(Serializer& dest) const
+{
+    if (!Serializable::Save(dest))
+        return false;
+
+    if (objectAnimation_ && objectAnimation_->GetName().Empty())
+    {
+        dest.WriteBool(true);
+        if (!objectAnimation_->Save(dest))
+            return false;
+    }
+    else
+        dest.WriteBool(false);
+
+    unsigned count = 0;
+    for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
+    {
+        AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
+        if (attributeAnimation->GetObjectAnimation())
+            continue;
+        ++count;
+    }
+
+    dest.WriteUInt(count);
+    for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
+    {
+        AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
+        if (attributeAnimation->GetObjectAnimation())
+            continue;
+
+        const AttributeInfo& attr = i->second_->GetAttributeInfo();
+        dest.WriteString(attr.name_);
+        if (!attributeAnimation->Save(dest))
+            return false;
+    }
+
+    return true;
+}
+
+bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault)
+{
+    if (!Serializable::LoadXML(source, setInstanceDefault))
+        return false;
+    
+    XMLElement elem = source.GetChild("ObjectAnimation");
+    if (elem)
+    {
+        SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
+        if (!objectAnimation->LoadXML(elem))
+            return false;
+
+        SetObjectAnimation(objectAnimation);
+    }
+
+    elem = source.GetChild("AttributeAnimation");
+    while (elem)
+    {
+        String name = elem.GetAttribute("name");
+        SharedPtr<AttributeAnimation> attributeAnimation(new AttributeAnimation(context_));
+        if (!attributeAnimation->LoadXML(elem))
+            return false;
+
+        SetAttributeAnimation(name, attributeAnimation);
+        elem = elem.GetNext("AttributeAnimation");
+    }
+
+    return true;
+}
+
+bool Animatable::SaveXML(XMLElement& dest) const
+{
+    if (!Serializable::SaveXML(dest))
+        return false;
+
+    // Object animation without name
+    if (objectAnimation_ && objectAnimation_->GetName().Empty())
+    {
+        XMLElement elem = dest.CreateChild("objectAnimation");
+        if (!objectAnimation_->SaveXML(elem))
+            return false;
+    }
+
+    for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
+    {
+        AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
+        if (attributeAnimation->GetObjectAnimation())
+            continue;
+
+        const AttributeInfo& attr = i->second_->GetAttributeInfo();
+        XMLElement elem = dest.CreateChild("attributeAnimation");
+        elem.SetAttribute("name", attr.name_);
+        if (!attributeAnimation->SaveXML(elem))
+            return false;
+    }
+
+    return true;
+}
+
 void Animatable::SetAnimationEnabled(bool animationEnabled)
 {
     animationEnabled_ = animationEnabled;
@@ -178,9 +305,7 @@ void Animatable::SetObjectAnimationAttr(ResourceRef value)
 
 ResourceRef Animatable::GetObjectAnimationAttr() const
 {
-    if (objectAnimation_ && !objectAnimation_->GetParentAnimation())
-        return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
-    return ResourceRef();
+    return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
 }
 
 void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)

+ 13 - 4
Source/Engine/Scene/Animatable.h

@@ -45,6 +45,15 @@ public:
     /// Register object factory.
     static void RegisterObject(Context* context);
 
+    /// Load from binary data. When setInstanceDefault is set to true, after setting the attribute value, store the value as instance's default value. Return true if successful.
+    virtual bool Load(Deserializer& source, bool setInstanceDefault = false);
+    /// Save as binary data. Return true if successful.
+    virtual bool Save(Serializer& dest) const;
+    /// Load from XML data. When setInstanceDefault is set to true, after setting the attribute value, store the value as instance's default value. Return true if successful.
+    virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
+    /// Save as XML data. Return true if successful.
+    virtual bool SaveXML(XMLElement& dest) const;
+
     /// Set animation enabled.
     void SetAnimationEnabled(bool enable);
     /// Set object animation.
@@ -65,14 +74,14 @@ public:
     ResourceRef GetObjectAnimationAttr() const;
 
 protected:
-    /// Handle object animation added.
-    virtual void OnObjectAnimationAdded(ObjectAnimation* objectAnimation);
-    /// Handle object animation removed.
-    virtual void OnObjectAnimationRemoved(ObjectAnimation* objectAnimation);
     /// Handle attribute animation added.
     virtual void OnAttributeAnimationAdded() = 0;
     /// Handle attribute animation removed.
     virtual void OnAttributeAnimationRemoved() = 0;
+    /// Handle object animation added.
+    void OnObjectAnimationAdded(ObjectAnimation* objectAnimation);
+    /// Handle object animation removed.
+    void OnObjectAnimationRemoved(ObjectAnimation* objectAnimation);
     /// Update attribute animations.
     void UpdateAttributeAnimations(float timeStep);
     /// Is animated network attribute.

+ 34 - 22
Source/Engine/Scene/AttributeAnimation.cpp

@@ -24,6 +24,8 @@
 #include "AttributeAnimation.h"
 #include "Context.h"
 #include "ObjectAnimation.h"
+#include "Deserializer.h"
+#include "Serializer.h"
 #include "XMLFile.h"
 
 #include "DebugNew.h"
@@ -52,26 +54,36 @@ void AttributeAnimation::RegisterObject(Context* context)
 
 bool AttributeAnimation::Load(Deserializer& source)
 {
-    XMLFile xmlFile(context_);
-    if (xmlFile.Load(source))
-        return false;
+    keyFrames_.Clear();
 
-    XMLElement rootElem = xmlFile.GetRoot("AttributeAnimation");
-    if (!rootElem)
-        return false;
+    cycleMode_ = (CycleMode)source.ReadInt();
+    valueType_ = (VariantType)source.ReadInt();
+    unsigned count = source.ReadUInt();
 
-    return LoadXML(rootElem);
+    for (unsigned i = 0; i < count; ++i)
+    {
+        float time = source.ReadFloat();
+        Variant value = source.ReadVariant();
+        SetKeyFrame(time, value);
+    }
+
+    return true;
 }
 
 bool AttributeAnimation::Save(Serializer& dest) const
 {
-    XMLFile xmlFile(context_);
+    dest.WriteInt((int)cycleMode_);
+    dest.WriteInt((int)valueType_);
+    dest.WriteUInt(keyFrames_.Size());
 
-    XMLElement rootElem = xmlFile.CreateRoot("AttributeAnimation");
-    if (!SaveXML(rootElem))
-        return false;
+    for (unsigned i = 0; i < keyFrames_.Size(); ++i)
+    {
+        const AttributeKeyFrame& keyFrame = keyFrames_[i];
+        dest.WriteFloat(keyFrame.time_);
+        dest.WriteVariant(keyFrame.value_);
+    }
 
-    return xmlFile.Save(dest);
+    return true;
 }
 
 bool AttributeAnimation::LoadXML(const XMLElement& source)
@@ -79,7 +91,7 @@ bool AttributeAnimation::LoadXML(const XMLElement& source)
     if (source.GetName() != "AttributeAnimation")
         return false;
     
-    keyframes_.Clear();
+    keyFrames_.Clear();
 
     cycleMode_ = (CycleMode)source.GetInt("cycle mode");
     valueType_ = (VariantType)source.GetInt("value type");
@@ -105,14 +117,14 @@ bool AttributeAnimation::SaveXML(XMLElement& dest) const
     dest.SetInt("cycle mode", (int)cycleMode_);
     dest.SetInt("value type", (int)valueType_);
 
-    for (unsigned i = 0; i < keyframes_.Size(); ++i)
+    for (unsigned i = 0; i < keyFrames_.Size(); ++i)
     {
-        const AttributeKeyFrame& keyFrame = keyframes_[i];
+        const AttributeKeyFrame& keyFrame = keyFrames_[i];
         XMLElement keyFrameEleme = dest.CreateChild("KeyFrame");
         keyFrameEleme.SetFloat("time", keyFrame.time_);
         keyFrameEleme.SetAttribute("value", keyFrame.value_.ToString());
     }
-    
+
     return true;
 }
 
@@ -136,7 +148,7 @@ void AttributeAnimation::SetValueType(VariantType valueType)
     isInterpolatable_ = (valueType_ == VAR_FLOAT) || (valueType_ == VAR_VECTOR2) || (valueType_ == VAR_VECTOR3) || 
         (valueType_ == VAR_VECTOR4) || (valueType_ == VAR_QUATERNION) || (valueType_ == VAR_COLOR);
 
-    keyframes_.Clear();
+    keyFrames_.Clear();
     beginTime_ = M_INFINITY;
     endTime_ = -M_INFINITY;
 }
@@ -155,15 +167,15 @@ bool AttributeAnimation::SetKeyFrame(float time, const Variant& value)
     keyFrame.time_ = time;
     keyFrame.value_ = value;
 
-    if (keyframes_.Empty() || time >= keyframes_.Back().time_)
-        keyframes_.Push(keyFrame);
+    if (keyFrames_.Empty() || time >= keyFrames_.Back().time_)
+        keyFrames_.Push(keyFrame);
     else
     {
-        for (unsigned i = 0; i < keyframes_.Size(); ++i)
+        for (unsigned i = 0; i < keyFrames_.Size(); ++i)
         {
-            if (time < keyframes_[i].time_)
+            if (time < keyFrames_[i].time_)
             {
-                keyframes_.Insert(i, keyFrame);
+                keyFrames_.Insert(i, keyFrame);
                 break;
             }
         }

+ 2 - 2
Source/Engine/Scene/AttributeAnimation.h

@@ -110,7 +110,7 @@ public:
     /// Calculate scaled time.
     float CalculateScaledTime(float currentTime) const;
     /// Return all key frames.
-    const Vector<AttributeKeyFrame>& GetKeyFrames() const { return keyframes_; }
+    const Vector<AttributeKeyFrame>& GetKeyFrames() const { return keyFrames_; }
     /// Has event frames.
     bool HasEventFrames() const { return eventFrames_.Size() != 0; }
     /// Return all event frames between time.
@@ -131,7 +131,7 @@ protected:
     /// End time.
     float endTime_;
     /// Key frames.
-    Vector<AttributeKeyFrame> keyframes_;
+    Vector<AttributeKeyFrame> keyFrames_;
     /// Event frames.
     Vector<AttributeEventFrame> eventFrames_;
 };

+ 0 - 26
Source/Engine/Scene/Node.cpp

@@ -1444,32 +1444,6 @@ unsigned Node::GetNumPersistentComponents() const
     return ret;
 }
 
-void Node::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
-{
-    Animatable::OnObjectAnimationAdded(objectAnimation);
-
-    const HashMap<String, SharedPtr<ObjectAnimation> >& childObjectAnimations = objectAnimation->GetObjectAnimations();
-    for (HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = childObjectAnimations.Begin(); i != childObjectAnimations.End(); ++i)
-    {
-        Node* child = GetChild(i->first_);
-        if (child)
-            child->SetObjectAnimation(i->second_);
-    }
-}
-
-void Node::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
-{
-    Animatable::OnObjectAnimationRemoved(objectAnimation);
-
-    const HashMap<String, SharedPtr<ObjectAnimation> >& childObjectAnimations = objectAnimation->GetObjectAnimations();
-    for (HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = childObjectAnimations.Begin(); i != childObjectAnimations.End(); ++i)
-    {
-        Node* child = GetChild(i->first_);
-        if (child && child->GetObjectAnimation() == i->second_)
-            child->SetObjectAnimation(0);
-    }
-}
-
 void Node::OnAttributeAnimationAdded()
 {
     if (attributeAnimationInstances_.Size() == 1)

+ 0 - 4
Source/Engine/Scene/Node.h

@@ -379,10 +379,6 @@ public:
     unsigned GetNumPersistentComponents() const;
     
 protected:
-    /// Handle object animation added.
-    virtual void OnObjectAnimationAdded(ObjectAnimation* objectAnimation);
-    /// Handle object animation removed.
-    virtual void OnObjectAnimationRemoved(ObjectAnimation* objectAnimation);
     /// Handle attribute animation added.
     virtual void OnAttributeAnimationAdded();
     /// Handle attribute animation removed.

+ 5 - 78
Source/Engine/Scene/ObjectAnimation.cpp

@@ -47,26 +47,14 @@ void ObjectAnimation::RegisterObject(Context* context)
 
 bool ObjectAnimation::Load(Deserializer& source)
 {
-    XMLFile xmlFile(context_);
-    if (xmlFile.Load(source))
-        return false;
-
-    XMLElement rootElem = xmlFile.GetRoot("ObjectAnimation");
-    if (!rootElem)
-        return false;
-
-    return LoadXML(rootElem);
+    // Not implement
+    return false;
 }
 
 bool ObjectAnimation::Save(Serializer& dest) const
 {
-    XMLFile xmlFile(context_);
-
-    XMLElement rootElem = xmlFile.CreateRoot("ObjectAnimation");
-    if (!SaveXML(rootElem))
-        return false;
-
-    return xmlFile.Save(dest);
+    // Not implement
+    return false;
 }
 
 bool ObjectAnimation::LoadXML(const XMLElement& source)
@@ -89,20 +77,6 @@ bool ObjectAnimation::LoadXML(const XMLElement& source)
         animElem = animElem.GetNext("AttributeAnimation");
     }
 
-    animElem = source.GetChild("ObjectAnimation");
-    while (animElem)
-    {
-        String name = animElem.GetAttribute("name");
-
-        SharedPtr<ObjectAnimation> animation(new ObjectAnimation(context_));
-        if (!animation->LoadXML(animElem))
-            return false;
-        
-        AddChildAnimation(name, animation);
-
-        animElem = animElem.GetNext("ObjectAnimation");
-    }
-
     return true;
 }
 
@@ -120,48 +94,9 @@ bool ObjectAnimation::SaveXML(XMLElement& dest) const
             return false;
     }
 
-    for (HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = objectAnimations_.Begin(); i != objectAnimations_.End(); ++i)
-    {
-        XMLElement animElem = dest.CreateChild("ObjectAnimation");
-        animElem.SetAttribute("name", i->first_);
-
-        if (!i->second_->SaveXML(animElem))
-            return false;
-    }
-
     return true;
 }
 
-void ObjectAnimation::AddChildAnimation(const String& name, ObjectAnimation* objectAnimation)
-{
-    if (!objectAnimation)
-        return;
-
-    objectAnimation->SetParentAnimation(this);
-    objectAnimations_[name] = objectAnimation;
-}
-
-void ObjectAnimation::RemoveChildAnimation(const String& name)
-{
-    RemoveChildAnimation(GetObjectAnimation(name));
-}
-
-void ObjectAnimation::RemoveChildAnimation(ObjectAnimation* objectAnimation)
-{
-    if (!objectAnimation)
-        return;
-
-    for (HashMap<String, SharedPtr<ObjectAnimation> >::Iterator i = objectAnimations_.Begin(); i != objectAnimations_.End(); ++i)
-    {
-        if (i->second_ == objectAnimation)
-        {
-            objectAnimation->SetParentAnimation(0);
-            objectAnimations_.Erase(i);
-            return;
-        }
-    }
-}
-
 void ObjectAnimation::AddAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation)
 {
     if (!attributeAnimation)
@@ -192,18 +127,10 @@ void ObjectAnimation::RemoveAttributeAnimation(AttributeAnimation* attributeAnim
     }
 }
 
-ObjectAnimation* ObjectAnimation::GetObjectAnimation(const String& name) const
-{
-    HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = objectAnimations_.Find(name);
-    if (i != objectAnimations_.End())
-        return i->second_;
-    return 0;
-}
-
 AttributeAnimation* ObjectAnimation::GetAttributeAnimation(const String& name) const
 {
     HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Find(name);
-    if (i != objectAnimations_.End())
+    if (i != attributeAnimations_.End())
         return i->second_;
     return 0;
 }

+ 0 - 19
Source/Engine/Scene/ObjectAnimation.h

@@ -51,15 +51,6 @@ public:
     /// Save as XML data. Return true if successful.
     bool SaveXML(XMLElement& dest) const;
 
-    /// Set parent animation.
-    void SetParentAnimation(ObjectAnimation* parentAnimation) { parentAnimation_ = parentAnimation; }
-    /// Add object animation.
-    void AddChildAnimation(const String& name, ObjectAnimation* objectAnimation);
-    /// Remove object animation.
-    void RemoveChildAnimation(const String& name);
-    /// Remove object animation.
-    void RemoveChildAnimation(ObjectAnimation* objectAnimation);
-
     /// Add attribute animation.
     void AddAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation);
     /// Remove attribute animation.
@@ -67,22 +58,12 @@ public:
     /// Remove attribute animation.
     void RemoveAttributeAnimation(AttributeAnimation* attributeAnimation);
 
-    /// Return parent animation.
-    ObjectAnimation* GetParentAnimation() const { return parentAnimation_; }
-    /// Return object animation by name.
-    ObjectAnimation* GetObjectAnimation(const String& name) const;
     /// Return attribute animation by name.
     AttributeAnimation* GetAttributeAnimation(const String& name) const;
-    /// Return all object animations.
-    const HashMap<String, SharedPtr<ObjectAnimation> >& GetObjectAnimations() const { return objectAnimations_; }
     /// Return all attribute animations.
     const HashMap<String, SharedPtr<AttributeAnimation> >& GetAttributeAnimations() const { return attributeAnimations_; }
 
 private:
-    /// Parent animation.
-    WeakPtr<ObjectAnimation> parentAnimation_;
-    /// Name to object animation mapping.
-    HashMap<String, SharedPtr<ObjectAnimation> > objectAnimations_;
     /// Name to attribute animation mapping.
     HashMap<String, SharedPtr<AttributeAnimation> > attributeAnimations_;
 };

+ 0 - 28
Source/Engine/UI/UIElement.cpp

@@ -1582,34 +1582,6 @@ UIElement* UIElement::GetElementEventSender() const
     return element;
 }
 
-
-void UIElement::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
-{
-    Animatable::OnObjectAnimationAdded(objectAnimation);
-
-    const HashMap<String, SharedPtr<ObjectAnimation> >& childObjectAnimations = objectAnimation->GetObjectAnimations();
-    for (HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = childObjectAnimations.Begin(); i != childObjectAnimations.End(); ++i)
-    {
-        UIElement* child = GetChild(i->first_);
-        if (child)
-            child->SetObjectAnimation(i->second_);
-    }
-}
-
-void UIElement::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
-{
-    Animatable::OnObjectAnimationRemoved(objectAnimation);
-
-    const HashMap<String, SharedPtr<ObjectAnimation> >& childObjectAnimations = objectAnimation->GetObjectAnimations();
-    for (HashMap<String, SharedPtr<ObjectAnimation> >::ConstIterator i = childObjectAnimations.Begin(); i != childObjectAnimations.End(); ++i)
-    {
-        UIElement* child = GetChild(i->first_);
-        if (child && child->GetObjectAnimation() == i->second_)
-            child->SetObjectAnimation(0);
-    }
-}
-
-
 void UIElement::OnAttributeAnimationAdded()
 {
     if (attributeAnimationInstances_.Size() == 1)

+ 0 - 4
Source/Engine/UI/UIElement.h

@@ -468,10 +468,6 @@ public:
     UIElement* GetElementEventSender() const;
 
 protected:
-    /// Handle object animation added.
-    virtual void OnObjectAnimationAdded(ObjectAnimation* objectAnimation);
-    /// Handle object animation removed.
-    virtual void OnObjectAnimationRemoved(ObjectAnimation* objectAnimation);
     /// Handle attribute animation added.
     virtual void OnAttributeAnimationAdded();
     /// Handle attribute animation removed.