diff options
author | Tomasz Chabora <kobewi4e@gmail.com> | 2020-02-17 18:29:14 +0100 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2022-08-16 19:44:41 +0200 |
commit | 2bdd7e9ea0383c06fe6a83445469e41222477abc (patch) | |
tree | 70c22e25063e0b608344ca5d02debc252c589a28 /scene | |
parent | d5d22ab035a611a567f73a2ee2d61a81c99c61b5 (diff) |
Add methods for node reparenting
Diffstat (limited to 'scene')
-rw-r--r-- | scene/2d/node_2d.cpp | 8 | ||||
-rw-r--r-- | scene/2d/node_2d.h | 1 | ||||
-rw-r--r-- | scene/3d/node_3d.cpp | 8 | ||||
-rw-r--r-- | scene/3d/node_3d.h | 1 | ||||
-rw-r--r-- | scene/gui/control.cpp | 8 | ||||
-rw-r--r-- | scene/gui/control.h | 1 | ||||
-rw-r--r-- | scene/main/node.cpp | 13 | ||||
-rw-r--r-- | scene/main/node.h | 1 |
8 files changed, 41 insertions, 0 deletions
diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index 4599785ce4..c427287752 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -131,6 +131,14 @@ void Node2D::_update_transform() { _notify_transform(); } +void Node2D::reparent(Node *p_parent, bool p_keep_global_transform) { + Transform2D temp = get_global_transform(); + Node::reparent(p_parent); + if (p_keep_global_transform) { + set_global_transform(temp); + } +} + void Node2D::set_position(const Point2 &p_pos) { if (_xform_dirty) { const_cast<Node2D *>(this)->_update_xform_values(); diff --git a/scene/2d/node_2d.h b/scene/2d/node_2d.h index 0d8a31e6bb..18a56cf5a3 100644 --- a/scene/2d/node_2d.h +++ b/scene/2d/node_2d.h @@ -72,6 +72,7 @@ public: virtual void _edit_set_rect(const Rect2 &p_edit_rect) override; #endif + virtual void reparent(Node *p_parent, bool p_keep_global_transform = true) override; void set_position(const Point2 &p_pos); void set_rotation(real_t p_radians); diff --git a/scene/3d/node_3d.cpp b/scene/3d/node_3d.cpp index 1de85d57a3..ba2cca1ae8 100644 --- a/scene/3d/node_3d.cpp +++ b/scene/3d/node_3d.cpp @@ -607,6 +607,14 @@ void Node3D::set_disable_gizmos(bool p_enabled) { #endif } +void Node3D::reparent(Node *p_parent, bool p_keep_global_transform) { + Transform3D temp = get_global_transform(); + Node::reparent(p_parent); + if (p_keep_global_transform) { + set_global_transform(temp); + } +} + void Node3D::set_disable_scale(bool p_enabled) { data.disable_scale = p_enabled; } diff --git a/scene/3d/node_3d.h b/scene/3d/node_3d.h index b1e129798d..50d3d6123b 100644 --- a/scene/3d/node_3d.h +++ b/scene/3d/node_3d.h @@ -210,6 +210,7 @@ public: virtual void set_transform_gizmo_visible(bool p_enabled) { data.transform_gizmo_visible = p_enabled; }; virtual bool is_transform_gizmo_visible() const { return data.transform_gizmo_visible; }; #endif + virtual void reparent(Node *p_parent, bool p_keep_global_transform = true) override; void set_disable_gizmos(bool p_enabled); void update_gizmos(); diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 6d0380c898..954c20f713 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -184,6 +184,14 @@ Size2 Control::_edit_get_minimum_size() const { } #endif +void Control::reparent(Node *p_parent, bool p_keep_global_transform) { + Transform2D temp = get_global_transform(); + Node::reparent(p_parent); + if (p_keep_global_transform) { + set_global_position(temp.get_origin()); + } +} + // Editor integration. void Control::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { diff --git a/scene/gui/control.h b/scene/gui/control.h index db19d09b11..009b9e1c6f 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -384,6 +384,7 @@ public: virtual Size2 _edit_get_minimum_size() const override; #endif + virtual void reparent(Node *p_parent, bool p_keep_global_transform = true) override; // Editor integration. diff --git a/scene/main/node.cpp b/scene/main/node.cpp index ea9788de27..def1c299b5 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -1435,6 +1435,18 @@ TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_ty return ret; } +void Node::reparent(Node *p_parent, bool p_keep_global_transform) { + ERR_FAIL_NULL(p_parent); + ERR_FAIL_NULL_MSG(data.parent, "Node needs a parent to be reparented."); + + if (p_parent == data.parent) { + return; + } + + data.parent->remove_child(this); + p_parent->add_child(this); +} + Node *Node::get_parent() const { return data.parent; } @@ -2792,6 +2804,7 @@ void Node::_bind_methods() { ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name); ClassDB::bind_method(D_METHOD("add_child", "node", "legible_unique_name", "internal"), &Node::add_child, DEFVAL(false), DEFVAL(0)); ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child); + ClassDB::bind_method(D_METHOD("reparent", "new_parent", "keep_global_transform"), &Node::reparent, DEFVAL(true)); ClassDB::bind_method(D_METHOD("get_child_count", "include_internal"), &Node::get_child_count, DEFVAL(false)); // Note that the default value bound for include_internal is false, while the method is declared with true. This is because internal nodes are irrelevant for GDSCript. ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::_get_children, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false)); diff --git a/scene/main/node.h b/scene/main/node.h index ccd1d561d2..806fe20f89 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -317,6 +317,7 @@ public: bool has_node_and_resource(const NodePath &p_path) const; Node *get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property = true) const; + virtual void reparent(Node *p_parent, bool p_keep_global_transform = true); Node *get_parent() const; Node *find_parent(const String &p_pattern) const; |