Browse Source

Adding support for fixed array sizes

Josh Engebretson 9 years ago
parent
commit
d4e502a7d3

+ 6 - 2
Script/AtomicEditor/ui/frames/inspector/AttributeInfoEdit.ts

@@ -1182,12 +1182,16 @@ class ArrayAttributeEdit extends AttributeInfoEdit {
         lp.width = 304;
         layout.layoutParams = lp;
 
-        var name = this.attrInfo.name + " Size";
-        if (name == "AnimationResources Size")
+        var name = this.attrInfo.name;
+        if (name == "AnimationResources")
             name = "Animations";
 
         var sizeEdit = this.sizeEdit = InspectorUtils.createAttrEditField(name, layout);
 
+        if (this.attrInfo.fixedArraySize) {
+            sizeEdit.disable();
+        }
+
         lp = new Atomic.UILayoutParams();
         lp.width = 160;
         sizeEdit.layoutParams = lp;

+ 4 - 1
Script/AtomicNET/AtomicNET/Scene/CSComponentCore.cs

@@ -182,7 +182,10 @@ namespace AtomicEngine
                                     {
                                          array.SetValue(variant.GetColor(), i);
                                     }
-
+                                    else if (elementType.IsSubclassOf(typeof(Resource)))
+                                    {
+                                        array.SetValue(variant.GetResource(), i);
+                                    }
                                     break;
                             }                            
                     

+ 14 - 1
Script/AtomicNET/AtomicNET/Scene/InspectorAttribute.cs

@@ -13,7 +13,20 @@ namespace AtomicEngine
 
         public readonly string DefaultValue;
 
-        // Tooltip which will be displayed in editor
+        /// <summary>
+        ///Specify a fixed array size for field 
+        /// </summary>
+        public int ArraySize
+        {
+            get { return arraySize; }
+            set { arraySize = value; }
+        }
+
+        private int arraySize = 0;
+
+        /// <summary>
+        /// Tooltip which will be displayed in editor 
+        /// </summary>
         public string Tooltip
         {
             get { return tooltip; }

+ 17 - 6
Script/AtomicNET/AtomicNETService/CSComponentInspector.cs

@@ -317,8 +317,8 @@ namespace AtomicTools
 
                 var typeCode = argsReader.ReadSerializationTypeCode();
 
-                // support string custom attr for now to simplify things
-                if (typeCode != SerializationTypeCode.String)
+                // support string/int custom attr for now to simplify things
+                if (typeCode != SerializationTypeCode.String && typeCode != SerializationTypeCode.Int32)
                     return false;
 
                 string name;
@@ -326,12 +326,23 @@ namespace AtomicTools
                 if (!CrackStringInAttributeValue(out name, ref argsReader))
                     return false;
 
-                string value;
+                if (typeCode == SerializationTypeCode.Int32)
+                {
+                    int value;
+                    value = argsReader.ReadInt32();
+                    inspectorField.CustomAttrNamedArgs[name] = value.ToString();
+                }
+                else
+                {
+                    string value;
 
-                if (!CrackStringInAttributeValue(out value, ref argsReader))
-                    return false;
+                    if (!CrackStringInAttributeValue(out value, ref argsReader))
+                        return false;
+
+                    inspectorField.CustomAttrNamedArgs[name] = value;
+                }
 
-                inspectorField.CustomAttrNamedArgs[name] = value;
+                
 
             }
 

+ 1 - 0
Script/TypeScript/AtomicWork.d.ts

@@ -122,6 +122,7 @@ declare module Atomic {
         dynamic: boolean;
         tooltip: string;
         isArray:boolean;
+        fixedArraySize:number;
     }
 
     export interface ShaderParameter {

+ 2 - 2
Source/Atomic/Script/ScriptComponentFile.cpp

@@ -53,10 +53,10 @@ void ScriptComponentFile::AddEnum(const String& enumName, const EnumInfo& enumIn
     enumValues.Push(enumInfo);
 }
 
-void ScriptComponentFile::AddField(const String& fieldName, VariantType variantType, const String& resourceTypeName, bool isArray, const String &classname, const String& tooltip)
+void ScriptComponentFile::AddField(const String& fieldName, VariantType variantType, const String& resourceTypeName, bool isArray, unsigned fixedArraySize, const String &classname, const String& tooltip)
 {
     FieldMap& fields = classFields_[classname];
-    FieldInfo finfo(fieldName, variantType, resourceTypeName, isArray);
+    FieldInfo finfo(fieldName, variantType, resourceTypeName, isArray, fixedArraySize);
     fields[fieldName] = finfo;
 
     if (tooltip.Length())

+ 6 - 3
Source/Atomic/Script/ScriptComponentFile.h

@@ -37,14 +37,16 @@ struct FieldInfo
         name_ = "UNINITIALIZED_FIELDINFO";
         variantType_ = VAR_NONE;
         isArray_ = false;
+        fixedArraySize_ = 0;
     }
 
-    FieldInfo(const String& name, VariantType variantType, const String& resourceTypeName = String::EMPTY, bool isArray = false)
+    FieldInfo(const String& name, VariantType variantType, const String& resourceTypeName = String::EMPTY, bool isArray = false, unsigned fixedArraySize = 0)
     {
         name_ = name;        
         variantType_ = variantType;
         resourceTypeName_ = resourceTypeName;
-        isArray_ = isArray;        
+        isArray_ = isArray;
+        fixedArraySize_ = fixedArraySize;
     }
 
     String name_;
@@ -52,6 +54,7 @@ struct FieldInfo
     // for resource ref variants
     String resourceTypeName_;
     bool isArray_;
+    unsigned fixedArraySize_;
 };
 
 struct EnumInfo
@@ -105,7 +108,7 @@ protected:
     void Clear();
 
     void AddEnum(const String& enumName, const EnumInfo& enumInfo, const String& classname = String::EMPTY);
-    void AddField(const String& fieldName, VariantType variantType, const String& resourceTypeName = String::EMPTY, bool isArray = false, const String& classname = String::EMPTY, const String& tooltip = String::EMPTY);
+    void AddField(const String& fieldName, VariantType variantType, const String& resourceTypeName = String::EMPTY, bool isArray = false, unsigned fixedArraySize = 0, const String& classname = String::EMPTY, const String& tooltip = String::EMPTY);
     void AddDefaultValue(const String& fieldName, const Variant& value, const String& classname = String::EMPTY);
 
     // only valid in editor

+ 32 - 0
Source/Atomic/Script/ScriptVariant.cpp

@@ -20,3 +20,35 @@
 // THE SOFTWARE.
 //
 
+#include "ScriptVariant.h"
+
+namespace Atomic
+
+{
+
+Resource* ScriptVariant::GetResource() const
+{
+    if (variant_.GetType() == VAR_RESOURCEREF)
+    {
+        const ResourceRef& ref = variant_.GetResourceRef();
+
+        if (!ref.name_.Length() || !ref.type_)
+            return 0;
+
+        ResourceCache* cache = ScriptSystem::GetContext()->GetSubsystem<ResourceCache>();
+
+        return cache->GetResource(ref.type_, ref.name_);       
+    }
+}
+
+void ScriptVariant::SetResource(Resource* resource)
+{
+    if (!resource)
+    {
+        variant_ = ResourceRef();
+        return;
+    }
+    variant_ = ResourceRef(resource->GetType(), resource->GetName());
+}
+
+}

+ 4 - 0
Source/Atomic/Script/ScriptVariant.h

@@ -78,6 +78,10 @@ namespace Atomic
 
         const Vector3& GetVector3() const { return variant_.GetVector3(); }
 
+        Resource* GetResource() const;
+
+        void SetResource(Resource* resource);
+
         void SetVector3(const Vector3& value) { variant_ = value; }
 
         const Quaternion& GetQuaternion() const { return variant_.GetQuaternion(); }

+ 2 - 3
Source/AtomicJS/Javascript/JSAPI.cpp

@@ -304,7 +304,7 @@ namespace Atomic
         return true;
     }
 
-    void js_push_default_variant(duk_context* ctx, VariantType variantType, Variant& value)
+    void js_get_default_variant(VariantType variantType, Variant& value)
     {
         value = Variant::EMPTY;
         
@@ -370,8 +370,7 @@ namespace Atomic
         default:
             break;
         }
-
-        js_push_variant(ctx, value);
+        
     }
 
     void js_to_variant(duk_context* ctx, int variantIdx, Variant &v, VariantType variantType)

+ 2 - 2
Source/AtomicJS/Javascript/JSAPI.h

@@ -61,8 +61,8 @@ void js_define_native_event(duk_context* ctx, const String& eventType, const Str
 void js_push_variant(duk_context* ctx, const Variant &v, int arrayIndex = -1);
 void js_push_variantmap(duk_context* ctx, const VariantMap &vmap);
 
-// Push a default value for the given variant type and set variantOut to the pushed value
-void js_push_default_variant(duk_context* ctx, VariantType variantType, Variant& variantOut);
+// Get a default value for the given variant type and set variantOut
+void js_get_default_variant(VariantType variantType, Variant& variantOut);
 
 /// Sets a variant value from the duktape stack
 void js_to_variant(duk_context* ctx, int variantIdx, Variant &v, VariantType variantType = VAR_NONE);

+ 56 - 13
Source/AtomicJS/Javascript/JSSceneSerializable.cpp

@@ -336,6 +336,32 @@ namespace Atomic
 
                             VariantVector* vector = vptr->GetVariantVectorPtr();
 
+                            if (finfo->fixedArraySize_)
+                            {
+                                if (vector->Size() != finfo->fixedArraySize_)
+                                {
+                                    if (vector->Size() < finfo->fixedArraySize_)
+                                    {
+                                        Variant value;
+                                        js_get_default_variant(finfo->variantType_, value);
+
+                                        unsigned oldSize = vector->Size();
+                                        vector->Resize(finfo->fixedArraySize_);
+
+                                        for (unsigned i = oldSize; i < finfo->fixedArraySize_; i++)
+                                        {
+                                            (*vector)[i] = value;
+                                        }
+                                    }
+                                    else
+                                    {
+                                        // truncate
+                                        vector->Resize(finfo->fixedArraySize_);
+                                    }
+                                    
+                                }
+                            }
+
                             if (arrayIndex >= vector->Size())
                             {
                                 duk_push_undefined(ctx);
@@ -347,7 +373,8 @@ namespace Atomic
                             if (current.GetType() != finfo->variantType_)
                             {
                                 Variant value;
-                                js_push_default_variant(ctx, finfo->variantType_, value);
+                                js_get_default_variant(finfo->variantType_, value);
+                                js_push_variant(ctx, value);
                                 (*vector)[arrayIndex] = value;
                             }
                             else
@@ -369,23 +396,36 @@ namespace Atomic
                             if (arrayIndex < 0)
                             {
                                 SharedPtr<ScriptVector> vector(new ScriptVector());
+
+                                if (finfo->fixedArraySize_)
+                                {
+                                    
+                                    VariantVector init(finfo->fixedArraySize_);
+                                    init.Resize(finfo->fixedArraySize_);
+                                    Variant value;
+                                    js_get_default_variant(finfo->variantType_, value);
+                                    
+                                    for (unsigned i = 0; i < finfo->fixedArraySize_; i++)
+                                    {
+                                        init[i] = value;
+                                    }
+
+                                    jsc->GetFieldValues()[finfo->name_] = init;
+
+                                    vector->AdaptFromVector(init);
+
+                                    
+
+                                }
+
                                 js_push_class_object_instance(ctx, vector);
                                 return 1;
                             }
                             else
                             {
-                                Variant value;
-                                js_push_default_variant(ctx, finfo->variantType_, value);
-
-                                VariantVector newVector;
-
-                                if (arrayIndex >= newVector.Size())
-                                {
-                                    newVector.Resize(arrayIndex + 1);
-                                }
-
-                                jsc->GetFieldValues()[name] = newVector;
-
+                                // we don't have the variant in the fieldmap
+                                ATOMIC_LOGERROR("Serializable_GetAttribute - indexing uninitialized array");
+                                duk_push_undefined(ctx);
                                 return 1;
                             }
                         }
@@ -461,6 +501,9 @@ namespace Atomic
                 duk_push_boolean(ctx, itr->second_.isArray_ ? 1 : 0);
                 duk_put_prop_string(ctx, -2, "isArray");
 
+                duk_push_number(ctx, itr->second_.fixedArraySize_);
+                duk_put_prop_string(ctx, -2, "fixedArraySize");
+
                 if (tooltips.Contains(itr->first_))
                 {
                     duk_push_string(ctx, tooltips[itr->first_]->CString());

+ 9 - 1
Source/AtomicNET/NETScript/CSComponentAssembly.cpp

@@ -93,6 +93,7 @@ namespace Atomic
 
                 bool isEnum = jfield.Get("isEnum").GetBool();
                 bool isArray = jfield.Get("isArray").GetBool();
+                unsigned fixedArraySize = 0;
                 String typeName = jfield.Get("typeName").GetString();
                 String resourceTypeName;
                 String fieldName = jfield.Get("name").GetString();
@@ -120,6 +121,13 @@ namespace Atomic
                     tooltip = caNamed["Tooltip"].GetString();
                 }
 
+                // fixed array size
+                if (caNamed.Contains("ArraySize"))
+                {
+                    fixedArraySize = (unsigned)ToInt(caNamed["ArraySize"].GetString().CString());
+                }
+
+
                 if (isEnum && assemblyEnums_.Contains(typeName) && !enumsAdded.Contains(fieldName))
                 {
                     varType = VAR_INT;
@@ -186,7 +194,7 @@ namespace Atomic
                     }
                 }
 
-                AddField(fieldName, varType, resourceTypeName, isArray, className, tooltip);
+                AddField(fieldName, varType, resourceTypeName, isArray, fixedArraySize, className, tooltip);
 
             }