Browse Source

Save event frame to xml, Remove binary load and save function in animation classes.

Aster Jian 11 years ago
parent
commit
3dfe01a3f0

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

@@ -50,79 +50,15 @@ void Animatable::RegisterObject(Context* context)
     ACCESSOR_ATTRIBUTE(Animatable, VAR_RESOURCEREF, "Object Animation", GetObjectAnimationAttr, SetObjectAnimationAttr, ResourceRef, ResourceRef(ObjectAnimation::GetTypeStatic()), AM_DEFAULT);
     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)
 bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault)
 {
 {
     if (!Serializable::LoadXML(source, setInstanceDefault))
     if (!Serializable::LoadXML(source, setInstanceDefault))
         return false;
         return false;
-    
-    XMLElement elem = source.GetChild("ObjectAnimation");
+
+    SetObjectAnimation(0);
+    attributeAnimationInstances_.Clear();
+
+    XMLElement elem = source.GetChild("objectAnimation");
     if (elem)
     if (elem)
     {
     {
         SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
         SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
@@ -132,7 +68,7 @@ bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault)
         SetObjectAnimation(objectAnimation);
         SetObjectAnimation(objectAnimation);
     }
     }
 
 
-    elem = source.GetChild("AttributeAnimation");
+    elem = source.GetChild("attributeAnimation");
     while (elem)
     while (elem)
     {
     {
         String name = elem.GetAttribute("name");
         String name = elem.GetAttribute("name");
@@ -141,7 +77,7 @@ bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault)
             return false;
             return false;
 
 
         SetAttributeAnimation(name, attributeAnimation);
         SetAttributeAnimation(name, attributeAnimation);
-        elem = elem.GetNext("AttributeAnimation");
+        elem = elem.GetNext("attributeAnimation");
     }
     }
 
 
     return true;
     return true;

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

@@ -45,10 +45,6 @@ public:
     /// Register object factory.
     /// Register object factory.
     static void RegisterObject(Context* context);
     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.
     /// 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);
     virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
     /// Save as XML data. Return true if successful.
     /// Save as XML data. Return true if successful.

+ 29 - 48
Source/Engine/Scene/AttributeAnimation.cpp

@@ -52,58 +52,33 @@ void AttributeAnimation::RegisterObject(Context* context)
     context->RegisterFactory<AttributeAnimation>();
     context->RegisterFactory<AttributeAnimation>();
 }
 }
 
 
-bool AttributeAnimation::Load(Deserializer& source)
-{
-    keyFrames_.Clear();
-
-    cycleMode_ = (CycleMode)source.ReadInt();
-    valueType_ = (VariantType)source.ReadInt();
-    unsigned count = source.ReadUInt();
-
-    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
-{
-    dest.WriteInt((int)cycleMode_);
-    dest.WriteInt((int)valueType_);
-    dest.WriteUInt(keyFrames_.Size());
-
-    for (unsigned i = 0; i < keyFrames_.Size(); ++i)
-    {
-        const AttributeKeyFrame& keyFrame = keyFrames_[i];
-        dest.WriteFloat(keyFrame.time_);
-        dest.WriteVariant(keyFrame.value_);
-    }
-
-    return true;
-}
-
 bool AttributeAnimation::LoadXML(const XMLElement& source)
 bool AttributeAnimation::LoadXML(const XMLElement& source)
 {
 {
-    if (source.GetName() != "AttributeAnimation")
-        return false;
-    
-    keyFrames_.Clear();
+    valueType_ = VAR_NONE;
+    eventFrames_.Clear();
 
 
-    cycleMode_ = (CycleMode)source.GetInt("cycle mode");
-    valueType_ = (VariantType)source.GetInt("value type");
+    cycleMode_ = (CycleMode)source.GetInt("cycleMode");
+    SetValueType((VariantType)source.GetInt("valueType"));
 
 
-    XMLElement keyFrameEleme = source.GetChild("KeyFrame");
+    XMLElement keyFrameEleme = source.GetChild("keyFrame");
     while (keyFrameEleme)
     while (keyFrameEleme)
     {
     {
         float time = keyFrameEleme.GetFloat("time");
         float time = keyFrameEleme.GetFloat("time");
         Variant value(valueType_, keyFrameEleme.GetAttribute("value"));
         Variant value(valueType_, keyFrameEleme.GetAttribute("value"));
         SetKeyFrame(time, value);
         SetKeyFrame(time, value);
 
 
-        keyFrameEleme = keyFrameEleme.GetNext("KeyFrame");
+        keyFrameEleme = keyFrameEleme.GetNext("keyFrame");
+    }
+
+    XMLElement eventFrameElem = source.GetChild("eventFrame");
+    while (eventFrameElem)
+    {
+        float time = eventFrameElem.GetFloat("time");
+        unsigned eventType = eventFrameElem.GetUInt("eventType");
+        VariantMap eventData = eventFrameElem.GetChild("eventData").GetVariantMap();
+        
+        SetEventFrame(time, StringHash(eventType), eventData);
+        eventFrameElem = eventFrameElem.GetNext("eventFrame");
     }
     }
 
 
     return true;
     return true;
@@ -111,20 +86,26 @@ bool AttributeAnimation::LoadXML(const XMLElement& source)
 
 
 bool AttributeAnimation::SaveXML(XMLElement& dest) const
 bool AttributeAnimation::SaveXML(XMLElement& dest) const
 {
 {
-    if (dest.GetName() != "AttributeAnimation")
-        return false;
-
-    dest.SetInt("cycle mode", (int)cycleMode_);
-    dest.SetInt("value type", (int)valueType_);
+    dest.SetInt("cycleMode", (int)cycleMode_);
+    dest.SetInt("valueType", (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");
+        XMLElement keyFrameEleme = dest.CreateChild("keyFrame");
         keyFrameEleme.SetFloat("time", keyFrame.time_);
         keyFrameEleme.SetFloat("time", keyFrame.time_);
         keyFrameEleme.SetAttribute("value", keyFrame.value_.ToString());
         keyFrameEleme.SetAttribute("value", keyFrame.value_.ToString());
     }
     }
 
 
+    for (unsigned i = 0; i < eventFrames_.Size(); ++i)
+    {
+        const AttributeEventFrame& eventFrame = eventFrames_[i];
+        XMLElement eventFrameElem = dest.CreateChild("eventFrame");
+        eventFrameElem.SetFloat("time", eventFrame.time_);
+        eventFrameElem.SetUInt("eventType", eventFrame.eventType_.Value());
+        eventFrameElem.CreateChild("eventData").SetVariantMap(eventFrame.eventData_);
+    }
+
     return true;
     return true;
 }
 }
 
 

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

@@ -75,10 +75,6 @@ public:
     /// Register object factory.
     /// Register object factory.
     static void RegisterObject(Context* context);
     static void RegisterObject(Context* context);
     
     
-    /// Load resource. Return true if successful.
-    virtual bool Load(Deserializer& source);
-    /// Save resource. Return true if successful.
-    virtual bool Save(Serializer& dest) const;
     /// Load from XML data. Return true if successful.
     /// Load from XML data. Return true if successful.
     bool LoadXML(const XMLElement& source);
     bool LoadXML(const XMLElement& source);
     /// Save as XML data. Return true if successful.
     /// Save as XML data. Return true if successful.

+ 4 - 20
Source/Engine/Scene/ObjectAnimation.cpp

@@ -45,25 +45,12 @@ void ObjectAnimation::RegisterObject(Context* context)
     context->RegisterFactory<ObjectAnimation>();
     context->RegisterFactory<ObjectAnimation>();
 }
 }
 
 
-bool ObjectAnimation::Load(Deserializer& source)
-{
-    // Not implement
-    return false;
-}
-
-bool ObjectAnimation::Save(Serializer& dest) const
-{
-    // Not implement
-    return false;
-}
-
 bool ObjectAnimation::LoadXML(const XMLElement& source)
 bool ObjectAnimation::LoadXML(const XMLElement& source)
 {
 {
-    if (source.GetName() != "ObjectAnimation")
-        return false;
+    attributeAnimations_.Clear();
     
     
     XMLElement animElem;
     XMLElement animElem;
-    animElem = source.GetChild("AttributeAnimation");
+    animElem = source.GetChild("attributeAnimation");
     while (animElem)
     while (animElem)
     {
     {
         String name = animElem.GetAttribute("name");
         String name = animElem.GetAttribute("name");
@@ -74,7 +61,7 @@ bool ObjectAnimation::LoadXML(const XMLElement& source)
 
 
         AddAttributeAnimation(name, animation);
         AddAttributeAnimation(name, animation);
 
 
-        animElem = animElem.GetNext("AttributeAnimation");
+        animElem = animElem.GetNext("attributeAnimation");
     }
     }
 
 
     return true;
     return true;
@@ -82,12 +69,9 @@ bool ObjectAnimation::LoadXML(const XMLElement& source)
 
 
 bool ObjectAnimation::SaveXML(XMLElement& dest) const
 bool ObjectAnimation::SaveXML(XMLElement& dest) const
 {
 {
-    if (dest.GetName() != "ObjectAnimation")
-        return false;
-
     for (HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
     for (HashMap<String, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
     {
     {
-        XMLElement animElem = dest.CreateChild("AttributeAnimation");
+        XMLElement animElem = dest.CreateChild("attributeAnimation");
         animElem.SetAttribute("name", i->first_);
         animElem.SetAttribute("name", i->first_);
 
 
         if (!i->second_->SaveXML(animElem))
         if (!i->second_->SaveXML(animElem))

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

@@ -42,10 +42,6 @@ public:
     /// Register object factory.
     /// Register object factory.
     static void RegisterObject(Context* context);
     static void RegisterObject(Context* context);
 
 
-    /// Load resource. Return true if successful.
-    virtual bool Load(Deserializer& source);
-    /// Save resource. Return true if successful.
-    virtual bool Save(Serializer& dest) const;
     /// Load from XML data. Return true if successful.
     /// Load from XML data. Return true if successful.
     bool LoadXML(const XMLElement& source);
     bool LoadXML(const XMLElement& source);
     /// Save as XML data. Return true if successful.
     /// Save as XML data. Return true if successful.