Bläddra i källkod

Adding Node::GetComponents, updates to allow getting all components using Component typehash, additional TypeScript decl

Josh Engebretson 10 år sedan
förälder
incheckning
1014851cb6

+ 6 - 2
Source/Atomic/Scene/Node.cpp

@@ -1023,11 +1023,13 @@ void Node::GetComponents(PODVector<Component*>& dest, StringHash type, bool recu
 {
     dest.Clear();
 
+    bool all = type == Component::GetTypeStatic();
+
     if (!recursive)
     {
         for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
         {
-            if ((*i)->GetType() == type)
+            if (all || ((*i)->GetType() == type))
                 dest.Push(*i);
         }
     }
@@ -1730,9 +1732,11 @@ void Node::GetChildrenWithComponentRecursive(PODVector<Node*>& dest, StringHash
 
 void Node::GetComponentsRecursive(PODVector<Component*>& dest, StringHash type) const
 {
+    bool all = type == Component::GetTypeStatic();
+
     for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
     {
-        if ((*i)->GetType() == type)
+        if (all || (*i)->GetType() == type)
             dest.Push(*i);
     }
     for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)

+ 36 - 0
Source/AtomicJS/Javascript/JSScene.cpp

@@ -76,6 +76,40 @@ static int Node_GetChildrenWithName(duk_context* ctx)
     return 1;
 }
 
+static int Node_GetComponents(duk_context* ctx)
+{
+    bool recursive = false;
+    StringHash typeHash = Component::GetTypeStatic();
+
+
+    if (duk_get_top(ctx) > 0)
+    {
+        if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
+            typeHash = duk_get_string(ctx, 0);
+    }
+
+    if (duk_get_top(ctx) > 1)
+        recursive = duk_require_boolean(ctx, 1) ? true : false;
+
+
+    duk_push_this(ctx);
+    Node* node = js_to_class_instance<Node>(ctx, -1, 0);
+
+    PODVector<Component*> dest;
+    node->GetComponents(dest, typeHash, recursive);
+
+    duk_push_array(ctx);
+
+    for (unsigned i = 0; i < dest.Size(); i++)
+    {
+        js_push_class_object_instance(ctx, dest[i], "Component");
+        duk_put_prop_index(ctx, -2, i);
+    }
+
+    return 1;
+}
+
+
 static int Scene_LoadXML(duk_context* ctx)
 {
     JSVM* vm = JSVM::GetJSVM(ctx);
@@ -117,6 +151,8 @@ void jsapi_init_scene(JSVM* vm)
     duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
     duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
     duk_put_prop_string(ctx, -2, "getChildrenWithName");
+    duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
+    duk_put_prop_string(ctx, -2, "getComponents");
     duk_push_c_function(ctx, Node_CreateJSComponent, 1);
     duk_put_prop_string(ctx, -2, "createJSComponent");
     duk_pop(ctx);

+ 9 - 0
Source/AtomicJS/Packages/Atomic/Scene.json

@@ -17,5 +17,14 @@
 			"ValueAnimationInfo" : ["ValueAnimation", "WrapMode", "float"]
 		}
 
+	},
+	"typescript_decl" : {
+
+		"Node" : [
+			"getChildrenWithName(name:string, recursive?:boolean):Node[];",
+			"getChildrenWithComponent(componentType:string, recursive?:boolean):Node[];",
+			"getComponents(componentType?:string, recursive?:boolean):Component[];"
+		]
 	}
+
 }