Browse Source

Improve CSComponent handling

-Skip generic components when inspecting a dll
-Get private fields from superclasses when inspecting CSComponent fields
-Don't release instances of CSComponents that have been disabled (should still be done at some point, but needs them to be recreated when enabled again)
-UInt32 and IntVector added to inspector typemap
-Additional GetComponents methods added to Node
-Added GetCSComponent(s) methods to Node
Matt Benic 9 years ago
parent
commit
8b66e09081

+ 1 - 0
Script/AtomicNET/AtomicNET/Core/AObject.cs

@@ -130,6 +130,7 @@ namespace AtomicEngine
 
 
             public int GetHashCode(SenderEventKey key)
             public int GetHashCode(SenderEventKey key)
             {
             {
+                // Based on http://stackoverflow.com/a/263416/156328
                 unchecked
                 unchecked
                 {
                 {
                     int hash = 17;
                     int hash = 17;

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

@@ -13,9 +13,18 @@ namespace AtomicEngine
             this.Type = type;
             this.Type = type;
 
 
             // Fields
             // Fields
+            List<FieldInfo> fields = new List<FieldInfo>();
+            fields.AddRange(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
+                .Where(field => field.IsDefined(typeof(InspectorAttribute), true)));
 
 
-            var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
-            .Where(field => field.IsDefined(typeof(InspectorAttribute), true));
+            // Inspector fields may be private. BindingFlags.NonPublic does not report private fields in superclasses
+            var baseType = type.BaseType;
+            while (baseType != typeof(CSComponent))
+            {
+                fields.AddRange(baseType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
+                .Where(field => field.IsDefined(typeof(InspectorAttribute), true)));
+                baseType = baseType.BaseType;
+            }
 
 
             InspectorFields = fields.ToArray<FieldInfo>();
             InspectorFields = fields.ToArray<FieldInfo>();
 
 
@@ -184,12 +193,15 @@ namespace AtomicEngine
 
 
                     var node = instance.Node;
                     var node = instance.Node;
 
 
-                    if (node != null && node.IsEnabled())
+                    // TODO: Ideally we want to remove disabled instances,
+                    // and re-add them when re-enabled
+                    if (node != null /*&& node.IsEnabled()*/)
                     {
                     {
 
 
                         if (node.Scene != null)
                         if (node.Scene != null)
                         {
                         {
-                            UpdateMethod.Invoke(instance, args);
+                            if (node.IsEnabled())
+                                UpdateMethod.Invoke(instance, args);
                         }
                         }
                         else
                         else
                         {
                         {

+ 93 - 32
Script/AtomicNET/AtomicNET/Scene/Node.cs

@@ -1,6 +1,4 @@
 using System;
 using System;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
 
 
 namespace AtomicEngine
 namespace AtomicEngine
 {
 {
@@ -15,51 +13,114 @@ namespace AtomicEngine
             if (type.IsSubclassOf(typeof(CSComponent)))
             if (type.IsSubclassOf(typeof(CSComponent)))
             {
             {
                 Component component = (Component)Activator.CreateInstance(type);
                 Component component = (Component)Activator.CreateInstance(type);
-                CSComponentCore.RegisterInstance((CSComponent) component);
+                CSComponentCore.RegisterInstance((CSComponent)component);
                 AddComponent(component, id, mode);
                 AddComponent(component, id, mode);
-                return (T) component;
+                return (T)component;
             }
             }
 
 
-            return (T) CreateComponent(type.Name, mode, id);
+            return (T)CreateComponent(type.Name, mode, id);
         }
         }
 
 
-		public void GetChildrenWithComponent<T>(Vector<Node> dest, bool recursive = false)
-		{
-			var type = typeof(T);
+        public void GetChildrenWithComponent<T>(Vector<Node> dest, bool recursive = false)
+        {
+            var type = typeof(T);
+
+            // If we're a CSComponent, get "CSComponents" native side and filter here
+            if (type.IsSubclassOf(typeof(CSComponent)))
+            {
+                Vector<Node> temp = new Vector<Node>();
 
 
-			// If we're a CSComponent, get "CSComponents" native side and filter here
-			if (type.IsSubclassOf(typeof(CSComponent)))
-			{
-				Vector<Node> temp = new Vector<Node>();
+                GetChildrenWithComponent(temp, "CSComponent", recursive);
 
 
-				GetChildrenWithComponent(temp, "CSComponent", recursive);
+                // filter based on actual type
 
 
-				// filter based on actual type
+                for (uint i = 0; i < temp.Size; i++)
+                {
+                    var node = temp[i];
 
 
-				for (uint i = 0; i < temp.Size; i++)
-				{
-					var node = temp[i];
+                    Vector<Component> components = new Vector<Component>();
+
+                    node.GetComponents(components, "Component", false);
+
+                    for (uint j = 0; j < components.Size; j++)
+                    {
+                        if (components[j].GetType() == type)
+                        {
+                            dest.Push(node);
+                            break;
+                        }
+                    }
+                }
+
+                return;
+            }
 
 
-					Vector<Component> components = new Vector<Component>();
+            GetChildrenWithComponent(dest, type.Name, recursive);
+        }
+
+        public T GetComponent<T>(bool recursive = false) where T : Component
+        {
+            return (T)GetComponent(typeof(T).Name, recursive);
+        }
+
+        public void GetComponents<T>(Vector<T> dest, bool recursive = false) where T : Component
+        {
+            Vector<Component> components = new Vector<Component>();
+            GetComponents(components, typeof(T).Name, recursive);
+            for (int i = 0; i < components.Size; i++)
+            {
+                dest.Push((T)components[i]);
+            }
+        }
 
 
-					node.GetComponents(components, "Component", false);
+        public void GetDerivedComponents<T>(Vector<T> dest, bool recursive = false) where T : Component
+        {
+            Vector<Component> components = new Vector<Component>();
+            GetComponents(components, recursive);
+            for (int i = 0; i < components.Size; ++i)
+            {
+                T t = components[i] as T;
+                if (t != null)
+                    dest.Push(t);
+            }
+        }
 
 
-					for (uint j = 0; j < components.Size; j++)
-					{
-						if (components[j].GetType() == type)
-						{
-							dest.Push(node);
-							break;
-						}						
-					}
-				}
+        public T GetCSComponent<T>(bool recursive = false) where T : CSComponent
+        {
+            Vector<Component> components = new Vector<Component>();
+            GetComponents(components, nameof(CSComponent), recursive);
+            for (int i = 0; i < components.Size; i++)
+            {
+                if (components[i].GetType() == typeof(T))
+                    return (T) components[i];
+            }
 
 
-				return;
-			}
+            return null;
+        }
 
 
-			GetChildrenWithComponent(dest, type.Name, recursive);
-		}
+        public void GetCSComponents<T>(Vector<T> dest, bool recursive = false) where T : CSComponent
+        {
+            Vector<Component> components = new Vector<Component>();
+            GetComponents(components, nameof(CSComponent), recursive);
+            for (int i = 0; i < components.Size; i++)
+            {
+                Component component = components[i];
+                if (component.GetType() == typeof(T))
+                    dest.Push((T)component);
+            }
+        }
 
 
+        public void GetDerivedCSComponents<T>(Vector<T> dest, bool recursive = false) where T : CSComponent
+        {
+            Vector<Component> components = new Vector<Component>();
+            GetComponents(components, nameof(CSComponent), recursive);
+            for (int i = 0; i < components.Size; ++i)
+            {
+                T t = components[i] as T;
+                if (t != null)
+                    dest.Push(t);
+            }
+        }
     }
     }
 
 
 }
 }

+ 5 - 0
Script/AtomicNET/AtomicNETService/AssemblyInspector.cs

@@ -78,6 +78,11 @@ namespace AtomicTools
 
 
                 var typeDef = metaReader.GetTypeDefinition(handle);
                 var typeDef = metaReader.GetTypeDefinition(handle);
 
 
+                // Is this a generic type?
+                var genericParams = typeDef.GetGenericParameters();
+                if (genericParams.Count != 0)
+                    continue;
+
                 var parentName = metaReader.GetString(typeDef.Name);
                 var parentName = metaReader.GetString(typeDef.Name);
 
 
                 var baseTypeHandle = typeDef.BaseType;
                 var baseTypeHandle = typeDef.BaseType;

+ 1 - 1
Script/AtomicNET/AtomicNETService/CSComponentInspector.cs

@@ -212,7 +212,7 @@ namespace AtomicTools
             // check whether we have an InspectorAttribute
             // check whether we have an InspectorAttribute
             if (metaReader.GetString(parentTypeRef.Name) != "InspectorAttribute")
             if (metaReader.GetString(parentTypeRef.Name) != "InspectorAttribute")
             {
             {
-                Console.WriteLine("parentTypeRef != InspectorAttribute");
+                //Console.WriteLine("parentTypeRef != InspectorAttribute");
                 return false;
                 return false;
             }
             }
 
 

+ 2 - 0
Source/AtomicNET/NETScript/CSComponentAssembly.cpp

@@ -51,6 +51,7 @@ namespace Atomic
     {
     {
         typeMap_["Boolean"] = VAR_BOOL;
         typeMap_["Boolean"] = VAR_BOOL;
         typeMap_["Int32"] = VAR_INT;
         typeMap_["Int32"] = VAR_INT;
+        typeMap_["UInt32"] = VAR_INT;
         typeMap_["Single"] = VAR_FLOAT;
         typeMap_["Single"] = VAR_FLOAT;
         typeMap_["Double"] = VAR_DOUBLE;
         typeMap_["Double"] = VAR_DOUBLE;
         typeMap_["String"] = VAR_STRING;
         typeMap_["String"] = VAR_STRING;
@@ -58,6 +59,7 @@ namespace Atomic
         typeMap_["Vector3"] = VAR_VECTOR3;
         typeMap_["Vector3"] = VAR_VECTOR3;
         typeMap_["Vector4"] = VAR_VECTOR4;
         typeMap_["Vector4"] = VAR_VECTOR4;
         typeMap_["Quaternion"] = VAR_QUATERNION;
         typeMap_["Quaternion"] = VAR_QUATERNION;
+        typeMap_["IntVector2"] = VAR_INTVECTOR2;
 
 
     }
     }