From eb74079dfbebabcab15688495bec7aa748a5bd9c Mon Sep 17 00:00:00 2001 From: Rindbee Date: Sun, 13 Nov 2022 14:52:37 +0800 Subject: [PATCH] Update the edit scene root in time after it is replaced When changing the type of the edited scene root node, it may be necessary to update edited scene root in time for the child nodes to work properly. --- doc/classes/Node.xml | 7 +++++++ editor/editor_node.cpp | 9 ++++++--- editor/scene_tree_dock.cpp | 5 ----- scene/main/node.cpp | 4 ++++ 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index 42418b3e8dd..7d76676854e 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -845,6 +845,13 @@ Emitted when the node is renamed. + + + + Emitted when this node is being replaced by the [param node], see [method replace_by]. + This signal is emitted [i]after[/i] [param node] has been added as a child of the original parent node, but [i]before[/i] all original child nodes have been reparented to [param node]. + + Emitted when the node enters the tree. diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 3681f61cd8b..b441855fc54 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -3430,10 +3430,12 @@ void EditorNode::_remove_scene(int index, bool p_change_tab) { } void EditorNode::set_edited_scene(Node *p_scene) { - if (get_editor_data().get_edited_scene_root()) { - if (get_editor_data().get_edited_scene_root()->get_parent() == scene_root) { - scene_root->remove_child(get_editor_data().get_edited_scene_root()); + Node *old_edited_scene_root = get_editor_data().get_edited_scene_root(); + if (old_edited_scene_root) { + if (old_edited_scene_root->get_parent() == scene_root) { + scene_root->remove_child(old_edited_scene_root); } + old_edited_scene_root->disconnect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene)); } get_editor_data().set_edited_scene_root(p_scene); @@ -3449,6 +3451,7 @@ void EditorNode::set_edited_scene(Node *p_scene) { if (p_scene->get_parent() != scene_root) { scene_root->add_child(p_scene, true); } + p_scene->connect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene)); } } diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 96166dab3fe..bdb9cd75c31 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -2468,11 +2468,6 @@ void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_prop } n->replace_by(newnode, true); - if (n == edited_scene) { - edited_scene = newnode; - EditorNode::get_singleton()->set_edited_scene(newnode); - } - //small hack to make collisionshapes and other kind of nodes to work for (int i = 0; i < newnode->get_child_count(); i++) { Node *c = newnode->get_child(i); diff --git a/scene/main/node.cpp b/scene/main/node.cpp index b947526e968..8f3c8453cc6 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -2500,6 +2500,8 @@ void Node::replace_by(Node *p_node, bool p_keep_groups) { parent->move_child(p_node, index_in_parent); } + emit_signal(SNAME("replacing_by"), p_node); + while (get_child_count()) { Node *child = get_child(0); remove_child(child); @@ -3040,7 +3042,9 @@ void Node::_bind_methods() { ADD_SIGNAL(MethodInfo("tree_exited")); ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node"))); ADD_SIGNAL(MethodInfo("child_exiting_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node"))); + ADD_SIGNAL(MethodInfo("child_order_changed")); + ADD_SIGNAL(MethodInfo("replacing_by", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node"))); ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_name", "get_name"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "unique_name_in_owner", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_unique_name_in_owner", "is_unique_name_in_owner");