Преглед на файлове

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)
 bool UnknownComponent::Load(Deserializer& source, bool setInstanceDefault)
 {
 {
     useXML_ = false;
     useXML_ = false;
+    xmlAttributes_.Clear();
+    xmlAttributeInfos_.Clear();
     
     
     // Assume we are reading from a component data buffer, and the type has already been read
     // Assume we are reading from a component data buffer, and the type has already been read
     unsigned dataSize = source.GetSize() - source.GetPosition();
     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)
 bool UnknownComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
 {
 {
     useXML_ = true;
     useXML_ = true;
+    xmlAttributes_.Clear();
+    xmlAttributeInfos_.Clear();
+    binaryAttributes_.Clear();
     
     
     XMLElement attrElem = source.GetChild("attribute");
     XMLElement attrElem = source.GetChild("attribute");
     while (attrElem)
     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");
         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;
     return true;
 }
 }
 
 
@@ -151,13 +167,11 @@ bool UnknownComponent::SaveXML(XMLElement& dest) const
     if (!dest.SetInt("id", id_))
     if (!dest.SetInt("id", id_))
         return false;
         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");
         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;
     return true;

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

@@ -41,6 +41,8 @@ public:
     virtual ShortStringHash GetType() const { return typeHash_; }
     virtual ShortStringHash GetType() const { return typeHash_; }
     /// Return type name of the stored component.
     /// Return type name of the stored component.
     virtual const String& GetTypeName() const { return typeName_; }
     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.
     /// Load from binary data. Return true if successful.
     virtual bool Load(Deserializer& source, bool setInstanceDefault = false);
     virtual bool Load(Deserializer& source, bool setInstanceDefault = false);
     /// Load from XML data. Return true if successful.
     /// Load from XML data. Return true if successful.
@@ -56,7 +58,7 @@ public:
     void SetType(ShortStringHash typeHash);
     void SetType(ShortStringHash typeHash);
     
     
     /// Return the XML format attributes. Empty when loaded with binary serialization.
     /// 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.
     /// Return the binary attributes. Empty when loaded with XML serialization.
     const PODVector<unsigned char>& GetBinaryAttributes() const { return binaryAttributes_; }
     const PODVector<unsigned char>& GetBinaryAttributes() const { return binaryAttributes_; }
     /// Return whether was loaded using XML data.
     /// Return whether was loaded using XML data.
@@ -72,8 +74,10 @@ private:
     ShortStringHash typeHash_;
     ShortStringHash typeHash_;
     /// Type name of the stored component.
     /// Type name of the stored component.
     String typeName_;
     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.
     /// Binary attributes.
     PODVector<unsigned char> binaryAttributes_;
     PODVector<unsigned char> binaryAttributes_;
     /// Flag of whether was loaded using XML data.
     /// Flag of whether was loaded using XML data.