Ver código fonte

Use a global hash-to-varname mapping table in the editor, which is saved along with the editor config. This allows intelligible var names in prefabs across scenes. Closes #540.

Lasse Öörni 11 anos atrás
pai
commit
f461c8234b

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

@@ -142,6 +142,7 @@ void LoadConfig()
     XMLElement viewElem = configElem.GetChild("view");
     XMLElement resourcesElem = configElem.GetChild("resources");
     XMLElement consoleElem = configElem.GetChild("console");
+    XMLElement varNamesElem = configElem.GetChild("varnames");
 
     if (!cameraElem.isNull)
     {
@@ -247,6 +248,9 @@ void LoadConfig()
         // Console does not exist yet at this point, so store the string in a global variable
         if (consoleElem.HasAttribute("commandinterpreter")) consoleCommandInterpreter = consoleElem.GetAttribute("commandinterpreter");
     }
+    
+    if (!varNamesElem.isNull)
+        globalVarNames = varNamesElem.GetVariantMap();
 }
 
 void SaveConfig()
@@ -262,6 +266,7 @@ void SaveConfig()
     XMLElement viewElem = configElem.CreateChild("view");
     XMLElement resourcesElem = configElem.CreateChild("resources");
     XMLElement consoleElem = configElem.CreateChild("console");
+    XMLElement varNamesElem = configElem.CreateChild("varnames");
 
     cameraElem.SetFloat("nearclip", viewNearClip);
     cameraElem.SetFloat("farclip", viewFarClip);
@@ -331,6 +336,8 @@ void SaveConfig()
 
     consoleElem.SetAttribute("commandinterpreter", console.commandInterpreter);
 
+    varNamesElem.SetVariantMap(globalVarNames);
+
     config.Save(File(configFileName, FILE_WRITE));
 }
 

+ 11 - 3
Bin/Data/Scripts/Editor/AttributeEditor.as

@@ -359,7 +359,15 @@ UIElement@ CreateAttributeEditor(ListView@ list, Array<Serializable@>@ serializa
         Array<StringHash>@ keys = map.keys;
         for (uint i = 0; i < keys.length; ++i)
         {
-            String varName = GetVariableName(keys[i]);
+            String varName = GetVarName(keys[i]);
+            if (varName.empty)
+            {
+                // UIElements will contain internal vars, which do not have known mappings. Skip these
+                if (cast<UIElement>(serializables[0]) !is null)
+                    continue;
+                // Else, for scene nodes, show as hexadecimal hashes if nothing else is available
+                varName = keys[i].ToString();
+            }
             Variant value = map[keys[i]];
 
             // The individual variant in the map is not an attribute of the serializable, the structure is reused for convenience
@@ -565,9 +573,9 @@ void LoadAttributeEditor(UIElement@ parent, const Variant&in value, const Attrib
             if (parent is null)
                 break;
 
-            String varName = GetVariableName(keys[subIndex]);
+            String varName = GetVarName(keys[subIndex]);
             if (varName.empty)
-                continue;
+                varName = keys[subIndex].ToString(); // Use hexadecimal if nothing else is available
 
             Variant firstValue = map[keys[subIndex]];
             bool sameValue = true;

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

@@ -370,7 +370,7 @@ class ResetAttributesAction : EditAction
             for (uint i = 0; i < keys.length; ++i)
             {
                 // If variable name is empty (or unregistered) then it is an internal variable and should be preserved
-                String name = GetVariableName(keys[i]);
+                String name = GetVarName(keys[i]);
                 if (name.empty)
                     internalVars[keys[i]] = element.vars[keys[i]];
             }

+ 9 - 8
Bin/Data/Scripts/Editor/EditorInspectorWindow.as

@@ -29,6 +29,9 @@ VariantMap scriptAttributes;
 const uint SCRIPTINSTANCE_ATTRIBUTE_IGNORE = 5;
 const uint LUASCRIPTINSTANCE_ATTRIBUTE_IGNORE = 4;
 
+// Node or UIElement hash-to-varname reverse mapping
+VariantMap globalVarNames;
+
 void InitXMLResources()
 {
     String[] resources = { "UI/EditorInspector_Attribute.xml", "UI/EditorInspector_Variable.xml", "UI/EditorInspector_Style.xml" };
@@ -635,6 +638,7 @@ void CreateNodeVariable(StringHash eventType, VariantMap& eventData)
 
     // Create scene variable
     editorScene.RegisterVar(newName);
+    globalVarNames[newName] = newName;
 
     Variant newValue = ExtractVariantType(eventData);
 
@@ -684,7 +688,7 @@ void CreateUIElementVariable(StringHash eventType, VariantMap& eventData)
         return;
 
     // Create UIElement variable
-    uiElementVarNames[newName] = newName;
+    globalVarNames[newName] = newName;
 
     Variant newValue = ExtractVariantType(eventData);
 
@@ -754,16 +758,13 @@ Variant ExtractVariantType(VariantMap& eventData)
 }
 
 /// Get back the human-readable variable name from the StringHash.
-String GetVariableName(StringHash hash)
+String GetVarName(StringHash hash)
 {
     // First try to get it from scene
     String name = editorScene.GetVarName(hash);
-    // Then from the UIElement variable names
-    if (name.empty && uiElementVarNames.Contains(hash))
-        name = uiElementVarNames[hash].ToString();
-    // Finally just convert to hexadecimal
-    if (name.empty)
-        name = hash.ToString();
+    // Then from the global variable reverse mappings
+    if (name.empty && globalVarNames.Contains(hash))
+        name = globalVarNames[hash].ToString();
     return name;
 }
 

+ 2 - 5
Bin/Data/Scripts/Editor/EditorUIElement.as

@@ -12,9 +12,6 @@ Array<XMLFile@> uiElementCopyBuffer;
 
 bool suppressUIElementChanges = false;
 
-// Registered UIElement user variable reverse mappings
-VariantMap uiElementVarNames;
-
 const StringHash FILENAME_VAR("FileName");
 const StringHash MODIFIED_VAR("Modified");
 const StringHash CHILD_ELEMENT_FILENAME_VAR("ChildElemFileName");
@@ -419,7 +416,7 @@ void FilterInternalVars(XMLElement source)
     XMLElement resultElem = resultSet.firstResult;
     while (resultElem.notNull)
     {
-        String name = GetVariableName(resultElem.GetUInt("hash"));
+        String name = GetVarName(resultElem.GetUInt("hash"));
         if (name.empty)
         {
             XMLElement parent = resultElem.parent;
@@ -448,7 +445,7 @@ void RegisterUIElementVar(XMLElement source)
     while (resultAttr.notNull)
     {
         String name = resultAttr.GetAttribute();
-        uiElementVarNames[name] = name;
+        globalVarNames[name] = name;
         resultAttr = resultAttr.nextResult;
     }
 }