| 2040 | } |
| 2041 | |
| 2042 | void Node::reparent(Node *p_parent, bool p_keep_global_transform) { |
| 2043 | ERR_THREAD_GUARD |
| 2044 | ERR_FAIL_NULL(p_parent); |
| 2045 | ERR_FAIL_NULL_MSG(data.parent, "Node needs a parent to be reparented."); |
| 2046 | ERR_FAIL_COND_MSG(p_parent == this, vformat("Can't reparent '%s' to itself.", p_parent->get_name())); |
| 2047 | |
| 2048 | if (p_parent == data.parent) { |
| 2049 | return; |
| 2050 | } |
| 2051 | |
| 2052 | bool preserve_owner = data.owner && (data.owner == p_parent || data.owner->is_ancestor_of(p_parent)); |
| 2053 | Node *owner_temp = data.owner; |
| 2054 | LocalVector<Node *> common_parents; |
| 2055 | |
| 2056 | // If the new parent is related to the owner, find all children of the reparented node who have the same owner so that we can reassign them. |
| 2057 | if (preserve_owner) { |
| 2058 | LocalVector<Node *> to_visit; |
| 2059 | |
| 2060 | to_visit.push_back(this); |
| 2061 | common_parents.push_back(this); |
| 2062 | |
| 2063 | while (to_visit.size() > 0) { |
| 2064 | Node *check = to_visit[to_visit.size() - 1]; |
| 2065 | to_visit.resize(to_visit.size() - 1); |
| 2066 | |
| 2067 | for (int i = 0; i < check->get_child_count(false); i++) { |
| 2068 | Node *child = check->get_child(i, false); |
| 2069 | to_visit.push_back(child); |
| 2070 | if (child->data.owner == owner_temp) { |
| 2071 | common_parents.push_back(child); |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | data.parent->remove_child(this); |
| 2078 | p_parent->add_child(this); |
| 2079 | |
| 2080 | // Reassign the old owner to those found nodes. |
| 2081 | if (preserve_owner) { |
| 2082 | for (Node *E : common_parents) { |
| 2083 | E->set_owner(owner_temp); |
| 2084 | } |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | Node *Node::get_parent() const { |
| 2089 | return data.parent; |
no test coverage detected