Browse Source

Adding ResourceRefList attribute editing

Josh Engebretson 10 years ago
parent
commit
840f467842

+ 127 - 5
Script/AtomicEditor/ui/frames/inspector/AttributeInfoEdit.ts

@@ -572,6 +572,16 @@ class ColorAttributeEdit extends NumberArrayAttributeEdit {
 
 class ResourceRefAttributeEdit extends AttributeInfoEdit {
 
+    refListIndex: number;
+
+    constructor(refListIndex: number = -1) {
+
+        super();
+
+        this.refListIndex = refListIndex;
+
+    }
+
     editField: Atomic.UIEditField;
 
     initialize(editType: SerializableEditType, attrInfo: Atomic.AttributeInfo): boolean {
@@ -589,7 +599,7 @@ class ResourceRefAttributeEdit extends AttributeInfoEdit {
 
     refresh() {
 
-        var uniform = this.editType.getUniformValue(this.attrInfo);
+        var uniform = this.editType.getUniformValue(this.attrInfo, this.refListIndex);
 
         if (uniform) {
 
@@ -598,8 +608,15 @@ class ResourceRefAttributeEdit extends AttributeInfoEdit {
             if (object) {
 
                 // for cached resources, use the asset name, otherwise use the resource path name
-                var resource = <Atomic.Resource>object.getAttribute(this.attrInfo.name);
+                var resource: Atomic.Resource;
+                if (this.refListIndex != -1) {
+                    resource = object.getAttribute(this.attrInfo.name).resources[this.refListIndex];
+                } else {
+                    resource = <Atomic.Resource>object.getAttribute(this.attrInfo.name);
+                }
+
                 var text = "";
+
                 if (resource) {
                     text = resource.name;
                     var asset = ToolCore.assetDatabase.getAssetByCachePath(resource.name);
@@ -646,8 +663,7 @@ class ResourceRefAttributeEdit extends AttributeInfoEdit {
             EditorUI.getModelOps().showResourceSelection("Select " + resourceTypeName + " Resource", importerName, function(asset: ToolCore.Asset) {
 
                 var resource = asset.getResource(resourceTypeName);
-
-                this.editType.onAttributeInfoEdited(this.attrInfo, resource);
+                this.editType.onAttributeInfoEdited(this.attrInfo, resource, this.refListIndex);
                 this.refresh();
 
             }.bind(this));
@@ -677,7 +693,7 @@ class ResourceRefAttributeEdit extends AttributeInfoEdit {
 
                     var resource = asset.getResource(resourceTypeName);
 
-                    this.editType.onAttributeInfoEdited(this.attrInfo, resource);
+                    this.editType.onAttributeInfoEdited(this.attrInfo, resource, this.refListIndex);
                     this.refresh();
 
 
@@ -690,6 +706,111 @@ class ResourceRefAttributeEdit extends AttributeInfoEdit {
 
 }
 
+class ResourceRefListAttributeEdit extends AttributeInfoEdit {
+
+    layout: Atomic.UILayout;
+    refEdits: ResourceRefAttributeEdit[] = [];
+
+    initialize(editType: SerializableEditType, attrInfo: Atomic.AttributeInfo): boolean {
+
+        return super.initialize(editType, attrInfo);
+
+    }
+
+    createRefEdit(index: number) {
+
+        var refEdit = new ResourceRefAttributeEdit(index);
+
+        refEdit.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
+        refEdit.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
+        refEdit.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
+
+        refEdit.initialize(this.editType, this.attrInfo);
+
+        this.layout.addChild(refEdit);
+
+        this.refEdits.push(refEdit);
+
+    }
+
+    createEditWidget() {
+
+        var layout = this.layout = new Atomic.UILayout();
+
+        layout.axis = Atomic.UI_AXIS_Y;
+        layout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
+        layout.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
+        layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
+
+        this.editWidget = layout;
+
+    }
+
+    createLayout() {
+
+        this.createEditWidget();
+
+        this.editWidget.subscribeToEvent(this.editWidget, "WidgetEvent", (data) => this.handleWidgetEvent(data));
+
+        this.addChild(this.editWidget);
+
+    }
+
+    refresh() {
+
+        var editType = this.editType;
+
+        var object = this.editType.getFirstObject();
+
+        if (!object) {
+            this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
+            return;
+        }
+
+        this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
+
+        var maxLength = -1;
+        var i;
+        for (i in editType.objects) {
+
+            object = editType.objects[i];
+            var value = object.getAttribute(this.attrInfo.name);
+            if (value.resources.length > maxLength) {
+
+                maxLength = value.resources.length;
+
+            }
+
+        }
+
+        if (maxLength == -1) {
+            this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
+            return;
+        }
+
+        for (i = this.refEdits.length; i < maxLength; i++) {
+
+            this.createRefEdit(i);
+
+        }
+
+        for (i = 0; i < this.refEdits.length; i++) {
+
+            var refEdit = this.refEdits[i];
+
+            if (i < maxLength) {
+                refEdit.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
+                refEdit.refresh();
+            }
+            else {
+                refEdit.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
+            }
+
+        }
+
+    }
+}
+
 
 
 AttributeInfoEdit.standardAttrEditTypes[Atomic.VAR_BOOL] = BoolAttributeEdit;
@@ -704,5 +825,6 @@ AttributeInfoEdit.standardAttrEditTypes[Atomic.VAR_QUATERNION] = QuaternionAttri
 AttributeInfoEdit.standardAttrEditTypes[Atomic.VAR_COLOR] = ColorAttributeEdit;
 
 AttributeInfoEdit.standardAttrEditTypes[Atomic.VAR_RESOURCEREF] = ResourceRefAttributeEdit;
+AttributeInfoEdit.standardAttrEditTypes[Atomic.VAR_RESOURCEREFLIST] = ResourceRefListAttributeEdit;
 
 export = AttributeInfoEdit;

+ 7 - 3
Script/AtomicEditor/ui/frames/inspector/SelectionSection.ts

@@ -10,6 +10,7 @@ abstract class SelectionSection extends Atomic.UISection {
     editType: SerializableEditType;
     attrLayout: Atomic.UILayout;
     suppressed: boolean = false;
+    customUI:SelectionSectionUI;
 
     attrEdits: { [name: string]: AttributeInfoEdit } = {};
 
@@ -34,6 +35,9 @@ abstract class SelectionSection extends Atomic.UISection {
 
         }
 
+        if (this.customUI)
+          this.customUI.refresh();
+
     }
 
     suppress(value: boolean) {
@@ -80,9 +84,9 @@ abstract class SelectionSection extends Atomic.UISection {
 
         if (SelectionSection.customSectionUI[this.editType.typeName]) {
 
-            var ui = new SelectionSection.customSectionUI[this.editType.typeName]();
-            ui.createUI(this.editType);
-            attrLayout.addChild(ui);
+            this.customUI = new SelectionSection.customSectionUI[this.editType.typeName]();
+            this.customUI.createUI(this.editType);
+            attrLayout.addChild(this.customUI);
 
         }
 

+ 1 - 3
Script/AtomicEditor/ui/frames/inspector/SelectionSectionCoreUI.ts

@@ -1,5 +1,5 @@
 
-
+import EditorUI = require("ui/EditorUI");
 import InspectorUtils = require("./InspectorUtils");
 import SelectionSection = require("./SelectionSection");
 import SelectionSectionUI = require("./SelectionSectionUI");
@@ -8,8 +8,6 @@ import SerializableEditType = require("./SerializableEditType");
 
 class CollisionShapeSectionUI extends SelectionSectionUI {
 
-    editType: SerializableEditType;
-
     createUI(editType: SerializableEditType) {
 
         this.editType = editType;

+ 4 - 0
Script/AtomicEditor/ui/frames/inspector/SelectionSectionUI.ts

@@ -5,6 +5,10 @@ class SelectionSectionUI extends Atomic.UILayout {
 
     editType: SerializableEditType;
 
+    refresh() {
+      
+    }
+
     createUI(editType: SerializableEditType) {
 
       this.editType = editType;

+ 32 - 6
Script/AtomicEditor/ui/frames/inspector/SerializableEditType.ts

@@ -2,6 +2,7 @@
 class SerializableEditType {
 
     constructor(serial: Atomic.Serializable) {
+
         this.typeName = serial.typeName;
         this.attrInfos = serial.getAttributes();
         this.addSerializable(serial);
@@ -27,14 +28,29 @@ class SerializableEditType {
             if (i == 0) {
 
                 value = object.getAttribute(attrInfo.name);
-                if (index >= 0)
-                    value = value[index];
+                if (index >= 0) {
+
+                    if (attrInfo.type == Atomic.VAR_RESOURCEREFLIST) {
+
+                        value = value.resources[index];
+
+                    } else {
+                        value = value[index];
+                    }
+                }
 
             } else {
 
                 var value2 = object.getAttribute(attrInfo.name);
-                if (index >= 0)
-                    value2 = value2[index];
+                if (index >= 0) {
+                    if (attrInfo.type == Atomic.VAR_RESOURCEREFLIST) {
+
+                        value2 = value2.resources[index];
+
+                    } else {
+                        value2 = value2[index];
+                    }
+                }
 
                 if (value != value2)
                     return false;
@@ -57,8 +73,18 @@ class SerializableEditType {
             if (index >= 0) {
 
                 var idxValue = object.getAttribute(attrInfo.name);
-                idxValue[index] = value;
-                object.setAttribute(attrInfo.name, idxValue);
+
+                if (attrInfo.type == Atomic.VAR_RESOURCEREFLIST) {
+
+                    idxValue.resources[index] = value;
+                    object.setAttribute(attrInfo.name, idxValue);
+
+                } else {
+
+                    idxValue[index] = value;
+                    object.setAttribute(attrInfo.name, idxValue);
+
+                }
 
             } else {
 

+ 68 - 5
Source/AtomicJS/Javascript/JSAPI.cpp

@@ -271,7 +271,7 @@ duk_bool_t js_check_is_buffer_and_get_data(duk_context* ctx, duk_idx_t idx, void
     return true;
 }
 
-void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
+void js_to_variant(duk_context* ctx, int variantIdx, Variant &v, VariantType variantType)
 {
     v.Clear();
 
@@ -363,16 +363,54 @@ void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
     // object check after array and buffer object check
     if (duk_is_object(ctx, variantIdx))
     {
-        RefCounted* o = js_to_class_instance<RefCounted>(ctx, variantIdx, 0);
-        if (o)
-            v = o;
+        if (variantType == VAR_RESOURCEREFLIST)
+        {
+            ResourceRefList refList;
+
+            duk_get_prop_string(ctx, variantIdx, "typeName");
+            refList.type_ = duk_to_string(ctx, -1);
+
+            duk_get_prop_string(ctx, variantIdx, "resources");
+            int length = duk_get_length(ctx, -1);
+
+            for (int i = 0; i < length; i++) {
+
+                duk_get_prop_index(ctx, -1, i);
+
+                Resource* resource = NULL;
+
+                if (duk_is_object(ctx, -1))
+                {
+                     resource = js_to_class_instance<Resource>(ctx, -1, 0);
+
+                }
+
+                if (resource) {
+                    refList.names_.Push(resource->GetName());
+                }
+                else
+                    refList.names_.Push(String::EMPTY);
+
+                duk_pop(ctx);
+            }
+
+            duk_pop_n(ctx, 2);
+
+            v = refList;
+        }
+        else
+        {
+            RefCounted* o = js_to_class_instance<RefCounted>(ctx, variantIdx, 0);
+            if (o)
+                v = o;
+        }
+
         return;
     }
 
 
 }
 
-
 // variant map Proxy getter, so we can convert access to string based
 // member lookup, to string hash on the fly
 
@@ -499,6 +537,31 @@ void js_push_variant(duk_context *ctx, const Variant& v)
         js_push_class_object_instance(ctx, resource);
     }   break;
 
+    case VAR_RESOURCEREFLIST:
+    {
+
+        const ResourceRefList& resourceRefList(v.GetResourceRefList());
+        const Context* context = JSVM::GetJSVM(ctx)->GetContext();
+
+        duk_push_object(ctx);
+        duk_push_string(ctx, context->GetTypeName(resourceRefList.type_).CString());
+        duk_put_prop_string(ctx, -2, "typeName");
+
+        duk_push_array(ctx);
+
+        ResourceCache* cache = context->GetSubsystem<ResourceCache>();
+
+        for (unsigned i = 0; i < resourceRefList.names_.Size(); i++) {
+
+            Resource* resource = cache->GetResource(resourceRefList.type_, resourceRefList.names_[i]);
+            js_push_class_object_instance(ctx, resource);
+            duk_put_prop_index(ctx, -2, i);
+        }
+
+        duk_put_prop_string(ctx, -2, "resources");
+
+    } break;
+
     case VAR_BOOL:
         duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
         break;

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

@@ -59,7 +59,7 @@ void js_class_get_constructor(duk_context* ctx, const char* package, const char
 void js_push_variant(duk_context* ctx, const Variant &v);
 void js_push_variantmap(duk_context* ctx, const VariantMap &vmap);
 
-void js_to_variant(duk_context* ctx, int variantIdx, Variant &v);
+void js_to_variant(duk_context* ctx, int variantIdx, Variant &v, VariantType variantType = VAR_NONE);
 
 void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v);
 

+ 26 - 24
Source/AtomicJS/Javascript/JSSceneSerializable.cpp

@@ -60,8 +60,6 @@ namespace Atomic
 static int Serializable_SetAttribute(duk_context* ctx)
 {
     const char* name = duk_to_string(ctx, 0);
-    Variant v;
-    js_to_variant(ctx, 1, v);
 
     duk_push_this(ctx);
     Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
@@ -87,6 +85,10 @@ static int Serializable_SetAttribute(duk_context* ctx)
         }
     }
 
+
+    Variant v;
+    js_to_variant(ctx, 1, v, variantType);
+
     ScriptComponent* jsc = NULL;
 
     // check dynamic
@@ -161,7 +163,7 @@ static int Serializable_SetAttribute(duk_context* ctx)
 
         }
 
-    }
+    }    
 
     if (isAttr)
     {
@@ -237,25 +239,6 @@ static int Serializable_GetAttribute(duk_context* ctx)
     return 1;
 }
 
-static const String& GetResourceRefClassName(Context* context, const ResourceRef& ref)
-{
-    const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context->GetObjectFactories();
-
-    HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
-
-    while (itr != factories.End())
-    {
-        if (itr->first_ == ref.type_)
-        {
-            return itr->second_->GetTypeName();
-        }
-
-        itr++;
-    }
-
-    return String::EMPTY;
-}
-
 static void GetDynamicAttributes(duk_context* ctx, unsigned& count, const VariantMap& defaultFieldValues,
                                  const FieldMap& fields,
                                  const EnumMap& enums)
@@ -275,7 +258,7 @@ static void GetDynamicAttributes(duk_context* ctx, unsigned& count, const Varian
                 if (defaultFieldValues[itr->first_]->GetType() == VAR_RESOURCEREF)
                 {
                     const ResourceRef& ref = defaultFieldValues[itr->first_]->GetResourceRef();
-                    const String& typeName = GetResourceRefClassName(JSVM::GetJSVM(ctx)->GetContext(), ref);
+                    const String& typeName = JSVM::GetJSVM(ctx)->GetContext()->GetTypeName(ref.type_);
 
                     if (typeName.Length())
                     {
@@ -367,7 +350,7 @@ static int Serializable_GetAttributes(duk_context* ctx)
                 if (attr->defaultValue_.GetType() == VAR_RESOURCEREF)
                 {
                     const ResourceRef& ref = attr->defaultValue_.GetResourceRef();
-                    const String& typeName = GetResourceRefClassName(serial->GetContext(), ref);
+                    const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
 
                     if (typeName.Length())
                     {
@@ -376,8 +359,27 @@ static int Serializable_GetAttributes(duk_context* ctx)
 
                     }
                 }
+
             }
 
+            if (attr->type_ == VAR_RESOURCEREFLIST)
+            {
+                if (attr->defaultValue_.GetType() == VAR_RESOURCEREFLIST)
+                {
+                    const ResourceRefList& ref = attr->defaultValue_.GetResourceRefList();
+                    const String& typeName = serial->GetContext()->GetTypeName(ref.type_);
+
+                    if (typeName.Length())
+                    {
+                        duk_push_string(ctx, typeName.CString());
+                        duk_put_prop_string(ctx, -2, "resourceTypeName");
+
+                    }
+                }
+
+            }
+
+
             duk_push_string(ctx, attr->name_.CString());
             duk_put_prop_string(ctx, -2, "name");
 

+ 1 - 0
Source/ToolCore/Assets/AssetDatabase.cpp

@@ -530,6 +530,7 @@ String AssetDatabase::GetResourceImporterName(const String& resourceTypeName)
     {
         resourceTypeToImporterType_["Sound"] = "AudioImporter";
         resourceTypeToImporterType_["Model"] = "ModelImporter";
+        resourceTypeToImporterType_["Material"] = "MaterialImporter";
         resourceTypeToImporterType_["Texture2D"] = "TextureImporter";
         resourceTypeToImporterType_["Sprite2D"] = "TextureImporter";
         resourceTypeToImporterType_["AnimatedSprite2D"] = "SpriterImporter";

+ 11 - 0
Source/ToolCore/Assets/MaterialImporter.cpp

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

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

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