Merge pull request #90566 from Rindbee/make-the-load-scene-open-in-current-scene-tab-if-it-is-empty

Make the loading scene open in the current scene tab if the current scene is empty
This commit is contained in:
Rémi Verschelde 2024-04-17 10:58:39 +02:00
commit 517d7cfd52
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 61 additions and 38 deletions

View file

@ -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;
}

View file

@ -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<Resource> res = ResourceLoader::load(path);
if (res.is_null()) {
return;
}
Ref<PackedScene> 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<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*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<Texture2D> texture = Ref<Texture2D>(Object::cast_to<Texture2D>(*res));
if (texture != nullptr && texture.is_valid()) {
Node *child = Object::cast_to<Node>(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<PackedScene> 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<Texture2D> texture = res;
if (texture.is_valid()) {
Node *child = Object::cast_to<Node>(ClassDB::instantiate(default_texture_node_type));
_create_nodes(target_node, child, path, drop_pos);
undo_redo->add_do_method(editor_selection, "add_node", child);
}
}