Browse Source

GetChildrenWithName + Javascript binding

JoshEngebretson 10 years ago
parent
commit
e63fa8e8b6

+ 7 - 2
Source/Atomic/Core/ProcessUtils.cpp

@@ -160,14 +160,19 @@ void OpenConsoleWindow()
 
 void PrintUnicode(const String& str, bool error)
 {
-    #if !defined(ANDROID) && !defined(IOS)
-    #ifdef WIN32
+#if !defined(ANDROID) && !defined(IOS)
+#ifdef WIN32
     HANDLE stream = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
     if (stream == INVALID_HANDLE_VALUE)
         return;
     WString strW(str);
     DWORD charsWritten;
     WriteConsoleW(stream, strW.CString(), strW.Length(), &charsWritten, 0);
+    
+    if (IsDebuggerPresent())
+    {
+        OutputDebugString(str.CString());
+    }
     #else
     fprintf(error ? stderr : stdout, "%s", str.CString());
     #endif

+ 38 - 0
Source/Atomic/Scene/Node.cpp

@@ -1739,6 +1739,44 @@ void Node::GetComponentsRecursive(PODVector<Component*>& dest, StringHash type)
         (*i)->GetComponentsRecursive(dest, type);
 }
 
+void Node::GetChildrenWithName(PODVector<Node*>& dest, const String& name, bool recursive) const
+{
+    GetChildrenWithName(dest, StringHash(name), recursive);
+}
+
+void Node::GetChildrenWithName(PODVector<Node*>& dest, const char* name, bool recursive) const
+{
+    GetChildrenWithName(dest, StringHash(name), recursive);
+}
+
+void Node::GetChildrenWithName(PODVector<Node*>& dest, StringHash nameHash, bool recursive) const
+{
+    dest.Clear();
+
+    if (!recursive)
+    {
+        for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
+        {
+            if ((*i)->GetNameHash() == nameHash)
+                dest.Push(*i);
+        }
+    }
+    else
+        GetChildrenWithNameRecursive(dest, nameHash);
+}
+
+void Node::GetChildrenWithNameRecursive(PODVector<Node*>& dest, StringHash nameHash) const
+{
+    for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
+    {
+        Node* node = *i;
+        if (node->GetNameHash() == nameHash)
+            dest.Push(node);
+        if (!node->children_.Empty())
+            node->GetChildrenWithNameRecursive(dest, nameHash);
+    }
+}
+
 Node* Node::CloneRecursive(Node* parent, SceneResolver& resolver, CreateMode mode)
 {
     // Create clone node

+ 9 - 0
Source/Atomic/Scene/Node.h

@@ -378,6 +378,13 @@ public:
     void GetChildren(PODVector<Node*>& dest, bool recursive = false) const;
     /// Return child scene nodes with a specific component.
     void GetChildrenWithComponent(PODVector<Node*>& dest, StringHash type, bool recursive = false) const;
+    /// Return child scene nodes by name, optionally recursive
+    void GetChildrenWithName(PODVector<Node*>& dest, const String& name, bool recursive = false) const;
+    /// Return child scene nodes by name, optionally recursive
+    void GetChildrenWithName(PODVector<Node*>& dest, const char* name, bool recursive = false) const;
+    /// Return child scene nodes by name hash, optionally recursive
+    void GetChildrenWithName(PODVector<Node*>& dest, StringHash nameHash, bool recursive = false) const;
+    /// Return child scene nodes with a specific component.
     /// Return child scene node by index.
     Node* GetChild(unsigned index) const;
     /// Return child scene node by name.
@@ -490,6 +497,8 @@ private:
     void GetChildrenRecursive(PODVector<Node*>& dest) const;
     /// Return child nodes with a specific component recursively.
     void GetChildrenWithComponentRecursive(PODVector<Node*>& dest, StringHash type) const;
+    /// Return child nodes by name, recursively
+    void GetChildrenWithNameRecursive(PODVector<Node*>& dest, StringHash nameHash) const;
     /// Return specific components recursively.
     void GetComponentsRecursive(PODVector<Component*>& dest, StringHash type) const;
     /// Clone node recursively.

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

@@ -50,6 +50,32 @@ static int Node_GetChildrenWithComponent(duk_context* ctx)
     return 1;
 }
 
+static int Node_GetChildrenWithName(duk_context* ctx)
+{
+    StringHash nameHash = duk_to_string(ctx, 0);
+
+    bool recursive = false;
+    if (duk_get_top(ctx) == 2)
+        if (duk_get_boolean(ctx, 1))
+            recursive = true;
+
+    duk_push_this(ctx);
+    Node* node = js_to_class_instance<Node>(ctx, -1, 0);
+
+    PODVector<Node*> dest;
+    node->GetChildrenWithName(dest, nameHash, recursive);
+
+    duk_push_array(ctx);
+
+    for (unsigned i = 0; i < dest.Size(); i++)
+    {
+        js_push_class_object_instance(ctx, dest[i], "Node");
+        duk_put_prop_index(ctx, -2, i);
+    }
+
+    return 1;
+}
+
 static int Scene_LoadXML(duk_context* ctx)
 {
     JSVM* vm = JSVM::GetJSVM(ctx);
@@ -89,6 +115,8 @@ void jsapi_init_scene(JSVM* vm)
     js_class_get_prototype(ctx, "Node");
     duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
     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_CreateJSComponent, 1);
     duk_put_prop_string(ctx, -2, "createJSComponent");
     duk_pop(ctx);