Browse Source

Added persistence of attributes for ScriptInstances in the editor, so that when an error occurs, the values are not reset to default.

hdunderscore 11 years ago
parent
commit
e20bf79dcd

+ 15 - 0
Bin/Data/Scripts/Editor/AttributeEditor.as

@@ -795,6 +795,16 @@ void UpdateAttributes(Array<Serializable@>@ serializables, ListView@ list, bool&
         list.viewPosition = oldViewPos;
         list.viewPosition = oldViewPos;
 }
 }
 
 
+void EditScriptAttributes(Component@ component, uint index)
+{
+    if (component !is null && component.typeName.Contains("ScriptInstance"))
+    {
+        String hash = GetComponentAttributeHash(component, index);
+        if (!hash.empty)
+            scriptAttributes[hash] = component.attributes[index];
+    }
+}
+
 void CreateDragSlider(LineEdit@ parent)
 void CreateDragSlider(LineEdit@ parent)
 {
 {
     Button@ dragSld = Button();
     Button@ dragSld = Button();
@@ -852,6 +862,9 @@ void EditAttribute(StringHash eventType, VariantMap& eventData)
 	    PostEditAttribute(serializables, index, oldValues);
 	    PostEditAttribute(serializables, index, oldValues);
     }
     }
 
 
+    // Update the stored script attributes if this is a ScriptInstance
+    EditScriptAttributes(serializables[0], index);
+
     inEditAttribute = false;
     inEditAttribute = false;
 
 
     // If not an intermediate edit, reload the editor fields with validated values
     // If not an intermediate edit, reload the editor fields with validated values
@@ -1096,6 +1109,8 @@ void PickResourceDone(StringHash eventType, VariantMap& eventData)
             target.attributes[resourcePickIndex] = Variant(attrs);
             target.attributes[resourcePickIndex] = Variant(attrs);
             target.ApplyAttributes();
             target.ApplyAttributes();
         }
         }
+
+        EditScriptAttributes(target, resourcePickIndex);
     }
     }
 
 
     PostEditAttribute(resourceTargets, resourcePickIndex, oldValues);
     PostEditAttribute(resourceTargets, resourcePickIndex, oldValues);

+ 56 - 1
Bin/Data/Scripts/Editor/EditorInspectorWindow.as

@@ -24,6 +24,11 @@ uint nodeContainerIndex = M_MAX_UNSIGNED;
 uint componentContainerStartIndex = 0;
 uint componentContainerStartIndex = 0;
 uint elementContainerIndex = M_MAX_UNSIGNED;
 uint elementContainerIndex = M_MAX_UNSIGNED;
 
 
+// Script Attribute session storage
+VariantMap scriptAttributes;
+const uint SCRIPTINSTANCE_ATTRIBUTE_IGNORE = 5;
+const uint LUASCRIPTINSTANCE_ATTRIBUTE_IGNORE = 4;
+
 void InitXMLResources()
 void InitXMLResources()
 {
 {
     String[] resources = { "UI/EditorInspector_Attribute.xml", "UI/EditorInspector_Variable.xml", "UI/EditorInspector_Style.xml" };
     String[] resources = { "UI/EditorInspector_Attribute.xml", "UI/EditorInspector_Variable.xml", "UI/EditorInspector_Style.xml" };
@@ -253,7 +258,14 @@ void UpdateAttributeInspector(bool fullUpdate = true)
 
 
             Array<Serializable@> components;
             Array<Serializable@> components;
             for (uint i = 0; i < numEditableComponents; ++i)
             for (uint i = 0; i < numEditableComponents; ++i)
-                components.Push(editComponents[j * numEditableComponents + i]);
+            {
+                Component@ component = editComponents[j * numEditableComponents + i];
+
+                if (component.typeName.Contains("ScriptInstance"))
+                    UpdateScriptAttributes(component);
+                    
+                components.Push(component);
+            }
 
 
             UpdateAttributes(components, container.GetChild("AttributeList"), fullUpdate);
             UpdateAttributes(components, container.GetChild("AttributeList"), fullUpdate);
             SetAttributeEditorID(container.GetChild("ResetToDefault", true), components);
             SetAttributeEditorID(container.GetChild("ResetToDefault", true), components);
@@ -320,6 +332,49 @@ void UpdateAttributeInspector(bool fullUpdate = true)
         HandleWindowLayoutUpdated();
         HandleWindowLayoutUpdated();
 }
 }
 
 
+String GetComponentAttributeHash(Component@ component, uint index)
+{
+    // We won't consider the main attributes, as they won't reset when an error occurs.
+    if (component.typeName == "ScriptInstance")
+    {
+        if (index <= SCRIPTINSTANCE_ATTRIBUTE_IGNORE)
+            return "";
+    }
+    else
+    {
+        if (index <= LUASCRIPTINSTANCE_ATTRIBUTE_IGNORE)
+            return "";
+    }
+    AttributeInfo attributeInfo = component.attributeInfos[index];
+    Variant attribute = component.attributes[index];
+    return String(component.id) + "-" + attributeInfo.name + "-" + attribute.typeName;
+}
+
+void UpdateScriptAttributes(Component@ component)
+{
+    for (uint i = Min(SCRIPTINSTANCE_ATTRIBUTE_IGNORE, LUASCRIPTINSTANCE_ATTRIBUTE_IGNORE) + 1; i < component.numAttributes; i++)
+    {
+        Variant attribute = component.attributes[i];
+        // Component/node ID's are always unique within a scene, based on a simple increment.
+        // This makes for a simple method of mapping a components attributes unique and consistent.
+        // We will also use the type name in the hash to be able to recall and differentiate type changes.
+        String hash = GetComponentAttributeHash(component, i);
+        if (hash.empty)
+            continue;
+
+        if (!scriptAttributes.Contains(hash))
+        {
+            // set the initial value to the default value.
+            scriptAttributes[hash] = attribute;
+        }
+        else
+        {
+            // recall the previously stored value
+            component.attributes[i] = scriptAttributes[hash];
+        }
+    }
+}
+
 /// Update the attribute list of the node container.
 /// Update the attribute list of the node container.
 void UpdateNodeAttributes()
 void UpdateNodeAttributes()
 {
 {