瀏覽代碼

In XML mode, allow editing of UnknownComponent attributes as strings. This is as far editing them can go without adding extra data to the whole XML serialization. Closes #149.

Lasse Öörni 12 年之前
父節點
當前提交
257c256d9d
共有 2 個文件被更改,包括 31 次插入13 次删除
  1. 24 10
      Source/Engine/Scene/UnknownComponent.cpp
  2. 7 3
      Source/Engine/Scene/UnknownComponent.h

+ 24 - 10
Source/Engine/Scene/UnknownComponent.cpp

@@ -91,6 +91,8 @@ void UnknownComponent::RegisterObject(Context* context)
 bool UnknownComponent::Load(Deserializer& source, bool setInstanceDefault)
 {
     useXML_ = false;
+    xmlAttributes_.Clear();
+    xmlAttributeInfos_.Clear();
     
     // Assume we are reading from a component data buffer, and the type has already been read
     unsigned dataSize = source.GetSize() - source.GetPosition();
@@ -101,19 +103,33 @@ bool UnknownComponent::Load(Deserializer& source, bool setInstanceDefault)
 bool UnknownComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
 {
     useXML_ = true;
+    xmlAttributes_.Clear();
+    xmlAttributeInfos_.Clear();
+    binaryAttributes_.Clear();
     
     XMLElement attrElem = source.GetChild("attribute");
     while (attrElem)
     {
-        Pair<String, String> attr;
-        attr.first_ = attrElem.GetAttribute("name");
-        attr.second_ = attrElem.GetAttribute("value");
-        if (!attr.first_.Empty())
-            xmlAttributes_.Push(attr);
+        AttributeInfo attr;
+        attr.mode_ = AM_FILE;
+        attr.name_ = attrElem.GetAttribute("name");
+        attr.type_ = VAR_STRING;
+        
+        if (!attr.name_.Empty())
+        {
+            String attrValue = attrElem.GetAttribute("value");
+            attr.defaultValue_ = String::EMPTY;
+            xmlAttributeInfos_.Push(attr);
+            xmlAttributes_.Push(attrValue);
+        }
         
         attrElem = attrElem.GetNext("attribute");
     }
     
+    // Fix up pointers to the attributes after all have been read
+    for (unsigned i = 0; i < xmlAttributeInfos_.Size(); ++i)
+        xmlAttributeInfos_[i].ptr_ = &xmlAttributes_[i];
+    
     return true;
 }
 
@@ -151,13 +167,11 @@ bool UnknownComponent::SaveXML(XMLElement& dest) const
     if (!dest.SetInt("id", id_))
         return false;
     
-    for (unsigned i = 0; i < xmlAttributes_.Size(); ++i)
+    for (unsigned i = 0; i < xmlAttributeInfos_.Size(); ++i)
     {
-        const Pair<String, String>& attr = xmlAttributes_[i];
-        
         XMLElement attrElem = dest.CreateChild("attribute");
-        attrElem.SetAttribute("name", attr.first_);
-        attrElem.SetAttribute("value", attr.second_);
+        attrElem.SetAttribute("name", xmlAttributeInfos_[i].name_);
+        attrElem.SetAttribute("value", xmlAttributes_[i]);
     }
     
     return true;

+ 7 - 3
Source/Engine/Scene/UnknownComponent.h

@@ -41,6 +41,8 @@ public:
     virtual ShortStringHash GetType() const { return typeHash_; }
     /// Return type name of the stored component.
     virtual const String& GetTypeName() const { return typeName_; }
+    /// Return attribute descriptions, or null if none defined.
+    virtual const Vector<AttributeInfo>* GetAttributes() const { return &xmlAttributeInfos_; }
     /// Load from binary data. Return true if successful.
     virtual bool Load(Deserializer& source, bool setInstanceDefault = false);
     /// Load from XML data. Return true if successful.
@@ -56,7 +58,7 @@ public:
     void SetType(ShortStringHash typeHash);
     
     /// Return the XML format attributes. Empty when loaded with binary serialization.
-    const Vector<Pair<String, String> >& GetXMLAttributes() const { return xmlAttributes_; }
+    const Vector<String>& GetXMLAttributes() const { return xmlAttributes_; }
     /// Return the binary attributes. Empty when loaded with XML serialization.
     const PODVector<unsigned char>& GetBinaryAttributes() const { return binaryAttributes_; }
     /// Return whether was loaded using XML data.
@@ -72,8 +74,10 @@ private:
     ShortStringHash typeHash_;
     /// Type name of the stored component.
     String typeName_;
-    /// XML format attributes.
-    Vector<Pair<String, String> > xmlAttributes_;
+    /// XML format attribute infos.
+    Vector<AttributeInfo> xmlAttributeInfos_;
+    /// XML format attribute data (as strings)
+    Vector<String> xmlAttributes_;
     /// Binary attributes.
     PODVector<unsigned char> binaryAttributes_;
     /// Flag of whether was loaded using XML data.