소스 검색

Remove GetJsonType() function from editor resource browser code since JSON files don't have a similar root element name, and it was causing the editor not to compile. Fix save of ResourceRef / ResourceRefList in JSON. Fix missing child nodes in JSON scenes. Fix editor to use the correct function to save / load JSON nodes. Fix number of attempts in finding the attribute matching JSON data.

Lasse Öörni 10 년 전
부모
커밋
5165aa91fb

+ 3 - 3
Source/Urho3D/Resource/JSONValue.cpp

@@ -367,7 +367,7 @@ void JSONValue::SetVariant(const Variant& variant, Context* context)
 Variant JSONValue::GetVariant() const
 {
     VariantType type = Variant::GetTypeFromName((*this)["type"].GetString());
-    return (*this)["value"].GetVariantValue(type);    
+    return (*this)["value"].GetVariantValue(type);
 }
 
 void JSONValue::SetVariantValue(const Variant& variant, Context* context)
@@ -411,7 +411,7 @@ void JSONValue::SetVariantValue(const Variant& variant, Context* context)
         {
             if (!context)
             {
-                URHO3D_LOGERROR("Context must not null for ResourceRef");
+                URHO3D_LOGERROR("Context must not be null for ResourceRef");
                 return;
             }
 
@@ -424,7 +424,7 @@ void JSONValue::SetVariantValue(const Variant& variant, Context* context)
         {
             if (!context)
             {
-                URHO3D_LOGERROR("Context must not null for ResourceRefList");
+                URHO3D_LOGERROR("Context must not be null for ResourceRefList");
                 return;
             }
 

+ 1 - 0
Source/Urho3D/Scene/Node.cpp

@@ -265,6 +265,7 @@ bool Node::SaveJSON(JSONValue& dest) const
             return false;
         childrenArray.Push(childVal);
     }
+    dest.Set("children", childrenArray);
 
     return true;
 }

+ 2 - 2
Source/Urho3D/Scene/Serializable.cpp

@@ -443,7 +443,7 @@ bool Serializable::LoadJSON(const JSONValue& source, bool setInstanceDefault)
         String name = it->first_;
         const JSONValue& value = it->second_;
         unsigned i = startIndex;
-        unsigned attempts = attributesObject.Size();
+        unsigned attempts = attributes->Size();
 
         while (attempts)
         {
@@ -582,7 +582,7 @@ bool Serializable::SaveJSON(JSONValue& dest) const
             attrVal = attr.enumNames_[enumValue];
         }
         else
-            attrVal.SetVariantValue(value);
+            attrVal.SetVariantValue(value, context_);
 
         attributesValue.Set(attr.name_, attrVal);
     }

+ 1 - 75
bin/Data/Scripts/Editor/EditorResourceBrowser.as

@@ -1046,7 +1046,7 @@ int GetResourceType(String path)
 
 int GetResourceType(String path, StringHash &out fileType, bool useCache = false)
 {
-    if (GetExtensionType(path, fileType) || GetBinaryType(path, fileType, useCache) || GetXmlType(path, fileType, useCache) || GetJsonType(path, fileType, useCache))
+    if (GetExtensionType(path, fileType) || GetBinaryType(path, fileType, useCache) || GetXmlType(path, fileType, useCache))
         return GetResourceType(fileType);
 
     return RESOURCE_TYPE_UNKNOWN;
@@ -1318,81 +1318,7 @@ bool GetXmlType(String path, StringHash &out fileType, bool useCache = false)
         XMLFile@ xml = XMLFile();
         if (xml.Load(file))
             name = xml.root.name;
-        else 
-            return false;
-    }
-
-    bool found = false;
-    if (!name.empty)
-    {
-        found = true;
-        StringHash type = StringHash(name);
-        if (type == XML_TYPE_SCENE)
-            fileType = XML_TYPE_SCENE;
-        else if (type == XML_TYPE_NODE)
-            fileType = XML_TYPE_NODE;
-        else if(type == XML_TYPE_MATERIAL)
-            fileType = XML_TYPE_MATERIAL;
-        else if(type == XML_TYPE_TECHNIQUE)
-            fileType = XML_TYPE_TECHNIQUE;
-        else if(type == XML_TYPE_PARTICLEEFFECT)
-            fileType = XML_TYPE_PARTICLEEFFECT;
-        else if(type == XML_TYPE_PARTICLEEMITTER)
-            fileType = XML_TYPE_PARTICLEEMITTER;
-        else if(type == XML_TYPE_TEXTURE)
-            fileType = XML_TYPE_TEXTURE;
-        else if(type == XML_TYPE_ELEMENT)
-            fileType = XML_TYPE_ELEMENT;
-        else if(type == XML_TYPE_ELEMENTS)
-            fileType = XML_TYPE_ELEMENTS;
-        else if (type == XML_TYPE_ANIMATION_SETTINGS)
-            fileType = XML_TYPE_ANIMATION_SETTINGS;
-        else if (type == XML_TYPE_RENDERPATH)
-            fileType = XML_TYPE_RENDERPATH;
-        else if (type == XML_TYPE_TEXTURE_ATLAS)
-            fileType = XML_TYPE_TEXTURE_ATLAS;
-        else if (type == XML_TYPE_2D_PARTICLE_EFFECT)
-            fileType = XML_TYPE_2D_PARTICLE_EFFECT;
-        else if (type == XML_TYPE_TEXTURE_3D)
-            fileType = XML_TYPE_TEXTURE_3D;
-        else if (type == XML_TYPE_CUBEMAP)
-            fileType = XML_TYPE_CUBEMAP;
-        else if (type == XML_TYPE_SPRITER_DATA)
-            fileType = XML_TYPE_SPRITER_DATA;
         else
-            found = false;
-    }
-    return found;
-}
-
-bool GetJsonType(String path, StringHash &out fileType, bool useCache = false)
-{
-    String extension = GetExtension(path);
-    if (extension == ".txt" || extension == ".xml" || extension == ".icns" || extension == ".atlas")
-        return false;
-
-    String name;
-    if (useCache)
-    {
-        JSONFile@ json = cache.GetResource("JSONFile", path);
-        if (json is null)
-            return false;
-
-        name = json.root.name;
-    }
-    else
-    {
-        File@ file = File();
-        if (!file.Open(path))
-            return false;
-
-        if (file.size == 0)
-            return false;
-
-        XMLFile@ xml = XMLFile();
-        if (xml.Load(file))
-            name = xml.root.name;
-        else 
             return false;
     }
 

+ 25 - 9
bin/Data/Scripts/Editor/EditorScene.as

@@ -216,10 +216,12 @@ bool LoadScene(const String&in fileName)
 
     String extension = GetExtension(fileName);
     bool loaded;
-    if (extension != ".xml")
-        loaded = editorScene.Load(file);
-    else
+    if (extension == ".xml")
         loaded = editorScene.LoadXML(file);
+    else if (extension == ".json")
+        loaded = editorScene.LoadJSON(file);
+    else
+        loaded = editorScene.Load(file);
 
     // Release resources which are not used by the new scene
     cache.ReleaseAllResources(false);
@@ -265,7 +267,13 @@ bool SaveScene(const String&in fileName)
     MakeBackup(fileName);
     File file(fileName, FILE_WRITE);
     String extension = GetExtension(fileName);
-    bool success = (extension != ".xml" ? editorScene.Save(file) : editorScene.SaveXML(file));
+    bool success;
+    if (extension == ".xml")
+        editorScene.SaveXML(file);
+    else if (extension == ".json")
+        editorScene.SaveJSON(file);
+    else
+        editorScene.Save(file);
     RemoveBackup(success, fileName);
 
     editorScene.updateEnabled = false;
@@ -400,10 +408,12 @@ Node@ InstantiateNodeFromFile(File@ file, const Vector3& position, const Quatern
     suppressSceneChanges = true;
 
     String extension = GetExtension(file.name);
-    if (extension != ".xml")
-        newNode = editorScene.Instantiate(file, position, rotation, mode);
-    else
+    if (extension == ".xml")
         newNode = editorScene.InstantiateXML(file, position, rotation, mode);
+    else if (extension == ".json")
+        newNode = editorScene.InstantiateJSON(file, position, rotation, mode);
+    else
+        newNode = editorScene.Instantiate(file, position, rotation, mode);
 
     suppressSceneChanges = false;
 
@@ -456,7 +466,13 @@ bool SaveNode(const String&in fileName)
     }
 
     String extension = GetExtension(fileName);
-    bool success = (extension != ".xml" ? editNode.Save(file) : editNode.SaveXML(file));
+    bool success;
+    if (extension == ".xml")
+        success = editNode.SaveXML(file);
+    else if (extension == ".json")
+        success = editNode.SaveJSON(file);
+    else
+        success = editNode.Save(file);
     RemoveBackup(success, fileName);
 
     if (success)
@@ -633,7 +649,7 @@ bool SceneCopy()
             sceneCopyBuffer.Push(xml);
         }
     }
-    // Copy nodes.
+    // Copy nodes
     else
     {
         for (uint i = 0; i < selectedNodes.length; ++i)

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

@@ -32,7 +32,7 @@ const uint MAX_QUICK_MENU_ITEMS = 10;
 
 const uint maxRecentSceneCount = 5;
 
-Array<String> uiSceneFilters = {"*.xml", "*.bin", "*.*"};
+Array<String> uiSceneFilters = {"*.xml", "*.json", "*.bin", "*.*"};
 Array<String> uiElementFilters = {"*.xml"};
 Array<String> uiAllFilters = {"*.*"};
 Array<String> uiScriptFilters = {"*.as", "*.*"};