Browse Source

Merge pull request #210 from AtomicGameEngine/JME-ATOMIC-207

Inspector Field Improvements
JoshEngebretson 10 years ago
parent
commit
23c210eacc

+ 2 - 2
Script/AtomicEditor/ui/frames/inspector/DataBinding.ts

@@ -176,7 +176,7 @@ class DataBinding {
 
 
                     EditorUI.getModelOps().showResourceSelection("Select " + attrInfo.resourceTypeName + " Resource", importerName, function(asset: ToolCore.Asset) {
                     EditorUI.getModelOps().showResourceSelection("Select " + attrInfo.resourceTypeName + " Resource", importerName, function(asset: ToolCore.Asset) {
 
 
-                        var resource = asset.resource;
+                        var resource = asset.getResource(attrInfo.resourceTypeName);
 
 
                         object.setAttribute(attrInfo.name, resource);
                         object.setAttribute(attrInfo.name, resource);
 
 
@@ -217,7 +217,7 @@ class DataBinding {
 
 
                         if (importer) {
                         if (importer) {
 
 
-                            var resource = asset.resource;
+                            var resource = asset.getResource(attrInfo.resourceTypeName);
                             object.setAttribute(attrInfo.name, resource);
                             object.setAttribute(attrInfo.name, resource);
                             if (resource) {
                             if (resource) {
                                 // use the asset name instead of the cache name
                                 // use the asset name instead of the cache name

+ 63 - 21
Source/AtomicJS/Javascript/JSComponentFile.cpp

@@ -101,10 +101,10 @@ bool JSComponentFile::PushModule()
     String pathName, fileName, ext;
     String pathName, fileName, ext;
     SplitPath(path, pathName, fileName, ext);
     SplitPath(path, pathName, fileName, ext);
 
 
-	if (path.Contains('/') || path.Contains('\\'))
-		pathName += "/" + fileName;
-	else
-		pathName = fileName;
+    if (path.Contains('/') || path.Contains('\\'))
+        pathName += "/" + fileName;
+    else
+        pathName = fileName;
 
 
     duk_get_global_string(ctx, "require");
     duk_get_global_string(ctx, "require");
     duk_push_string(ctx, pathName.CString());
     duk_push_string(ctx, pathName.CString());
@@ -224,49 +224,56 @@ bool JSComponentFile::BeginLoad(Deserializer& source)
 
 
     String eval;
     String eval;
     bool valid = false;
     bool valid = false;
+    int leftBracketCount = 0;
+    int rightBracketCount = 0;
     for (unsigned i = 0; i < lines.Size(); i++)
     for (unsigned i = 0; i < lines.Size(); i++)
     {
     {
         String line = lines[i];
         String line = lines[i];
 
 
+        bool added = false;
+
         if (!eval.Length())
         if (!eval.Length())
         {
         {
             line = line.Trimmed();
             line = line.Trimmed();
 
 
             if (line.StartsWith("inspectorFields"))
             if (line.StartsWith("inspectorFields"))
             {
             {
+                added = true;
                 eval = line + "\n";
                 eval = line + "\n";
-                if (line.Contains("}"))
-                {
-                    valid = true;
-                    break;
-                }
             }
             }
             else if (line.StartsWith("this.inspectorFields"))
             else if (line.StartsWith("this.inspectorFields"))
             {
             {
+                added = true;
                 eval = line.Substring(5) + "\n";
                 eval = line.Substring(5) + "\n";
-                if (line.Contains("}"))
-                {
-                    valid = true;
-                    break;
-                }
             }
             }
             else if (line.StartsWith("var inspectorFields"))
             else if (line.StartsWith("var inspectorFields"))
             {
             {
+                added = true;
                 eval = line.Substring(4) + "\n";
                 eval = line.Substring(4) + "\n";
-                if (line.Contains("}"))
-                {
-                    valid = true;
-                    break;
-                }
             }
             }
 
 
         }
         }
         else
         else
         {
         {
+            added = true;
             eval += line + "\n";
             eval += line + "\n";
         }
         }
 
 
-        if (line.Contains("}") && eval.Length())
+        if (added) {
+
+            for (unsigned j = 0; j < line.Length(); j++)
+            {
+                if (line.At(j) == '{')
+                    leftBracketCount++;
+
+                else if (line.At(j) == '}')
+                    rightBracketCount++;
+
+            }
+
+        }
+
+        if (eval.Length() && leftBracketCount && leftBracketCount == rightBracketCount)
         {
         {
             valid = true;
             valid = true;
             break;
             break;
@@ -359,7 +366,42 @@ bool JSComponentFile::BeginLoad(Deserializer& source)
 
 
                                 duk_pop(ctx);
                                 duk_pop(ctx);
 
 
-                                if (length > 1)
+                                // detect int enum
+                                if (length > 1 && (variantType == VAR_INT || variantType == VAR_FLOAT
+                                                   || variantType == VAR_DOUBLE))
+                                {
+                                    duk_get_prop_index(ctx, -1, 1);
+
+                                    if (duk_is_number(ctx, -1))
+                                    {
+                                        js_to_variant(ctx, -1, defaultValue);
+                                    }
+                                    else if (duk_is_array(ctx, -1))
+                                    {
+                                        int enumLength = duk_get_length(ctx, -1);
+
+                                        for (unsigned i = 0; i < enumLength; i++)
+                                        {
+                                            duk_get_prop_index(ctx, -1, i);
+                                            String enumName = duk_require_string(ctx, -1);
+                                            enums_[name].Push(EnumInfo(enumName, Variant(float(i))));
+                                            duk_pop(ctx);
+                                        }
+
+                                    }
+
+                                    duk_pop(ctx);
+
+                                    if (length > 2)
+                                    {
+                                        duk_get_prop_index(ctx, -1, 2);
+                                        // default value
+                                        js_to_variant(ctx, -1, defaultValue);
+                                        duk_pop(ctx);
+                                    }
+
+                                }
+                                else if (length > 1)
                                 {
                                 {
                                     duk_get_prop_index(ctx, -1, 1);
                                     duk_get_prop_index(ctx, -1, 1);
                                     // default value
                                     // default value

+ 18 - 2
Source/AtomicJS/Javascript/JSComponentFile.h

@@ -24,6 +24,7 @@
 
 
 #include <Atomic/Resource/Resource.h>
 #include <Atomic/Resource/Resource.h>
 #include <Atomic/Container/ArrayPtr.h>
 #include <Atomic/Container/ArrayPtr.h>
+#include <Atomic/Container/List.h>
 
 
 namespace Atomic
 namespace Atomic
 {
 {
@@ -36,6 +37,19 @@ class ATOMIC_API JSComponentFile : public Resource
     OBJECT(JSComponentFile);
     OBJECT(JSComponentFile);
 
 
 public:
 public:
+
+    struct EnumInfo
+    {
+        EnumInfo(const String& name = String::EMPTY, const Variant& v = Variant::EMPTY)
+        {
+            name_ = name;
+            value_ = v;
+        }
+
+        String name_;
+        Variant value_;
+    };
+
     /// Construct.
     /// Construct.
     JSComponentFile(Context* context);
     JSComponentFile(Context* context);
     /// Destruct.
     /// Destruct.
@@ -43,8 +57,10 @@ public:
     /// Register object factory.
     /// Register object factory.
     static void RegisterObject(Context* context);
     static void RegisterObject(Context* context);
 
 
-    const HashMap<String, VariantType>& GetFields() const { return fields_; }
+    const HashMap<String, VariantType>& GetFields() const { return fields_; }    
     const VariantMap& GetDefaultFieldValues() const { return defaultFieldValues_; }
     const VariantMap& GetDefaultFieldValues() const { return defaultFieldValues_; }
+    const HashMap<String, Vector<EnumInfo>>& GetEnums() const { return enums_; }
+    void GetDefaultFieldValue(const String& name, Variant& v);
 
 
     /// Load resource from stream. May be called from a worker thread. Return true if successful.
     /// Load resource from stream. May be called from a worker thread. Return true if successful.
     virtual bool BeginLoad(Deserializer& source);
     virtual bool BeginLoad(Deserializer& source);
@@ -55,7 +71,6 @@ public:
 
 
     JSComponent* CreateJSComponent();
     JSComponent* CreateJSComponent();
     bool PushModule();
     bool PushModule();
-    void GetDefaultFieldValue(const String& name, Variant& v);
 
 
 private:
 private:
 
 
@@ -63,6 +78,7 @@ private:
 
 
     bool scriptClass_;
     bool scriptClass_;
     HashMap<String, VariantType> fields_;
     HashMap<String, VariantType> fields_;
+    HashMap<String, Vector<EnumInfo>> enums_;
     VariantMap defaultFieldValues_;
     VariantMap defaultFieldValues_;
 
 
 };
 };

+ 29 - 0
Source/AtomicJS/Javascript/JSSceneSerializable.cpp

@@ -77,11 +77,24 @@ static int Serializable_SetAttribute(duk_context* ctx)
             if (file)
             if (file)
             {
             {
                 const HashMap<String, VariantType>& fields = file->GetFields();
                 const HashMap<String, VariantType>& fields = file->GetFields();
+                const HashMap<String, Vector<JSComponentFile::EnumInfo>>& enums = file->GetEnums();
 
 
                 if (fields.Contains(name))
                 if (fields.Contains(name))
                 {
                 {
                     HashMap<String, VariantType>::ConstIterator itr = fields.Find(name);
                     HashMap<String, VariantType>::ConstIterator itr = fields.Find(name);
                     variantType = itr->second_;
                     variantType = itr->second_;
+
+                    if (enums.Contains(name))
+                    {
+                        int idx = (int) v.GetFloat();
+
+                        if (idx > 0 && idx < enums[name]->Size())
+                        {
+                            VariantMap& values = jsc->GetFieldValues();
+                            values[name] = enums[name]->At(idx).value_;
+                            return 0;
+                        }
+                    }
                 }
                 }
             }
             }
         }
         }
@@ -314,6 +327,7 @@ static int Serializable_GetAttributes(duk_context* ctx)
 
 
             const VariantMap& defaultFieldValues = file->GetDefaultFieldValues();
             const VariantMap& defaultFieldValues = file->GetDefaultFieldValues();
             const HashMap<String, VariantType>& fields =  file->GetFields();
             const HashMap<String, VariantType>& fields =  file->GetFields();
+            const HashMap<String, Vector<JSComponentFile::EnumInfo>>& enums = file->GetEnums();
 
 
             if (fields.Size())
             if (fields.Size())
             {
             {
@@ -355,6 +369,21 @@ static int Serializable_GetAttributes(duk_context* ctx)
 
 
                     duk_push_array(ctx);
                     duk_push_array(ctx);
 
 
+                    if (enums.Contains(itr->first_))
+                    {
+                        unsigned enumCount = 0;
+                        const Vector<JSComponentFile::EnumInfo>* infos = enums[itr->first_];
+                        Vector<JSComponentFile::EnumInfo>::ConstIterator eitr = infos->Begin();
+
+                        while (eitr != infos->End())
+                        {
+                            duk_push_string(ctx, eitr->name_.CString());
+                            duk_put_prop_index(ctx, -2, enumCount++);
+                            eitr++;
+                        }
+
+                    }
+
                     duk_put_prop_string(ctx, -2, "enumNames");
                     duk_put_prop_string(ctx, -2, "enumNames");
 
 
                     // store attr object
                     // store attr object

+ 2 - 2
Source/ToolCore/Assets/Asset.cpp

@@ -343,10 +343,10 @@ bool Asset::SetPath(const String& path)
 
 
 }
 }
 
 
-Resource* Asset::GetResource()
+Resource* Asset::GetResource(const String &typeName)
 {
 {
     if (importer_)
     if (importer_)
-        return importer_->GetResource();
+        return importer_->GetResource(typeName);
 
 
     return 0;
     return 0;
 }
 }

+ 1 - 1
Source/ToolCore/Assets/Asset.h

@@ -38,7 +38,7 @@ public:
     String GetRelativePath();
     String GetRelativePath();
     String GetCachePath() const;
     String GetCachePath() const;
 
 
-    Resource* GetResource();
+    Resource* GetResource(const String& typeName = String::EMPTY);
 
 
     const StringHash GetImporterType() { return importer_.Null() ? String::EMPTY : importer_->GetType(); }
     const StringHash GetImporterType() { return importer_.Null() ? String::EMPTY : importer_->GetType(); }
     const String& GetImporterTypeName() { return importer_.Null() ? String::EMPTY : importer_->GetTypeName(); }
     const String& GetImporterTypeName() { return importer_.Null() ? String::EMPTY : importer_->GetTypeName(); }

+ 2 - 2
Source/ToolCore/Assets/AssetDatabase.cpp

@@ -488,9 +488,9 @@ String AssetDatabase::GetResourceImporterName(const String& resourceTypeName)
     {
     {
         resourceTypeToImporterType_["Sound"] = "AudioImporter";
         resourceTypeToImporterType_["Sound"] = "AudioImporter";
         resourceTypeToImporterType_["Model"] = "ModelImporter";
         resourceTypeToImporterType_["Model"] = "ModelImporter";
+        resourceTypeToImporterType_["Texture2D"] = "TextureImporter";
+        resourceTypeToImporterType_["Sprite2D"] = "TextureImporter";
         resourceTypeToImporterType_["JSComponentFile"] = "JavascriptImporter";
         resourceTypeToImporterType_["JSComponentFile"] = "JavascriptImporter";
-
-
     }
     }
 
 
     if (!resourceTypeToImporterType_.Contains(resourceTypeName))
     if (!resourceTypeToImporterType_.Contains(resourceTypeName))

+ 1 - 1
Source/ToolCore/Assets/AssetImporter.h

@@ -34,7 +34,7 @@ public:
 
 
     Asset* GetAsset() { return asset_; }
     Asset* GetAsset() { return asset_; }
 
 
-    virtual Resource* GetResource() { return 0; }
+    virtual Resource* GetResource(const String& typeName = String::EMPTY) { return 0; }
 
 
     bool RequiresCacheFile() const { return requiresCacheFile_; }
     bool RequiresCacheFile() const { return requiresCacheFile_; }
 
 

+ 1 - 1
Source/ToolCore/Assets/AudioImporter.cpp

@@ -50,7 +50,7 @@ bool AudioImporter::SaveSettingsInternal()
 }
 }
 
 
 
 
-Resource* AudioImporter::GetResource()
+Resource* AudioImporter::GetResource(const String &typeName)
 {
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     Sound* sound = cache->GetResource<Sound>(asset_->GetPath());
     Sound* sound = cache->GetResource<Sound>(asset_->GetPath());

+ 1 - 1
Source/ToolCore/Assets/AudioImporter.h

@@ -17,7 +17,7 @@ public:
 
 
     virtual void SetDefaults();
     virtual void SetDefaults();
 
 
-    Resource* GetResource();
+    Resource* GetResource(const String& typeName = String::EMPTY);
 
 
 protected:
 protected:
 
 

+ 1 - 1
Source/ToolCore/Assets/JavascriptImporter.cpp

@@ -79,7 +79,7 @@ bool JavascriptImporter::SaveSettingsInternal()
     return true;
     return true;
 }
 }
 
 
-Resource* JavascriptImporter::GetResource()
+Resource* JavascriptImporter::GetResource(const String& typeName)
 {
 {
     if (!isComponentFile_)
     if (!isComponentFile_)
         return 0;
         return 0;

+ 1 - 1
Source/ToolCore/Assets/JavascriptImporter.h

@@ -19,7 +19,7 @@ public:
 
 
     bool IsComponentFile() { return isComponentFile_; }
     bool IsComponentFile() { return isComponentFile_; }
 
 
-    Resource* GetResource();
+    Resource* GetResource(const String& typeName = String::EMPTY);
 
 
 protected:
 protected:
 
 

+ 1 - 1
Source/ToolCore/Assets/ModelImporter.cpp

@@ -295,7 +295,7 @@ bool ModelImporter::SaveSettingsInternal()
     return true;
     return true;
 }
 }
 
 
-Resource* ModelImporter::GetResource()
+Resource* ModelImporter::GetResource(const String& typeName)
 {
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ResourceCache* cache = GetSubsystem<ResourceCache>();
 
 

+ 1 - 1
Source/ToolCore/Assets/ModelImporter.h

@@ -63,7 +63,7 @@ public:
     unsigned GetAnimationCount();
     unsigned GetAnimationCount();
     void SetAnimationCount(unsigned count);
     void SetAnimationCount(unsigned count);
 
 
-    Resource* GetResource();
+    Resource* GetResource(const String& typeName = String::EMPTY);
 
 
     AnimationImportInfo* GetAnimationInfo(unsigned index) { return animationInfo_[index]; }
     AnimationImportInfo* GetAnimationInfo(unsigned index) { return animationInfo_[index]; }
 
 

+ 10 - 0
Source/ToolCore/Assets/TextureImporter.cpp

@@ -69,5 +69,15 @@ bool TextureImporter::SaveSettingsInternal()
     return true;
     return true;
 }
 }
 
 
+Resource* TextureImporter::GetResource(const String& typeName)
+{
+    if (!typeName.Length())
+        return 0;
+
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    return cache->GetResource(typeName, asset_->GetPath());
+
+}
+
 
 
 }
 }

+ 2 - 0
Source/ToolCore/Assets/TextureImporter.h

@@ -17,6 +17,8 @@ public:
 
 
     virtual void SetDefaults();
     virtual void SetDefaults();
 
 
+    Resource* GetResource(const String& typeName = String::EMPTY);
+
 protected:
 protected:
 
 
     bool Import();
     bool Import();