diff options
Diffstat (limited to 'editor/dependency_editor.cpp')
-rw-r--r-- | editor/dependency_editor.cpp | 171 |
1 files changed, 100 insertions, 71 deletions
diff --git a/editor/dependency_editor.cpp b/editor/dependency_editor.cpp index 25e155aafe..4f89e1b2d1 100644 --- a/editor/dependency_editor.cpp +++ b/editor/dependency_editor.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,14 +30,17 @@ #include "dependency_editor.h" +#include "core/config/project_settings.h" +#include "core/io/file_access.h" #include "core/io/resource_loader.h" -#include "core/os/file_access.h" -#include "editor_node.h" -#include "editor_scale.h" +#include "editor/editor_file_dialog.h" +#include "editor/editor_file_system.h" +#include "editor/editor_node.h" +#include "editor/editor_scale.h" #include "scene/gui/margin_container.h" void DependencyEditor::_searched(const String &p_path) { - Map<String, String> dep_rename; + HashMap<String, String> dep_rename; dep_rename[replacing] = p_path; ResourceLoader::rename_dependencies(editing, dep_rename); @@ -46,22 +49,28 @@ void DependencyEditor::_searched(const String &p_path) { _update_file(); } -void DependencyEditor::_load_pressed(Object *p_item, int p_cell, int p_button) { +void DependencyEditor::_load_pressed(Object *p_item, int p_cell, int p_button, MouseButton p_mouse_button) { + if (p_mouse_button != MouseButton::LEFT) { + return; + } TreeItem *ti = Object::cast_to<TreeItem>(p_item); replacing = ti->get_text(1); search->set_title(TTR("Search Replacement For:") + " " + replacing.get_file()); + // Set directory to closest existing directory. + search->set_current_dir(replacing.get_base_dir()); + search->clear_filters(); List<String> ext; ResourceLoader::get_recognized_extensions_for_type(ti->get_metadata(0), &ext); - for (List<String>::Element *E = ext.front(); E; E = E->next()) { - search->add_filter("*" + E->get()); + for (const String &E : ext) { + search->add_filter("*" + E); } search->popup_file_dialog(); } -void DependencyEditor::_fix_and_find(EditorFileSystemDirectory *efsd, Map<String, Map<String, String>> &candidates) { +void DependencyEditor::_fix_and_find(EditorFileSystemDirectory *efsd, HashMap<String, HashMap<String, String>> &candidates) { for (int i = 0; i < efsd->get_subdir_count(); i++) { _fix_and_find(efsd->get_subdir(i), candidates); } @@ -74,23 +83,23 @@ void DependencyEditor::_fix_and_find(EditorFileSystemDirectory *efsd, Map<String String path = efsd->get_file_path(i); - for (Map<String, String>::Element *E = candidates[file].front(); E; E = E->next()) { - if (E->get() == String()) { - E->get() = path; + for (KeyValue<String, String> &E : candidates[file]) { + if (E.value.is_empty()) { + E.value = path; continue; } //must match the best, using subdirs - String existing = E->get().replace_first("res://", ""); + String existing = E.value.replace_first("res://", ""); String current = path.replace_first("res://", ""); - String lost = E->key().replace_first("res://", ""); + String lost = E.key.replace_first("res://", ""); Vector<String> existingv = existing.split("/"); - existingv.invert(); + existingv.reverse(); Vector<String> currentv = current.split("/"); - currentv.invert(); + currentv.reverse(); Vector<String> lostv = lost.split("/"); - lostv.invert(); + lostv.reverse(); int existing_score = 0; int current_score = 0; @@ -107,7 +116,7 @@ void DependencyEditor::_fix_and_find(EditorFileSystemDirectory *efsd, Map<String if (current_score > existing_score) { //if it was the same, could track distance to new path but.. - E->get() = path; //replace by more accurate + E.value = path; //replace by more accurate } } } @@ -118,25 +127,25 @@ void DependencyEditor::_fix_all() { return; } - Map<String, Map<String, String>> candidates; + HashMap<String, HashMap<String, String>> candidates; - for (List<String>::Element *E = missing.front(); E; E = E->next()) { - String base = E->get().get_file(); + for (const String &E : missing) { + String base = E.get_file(); if (!candidates.has(base)) { - candidates[base] = Map<String, String>(); + candidates[base] = HashMap<String, String>(); } - candidates[base][E->get()] = ""; + candidates[base][E] = ""; } _fix_and_find(EditorFileSystem::get_singleton()->get_filesystem(), candidates); - Map<String, String> remaps; + HashMap<String, String> remaps; - for (Map<String, Map<String, String>>::Element *E = candidates.front(); E; E = E->next()) { - for (Map<String, String>::Element *F = E->get().front(); F; F = F->next()) { - if (F->get() != String()) { - remaps[F->key()] = F->get(); + for (KeyValue<String, HashMap<String, String>> &E : candidates) { + for (const KeyValue<String, String> &F : E.value) { + if (!F.value.is_empty()) { + remaps[F.key] = F.value; } } } @@ -162,24 +171,31 @@ void DependencyEditor::_update_list() { TreeItem *root = tree->create_item(); - Ref<Texture2D> folder = tree->get_theme_icon("folder", "FileDialog"); + Ref<Texture2D> folder = tree->get_theme_icon(SNAME("folder"), SNAME("FileDialog")); bool broken = false; - for (List<String>::Element *E = deps.front(); E; E = E->next()) { + for (const String &n : deps) { TreeItem *item = tree->create_item(root); - - String n = E->get(); String path; String type; - if (n.find("::") != -1) { + if (n.contains("::")) { path = n.get_slice("::", 0); type = n.get_slice("::", 1); } else { path = n; type = "Resource"; } + + ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(path); + if (uid != ResourceUID::INVALID_ID) { + // dependency is in uid format, obtain proper path + ERR_CONTINUE(!ResourceUID::get_singleton()->has_id(uid)); + + path = ResourceUID::get_singleton()->get_id_path(uid); + } + String name = path.get_file(); Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(type); @@ -226,12 +242,18 @@ DependencyEditor::DependencyEditor() { tree->set_columns(2); tree->set_column_titles_visible(true); tree->set_column_title(0, TTR("Resource")); + tree->set_column_clip_content(0, true); + tree->set_column_expand_ratio(0, 2); tree->set_column_title(1, TTR("Path")); + tree->set_column_clip_content(1, true); + tree->set_column_expand_ratio(1, 1); tree->set_hide_root(true); - tree->connect("button_pressed", callable_mp(this, &DependencyEditor::_load_pressed)); + tree->connect("button_clicked", callable_mp(this, &DependencyEditor::_load_pressed)); HBoxContainer *hbc = memnew(HBoxContainer); Label *label = memnew(Label(TTR("Dependencies:"))); + label->set_theme_type_variation("HeaderSmall"); + hbc->add_child(label); hbc->add_spacer(); fixdeps = memnew(Button(TTR("Fix Broken"))); @@ -255,14 +277,19 @@ DependencyEditor::DependencyEditor() { } ///////////////////////////////////// -void DependencyEditorOwners::_list_rmb_select(int p_item, const Vector2 &p_pos) { +void DependencyEditorOwners::_list_rmb_clicked(int p_item, const Vector2 &p_pos, MouseButton p_mouse_button_index) { + if (p_mouse_button_index != MouseButton::RIGHT) { + return; + } + file_options->clear(); - file_options->set_size(Size2(1, 1)); + file_options->reset_size(); if (p_item >= 0) { file_options->add_item(TTR("Open"), FILE_OPEN); } - file_options->set_position(owners->get_global_position() + p_pos); + file_options->set_position(owners->get_screen_position() + p_pos); + file_options->reset_size(); file_options->popup(); } @@ -270,9 +297,9 @@ void DependencyEditorOwners::_select_file(int p_idx) { String fpath = owners->get_item_text(p_idx); if (ResourceLoader::get_resource_type(fpath) == "PackedScene") { - editor->open_request(fpath); + EditorNode::get_singleton()->open_request(fpath); hide(); - emit_signal("confirmed"); + emit_signal(SNAME("confirmed")); } } @@ -325,19 +352,17 @@ void DependencyEditorOwners::show(const String &p_path) { _fill_owners(EditorFileSystem::get_singleton()->get_filesystem()); popup_centered_ratio(0.3); - set_title(TTR("Owners Of:") + " " + p_path.get_file()); + set_title(vformat(TTR("Owners of: %s (Total: %d)"), p_path.get_file(), owners->get_item_count())); } -DependencyEditorOwners::DependencyEditorOwners(EditorNode *p_editor) { - editor = p_editor; - +DependencyEditorOwners::DependencyEditorOwners() { file_options = memnew(PopupMenu); add_child(file_options); file_options->connect("id_pressed", callable_mp(this, &DependencyEditorOwners::_file_option)); owners = memnew(ItemList); owners->set_select_mode(ItemList::SELECT_SINGLE); - owners->connect("item_rmb_selected", callable_mp(this, &DependencyEditorOwners::_list_rmb_select)); + owners->connect("item_clicked", callable_mp(this, &DependencyEditorOwners::_list_rmb_clicked)); owners->connect("item_activated", callable_mp(this, &DependencyEditorOwners::_select_file)); owners->set_allow_rmb_select(true); add_child(owners); @@ -395,7 +420,7 @@ void DependencyRemoveDialog::_build_removed_dependency_tree(const Vector<Removed owners->clear(); owners->create_item(); // root - Map<String, TreeItem *> tree_items; + HashMap<String, TreeItem *> tree_items; for (int i = 0; i < p_removed.size(); i++) { RemovedDependency rd = p_removed[i]; @@ -406,17 +431,17 @@ void DependencyRemoveDialog::_build_removed_dependency_tree(const Vector<Removed if (!tree_items.has(rd.dependency_folder)) { TreeItem *folder_item = owners->create_item(owners->get_root()); folder_item->set_text(0, rd.dependency_folder); - folder_item->set_icon(0, owners->get_theme_icon("Folder", "EditorIcons")); + folder_item->set_icon(0, owners->get_theme_icon(SNAME("Folder"), SNAME("EditorIcons"))); tree_items[rd.dependency_folder] = folder_item; } TreeItem *dependency_item = owners->create_item(tree_items[rd.dependency_folder]); dependency_item->set_text(0, rd.dependency); - dependency_item->set_icon(0, owners->get_theme_icon("Warning", "EditorIcons")); + dependency_item->set_icon(0, owners->get_theme_icon(SNAME("Warning"), SNAME("EditorIcons"))); tree_items[rd.dependency] = dependency_item; } else { TreeItem *dependency_item = owners->create_item(owners->get_root()); dependency_item->set_text(0, rd.dependency); - dependency_item->set_icon(0, owners->get_theme_icon("Warning", "EditorIcons")); + dependency_item->set_icon(0, owners->get_theme_icon(SNAME("Warning"), SNAME("EditorIcons"))); tree_items[rd.dependency] = dependency_item; } } @@ -450,13 +475,13 @@ void DependencyRemoveDialog::show(const Vector<String> &p_folders, const Vector< removed_deps.sort(); if (removed_deps.is_empty()) { owners->hide(); - text->set_text(TTR("Remove selected files from the project? (no undo)\nYou can find the removed files in the system trash to restore them.")); - set_size(Size2()); + text->set_text(TTR("Remove the selected files from the project? (Cannot be undone.)\nDepending on your filesystem configuration, the files will either be moved to the system trash or deleted permanently.")); + reset_size(); popup_centered(); } else { _build_removed_dependency_tree(removed_deps); owners->show(); - text->set_text(TTR("The files being removed are required by other resources in order for them to work.\nRemove them anyway? (no undo)\nYou can find the removed files in the system trash to restore them.")); + text->set_text(TTR("The files being removed are required by other resources in order for them to work.\nRemove them anyway? (Cannot be undone.)\nDepending on your filesystem configuration, the files will either be moved to the system trash or deleted permanently.")); popup_centered(Size2(500, 350)); } EditorFileSystem::get_singleton()->scan_changes(); @@ -465,7 +490,7 @@ void DependencyRemoveDialog::show(const Vector<String> &p_folders, const Vector< void DependencyRemoveDialog::ok_pressed() { for (int i = 0; i < files_to_delete.size(); ++i) { if (ResourceCache::has(files_to_delete[i])) { - Resource *res = ResourceCache::get(files_to_delete[i]); + Ref<Resource> res = ResourceCache::get_ref(files_to_delete[i]); res->set_path(""); } @@ -502,7 +527,7 @@ void DependencyRemoveDialog::ok_pressed() { if (err != OK) { EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + files_to_delete[i] + "\n"); } else { - emit_signal("file_removed", files_to_delete[i]); + emit_signal(SNAME("file_removed"), files_to_delete[i]); } } @@ -519,7 +544,7 @@ void DependencyRemoveDialog::ok_pressed() { if (err != OK) { EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + dirs_to_delete[i] + "\n"); } else { - emit_signal("folder_removed", dirs_to_delete[i]); + emit_signal(SNAME("folder_removed"), dirs_to_delete[i]); } } @@ -553,7 +578,7 @@ void DependencyRemoveDialog::_bind_methods() { } DependencyRemoveDialog::DependencyRemoveDialog() { - get_ok_button()->set_text(TTR("Remove")); + set_ok_button_text(TTR("Remove")); VBoxContainer *vb = memnew(VBoxContainer); add_child(vb); @@ -619,8 +644,8 @@ DependencyErrorDialog::DependencyErrorDialog() { files->set_v_size_flags(Control::SIZE_EXPAND_FILL); set_min_size(Size2(500, 220) * EDSCALE); - get_ok_button()->set_text(TTR("Open Anyway")); - get_cancel_button()->set_text(TTR("Close")); + set_ok_button_text(TTR("Open Anyway")); + set_cancel_button_text(TTR("Close")); text = memnew(Label); vb->add_child(text); @@ -659,7 +684,7 @@ bool OrphanResourcesDialog::_fill_owners(EditorFileSystemDirectory *efsd, HashMa if (p_parent) { dir_item = files->create_item(p_parent); dir_item->set_text(0, efsd->get_subdir(i)->get_name()); - dir_item->set_icon(0, files->get_theme_icon("folder", "FileDialog")); + dir_item->set_icon(0, files->get_theme_icon(SNAME("folder"), SNAME("FileDialog"))); } bool children = _fill_owners(efsd->get_subdir(i), refs, dir_item); @@ -695,7 +720,7 @@ bool OrphanResourcesDialog::_fill_owners(EditorFileSystemDirectory *efsd, HashMa int ds = efsd->get_file_deps(i).size(); ti->set_text(1, itos(ds)); if (ds) { - ti->add_button(1, files->get_theme_icon("GuiVisibilityVisible", "EditorIcons"), -1, false, TTR("Show Dependencies")); + ti->add_button(1, files->get_theme_icon(SNAME("GuiVisibilityVisible"), SNAME("EditorIcons")), -1, false, TTR("Show Dependencies")); } ti->set_metadata(0, path); has_children = true; @@ -725,8 +750,8 @@ void OrphanResourcesDialog::_find_to_delete(TreeItem *p_item, List<String> &path paths.push_back(p_item->get_metadata(0)); } - if (p_item->get_children()) { - _find_to_delete(p_item->get_children(), paths); + if (p_item->get_first_child()) { + _find_to_delete(p_item->get_first_child(), paths); } p_item = p_item->get_next(); @@ -734,16 +759,18 @@ void OrphanResourcesDialog::_find_to_delete(TreeItem *p_item, List<String> &path } void OrphanResourcesDialog::_delete_confirm() { - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); - for (List<String>::Element *E = paths.front(); E; E = E->next()) { - da->remove(E->get()); - EditorFileSystem::get_singleton()->update_file(E->get()); + Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + for (const String &E : paths) { + da->remove(E); + EditorFileSystem::get_singleton()->update_file(E); } - memdelete(da); refresh(); } -void OrphanResourcesDialog::_button_pressed(Object *p_item, int p_column, int p_id) { +void OrphanResourcesDialog::_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) { + if (p_button != MouseButton::LEFT) { + return; + } TreeItem *ti = Object::cast_to<TreeItem>(p_item); String path = ti->get_metadata(0); @@ -756,7 +783,7 @@ void OrphanResourcesDialog::_bind_methods() { OrphanResourcesDialog::OrphanResourcesDialog() { set_title(TTR("Orphan Resource Explorer")); delete_confirm = memnew(ConfirmationDialog); - get_ok_button()->set_text(TTR("Delete")); + set_ok_button_text(TTR("Delete")); add_child(delete_confirm); dep_edit = memnew(DependencyEditor); add_child(dep_edit); @@ -769,12 +796,14 @@ OrphanResourcesDialog::OrphanResourcesDialog() { files = memnew(Tree); files->set_columns(2); files->set_column_titles_visible(true); - files->set_column_min_width(1, 100); + files->set_column_custom_minimum_width(1, 100 * EDSCALE); files->set_column_expand(0, true); + files->set_column_clip_content(0, true); files->set_column_expand(1, false); + files->set_column_clip_content(1, true); files->set_column_title(0, TTR("Resource")); files->set_column_title(1, TTR("Owns")); files->set_hide_root(true); vbc->add_margin_child(TTR("Resources Without Explicit Ownership:"), files, true); - files->connect("button_pressed", callable_mp(this, &OrphanResourcesDialog::_button_pressed)); + files->connect("button_clicked", callable_mp(this, &OrphanResourcesDialog::_button_pressed)); } |