Browse Source

Fixed a bug in XMLElement::RemoveChild() method. Implemented 'Save child UI-element' functionality.

Wei Tjong Yao 12 years ago
parent
commit
2e57e55e84

+ 13 - 17
Bin/Data/Scripts/Editor/EditorScene.as

@@ -233,12 +233,11 @@ void CreateComponent(const String&in componentType)
         }
     }
 
-    // Although the edit nodes selection are not changed, call to ensure attribute inspector notices new components of the edit nodes   
-    HandleHierarchyListSelectionChange();
-
     SaveEditActionGroup(group);
-
     SetSceneModified();
+
+    // Although the edit nodes selection are not changed, call to ensure attribute inspector notices new components of the edit nodes
+    HandleHierarchyListSelectionChange();
 }
 
 void LoadNode(const String&in fileName)
@@ -285,25 +284,22 @@ void LoadNode(const String&in fileName)
 
 void SaveNode(const String&in fileName)
 {
-    if (fileName.empty || GetFileName(fileName).empty)
+    if (fileName.empty)
         return;
 
     ui.cursor.shape = CS_BUSY;
 
-    if (selectedNodes.length == 1)
-    {
-        File file(fileName, FILE_WRITE);
-        if (!file.open)
-            return;
+    File file(fileName, FILE_WRITE);
+    if (!file.open)
+        return;
 
-        String extension = GetExtension(fileName);
-        if (extension != ".xml")
-            selectedNodes[0].Save(file);
-        else
-            selectedNodes[0].SaveXML(file);
+    String extension = GetExtension(fileName);
+    if (extension != ".xml")
+        editNode.Save(file);
+    else
+        editNode.SaveXML(file);
 
-        instantiateFileName = fileName;
-    }
+    instantiateFileName = fileName;
 }
 
 void UpdateScene(float timeStep)

+ 1 - 1
Bin/Data/Scripts/Editor/EditorUI.as

@@ -292,7 +292,7 @@ bool PickFile()
     }
     else if (action == "Save node as...")
     {
-        if (selectedNodes.length == 1 && selectedNodes[0] !is editorScene)
+        if (editNode !is null && editNode !is editorScene)
         {
             CreateFileSelector("Save node", "Save", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
             uiFileSelector.fileName = GetFileNameAndExtension(instantiateFileName);

+ 48 - 1
Bin/Data/Scripts/Editor/EditorUIElement.as

@@ -220,7 +220,22 @@ void LoadChildUIElement(const String&in fileName)
 
 void SaveChildUIElement(const String&in fileName)
 {
-    // TODO
+    if (fileName.empty)
+        return;
+
+    ui.cursor.shape = CS_BUSY;
+
+    File file(fileName, FILE_WRITE);
+    if (!file.open)
+        return;
+
+    XMLFile@ elementData = XMLFile();
+    XMLElement rootElem = elementData.CreateRoot("element");
+    // Need another nested element tag otherwise the LoadXML() does not work as expected
+    XMLElement childElem = rootElem.CreateChild("element");
+    editUIElement.SaveXML(childElem);
+    FilterInternalVars(childElem);
+    elementData.Save(file);
 }
 
 void SetUIElementDefaultStyle(const String&in fileName)
@@ -245,6 +260,38 @@ void SetUIElementDefaultStyle(const String&in fileName)
     uiElementDefaultStyle.Load(file);
 }
 
+void FilterInternalVars(XMLElement source)
+{
+    // Remove var that does not have its key registered in the 'attribute' tag
+    XMLElement childElem = source.GetChild("attribute");
+    while (childElem.notNull)
+    {
+        if (childElem.GetAttribute("name") == "Variables")
+        {
+            XMLElement variantElem = childElem.GetChild("variant");
+            while (variantElem.notNull)
+            {
+                XMLElement nextVariantElem = childElem.GetNext("variant");
+
+                // If variable name is empty then it is an internal variable
+                if (scene.GetVarName(variantElem.GetUInt("hash")).empty)
+                    childElem.RemoveChild(variantElem);
+
+                variantElem = nextVariantElem;
+            }
+        }
+        childElem = childElem.GetNext("attribute");
+    }
+
+    // Perform the action recursively for each 'element' tag
+    childElem = source.GetChild("element");
+    while (childElem.notNull)
+    {
+        FilterInternalVars(childElem);
+        childElem = childElem.GetNext("element");
+    }
+}
+
 bool UIElementCut()
 {
     if (UIElementCopy())

+ 1 - 1
Engine/Resource/XMLElement.cpp

@@ -73,7 +73,7 @@ XMLElement XMLElement::CreateChild(const char* name)
 
 bool XMLElement::RemoveChild(const XMLElement& element)
 {
-    if (!file_ || !node_ || element.node_)
+    if (!file_ || !node_ || !element.node_)
         return false;
 
     pugi::xml_node node(node_);