dependency_editor.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*************************************************************************/
  2. /* dependency_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "dependency_editor.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/file_access.h"
  33. #include "editor_node.h"
  34. #include "scene/gui/margin_container.h"
  35. void DependencyEditor::_notification(int p_what) {
  36. }
  37. void DependencyEditor::_searched(const String &p_path) {
  38. Map<String, String> dep_rename;
  39. dep_rename[replacing] = p_path;
  40. ResourceLoader::rename_dependencies(editing, dep_rename);
  41. _update_list();
  42. _update_file();
  43. }
  44. void DependencyEditor::_load_pressed(Object *p_item, int p_cell, int p_button) {
  45. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  46. String fname = ti->get_text(0);
  47. replacing = ti->get_text(1);
  48. search->set_title(TTR("Search Replacement For:") + " " + replacing.get_file());
  49. search->clear_filters();
  50. List<String> ext;
  51. ResourceLoader::get_recognized_extensions_for_type(ti->get_metadata(0), &ext);
  52. for (List<String>::Element *E = ext.front(); E; E = E->next()) {
  53. search->add_filter("*" + E->get());
  54. }
  55. search->popup_centered_ratio();
  56. }
  57. void DependencyEditor::_fix_and_find(EditorFileSystemDirectory *efsd, Map<String, Map<String, String> > &candidates) {
  58. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  59. _fix_and_find(efsd->get_subdir(i), candidates);
  60. }
  61. for (int i = 0; i < efsd->get_file_count(); i++) {
  62. String file = efsd->get_file(i);
  63. if (!candidates.has(file))
  64. continue;
  65. String path = efsd->get_file_path(i);
  66. for (Map<String, String>::Element *E = candidates[file].front(); E; E = E->next()) {
  67. if (E->get() == String()) {
  68. E->get() = path;
  69. continue;
  70. }
  71. //must match the best, using subdirs
  72. String existing = E->get().replace_first("res://", "");
  73. String current = path.replace_first("res://", "");
  74. String lost = E->key().replace_first("res://", "");
  75. Vector<String> existingv = existing.split("/");
  76. existingv.invert();
  77. Vector<String> currentv = current.split("/");
  78. currentv.invert();
  79. Vector<String> lostv = lost.split("/");
  80. lostv.invert();
  81. int existing_score = 0;
  82. int current_score = 0;
  83. for (int j = 0; j < lostv.size(); j++) {
  84. if (j < existingv.size() && lostv[j] == existingv[j]) {
  85. existing_score++;
  86. }
  87. if (j < currentv.size() && lostv[j] == currentv[j]) {
  88. current_score++;
  89. }
  90. }
  91. if (current_score > existing_score) {
  92. //if it was the same, could track distance to new path but..
  93. E->get() = path; //replace by more accurate
  94. }
  95. }
  96. }
  97. }
  98. void DependencyEditor::_fix_all() {
  99. if (!EditorFileSystem::get_singleton()->get_filesystem())
  100. return;
  101. Map<String, Map<String, String> > candidates;
  102. for (List<String>::Element *E = missing.front(); E; E = E->next()) {
  103. String base = E->get().get_file();
  104. if (!candidates.has(base)) {
  105. candidates[base] = Map<String, String>();
  106. }
  107. candidates[base][E->get()] = "";
  108. }
  109. _fix_and_find(EditorFileSystem::get_singleton()->get_filesystem(), candidates);
  110. Map<String, String> remaps;
  111. for (Map<String, Map<String, String> >::Element *E = candidates.front(); E; E = E->next()) {
  112. for (Map<String, String>::Element *F = E->get().front(); F; F = F->next()) {
  113. if (F->get() != String()) {
  114. remaps[F->key()] = F->get();
  115. }
  116. }
  117. }
  118. if (remaps.size()) {
  119. ResourceLoader::rename_dependencies(editing, remaps);
  120. _update_list();
  121. _update_file();
  122. }
  123. }
  124. void DependencyEditor::_update_file() {
  125. EditorFileSystem::get_singleton()->update_file(editing);
  126. }
  127. void DependencyEditor::_update_list() {
  128. List<String> deps;
  129. ResourceLoader::get_dependencies(editing, &deps, true);
  130. tree->clear();
  131. missing.clear();
  132. TreeItem *root = tree->create_item();
  133. Ref<Texture> folder = get_icon("folder", "FileDialog");
  134. bool broken = false;
  135. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  136. TreeItem *item = tree->create_item(root);
  137. String n = E->get();
  138. String path;
  139. String type;
  140. if (n.find("::") != -1) {
  141. path = n.get_slice("::", 0);
  142. type = n.get_slice("::", 1);
  143. } else {
  144. path = n;
  145. type = "Resource";
  146. }
  147. String name = path.get_file();
  148. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(type);
  149. item->set_text(0, name);
  150. item->set_icon(0, icon);
  151. item->set_metadata(0, type);
  152. item->set_text(1, path);
  153. if (!FileAccess::exists(path)) {
  154. item->set_custom_color(1, Color(1, 0.4, 0.3));
  155. missing.push_back(path);
  156. broken = true;
  157. }
  158. item->add_button(1, folder, 0);
  159. }
  160. fixdeps->set_disabled(!broken);
  161. }
  162. void DependencyEditor::edit(const String &p_path) {
  163. editing = p_path;
  164. set_title(TTR("Dependencies For:") + " " + p_path.get_file());
  165. _update_list();
  166. popup_centered_ratio();
  167. if (EditorNode::get_singleton()->is_scene_open(p_path)) {
  168. EditorNode::get_singleton()->show_warning(vformat(TTR("Scene '%s' is currently being edited.\nChanges will not take effect unless reloaded."), p_path.get_file()));
  169. } else if (ResourceCache::has(p_path)) {
  170. EditorNode::get_singleton()->show_warning(vformat(TTR("Resource '%s' is in use.\nChanges will take effect when reloaded."), p_path.get_file()));
  171. }
  172. }
  173. void DependencyEditor::_bind_methods() {
  174. ClassDB::bind_method(D_METHOD("_searched"), &DependencyEditor::_searched);
  175. ClassDB::bind_method(D_METHOD("_load_pressed"), &DependencyEditor::_load_pressed);
  176. ClassDB::bind_method(D_METHOD("_fix_all"), &DependencyEditor::_fix_all);
  177. }
  178. DependencyEditor::DependencyEditor() {
  179. VBoxContainer *vb = memnew(VBoxContainer);
  180. vb->set_name(TTR("Dependencies"));
  181. add_child(vb);
  182. tree = memnew(Tree);
  183. tree->set_columns(2);
  184. tree->set_column_titles_visible(true);
  185. tree->set_column_title(0, TTR("Resource"));
  186. tree->set_column_title(1, TTR("Path"));
  187. tree->set_hide_root(true);
  188. tree->connect("button_pressed", this, "_load_pressed");
  189. HBoxContainer *hbc = memnew(HBoxContainer);
  190. Label *label = memnew(Label(TTR("Dependencies:")));
  191. hbc->add_child(label);
  192. hbc->add_spacer();
  193. fixdeps = memnew(Button(TTR("Fix Broken")));
  194. hbc->add_child(fixdeps);
  195. fixdeps->connect("pressed", this, "_fix_all");
  196. vb->add_child(hbc);
  197. MarginContainer *mc = memnew(MarginContainer);
  198. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  199. mc->add_child(tree);
  200. vb->add_child(mc);
  201. set_title(TTR("Dependency Editor"));
  202. search = memnew(EditorFileDialog);
  203. search->connect("file_selected", this, "_searched");
  204. search->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  205. search->set_title(TTR("Search Replacement Resource:"));
  206. add_child(search);
  207. }
  208. /////////////////////////////////////
  209. void DependencyEditorOwners::_list_rmb_select(int p_item, const Vector2 &p_pos) {
  210. file_options->clear();
  211. file_options->set_size(Size2(1, 1));
  212. if (p_item >= 0) {
  213. file_options->add_item(TTR("Open"), FILE_OPEN);
  214. }
  215. file_options->set_position(owners->get_global_position() + p_pos);
  216. file_options->popup();
  217. }
  218. void DependencyEditorOwners::_select_file(int p_idx) {
  219. String fpath = owners->get_item_text(p_idx);
  220. if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
  221. editor->open_request(fpath);
  222. hide();
  223. emit_signal("confirmed");
  224. }
  225. }
  226. void DependencyEditorOwners::_file_option(int p_option) {
  227. switch (p_option) {
  228. case FILE_OPEN: {
  229. int idx = owners->get_current();
  230. if (idx < 0 || idx >= owners->get_item_count())
  231. break;
  232. _select_file(idx);
  233. } break;
  234. }
  235. }
  236. void DependencyEditorOwners::_bind_methods() {
  237. ClassDB::bind_method(D_METHOD("_list_rmb_select"), &DependencyEditorOwners::_list_rmb_select);
  238. ClassDB::bind_method(D_METHOD("_file_option"), &DependencyEditorOwners::_file_option);
  239. ClassDB::bind_method(D_METHOD("_select_file"), &DependencyEditorOwners::_select_file);
  240. }
  241. void DependencyEditorOwners::_fill_owners(EditorFileSystemDirectory *efsd) {
  242. if (!efsd)
  243. return;
  244. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  245. _fill_owners(efsd->get_subdir(i));
  246. }
  247. for (int i = 0; i < efsd->get_file_count(); i++) {
  248. Vector<String> deps = efsd->get_file_deps(i);
  249. bool found = false;
  250. for (int j = 0; j < deps.size(); j++) {
  251. if (deps[j] == editing) {
  252. found = true;
  253. break;
  254. }
  255. }
  256. if (!found)
  257. continue;
  258. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(efsd->get_file_type(i));
  259. owners->add_item(efsd->get_file_path(i), icon);
  260. }
  261. }
  262. void DependencyEditorOwners::show(const String &p_path) {
  263. editing = p_path;
  264. owners->clear();
  265. _fill_owners(EditorFileSystem::get_singleton()->get_filesystem());
  266. popup_centered_ratio();
  267. set_title(TTR("Owners Of:") + " " + p_path.get_file());
  268. }
  269. DependencyEditorOwners::DependencyEditorOwners(EditorNode *p_editor) {
  270. editor = p_editor;
  271. file_options = memnew(PopupMenu);
  272. add_child(file_options);
  273. file_options->connect("id_pressed", this, "_file_option");
  274. owners = memnew(ItemList);
  275. owners->set_select_mode(ItemList::SELECT_SINGLE);
  276. owners->connect("item_rmb_selected", this, "_list_rmb_select");
  277. owners->connect("item_activated", this, "_select_file");
  278. owners->set_allow_rmb_select(true);
  279. add_child(owners);
  280. }
  281. ///////////////////////
  282. void DependencyRemoveDialog::_find_files_in_removed_folder(EditorFileSystemDirectory *efsd, const String &p_folder) {
  283. if (!efsd)
  284. return;
  285. for (int i = 0; i < efsd->get_subdir_count(); ++i) {
  286. _find_files_in_removed_folder(efsd->get_subdir(i), p_folder);
  287. }
  288. for (int i = 0; i < efsd->get_file_count(); i++) {
  289. String file = efsd->get_file_path(i);
  290. ERR_FAIL_COND(all_remove_files.has(file)); //We are deleting a directory which is contained in a directory we are deleting...
  291. all_remove_files[file] = p_folder; //Point the file to the ancestor directory we are deleting so we know what to parent it under in the tree.
  292. }
  293. }
  294. void DependencyRemoveDialog::_find_all_removed_dependencies(EditorFileSystemDirectory *efsd, Vector<RemovedDependency> &p_removed) {
  295. if (!efsd)
  296. return;
  297. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  298. _find_all_removed_dependencies(efsd->get_subdir(i), p_removed);
  299. }
  300. for (int i = 0; i < efsd->get_file_count(); i++) {
  301. const String path = efsd->get_file_path(i);
  302. //It doesn't matter if a file we are about to delete will have some of its dependencies removed too
  303. if (all_remove_files.has(path))
  304. continue;
  305. Vector<String> all_deps = efsd->get_file_deps(i);
  306. for (int j = 0; j < all_deps.size(); ++j) {
  307. if (all_remove_files.has(all_deps[j])) {
  308. RemovedDependency dep;
  309. dep.file = path;
  310. dep.file_type = efsd->get_file_type(i);
  311. dep.dependency = all_deps[j];
  312. dep.dependency_folder = all_remove_files[all_deps[j]];
  313. p_removed.push_back(dep);
  314. }
  315. }
  316. }
  317. }
  318. void DependencyRemoveDialog::_build_removed_dependency_tree(const Vector<RemovedDependency> &p_removed) {
  319. owners->clear();
  320. owners->create_item(); // root
  321. Map<String, TreeItem *> tree_items;
  322. for (int i = 0; i < p_removed.size(); i++) {
  323. RemovedDependency rd = p_removed[i];
  324. //Ensure that the dependency is already in the tree
  325. if (!tree_items.has(rd.dependency)) {
  326. if (rd.dependency_folder.length() > 0) {
  327. //Ensure the ancestor folder is already in the tree
  328. if (!tree_items.has(rd.dependency_folder)) {
  329. TreeItem *folder_item = owners->create_item(owners->get_root());
  330. folder_item->set_text(0, rd.dependency_folder);
  331. folder_item->set_icon(0, get_icon("Folder", "EditorIcons"));
  332. tree_items[rd.dependency_folder] = folder_item;
  333. }
  334. TreeItem *dependency_item = owners->create_item(tree_items[rd.dependency_folder]);
  335. dependency_item->set_text(0, rd.dependency);
  336. dependency_item->set_icon(0, get_icon("Warning", "EditorIcons"));
  337. tree_items[rd.dependency] = dependency_item;
  338. } else {
  339. TreeItem *dependency_item = owners->create_item(owners->get_root());
  340. dependency_item->set_text(0, rd.dependency);
  341. dependency_item->set_icon(0, get_icon("Warning", "EditorIcons"));
  342. tree_items[rd.dependency] = dependency_item;
  343. }
  344. }
  345. //List this file under this dependency
  346. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(rd.file_type);
  347. TreeItem *file_item = owners->create_item(tree_items[rd.dependency]);
  348. file_item->set_text(0, rd.file);
  349. file_item->set_icon(0, icon);
  350. }
  351. }
  352. void DependencyRemoveDialog::show(const Vector<String> &p_folders, const Vector<String> &p_files) {
  353. all_remove_files.clear();
  354. dirs_to_delete.clear();
  355. files_to_delete.clear();
  356. owners->clear();
  357. for (int i = 0; i < p_folders.size(); ++i) {
  358. String folder = p_folders[i].ends_with("/") ? p_folders[i] : (p_folders[i] + "/");
  359. _find_files_in_removed_folder(EditorFileSystem::get_singleton()->get_filesystem_path(folder), folder);
  360. dirs_to_delete.push_back(folder);
  361. }
  362. for (int i = 0; i < p_files.size(); ++i) {
  363. all_remove_files[p_files[i]] = String();
  364. files_to_delete.push_back(p_files[i]);
  365. }
  366. Vector<RemovedDependency> removed_deps;
  367. _find_all_removed_dependencies(EditorFileSystem::get_singleton()->get_filesystem(), removed_deps);
  368. removed_deps.sort();
  369. if (removed_deps.empty()) {
  370. owners->hide();
  371. text->set_text(TTR("Remove selected files from the project? (no undo)"));
  372. popup_centered_minsize(Size2(400, 100));
  373. } else {
  374. _build_removed_dependency_tree(removed_deps);
  375. owners->show();
  376. text->set_text(TTR("The files being removed are required by other resources in order for them to work.\nRemove them anyway? (no undo)"));
  377. popup_centered_minsize(Size2(500, 350));
  378. }
  379. }
  380. void DependencyRemoveDialog::ok_pressed() {
  381. for (int i = 0; i < files_to_delete.size(); ++i) {
  382. if (ResourceCache::has(files_to_delete[i])) {
  383. Resource *res = ResourceCache::get(files_to_delete[i]);
  384. res->set_path("");
  385. }
  386. // If the file we are deleting is the main scene, clear its definition.
  387. if (files_to_delete[i] == ProjectSettings::get_singleton()->get("application/run/main_scene")) {
  388. ProjectSettings::get_singleton()->set("application/run/main_scene", "");
  389. }
  390. String path = OS::get_singleton()->get_resource_dir() + files_to_delete[i].replace_first("res://", "/");
  391. print_verbose("Moving to trash: " + path);
  392. Error err = OS::get_singleton()->move_to_trash(path);
  393. if (err != OK) {
  394. EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + files_to_delete[i] + "\n");
  395. } else {
  396. emit_signal("file_removed", files_to_delete[i]);
  397. }
  398. }
  399. if (dirs_to_delete.size() == 0) {
  400. // If we only deleted files we should only need to tell the file system about the files we touched.
  401. for (int i = 0; i < files_to_delete.size(); ++i)
  402. EditorFileSystem::get_singleton()->update_file(files_to_delete[i]);
  403. } else {
  404. for (int i = 0; i < dirs_to_delete.size(); ++i) {
  405. String path = OS::get_singleton()->get_resource_dir() + dirs_to_delete[i].replace_first("res://", "/");
  406. print_verbose("Moving to trash: " + path);
  407. Error err = OS::get_singleton()->move_to_trash(path);
  408. if (err != OK) {
  409. EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + dirs_to_delete[i] + "\n");
  410. } else {
  411. emit_signal("folder_removed", dirs_to_delete[i]);
  412. }
  413. }
  414. EditorFileSystem::get_singleton()->scan_changes();
  415. }
  416. // If some files/dirs would be deleted, favorite dirs need to be updated
  417. Vector<String> previous_favorites = EditorSettings::get_singleton()->get_favorites();
  418. Vector<String> new_favorites;
  419. for (int i = 0; i < previous_favorites.size(); ++i) {
  420. if (previous_favorites[i].ends_with("/")) {
  421. if (dirs_to_delete.find(previous_favorites[i]) < 0)
  422. new_favorites.push_back(previous_favorites[i]);
  423. } else {
  424. if (files_to_delete.find(previous_favorites[i]) < 0)
  425. new_favorites.push_back(previous_favorites[i]);
  426. }
  427. }
  428. if (new_favorites.size() < previous_favorites.size()) {
  429. EditorSettings::get_singleton()->set_favorites(new_favorites);
  430. }
  431. }
  432. void DependencyRemoveDialog::_bind_methods() {
  433. ADD_SIGNAL(MethodInfo("file_removed", PropertyInfo(Variant::STRING, "file")));
  434. ADD_SIGNAL(MethodInfo("folder_removed", PropertyInfo(Variant::STRING, "folder")));
  435. }
  436. DependencyRemoveDialog::DependencyRemoveDialog() {
  437. VBoxContainer *vb = memnew(VBoxContainer);
  438. add_child(vb);
  439. text = memnew(Label);
  440. vb->add_child(text);
  441. owners = memnew(Tree);
  442. owners->set_hide_root(true);
  443. vb->add_child(owners);
  444. owners->set_v_size_flags(SIZE_EXPAND_FILL);
  445. get_ok()->set_text(TTR("Remove"));
  446. }
  447. //////////////
  448. void DependencyErrorDialog::show(Mode p_mode, const String &p_for_file, const Vector<String> &report) {
  449. mode = p_mode;
  450. for_file = p_for_file;
  451. set_title(TTR("Error loading:") + " " + p_for_file.get_file());
  452. files->clear();
  453. TreeItem *root = files->create_item(NULL);
  454. for (int i = 0; i < report.size(); i++) {
  455. String dep;
  456. String type = "Object";
  457. dep = report[i].get_slice("::", 0);
  458. if (report[i].get_slice_count("::") > 0)
  459. type = report[i].get_slice("::", 1);
  460. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(type);
  461. TreeItem *ti = files->create_item(root);
  462. ti->set_text(0, dep);
  463. ti->set_icon(0, icon);
  464. }
  465. popup_centered_minsize(Size2(500, 220));
  466. }
  467. void DependencyErrorDialog::ok_pressed() {
  468. switch (mode) {
  469. case MODE_SCENE:
  470. EditorNode::get_singleton()->load_scene(for_file, true);
  471. break;
  472. case MODE_RESOURCE:
  473. EditorNode::get_singleton()->load_resource(for_file, true);
  474. break;
  475. }
  476. }
  477. void DependencyErrorDialog::custom_action(const String &) {
  478. EditorNode::get_singleton()->fix_dependencies(for_file);
  479. }
  480. DependencyErrorDialog::DependencyErrorDialog() {
  481. VBoxContainer *vb = memnew(VBoxContainer);
  482. add_child(vb);
  483. files = memnew(Tree);
  484. files->set_hide_root(true);
  485. vb->add_margin_child(TTR("Load failed due to missing dependencies:"), files, true);
  486. files->set_v_size_flags(SIZE_EXPAND_FILL);
  487. files->set_custom_minimum_size(Size2(1, 200));
  488. get_ok()->set_text(TTR("Open Anyway"));
  489. get_cancel()->set_text(TTR("Close"));
  490. text = memnew(Label);
  491. vb->add_child(text);
  492. text->set_text(TTR("Which action should be taken?"));
  493. fdep = add_button(TTR("Fix Dependencies"), true, "fixdeps");
  494. set_title(TTR("Errors loading!"));
  495. }
  496. //////////////////////////////////////////////////////////////////////
  497. void OrphanResourcesDialog::ok_pressed() {
  498. paths.clear();
  499. _find_to_delete(files->get_root(), paths);
  500. if (paths.empty())
  501. return;
  502. delete_confirm->set_text(vformat(TTR("Permanently delete %d item(s)? (No undo!)"), paths.size()));
  503. delete_confirm->popup_centered_minsize();
  504. }
  505. bool OrphanResourcesDialog::_fill_owners(EditorFileSystemDirectory *efsd, HashMap<String, int> &refs, TreeItem *p_parent) {
  506. if (!efsd)
  507. return false;
  508. bool has_children = false;
  509. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  510. TreeItem *dir_item = NULL;
  511. if (p_parent) {
  512. dir_item = files->create_item(p_parent);
  513. dir_item->set_text(0, efsd->get_subdir(i)->get_name());
  514. dir_item->set_icon(0, get_icon("folder", "FileDialog"));
  515. }
  516. bool children = _fill_owners(efsd->get_subdir(i), refs, dir_item);
  517. if (p_parent) {
  518. if (!children) {
  519. memdelete(dir_item);
  520. } else {
  521. has_children = true;
  522. }
  523. }
  524. }
  525. for (int i = 0; i < efsd->get_file_count(); i++) {
  526. if (!p_parent) {
  527. Vector<String> deps = efsd->get_file_deps(i);
  528. for (int j = 0; j < deps.size(); j++) {
  529. if (!refs.has(deps[j])) {
  530. refs[deps[j]] = 1;
  531. }
  532. }
  533. } else {
  534. String path = efsd->get_file_path(i);
  535. if (!refs.has(path)) {
  536. TreeItem *ti = files->create_item(p_parent);
  537. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  538. ti->set_text(0, efsd->get_file(i));
  539. ti->set_editable(0, true);
  540. String type = efsd->get_file_type(i);
  541. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(type);
  542. ti->set_icon(0, icon);
  543. int ds = efsd->get_file_deps(i).size();
  544. ti->set_text(1, itos(ds));
  545. if (ds) {
  546. ti->add_button(1, get_icon("GuiVisibilityVisible", "EditorIcons"));
  547. }
  548. ti->set_metadata(0, path);
  549. has_children = true;
  550. }
  551. }
  552. }
  553. return has_children;
  554. }
  555. void OrphanResourcesDialog::refresh() {
  556. HashMap<String, int> refs;
  557. _fill_owners(EditorFileSystem::get_singleton()->get_filesystem(), refs, NULL);
  558. files->clear();
  559. TreeItem *root = files->create_item();
  560. _fill_owners(EditorFileSystem::get_singleton()->get_filesystem(), refs, root);
  561. }
  562. void OrphanResourcesDialog::show() {
  563. refresh();
  564. popup_centered_ratio();
  565. }
  566. void OrphanResourcesDialog::_find_to_delete(TreeItem *p_item, List<String> &paths) {
  567. while (p_item) {
  568. if (p_item->get_cell_mode(0) == TreeItem::CELL_MODE_CHECK && p_item->is_checked(0)) {
  569. paths.push_back(p_item->get_metadata(0));
  570. }
  571. if (p_item->get_children()) {
  572. _find_to_delete(p_item->get_children(), paths);
  573. }
  574. p_item = p_item->get_next();
  575. }
  576. }
  577. void OrphanResourcesDialog::_delete_confirm() {
  578. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  579. for (List<String>::Element *E = paths.front(); E; E = E->next()) {
  580. da->remove(E->get());
  581. EditorFileSystem::get_singleton()->update_file(E->get());
  582. }
  583. memdelete(da);
  584. refresh();
  585. }
  586. void OrphanResourcesDialog::_button_pressed(Object *p_item, int p_column, int p_id) {
  587. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  588. String path = ti->get_metadata(0);
  589. dep_edit->edit(path);
  590. }
  591. void OrphanResourcesDialog::_bind_methods() {
  592. ClassDB::bind_method(D_METHOD("_delete_confirm"), &OrphanResourcesDialog::_delete_confirm);
  593. ClassDB::bind_method(D_METHOD("_button_pressed"), &OrphanResourcesDialog::_button_pressed);
  594. }
  595. OrphanResourcesDialog::OrphanResourcesDialog() {
  596. VBoxContainer *vbc = memnew(VBoxContainer);
  597. add_child(vbc);
  598. files = memnew(Tree);
  599. files->set_columns(2);
  600. files->set_column_titles_visible(true);
  601. files->set_column_min_width(1, 100);
  602. files->set_column_expand(0, true);
  603. files->set_column_expand(1, false);
  604. files->set_column_title(0, TTR("Resource"));
  605. files->set_column_title(1, TTR("Owns"));
  606. files->set_hide_root(true);
  607. vbc->add_margin_child(TTR("Resources Without Explicit Ownership:"), files, true);
  608. set_title(TTR("Orphan Resource Explorer"));
  609. delete_confirm = memnew(ConfirmationDialog);
  610. delete_confirm->set_text(TTR("Delete selected files?"));
  611. get_ok()->set_text(TTR("Delete"));
  612. add_child(delete_confirm);
  613. dep_edit = memnew(DependencyEditor);
  614. add_child(dep_edit);
  615. files->connect("button_pressed", this, "_button_pressed");
  616. delete_confirm->connect("confirmed", this, "_delete_confirm");
  617. set_hide_on_ok(false);
  618. }