Browse Source

ResourceRef fields

Josh Engebretson 10 years ago
parent
commit
4fb2639f05

+ 4 - 0
Build/AtomicNET/AtomicNETEngine/ComponentCore.cs

@@ -111,6 +111,10 @@ public static class ComponentCore
                   {
                     field.SetValue(component, fieldMap.GetQuaternion(field.Name));
                   }
+                  else if (fieldType.IsSubclassOf(typeof(Resource)))
+                  {
+                      field.SetValue(component, fieldMap.GetResourceFromRef(field.Name));
+                  }
                   else if (fieldType.IsSubclassOf(typeof(RefCounted)))
                   {
                       field.SetValue(component, fieldMap.GetPtr(field.Name));

+ 5 - 2
Build/AtomicNETTest/MyClass.cs

@@ -17,7 +17,7 @@ namespace AtomicNETTest
         public override void Update(float timeStep)
         {
             Node.Yaw(timeStep * 75);
-            //Console.WriteLine("TICK! : {0}", nativeInstance);
+            Console.WriteLine("TICK! : {0}", MySprite2DValue.Texture.Name);
         }
 
         [Inspector]
@@ -26,6 +26,9 @@ namespace AtomicNETTest
         [Inspector]
         public int MyIntValue = 5;
 
+        [Inspector]
+        public int MyOtherIntValue = 101;
+
         [Inspector]
         public Vector3 MyVector3Value = new Vector3(1, 1, 1);
 
@@ -42,7 +45,7 @@ namespace AtomicNETTest
         public BehaviorState State = BehaviorState.Neutral;
 
         [Inspector("Sprites/star.png")]
-        public Sprite2D sprite2D;
+        public Sprite2D MySprite2DValue;
 
     }
 

+ 38 - 4
Source/AtomicNET/NETCore/NETAssemblyFile.cpp

@@ -112,16 +112,50 @@ bool NETAssemblyFile::ParseComponentClassJSON(const JSONValue& json)
 
             if (varType == VAR_NONE)
             {
-                LOGERRORF("Component Class %s contains unmappable type %s in field %s",
+                // FIXME: We need to be able to test if a type is a ResourceRef, this isn't really the way to achieve that
+                const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context_->GetObjectFactories();
+                HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
+
+                while (itr != factories.End())
+                {
+                    if ( itr->second_->GetTypeName() == typeName)
+                    {
+                        varType = VAR_RESOURCEREF;
+                        break;
+                    }
+
+                    itr++;
+                }
+
+                if (varType == VAR_NONE)
+                {
+                    LOGERRORF("Component Class %s contains unmappable type %s in field %s",
                           className.CString(), typeName.CString(), fieldName.CString());
 
-                continue;
+                    continue;
+                }
+
             }
 
-            if (defaultValue.Length())
+            if (!defaultValue.Length() && varType == VAR_RESOURCEREF)
+            {
+                // We still need a default value for ResourceRef's so we know the classtype
+                AddDefaultValue(fieldName, ResourceRef(typeName), className);
+            }
+            else
             {
                 Variant value;
-                value.FromString(varType, defaultValue);
+
+                if (varType == VAR_RESOURCEREF)
+                {
+                    ResourceRef rref(typeName);
+                    rref.name_ = defaultValue;
+                    value = rref;
+                }
+                else
+                {
+                    value.FromString(varType, defaultValue);
+                }
 
                 AddDefaultValue(fieldName, value, className);
             }

+ 22 - 0
Source/AtomicNET/NETCore/NETVariant.h

@@ -2,6 +2,9 @@
 #pragma once
 
 #include <Atomic/Core/Variant.h>
+#include <Atomic/Resource/ResourceCache.h>
+
+#include "NETCore.h"
 
 namespace Atomic
 {
@@ -112,6 +115,25 @@ public:
         return variant->GetType();
     }
 
+    Resource* GetResourceFromRef(StringHash key) const
+    {
+        Variant* variant = vmap_[key];
+
+        if (variant && variant->GetType() == VAR_RESOURCEREF)
+        {
+            const ResourceRef& ref = variant->GetResourceRef();
+
+            if (!ref.name_.Length())
+                return 0;
+
+            return NETCore::GetContext()->GetSubsystem<ResourceCache>()->GetResource(ref.type_, ref.name_);
+
+        }
+
+        return 0;
+
+    }
+
 
     bool Contains(StringHash key) const
     {