Merge pull request #100877 from larspet/load-scene-or-resource

EditorNode: Add function to load file as either scene or resource
This commit is contained in:
Thaddeus Crews 2025-03-13 08:57:16 -05:00
commit ad50c26273
No known key found for this signature in database
GPG Key ID: 62181B86FE9E5D84
16 changed files with 57 additions and 142 deletions

View File

@ -317,12 +317,8 @@ void DependencyEditorOwners::_list_rmb_clicked(int p_item, const Vector2 &p_pos,
void DependencyEditorOwners::_select_file(int p_idx) {
String fpath = owners->get_item_text(p_idx);
EditorNode::get_singleton()->load_scene_or_resource(fpath);
if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
EditorNode::get_singleton()->open_request(fpath);
} else {
EditorNode::get_singleton()->load_resource(fpath);
}
hide();
emit_signal(SceneStringName(confirmed));
}
@ -713,8 +709,7 @@ DependencyRemoveDialog::DependencyRemoveDialog() {
//////////////
void DependencyErrorDialog::show(Mode p_mode, const String &p_for_file, const Vector<String> &report) {
mode = p_mode;
void DependencyErrorDialog::show(const String &p_for_file, const Vector<String> &report) {
for_file = p_for_file;
set_title(TTR("Error loading:") + " " + p_for_file.get_file());
files->clear();
@ -739,14 +734,7 @@ void DependencyErrorDialog::show(Mode p_mode, const String &p_for_file, const Ve
}
void DependencyErrorDialog::ok_pressed() {
switch (mode) {
case MODE_SCENE:
EditorNode::get_singleton()->load_scene(for_file, true);
break;
case MODE_RESOURCE:
EditorNode::get_singleton()->load_resource(for_file, true);
break;
}
EditorNode::get_singleton()->load_scene_or_resource(for_file, true);
}
void DependencyErrorDialog::custom_action(const String &) {
@ -771,8 +759,6 @@ DependencyErrorDialog::DependencyErrorDialog() {
vb->add_child(text);
text->set_text(TTR("Which action should be taken?"));
mode = Mode::MODE_RESOURCE;
fdep = add_button(TTR("Fix Dependencies"), true, "fixdeps");
set_title(TTR("Errors loading!"));

View File

@ -139,12 +139,6 @@ public:
class DependencyErrorDialog : public ConfirmationDialog {
GDCLASS(DependencyErrorDialog, ConfirmationDialog);
public:
enum Mode {
MODE_SCENE,
MODE_RESOURCE,
};
private:
String for_file;
Mode mode;
@ -155,7 +149,7 @@ private:
void custom_action(const String &) override;
public:
void show(Mode p_mode, const String &p_for_file, const Vector<String> &report);
void show(const String &p_for_file, const Vector<String> &report);
DependencyErrorDialog();
};

View File

@ -349,11 +349,7 @@ void EditorAutoloadSettings::_autoload_activated() {
}
void EditorAutoloadSettings::_autoload_open(const String &fpath) {
if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
EditorNode::get_singleton()->open_request(fpath);
} else {
EditorNode::get_singleton()->load_resource(fpath);
}
EditorNode::get_singleton()->load_scene_or_resource(fpath);
ProjectSettingsEditor::get_singleton()->hide();
}

View File

@ -3989,11 +3989,7 @@ void EditorHelpBit::_meta_clicked(const String &p_select) {
String path = ProjectSettings::get_singleton()->globalize_path(p_select.trim_prefix("open-file:"));
OS::get_singleton()->shell_show_in_file_manager(path, true);
} else if (p_select.begins_with("open-res:")) {
if (help_data.doc_type.type == "PackedScene") {
EditorNode::get_singleton()->load_scene(p_select.trim_prefix("open-res:"));
} else {
EditorNode::get_singleton()->load_resource(p_select.trim_prefix("open-res:"));
}
EditorNode::get_singleton()->load_scene_or_resource(p_select.trim_prefix("open-res:"));
} else if (p_select.begins_with("show:")) {
FileSystemDock::get_singleton()->navigate_to_path(p_select.trim_prefix("show:"));
} else if (p_select.begins_with("http:") || p_select.begins_with("https:")) {

View File

@ -647,8 +647,7 @@ void EditorInterface::open_scene_from_path(const String &scene_path, bool p_set_
if (EditorNode::get_singleton()->is_changing_scene()) {
return;
}
EditorNode::get_singleton()->open_request(scene_path, p_set_inherited);
EditorNode::get_singleton()->load_scene(scene_path, false, p_set_inherited);
}
void EditorInterface::reload_scene_from_path(const String &scene_path) {

View File

@ -609,10 +609,6 @@ void EditorNode::_notification(int p_what) {
} break;
case NOTIFICATION_PROCESS: {
if (opening_prev && !confirmation->is_visible()) {
opening_prev = false;
}
bool global_unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(EditorUndoRedoManager::GLOBAL_HISTORY);
bool scene_or_global_unsaved = global_unsaved || EditorUndoRedoManager::get_singleton()->is_history_unsaved(editor_data.get_current_edited_scene_history_id());
if (unsaved_cache != scene_or_global_unsaved) {
@ -1343,7 +1339,7 @@ Error EditorNode::load_resource(const String &p_resource, bool p_ignore_broken_d
for (const String &E : dependency_errors[p_resource]) {
errors.push_back(E);
}
dependency_error->show(DependencyErrorDialog::MODE_RESOURCE, p_resource, errors);
dependency_error->show(p_resource, errors);
dependency_errors.erase(p_resource);
return ERR_FILE_MISSING_DEPENDENCIES;
@ -1353,6 +1349,16 @@ Error EditorNode::load_resource(const String &p_resource, bool p_ignore_broken_d
return OK;
}
Error EditorNode::load_scene_or_resource(const String &p_path, bool p_ignore_broken_deps, bool p_change_scene_tab_if_already_open) {
if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(p_path), "PackedScene")) {
if (!p_change_scene_tab_if_already_open && EditorNode::get_singleton()->is_scene_open(p_path)) {
return OK;
}
return EditorNode::get_singleton()->load_scene(p_path, p_ignore_broken_deps);
}
return EditorNode::get_singleton()->load_resource(p_path, p_ignore_broken_deps);
}
void EditorNode::edit_node(Node *p_node) {
push_item(p_node);
}
@ -2796,13 +2802,9 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
quick_open_dialog->popup_dialog({ "Script" }, callable_mp(this, &EditorNode::_quick_opened));
} break;
case FILE_OPEN_PREV: {
if (previous_scenes.is_empty()) {
break;
if (!prev_closed_scenes.is_empty()) {
load_scene(prev_closed_scenes.back()->get());
}
opening_prev = true;
open_request(previous_scenes.back()->get());
previous_scenes.pop_back();
} break;
case EditorSceneTabs::SCENE_CLOSE_OTHERS: {
tab_closing_menu_option = -1;
@ -3458,10 +3460,7 @@ void EditorNode::_discard_changes(const String &p_str) {
case SCENE_TAB_CLOSE: {
Node *scene = editor_data.get_edited_scene_root(tab_closing_idx);
if (scene != nullptr) {
String scene_filename = scene->get_scene_file_path();
if (!scene_filename.is_empty()) {
previous_scenes.push_back(scene_filename);
}
_update_prev_closed_scenes(scene->get_scene_file_path(), true);
}
// Don't close tabs when exiting the editor (required for "restore_scenes_on_load" setting).
@ -3519,14 +3518,9 @@ void EditorNode::_update_file_menu_opened() {
file_menu->set_item_disabled(file_menu->get_item_index(FILE_SAVE_ALL_SCENES), true);
file_menu->set_item_tooltip(file_menu->get_item_index(FILE_SAVE_ALL_SCENES), TTR("All scenes are already saved."));
}
file_menu->set_item_disabled(file_menu->get_item_index(FILE_OPEN_PREV), previous_scenes.is_empty());
_update_undo_redo_allowed();
}
void EditorNode::_update_file_menu_closed() {
file_menu->set_item_disabled(file_menu->get_item_index(FILE_OPEN_PREV), false);
}
void EditorNode::_palette_quick_open_dialog() {
quick_open_color_palette->popup_dialog({ "ColorPalette" }, palette_file_selected_callback);
quick_open_color_palette->set_title(TTR("Quick Open Color Palette..."));
@ -4044,6 +4038,8 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
}
String lpath = ProjectSettings::get_singleton()->localize_path(ResourceUID::ensure_path(p_scene));
_update_prev_closed_scenes(lpath, false);
if (!p_set_inherited) {
for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
if (editor_data.get_scene_path(i) == lpath) {
@ -4063,7 +4059,6 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
if (!lpath.begins_with("res://")) {
show_accept(TTR("Error loading scene, it must be inside the project path. Use 'Import' to open the scene, then save it inside the project path."), TTR("OK"));
opening_prev = false;
return ERR_FILE_NOT_FOUND;
}
@ -4093,8 +4088,7 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
for (const String &E : dependency_errors[lpath]) {
errors.push_back(E);
}
dependency_error->show(DependencyErrorDialog::MODE_SCENE, lpath, errors);
opening_prev = false;
dependency_error->show(lpath, errors);
if (prev != -1 && prev != idx) {
_set_current_scene(prev);
@ -4105,7 +4099,6 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
if (sdata.is_null()) {
_dialog_display_load_error(lpath, err);
opening_prev = false;
if (prev != -1 && prev != idx) {
_set_current_scene(prev);
@ -4138,11 +4131,9 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
}
Node *new_scene = sdata->instantiate(p_set_inherited ? PackedScene::GEN_EDIT_STATE_MAIN_INHERITED : PackedScene::GEN_EDIT_STATE_MAIN);
if (!new_scene) {
sdata.unref();
_dialog_display_load_error(lpath, ERR_FILE_CORRUPT);
opening_prev = false;
if (prev != -1 && prev != idx) {
_set_current_scene(prev);
editor_data.remove_scene(idx);
@ -4176,8 +4167,6 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
editor_folding.save_scene_folding(new_scene, lpath);
}
opening_prev = false;
EditorDebuggerNode::get_singleton()->update_live_edit_root();
if (restoring_scenes) {
@ -4558,19 +4547,8 @@ void EditorNode::replace_history_reimported_nodes(Node *p_original_root_node, No
}
}
void EditorNode::open_request(const String &p_path, bool p_set_inherited) {
if (!opening_prev) {
List<String>::Element *prev_scene_item = previous_scenes.find(p_path);
if (prev_scene_item != nullptr) {
prev_scene_item->erase();
}
}
load_scene(p_path, false, p_set_inherited); // As it will be opened in separate tab.
}
bool EditorNode::has_previous_scenes() const {
return !previous_scenes.is_empty();
bool EditorNode::has_previous_closed_scenes() const {
return !prev_closed_scenes.is_empty();
}
void EditorNode::edit_foreign_resource(Ref<Resource> p_resource) {
@ -4651,6 +4629,17 @@ void EditorNode::_show_messages() {
center_split->set_split_offset(old_split_ofs);
}
void EditorNode::_update_prev_closed_scenes(const String &p_scene_path, bool p_add_scene) {
if (!p_scene_path.is_empty()) {
if (p_add_scene) {
prev_closed_scenes.push_back(p_scene_path);
} else {
prev_closed_scenes.erase(p_scene_path);
}
file_menu->set_item_disabled(file_menu->get_item_index(FILE_OPEN_PREV), prev_closed_scenes.is_empty());
}
}
void EditorNode::_add_to_recent_scenes(const String &p_scene) {
Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
if (rc.has(p_scene)) {
@ -4697,11 +4686,7 @@ void EditorNode::_update_recent_scenes() {
}
void EditorNode::_quick_opened(const String &p_file_path) {
if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(p_file_path), "PackedScene")) {
open_request(p_file_path);
} else {
load_resource(p_file_path);
}
load_scene_or_resource(p_file_path);
}
void EditorNode::_project_run_started() {
@ -7896,7 +7881,6 @@ EditorNode::EditorNode() {
file_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorNode::_menu_option));
file_menu->connect("about_to_popup", callable_mp(this, &EditorNode::_update_file_menu_opened));
file_menu->connect("popup_hide", callable_mp(this, &EditorNode::_update_file_menu_closed));
settings_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorNode::_menu_option));

View File

@ -345,7 +345,7 @@ private:
PopupMenu *recent_scenes = nullptr;
String _recent_scene;
List<String> previous_scenes;
List<String> prev_closed_scenes;
String defer_load_scene;
Node *_last_instantiated_scene = nullptr;
@ -416,7 +416,6 @@ private:
bool cmdline_mode = false;
bool convert_old = false;
bool immediate_dialog_confirmed = false;
bool opening_prev = false;
bool restoring_scenes = false;
bool unsaved_cache = true;
@ -523,7 +522,6 @@ private:
void _tool_menu_option(int p_idx);
void _export_as_menu_option(int p_idx);
void _update_file_menu_opened();
void _update_file_menu_closed();
void _palette_quick_open_dialog();
void _remove_plugin_from_enabled(const String &p_name);
@ -573,9 +571,12 @@ private:
void _project_run_started();
void _project_run_stopped();
void _update_prev_closed_scenes(const String &p_scene_path, bool p_add_scene);
void _add_to_recent_scenes(const String &p_scene);
void _update_recent_scenes();
void _open_recent_scene(int p_idx);
void _dropped_files(const Vector<String> &p_files);
void _add_dropped_files_recursive(const Vector<String> &p_files, String to_path);
@ -731,7 +732,7 @@ public:
ProjectSettingsEditor *get_project_settings() { return project_settings_editor; }
void trigger_menu_option(int p_option, bool p_confirmed);
bool has_previous_scenes() const;
bool has_previous_closed_scenes() const;
void new_inherited_scene() { _menu_option_confirm(FILE_NEW_INHERITED_SCENE, false); }
@ -765,7 +766,6 @@ public:
void replace_resources_in_scenes(
const Vector<Ref<Resource>> &p_source_resources,
const Vector<Ref<Resource>> &p_target_resource);
void open_request(const String &p_path, bool p_set_inherited = false);
void edit_foreign_resource(Ref<Resource> p_resource);
bool is_resource_read_only(Ref<Resource> p_resource, bool p_foreign_resources_are_writable = false);
@ -784,6 +784,7 @@ public:
int new_scene();
Error load_scene(const String &p_scene, bool p_ignore_broken_deps = false, bool p_set_inherited = false, bool p_force_open_imported = false, bool p_silent_change_tab = false);
Error load_resource(const String &p_resource, bool p_ignore_broken_deps = false);
Error load_scene_or_resource(const String &p_file, bool p_ignore_broken_deps = false, bool p_change_scene_tab_if_already_open = true);
HashMap<StringName, Variant> get_modified_properties_for_node(Node *p_node, bool p_node_references_only);
HashMap<StringName, Variant> get_modified_properties_reference_to_nodes(Node *p_node, List<Node *> &p_nodes_referenced_by);

View File

@ -1232,10 +1232,8 @@ void FileSystemDock::_select_file(const String &p_path, bool p_select_in_favorit
}
String resource_type = ResourceLoader::get_resource_type(fpath);
if (resource_type == "PackedScene" || resource_type == "AnimationLibrary") {
bool is_imported = false;
{
List<String> importer_exts;
ResourceImporterScene::get_scene_importer_extensions(&importer_exts);
@ -1250,10 +1248,8 @@ void FileSystemDock::_select_file(const String &p_path, bool p_select_in_favorit
if (is_imported) {
SceneImportSettingsDialog::get_singleton()->open_settings(p_path, resource_type);
} else if (resource_type == "PackedScene") {
EditorNode::get_singleton()->open_request(fpath);
} else {
EditorNode::get_singleton()->load_resource(fpath);
EditorNode::get_singleton()->load_scene_or_resource(fpath);
}
} else if (ResourceLoader::is_imported(fpath)) {
// If the importer has advanced settings, show them.
@ -1273,7 +1269,6 @@ void FileSystemDock::_select_file(const String &p_path, bool p_select_in_favorit
if (!used_advanced_settings) {
EditorNode::get_singleton()->load_resource(fpath);
}
} else {
EditorNode::get_singleton()->load_resource(fpath);
}

View File

@ -203,7 +203,7 @@ void EditorSceneTabs::_update_context_menu() {
scene_tabs_context_menu->set_item_text(-1, TTR("Close Tab"));
scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/reopen_closed_scene"), EditorNode::FILE_OPEN_PREV);
scene_tabs_context_menu->set_item_text(-1, TTR("Undo Close Tab"));
DISABLE_LAST_OPTION_IF(!EditorNode::get_singleton()->has_previous_scenes());
DISABLE_LAST_OPTION_IF(!EditorNode::get_singleton()->has_previous_closed_scenes());
scene_tabs_context_menu->add_item(TTR("Close Other Tabs"), SCENE_CLOSE_OTHERS);
DISABLE_LAST_OPTION_IF(EditorNode::get_editor_data().get_edited_scene_count() <= 1);
scene_tabs_context_menu->add_item(TTR("Close Tabs to the Right"), SCENE_CLOSE_RIGHT);

View File

@ -1597,7 +1597,7 @@ bool CanvasItemEditor::_gui_input_open_scene_on_double_click(const Ref<InputEven
if (selection.size() == 1) {
CanvasItem *ci = selection.front()->get();
if (!ci->get_scene_file_path().is_empty() && ci != EditorNode::get_singleton()->get_edited_scene()) {
EditorNode::get_singleton()->open_request(ci->get_scene_file_path());
EditorNode::get_singleton()->load_scene(ci->get_scene_file_path());
return true;
}
}

View File

@ -36,7 +36,7 @@
void PackedSceneEditor::_on_open_scene_pressed() {
// Using deferred call because changing scene updates the Inspector and thus destroys this plugin.
callable_mp(EditorNode::get_singleton(), &EditorNode::open_request).call_deferred(packed_scene->get_path(), false);
callable_mp(EditorNode::get_singleton(), &EditorNode::load_scene).call_deferred(packed_scene->get_path(), false, false, false, false);
}
void PackedSceneEditor::_notification(int p_what) {

View File

@ -865,13 +865,8 @@ void ScriptEditor::_open_recent_script(int p_idx) {
} else if (path.contains("::")) {
// built-in script
String res_path = path.get_slice("::", 0);
if (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
EditorNode::get_singleton()->load_scene(res_path);
}
} else {
EditorNode::get_singleton()->load_resource(res_path);
}
EditorNode::get_singleton()->load_scene_or_resource(res_path, false, false);
Ref<Script> scr = ResourceLoader::load(path);
if (scr.is_valid()) {
edit(scr, true);
@ -1376,13 +1371,7 @@ void ScriptEditor::_menu_option(int p_option) {
if (extensions.find(path.get_extension()) || built_in) {
if (built_in) {
String res_path = path.get_slice("::", 0);
if (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
EditorNode::get_singleton()->load_scene(res_path);
}
} else {
EditorNode::get_singleton()->load_resource(res_path);
}
EditorNode::get_singleton()->load_scene_or_resource(res_path, false, false);
}
Ref<Resource> scr = ResourceLoader::load(path);
@ -4559,13 +4548,7 @@ void ScriptEditorPlugin::edit(Object *p_object) {
String res_path = p_script->get_path().get_slice("::", 0);
if (p_script->is_built_in() && !res_path.is_empty()) {
if (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
EditorNode::get_singleton()->load_scene(res_path);
}
} else {
EditorNode::get_singleton()->load_resource(res_path);
}
EditorNode::get_singleton()->load_scene_or_resource(res_path, false, false);
}
script_editor->edit(p_script);
} else if (Object::cast_to<JSON>(p_object)) {

View File

@ -975,19 +975,7 @@ void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_c
if (ScriptServer::is_global_class(p_symbol)) {
EditorNode::get_singleton()->load_resource(ScriptServer::get_global_class_path(p_symbol));
} else if (p_symbol.is_resource_file() || p_symbol.begins_with("uid://")) {
String symbol = p_symbol;
if (symbol.begins_with("uid://")) {
symbol = ResourceUID::uid_to_path(symbol);
}
List<String> scene_extensions;
ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
if (scene_extensions.find(symbol.get_extension())) {
EditorNode::get_singleton()->load_scene(symbol);
} else {
EditorNode::get_singleton()->load_resource(symbol);
}
EditorNode::get_singleton()->load_scene_or_resource(p_symbol);
} else if (lc_error == OK) {
_goto_line(p_row);
@ -1082,14 +1070,7 @@ void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_c
// Every symbol other than absolute path is relative path so keep this condition at last.
String path = _get_absolute_path(p_symbol);
if (FileAccess::exists(path)) {
List<String> scene_extensions;
ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
if (scene_extensions.find(path.get_extension())) {
EditorNode::get_singleton()->load_scene(path);
} else {
EditorNode::get_singleton()->load_resource(path);
}
EditorNode::get_singleton()->load_scene_or_resource(path);
}
}
}

View File

@ -233,7 +233,7 @@ void TileSetScenesCollectionSourceEditor::_scene_thumbnail_done(const String &p_
void TileSetScenesCollectionSourceEditor::_scenes_list_item_activated(int p_index) {
Ref<PackedScene> packed_scene = tile_set_scenes_collection_source->get_scene_tile_scene(scene_tiles_list->get_item_metadata(p_index));
if (packed_scene.is_valid()) {
EditorNode::get_singleton()->open_request(packed_scene->get_path());
EditorNode::get_singleton()->load_scene(packed_scene->get_path());
}
}

View File

@ -548,7 +548,7 @@ void VersionControlEditorPlugin::_cell_button_pressed(Object *p_item, int p_colu
file_path = "res://" + file_path;
if (ResourceLoader::get_resource_type(file_path) == "PackedScene") {
EditorNode::get_singleton()->open_request(file_path);
EditorNode::get_singleton()->load_scene(file_path);
} else if (file_path.ends_with(".gd")) {
EditorNode::get_singleton()->load_resource(file_path);
ScriptEditor::get_singleton()->reload_scripts();

View File

@ -1806,7 +1806,7 @@ void SceneTreeDock::_node_strip_signal_inheritance(Node *p_node) {
}
void SceneTreeDock::_load_request(const String &p_path) {
EditorNode::get_singleton()->open_request(p_path);
EditorNode::get_singleton()->load_scene(p_path);
_local_tree_selected();
}