浏览代码

Add Node::IsChildOf member function.

Eugene Kozlov 8 年之前
父节点
当前提交
76ddec737e
共有 2 个文件被更改,包括 18 次插入8 次删除
  1. 14 7
      Source/Urho3D/Scene/Node.cpp
  2. 4 1
      Source/Urho3D/Scene/Node.h

+ 14 - 7
Source/Urho3D/Scene/Node.cpp

@@ -800,13 +800,8 @@ void Node::AddChild(Node* node, unsigned index)
     if (!node || node == this || node->parent_ == this)
         return;
     // Check for possible cyclic parent assignment
-    Node* parent = parent_;
-    while (parent)
-    {
-        if (parent == node)
-            return;
-        parent = parent->parent_;
-    }
+    if (IsChildOf(node))
+        return;
 
     // Keep a shared ptr to the node while transferring
     SharedPtr<Node> nodeShared(node);
@@ -1364,6 +1359,18 @@ bool Node::HasTag(const String& tag) const
     return impl_->tags_.Contains(tag);
 }
 
+bool Node::IsChildOf(Node* node) const
+{
+    Node* parent = parent_;
+    while (parent)
+    {
+        if (parent == node)
+            return true;
+        parent = parent->parent_;
+    }
+    return false;
+}
+
 const Variant& Node::GetVar(StringHash key) const
 {
     VariantMap::ConstIterator i = vars_.Find(key);

+ 4 - 1
Source/Urho3D/Scene/Node.h

@@ -355,10 +355,13 @@ public:
     /// Return scene.
     Scene* GetScene() const { return scene_; }
 
+    /// Return whether is a direct or indirect child of specified node.
+    bool IsChildOf(Node* node) const;
+
     /// Return whether is enabled. Disables nodes effectively disable all their components.
     bool IsEnabled() const { return enabled_; }
 
-    /// Returns the node's last own enabled state. May be different than the value returned by IsEnabled when SetDeepEnabled has been used.
+    /// Return the node's last own enabled state. May be different than the value returned by IsEnabled when SetDeepEnabled has been used.
     bool IsEnabledSelf() const { return enabledPrev_; }
 
     /// Return owner connection in networking.