From 27da0deb034260e84684501c1eb501dbe361cfd0 Mon Sep 17 00:00:00 2001 From: "George L. Albany" Date: Sun, 3 Jul 2022 04:26:38 -0400 Subject: [PATCH] Expose `Node` child manipulation callbacks Corrected Control's not calling base child_notify methods Corrected Window not calling base child_notify methods --- doc/classes/Node.xml | 22 ++++++++++++++++++++++ scene/main/node.cpp | 12 +++++++++--- scene/main/node.h | 4 ++++ scene/main/window.cpp | 4 ++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index d9732da3a3f..eb39a778a59 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -22,6 +22,14 @@ https://github.com/godotengine/godot-demo-projects/ + + + + + Called after a node is added as a child. + [b]Note:[/b] This method is called on application start for every node already in the tree as well. + + @@ -55,6 +63,13 @@ [b]Note:[/b] This method is only called if the node is present in the scene tree (i.e. if it's not an orphan). + + + + + Called after a child is moved within the node. + + @@ -84,6 +99,13 @@ [b]Note:[/b] [method _ready] may be called only once for each node. After removing a node from the scene tree and adding it again, [code]_ready[/code] will not be called a second time. This can be bypassed by requesting another call with [method request_ready], which may be called anywhere before adding the node again. + + + + + Called when a child is removed within the node. + + diff --git a/scene/main/node.cpp b/scene/main/node.cpp index a2b0f1a8250..e283379c530 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -411,15 +411,18 @@ void Node::_propagate_groups_dirty() { } void Node::add_child_notify(Node *p_child) { - // to be used when not wanted + // Called when a node is added as a child to this one. + GDVIRTUAL_CALL(_add_child_notify, p_child); } void Node::remove_child_notify(Node *p_child) { - // to be used when not wanted + // Called when a child is removed from this node. + GDVIRTUAL_CALL(_remove_child_notify, p_child); } void Node::move_child_notify(Node *p_child) { - // to be used when not wanted + // Called when a child is moved inside this node. + GDVIRTUAL_CALL(_move_child_notify, p_child); } void Node::owner_changed_notify() { @@ -2956,6 +2959,9 @@ void Node::_bind_methods() { GDVIRTUAL_BIND(_shortcut_input, "event"); GDVIRTUAL_BIND(_unhandled_input, "event"); GDVIRTUAL_BIND(_unhandled_key_input, "event"); + GDVIRTUAL_BIND(_add_child_notify, "child"); + GDVIRTUAL_BIND(_remove_child_notify, "child"); + GDVIRTUAL_BIND(_move_child_notify, "child"); } String Node::_get_name_num_separator() { diff --git a/scene/main/node.h b/scene/main/node.h index 4e6530cccd3..0dcc54f3d3e 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -246,6 +246,10 @@ protected: GDVIRTUAL1(_unhandled_input, Ref) GDVIRTUAL1(_unhandled_key_input, Ref) + GDVIRTUAL1(_add_child_notify, Node *) + GDVIRTUAL1(_remove_child_notify, Node *) + GDVIRTUAL1(_move_child_notify, Node *) + public: enum { // you can make your own, but don't use the same numbers as other notifications in other nodes diff --git a/scene/main/window.cpp b/scene/main/window.cpp index ebe9587b31d..3ef3de0238d 100644 --- a/scene/main/window.cpp +++ b/scene/main/window.cpp @@ -1284,12 +1284,16 @@ Rect2i Window::get_usable_parent_rect() const { } void Window::add_child_notify(Node *p_child) { + Viewport::add_child_notify(p_child); + if (is_inside_tree() && wrap_controls) { child_controls_changed(); } } void Window::remove_child_notify(Node *p_child) { + Viewport::remove_child_notify(p_child); + if (is_inside_tree() && wrap_controls) { child_controls_changed(); }