Skip to content

Commit

Permalink
EditorNode: Add function to load file as scene or resource
Browse files Browse the repository at this point in the history
  • Loading branch information
larspet committed Dec 28, 2024
1 parent 75ce426 commit dc86193
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 152 deletions.
20 changes: 3 additions & 17 deletions editor/dependency_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -720,8 +716,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();
Expand All @@ -746,14 +741,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 &) {
Expand All @@ -778,8 +766,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!"));
Expand Down
8 changes: 1 addition & 7 deletions editor/dependency_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ class DependencyRemoveDialog : public ConfirmationDialog {
class DependencyErrorDialog : public ConfirmationDialog {
GDCLASS(DependencyErrorDialog, ConfirmationDialog);

public:
enum Mode {
MODE_SCENE,
MODE_RESOURCE,
};

private:
String for_file;
Mode mode;
Expand All @@ -154,7 +148,7 @@ class DependencyErrorDialog : public ConfirmationDialog {
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();
};

Expand Down
6 changes: 1 addition & 5 deletions editor/editor_autoload_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
3 changes: 1 addition & 2 deletions editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,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) {
Expand Down
90 changes: 32 additions & 58 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,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) {
Expand Down Expand Up @@ -1293,7 +1289,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;
Expand All @@ -1303,6 +1299,13 @@ 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) {
if (ResourceLoader::get_resource_type(p_path) == "PackedScene") {
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);
}
Expand Down Expand Up @@ -2705,14 +2708,10 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
case FILE_QUICK_OPEN_SCRIPT: {
quick_open_dialog->popup_dialog({ "Script" }, callable_mp(this, &EditorNode::_quick_opened));
} break;
case FILE_OPEN_PREV: {
if (previous_scenes.is_empty()) {
break;
case FILE_OPEN_PREV_SCENE: {
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 FILE_CLOSE_OTHERS: {
tab_closing_menu_option = -1;
Expand Down Expand Up @@ -2845,16 +2844,9 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
confirmation->set_ok_button_text(TTR("Open"));
confirmation->set_text(TTR("Current scene not saved. Open anyway?"));
confirmation->popup_centered();
break;
}

bool oprev = opening_prev;
Error err = load_scene(external_file);
if (err == OK && oprev) {
previous_scenes.pop_back();
opening_prev = false;
} else {
load_scene(external_file);
}

} break;

case EDIT_UNDO: {
Expand Down Expand Up @@ -3410,10 +3402,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).
Expand All @@ -3438,14 +3427,9 @@ void EditorNode::_discard_changes(const String &p_str) {
}

void EditorNode::_update_file_menu_opened() {
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..."));
Expand Down Expand Up @@ -3961,6 +3945,8 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
}

String lpath = 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) {
Expand All @@ -3981,7 +3967,6 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
lpath = ProjectSettings::get_singleton()->localize_path(lpath);
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;
}

Expand Down Expand Up @@ -4011,8 +3996,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);
Expand All @@ -4023,7 +4007,6 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b

if (!sdata.is_valid()) {
_dialog_display_load_error(lpath, err);
opening_prev = false;

if (prev != -1 && prev != idx) {
_set_current_scene(prev);
Expand Down Expand Up @@ -4056,11 +4039,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);
Expand Down Expand Up @@ -4094,8 +4075,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) {
Expand Down Expand Up @@ -4476,19 +4455,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) {
Expand Down Expand Up @@ -4569,6 +4537,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_SCENE), 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)) {
Expand Down Expand Up @@ -4615,11 +4594,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() {
Expand Down Expand Up @@ -7269,7 +7244,7 @@ EditorNode::EditorNode() {
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/new_scene", TTRC("New Scene"), KeyModifierMask::CMD_OR_CTRL + Key::N), FILE_NEW_SCENE);
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/new_inherited_scene", TTRC("New Inherited Scene..."), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::N), FILE_NEW_INHERITED_SCENE);
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/open_scene", TTRC("Open Scene..."), KeyModifierMask::CMD_OR_CTRL + Key::O), FILE_OPEN_SCENE);
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/reopen_closed_scene", TTRC("Reopen Closed Scene"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::T), FILE_OPEN_PREV);
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/reopen_closed_scene", TTRC("Reopen Closed Scene"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::T), FILE_OPEN_PREV_SCENE);

recent_scenes = memnew(PopupMenu);
file_menu->add_submenu_node_item(TTR("Open Recent"), recent_scenes, FILE_OPEN_RECENT);
Expand Down Expand Up @@ -7714,7 +7689,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));

Expand Down
14 changes: 7 additions & 7 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ class EditorNode : public Node {
FILE_EXPLORE_ANDROID_BUILD_TEMPLATES,
FILE_SAVE_OPTIMIZED,
FILE_OPEN_RECENT,
FILE_OPEN_OLD_SCENE,
FILE_QUICK_OPEN,
FILE_QUICK_OPEN_SCENE,
FILE_QUICK_OPEN_SCRIPT,
FILE_OPEN_PREV,
FILE_OPEN_PREV_SCENE,
FILE_CLOSE,
FILE_CLOSE_OTHERS,
FILE_CLOSE_RIGHT,
Expand Down Expand Up @@ -357,7 +356,7 @@ class EditorNode : public Node {

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;

Expand Down Expand Up @@ -428,7 +427,6 @@ class EditorNode : public Node {
bool cmdline_export_mode = false;
bool convert_old = false;
bool immediate_dialog_confirmed = false;
bool opening_prev = false;
bool restoring_scenes = false;
bool unsaved_cache = true;

Expand Down Expand Up @@ -537,7 +535,6 @@ class EditorNode : public Node {
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);
Expand Down Expand Up @@ -583,9 +580,12 @@ class EditorNode : public Node {
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);

Expand Down Expand Up @@ -735,7 +735,7 @@ class EditorNode : public Node {
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); }

Expand Down Expand Up @@ -770,7 +770,6 @@ class EditorNode : public Node {
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);
Expand All @@ -789,6 +788,7 @@ class EditorNode : public Node {
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);

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);
Expand Down
Loading

0 comments on commit dc86193

Please sign in to comment.