From b5157e06860e2ca9dd7d68dad8bccd6a015edd59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E9=9D=92=E5=B1=B1?= Date: Fri, 12 Apr 2024 14:48:29 +0800 Subject: [PATCH] Make the loading scene open in the current scene tab if the current scene is empty --- editor/editor_node.cpp | 34 ++++++---- editor/plugins/canvas_item_editor_plugin.cpp | 65 ++++++++++++-------- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index e5ef8883700..d6cbc895d5c 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -3926,14 +3926,18 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b } int prev = editor_data.get_edited_scene(); - int idx = editor_data.add_edited_scene(-1); + int idx = prev; - if (!editor_data.get_edited_scene_root() && editor_data.get_edited_scene_count() == 2) { - _remove_edited_scene(); - } else if (p_silent_change_tab) { - _set_current_scene_nocheck(idx); + if (prev == -1 || editor_data.get_edited_scene_root() || !editor_data.get_scene_path(prev).is_empty()) { + idx = editor_data.add_edited_scene(-1); + + if (p_silent_change_tab) { + _set_current_scene_nocheck(idx); + } else { + _set_current_scene(idx); + } } else { - _set_current_scene(idx); + EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id()); } dependency_errors.clear(); @@ -3950,7 +3954,7 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b dependency_error->show(DependencyErrorDialog::MODE_SCENE, lpath, errors); opening_prev = false; - if (prev != -1) { + if (prev != -1 && prev != idx) { _set_current_scene(prev); editor_data.remove_scene(idx); } @@ -3961,7 +3965,7 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b _dialog_display_load_error(lpath, err); opening_prev = false; - if (prev != -1) { + if (prev != -1 && prev != idx) { _set_current_scene(prev); editor_data.remove_scene(idx); } @@ -3997,7 +4001,7 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b sdata.unref(); _dialog_display_load_error(lpath, ERR_FILE_CORRUPT); opening_prev = false; - if (prev != -1) { + if (prev != -1 && prev != idx) { _set_current_scene(prev); editor_data.remove_scene(idx); } @@ -4023,10 +4027,6 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b _load_editor_plugin_states_from_config(editor_state_cf); } - _update_title(); - scene_tabs->update_scene_tabs(); - _add_to_recent_scenes(lpath); - if (editor_folding.has_folding_data(lpath)) { editor_folding.load_scene_folding(new_scene, lpath); } else if (EDITOR_GET("interface/inspector/auto_unfold_foreign_scenes")) { @@ -4066,6 +4066,14 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b save_editor_layout_delayed(); } + if (p_set_inherited) { + EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(editor_data.get_current_edited_scene_history_id()); + } + + _update_title(); + scene_tabs->update_scene_tabs(); + _add_to_recent_scenes(lpath); + return OK; } diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 8d24e90ccef..8a9118a03ed 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -5860,13 +5860,34 @@ bool CanvasItemEditorViewport::_create_instance(Node *parent, String &path, cons } void CanvasItemEditorViewport::_perform_drop_data() { + ERR_FAIL_COND(selected_files.size() <= 0); + _remove_preview(); - // Without root dropping multiple files is not allowed - if (!target_node && selected_files.size() > 1) { - accept->set_text(TTR("Cannot instantiate multiple nodes without root.")); - accept->popup_centered(); - return; + if (!target_node) { + // Without root dropping multiple files is not allowed + if (selected_files.size() > 1) { + accept->set_text(TTR("Cannot instantiate multiple nodes without root.")); + accept->popup_centered(); + return; + } + + const String &path = selected_files[0]; + Ref res = ResourceLoader::load(path); + if (res.is_null()) { + return; + } + + Ref scene = res; + if (scene.is_valid()) { + // Without root node act the same as "Load Inherited Scene". + Error err = EditorNode::get_singleton()->load_scene(path, false, true); + if (err != OK) { + accept->set_text(vformat(TTR("Error instantiating scene from %s."), path.get_file())); + accept->popup_centered(); + } + return; + } } PackedStringArray error_files; @@ -5882,27 +5903,21 @@ void CanvasItemEditorViewport::_perform_drop_data() { if (res.is_null()) { continue; } - Ref scene = Ref(Object::cast_to(*res)); - if (scene != nullptr && scene.is_valid()) { - if (!target_node) { - // Without root node act the same as "Load Inherited Scene" - Error err = EditorNode::get_singleton()->load_scene(path, false, true); - if (err != OK) { - error_files.push_back(path.get_file()); - } - } else { - bool success = _create_instance(target_node, path, drop_pos); - if (!success) { - error_files.push_back(path.get_file()); - } - } - } else { - Ref texture = Ref(Object::cast_to(*res)); - if (texture != nullptr && texture.is_valid()) { - Node *child = Object::cast_to(ClassDB::instantiate(default_texture_node_type)); - _create_nodes(target_node, child, path, drop_pos); - undo_redo->add_do_method(editor_selection, "add_node", child); + + Ref scene = res; + if (scene.is_valid()) { + bool success = _create_instance(target_node, path, drop_pos); + if (!success) { + error_files.push_back(path.get_file()); } + continue; + } + + Ref texture = res; + if (texture.is_valid()) { + Node *child = Object::cast_to(ClassDB::instantiate(default_texture_node_type)); + _create_nodes(target_node, child, path, drop_pos); + undo_redo->add_do_method(editor_selection, "add_node", child); } }