Godot Engine – Multi-platform 2D and 3D game engine
Implement adding child nodes for each node selected in the Scene tree dock
Dexter35fe8b1c5e64
diff --git a/editor/docks/scene_tree_dock.cpp b/editor/docks/scene_tree_dock.cppindex 143ca73ac5e..e19c645cb0e 100644--- a/editor/docks/scene_tree_dock.cpp+++ b/editor/docks/scene_tree_dock.cpp@@ -84,7 +84,10 @@ void SceneTreeDock::_nodes_drag_begin() {}void SceneTreeDock::_quick_open(const String &p_file_path) {- instantiate_scenes({ p_file_path }, scene_tree->get_selected());+ List<Node *> selection = editor_selection->get_top_selected_node_list();+ for (Node *node : selection) {+ instantiate_scenes({ p_file_path }, node);+ }}static void _restore_treeitem_custom_color(TreeItem *p_item) {@@ -404,7 +407,7 @@ void SceneTreeDock::_perform_instantiate_scenes(const Vector<String> &p_files, N}EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();- undo_redo->create_action_for_history(TTRN("Instantiate Scene", "Instantiate Scenes", instances.size()), editor_data->get_current_edited_scene_history_id());+ undo_redo->create_action_for_history(TTRN("Instantiate Scene", "Instantiate Scenes", instances.size()), editor_data->get_current_edited_scene_history_id(), UndoRedo::MERGE_ALL);undo_redo->add_do_method(editor_selection, "clear");for (int i = 0; i < instances.size(); i++) {@@ -3158,6 +3161,58 @@ Node *SceneTreeDock::_do_create(Node *p_parent) {return child;}+Node *SceneTreeDock::_do_create_multi(Ref<MultiNodeEdit> p_parent) {+ Node *last_created = nullptr;+ Ref<MultiNodeEdit> mne = memnew(MultiNodeEdit);+ EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();+ undo_redo->create_action_for_history(TTR("Create Node(s)"), editor_data->get_current_edited_scene_history_id());++ for (int i = 0; i < p_parent->get_node_count(); i++) {+ Node *parent = edited_scene->get_node(p_parent->get_node(i));+ Variant c = create_dialog->instantiate_selected();+ Node *child = Object::cast_to<Node>(c);+ ERR_FAIL_NULL_V(child, nullptr);++ String new_name = parent->validate_child_name(child);+ if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {+ new_name = adjust_name_casing(new_name);+ }+ child->set_name(new_name);++ if (edited_scene) {+ parent->add_child(child, true);+ child->set_owner(edited_scene);+ undo_redo->add_do_method(parent, "add_child", child, true);+ undo_redo->add_do_method(child, "set_owner", edited_scene);+ undo_redo->add_do_reference(child);+ undo_redo->add_undo_method(parent, "remove_child", child);++ EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();+ ed->live_debug_create_node(edited_scene->get_path_to(parent), child->get_class(), new_name);++ undo_redo->add_do_method(ed, "live_debug_create_node", edited_scene->get_path_to(parent), child->get_class(), new_name);+ undo_redo->add_undo_method(ed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).path_join(new_name)));++ } else {+ EditorNode::get_singleton()->set_edited_scene(child);+ scene_tree->update_tree();++ undo_redo->add_do_method(EditorNode::get_singleton(), "set_edited_scene", child);+ undo_redo->add_do_method(scene_tree, "update_tree");+ undo_redo->add_do_reference(child);+ undo_redo->add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);+ }+ mne->add_node(edited_scene->get_path_to(child));+ last_created = child;+ }+ _post_do_create_multi(mne);++ undo_redo->add_do_method(this, "_post_do_create_multi", mne);+ undo_redo->commit_action(false);++ return last_created;+}+void SceneTreeDock::_post_do_create(Node *p_child) {editor_selection->clear();editor_selection->add_node(p_child);@@ -3180,25 +3235,59 @@ void SceneTreeDock::_post_do_create(Node *p_child) {}}+void SceneTreeDock::_post_do_create_multi(Ref<MultiNodeEdit> p_child) {+ editor_selection->clear();+ for (int i = 0; i < p_child->get_node_count(); i++) {+ Node *child = edited_scene->get_node(p_child->get_node(i));+ editor_selection->add_node(child);++ // Make editor more comfortable, so some controls don't appear super shrunk.+ Control *control = Object::cast_to<Control>(child);+ if (control) {+ Size2 ms = control->get_minimum_size();+ if (ms.width < 4) {+ ms.width = 40;+ }+ if (ms.height < 4) {+ ms.height = 40;+ }+ if (control->is_layout_rtl()) {+ control->set_position(control->get_position() - Vector2(ms.x, 0));+ }+ control->set_size(ms);+ }+ }+ _push_item(p_child.ptr());++ emit_signal(SNAME("node_created"), p_child);+}+void SceneTreeDock::_create() {if (current_option == TOOL_NEW) {- Node *parent = nullptr;-if (edited_scene) {+ Ref<MultiNodeEdit> mne = memnew(MultiNodeEdit);// If root exists in edited scene- parent = scene_tree->get_selected();- if (!parent) {- parent = edited_scene;+ const HashMap<ObjectID, Object *> &selection = editor_selection->get_selection();+ if (selection.size() > 1) {+ for (const KeyValue<ObjectID, Object *> &E : selection) {+ if (Node *node = ObjectDB::get_instance<Node>(E.key)) {+ mne->add_node(edited_scene->get_path_to(node));+ }+ }+ _do_create_multi(mne);+ } else {+ Node *parent = selection.is_empty()+ ? edited_scene+ : ObjectDB::get_instance<Node>(selection.begin()->key);+ _do_create(parent);}-} else {// If no root exist in edited scene- parent = scene_root;+ Node *parent = scene_root;ERR_FAIL_NULL(parent);+ _do_create(parent);}- _do_create(parent);-} else if (current_option == TOOL_CHANGE_TYPE) {const List<Node *> full_selection = editor_selection->get_full_selected_node_list();ERR_FAIL_COND(full_selection.is_empty());@@ -3983,7 +4072,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {Ref<Script> existing_script;bool existing_script_removable = true;bool allow_attach_new_script = true;- if (selection.size() == 1) {+ if (selection.size() > 0) {BEGIN_SECTION()Node *selected = selection.front()->get();@@ -3991,9 +4080,11 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {subresources.clear();menu_subresources->clear();menu_subresources->reset_size();- _add_children_to_popup(selected, 0);- if (menu->get_item_count() > 0) {- menu->add_separator();+ if (selection.size() == 1) {+ _add_children_to_popup(selected, 0);+ if (menu->get_item_count() > 0) {+ menu->add_separator();+ }}menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Add")), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);@@ -4001,7 +4092,9 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {}menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Collapse")), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE);- existing_script = selected->get_script();+ if (selection.size() == 1) {+ existing_script = selected->get_script();+ }if (EditorNode::get_singleton()->get_object_custom_type_base(selected) == existing_script) {existing_script_removable = false;@@ -5034,6 +5127,7 @@ void SceneTreeDock::_edit_subresource(int p_idx, const PopupMenu *p_from_menu) {void SceneTreeDock::_bind_methods() {ClassDB::bind_method(D_METHOD("_post_do_create"), &SceneTreeDock::_post_do_create);+ ClassDB::bind_method(D_METHOD("_post_do_create_multi"), &SceneTreeDock::_post_do_create_multi);ClassDB::bind_method(D_METHOD("_apply_owners_map"), &SceneTreeDock::_apply_owners_map);ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);ClassDB::bind_method(D_METHOD("_reparent_nodes_to_root"), &SceneTreeDock::_reparent_nodes_to_root);diff --git a/editor/docks/scene_tree_dock.h b/editor/docks/scene_tree_dock.hindex 26194021d80..4782cd8ef4a 100644--- a/editor/docks/scene_tree_dock.h+++ b/editor/docks/scene_tree_dock.h@@ -48,6 +48,7 @@ class ShaderMaterial;class StyleBoxFlat;class TextureRect;class VBoxContainer;+class MultiNodeEdit;class SceneTreeDock : public EditorDock {GDCLASS(SceneTreeDock, EditorDock);@@ -182,7 +183,9 @@ class SceneTreeDock : public EditorDock {void _create();Node *_do_create(Node *p_parent);+ Node *_do_create_multi(Ref<MultiNodeEdit> p_parent);void _post_do_create(Node *p_child);+ void _post_do_create_multi(Ref<MultiNodeEdit> p_child);Node *scene_root = nullptr;Node *edited_scene = nullptr;Node *pending_click_select = nullptr;