Bläddra i källkod

Merge remote-tracking branch 'remotes/hdunderscore/Editor_ScriptInstance_Persistance'

Lasse Öörni 11 år sedan
förälder
incheckning
81d19ec037

+ 12 - 0
Bin/Data/Scripts/Editor.as

@@ -66,6 +66,8 @@ void FirstFrame()
     ParseArguments();
     // Switch to real frame handler after initialization
     SubscribeToEvent("Update", "HandleUpdate");
+    SubscribeToEvent("ReloadFinished", "HandleReloadFinished");
+    SubscribeToEvent("ReloadFailed", "HandleReloadFailed");
 }
 
 void Stop()
@@ -109,6 +111,16 @@ void HandleUpdate(StringHash eventType, VariantMap& eventData)
     UpdateDirtyUI();
 }
 
+void HandleReloadFinished(StringHash eventType, VariantMap& eventData)
+{
+    attributesFullDirty = true;
+}
+
+void HandleReloadFailed(StringHash eventType, VariantMap& eventData)
+{
+    attributesFullDirty = true;
+}
+
 void LoadConfig()
 {
     if (!fileSystem.FileExists(configFileName))

+ 44 - 9
Bin/Data/Scripts/Editor/AttributeEditor.as

@@ -795,6 +795,16 @@ void UpdateAttributes(Array<Serializable@>@ serializables, ListView@ list, bool&
         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)
 {
     Button@ dragSld = Button();
@@ -834,6 +844,7 @@ void EditAttribute(StringHash eventType, VariantMap& eventData)
     inEditAttribute = true;
 
     Array<Variant> oldValues;
+
     if (!dragEditAttribute)
     {
 	    // Store old values so that PostEditAttribute can create undo actions
@@ -844,14 +855,16 @@ void EditAttribute(StringHash eventType, VariantMap& eventData)
     StoreAttributeEditor(parent, serializables, index, subIndex, coordinate);
     for (uint i = 0; i < serializables.length; ++i)
 	    serializables[i].ApplyAttributes();
-
-    //disable undo 
+    
     if (!dragEditAttribute)
     {
 	    // Do the editor post logic after attribute has been modified.
 	    PostEditAttribute(serializables, index, oldValues);
     }
 
+    // Update the stored script attributes if this is a ScriptInstance    
+    EditScriptAttributes(serializables[0], index);
+
     inEditAttribute = false;
 
     // If not an intermediate edit, reload the editor fields with validated values
@@ -865,15 +878,15 @@ void LineDragBegin(StringHash eventType, VariantMap& eventData)
     UIElement@ label = eventData["Element"].GetPtr();
     int x = eventData["X"].GetInt();
     label.vars["posX"] = x;
-    
-    //store value old value before dragging 
+
+    // Store the old value before dragging
     dragEditAttribute = false;
     LineEdit@ selectedNumEditor = label.parent;
-    //not convenient way to trigger EditAttribute event
-    selectedNumEditor.text = selectedNumEditor.text;
+
     selectedNumEditor.vars["DragBeginValue"] = selectedNumEditor.text;
     selectedNumEditor.cursorPosition = 0;
 
+    // Set mouse mode to user preference
     SetMouseMode(true);
 }
 
@@ -882,6 +895,9 @@ void LineDragMove(StringHash eventTypem, VariantMap& eventData)
     UIElement@ label = eventData["Element"].GetPtr();
     LineEdit@ selectedNumEditor = label.parent;
 
+    // Prevent undo
+    dragEditAttribute = true;
+
     int x = eventData["X"].GetInt();
     int posx = label.vars["posX"].GetInt();
     float val = input.mouseMoveX;
@@ -891,13 +907,28 @@ void LineDragMove(StringHash eventTypem, VariantMap& eventData)
     label.vars["posX"] = x;
     selectedNumEditor.text = fieldVal;
     selectedNumEditor.cursorPosition = 0;
-    //disable storing undo 
-    dragEditAttribute = true;
 }
 
 void LineDragEnd(StringHash eventType, VariantMap& eventData)
 {
+    UIElement@ label = eventData["Element"].GetPtr();
+    LineEdit@ selectedNumEditor = label.parent;
+
+    // Prepare the attributes to store an undo with:
+    // - old value = drag begin value
+    // - new value = final value
+
+    String finalValue = selectedNumEditor.text;
+    // Reset attribute to begin value, and prevent undo
+    dragEditAttribute = true;
+    selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
+
+    // Store final value, allow undo
     dragEditAttribute = false;
+    selectedNumEditor.text = finalValue;
+    selectedNumEditor.cursorPosition = 0;
+
+    // Revert mouse to normal behaviour
     SetMouseMode(false);
 }
 
@@ -905,11 +936,13 @@ void LineDragCancel(StringHash eventType, VariantMap& eventData)
 {
     UIElement@ label = eventData["Element"].GetPtr();
 
-    //prevent undo triggering
+    // Reset value to what it was when drag edit began, preventing undo.
     dragEditAttribute = true;
     LineEdit@ selectedNumEditor = label.parent;
     selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
     selectedNumEditor.cursorPosition = 0;
+
+    // Revert mouse to normal behaviour
     SetMouseMode(false);
 }
 
@@ -1096,6 +1129,8 @@ void PickResourceDone(StringHash eventType, VariantMap& eventData)
             target.attributes[resourcePickIndex] = Variant(attrs);
             target.ApplyAttributes();
         }
+
+        EditScriptAttributes(target, resourcePickIndex);
     }
 
     PostEditAttribute(resourceTargets, resourcePickIndex, oldValues);

+ 4 - 0
Bin/Data/Scripts/Editor/EditorActions.as

@@ -320,6 +320,8 @@ class EditAttributeAction : EditAction
                 SetUIElementModified(target);
             else
                 SetSceneModified();
+                
+            EditScriptAttributes(target, attrIndex);
         }
     }
 
@@ -339,6 +341,8 @@ class EditAttributeAction : EditAction
                 SetUIElementModified(target);
             else
                 SetSceneModified();
+                
+            EditScriptAttributes(target, attrIndex);
         }
     }
 }

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

@@ -24,6 +24,11 @@ uint nodeContainerIndex = M_MAX_UNSIGNED;
 uint componentContainerStartIndex = 0;
 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()
 {
     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;
             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);
             SetAttributeEditorID(container.GetChild("ResetToDefault", true), components);
@@ -320,6 +332,50 @@ void UpdateAttributeInspector(bool fullUpdate = true)
         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];
+        }
+    }
+    component.ApplyAttributes();
+}
+
 /// Update the attribute list of the node container.
 void UpdateNodeAttributes()
 {

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

@@ -80,6 +80,9 @@ bool ResetScene()
     }
     else
         messageBoxCallback = null;
+        
+    // Clear stored script attributes
+    scriptAttributes.Clear();
 
     suppressSceneChanges = true;
 
@@ -180,6 +183,9 @@ bool LoadScene(const String&in fileName)
         MessageBox("Could not open file.\n" + fileName);
         return false;
     }
+    
+    // Reset stored script attributes.
+    scriptAttributes.Clear();
 
     // Add the scene's resource path in case it's necessary
     String newScenePath = GetPath(fileName);
@@ -221,6 +227,15 @@ bool LoadScene(const String&in fileName)
     CreateGrid();
     SetActiveViewport(viewports[0]);
 
+    // Store all ScriptInstance and LuaScriptInstance attributes
+    Array<Component@>@ components = scene.GetComponents("ScriptInstance", true);
+    for (uint i = 0; i < components.length; i++)
+        UpdateScriptAttributes(components[i]);
+
+    components = scene.GetComponents("LuaScriptInstance", true);
+    for (uint i = 0; i < components.length; i++)
+        UpdateScriptAttributes(components[i]);
+
     return loaded;
 }