dependency_editor.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*************************************************************************/
  2. /* dependency_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_node.h"
  32. #include "io/resource_loader.h"
  33. #include "os/file_access.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;
  149. if (has_icon(type, "EditorIcons")) {
  150. icon = get_icon(type, "EditorIcons");
  151. } else {
  152. icon = get_icon("Object", "EditorIcons");
  153. }
  154. item->set_text(0, name);
  155. item->set_icon(0, icon);
  156. item->set_metadata(0, type);
  157. item->set_text(1, path);
  158. if (!FileAccess::exists(path)) {
  159. item->set_custom_color(1, Color(1, 0.4, 0.3));
  160. missing.push_back(path);
  161. broken = true;
  162. }
  163. item->add_button(1, folder, 0);
  164. }
  165. fixdeps->set_disabled(!broken);
  166. }
  167. void DependencyEditor::edit(const String &p_path) {
  168. editing = p_path;
  169. set_title(TTR("Dependencies For:") + " " + p_path.get_file());
  170. _update_list();
  171. popup_centered_ratio();
  172. if (EditorNode::get_singleton()->is_scene_open(p_path)) {
  173. EditorNode::get_singleton()->show_warning(vformat(TTR("Scene '%s' is currently being edited.\nChanges will not take effect unless reloaded."), p_path.get_file()));
  174. } else if (ResourceCache::has(p_path)) {
  175. EditorNode::get_singleton()->show_warning(vformat(TTR("Resource '%s' is in use.\nChanges will take effect when reloaded."), p_path.get_file()));
  176. }
  177. }
  178. void DependencyEditor::_bind_methods() {
  179. ClassDB::bind_method(D_METHOD("_searched"), &DependencyEditor::_searched);
  180. ClassDB::bind_method(D_METHOD("_load_pressed"), &DependencyEditor::_load_pressed);
  181. ClassDB::bind_method(D_METHOD("_fix_all"), &DependencyEditor::_fix_all);
  182. }
  183. DependencyEditor::DependencyEditor() {
  184. VBoxContainer *vb = memnew(VBoxContainer);
  185. vb->set_name(TTR("Dependencies"));
  186. add_child(vb);
  187. tree = memnew(Tree);
  188. tree->set_columns(2);
  189. tree->set_column_titles_visible(true);
  190. tree->set_column_title(0, TTR("Resource"));
  191. tree->set_column_title(1, TTR("Path"));
  192. tree->set_hide_root(true);
  193. tree->connect("button_pressed", this, "_load_pressed");
  194. HBoxContainer *hbc = memnew(HBoxContainer);
  195. Label *label = memnew(Label(TTR("Dependencies:")));
  196. hbc->add_child(label);
  197. hbc->add_spacer();
  198. fixdeps = memnew(Button(TTR("Fix Broken")));
  199. hbc->add_child(fixdeps);
  200. fixdeps->connect("pressed", this, "_fix_all");
  201. vb->add_child(hbc);
  202. MarginContainer *mc = memnew(MarginContainer);
  203. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  204. mc->add_child(tree);
  205. vb->add_child(mc);
  206. set_title(TTR("Dependency Editor"));
  207. search = memnew(EditorFileDialog);
  208. search->connect("file_selected", this, "_searched");
  209. search->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  210. search->set_title(TTR("Search Replacement Resource:"));
  211. add_child(search);
  212. }
  213. /////////////////////////////////////
  214. void DependencyEditorOwners::_fill_owners(EditorFileSystemDirectory *efsd) {
  215. if (!efsd)
  216. return;
  217. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  218. _fill_owners(efsd->get_subdir(i));
  219. }
  220. for (int i = 0; i < efsd->get_file_count(); i++) {
  221. Vector<String> deps = efsd->get_file_deps(i);
  222. //print_line(":::"+efsd->get_file_path(i));
  223. bool found = false;
  224. for (int j = 0; j < deps.size(); j++) {
  225. //print_line("\t"+deps[j]+" vs "+editing);
  226. if (deps[j] == editing) {
  227. //print_line("found");
  228. found = true;
  229. break;
  230. }
  231. }
  232. if (!found)
  233. continue;
  234. Ref<Texture> icon;
  235. String type = efsd->get_file_type(i);
  236. if (!has_icon(type, "EditorIcons")) {
  237. icon = get_icon("Object", "EditorIcons");
  238. } else {
  239. icon = get_icon(type, "EditorIcons");
  240. }
  241. owners->add_item(efsd->get_file_path(i), icon);
  242. }
  243. }
  244. void DependencyEditorOwners::show(const String &p_path) {
  245. editing = p_path;
  246. owners->clear();
  247. _fill_owners(EditorFileSystem::get_singleton()->get_filesystem());
  248. popup_centered_ratio();
  249. set_title(TTR("Owners Of:") + " " + p_path.get_file());
  250. }
  251. DependencyEditorOwners::DependencyEditorOwners() {
  252. owners = memnew(ItemList);
  253. add_child(owners);
  254. }
  255. ///////////////////////
  256. void DependencyRemoveDialog::_find_files_in_removed_folder(EditorFileSystemDirectory *efsd, const String &p_folder) {
  257. if (!efsd)
  258. return;
  259. for (int i = 0; i < efsd->get_subdir_count(); ++i) {
  260. _find_files_in_removed_folder(efsd->get_subdir(i), p_folder);
  261. }
  262. for (int i = 0; i < efsd->get_file_count(); i++) {
  263. String file = efsd->get_file_path(i);
  264. ERR_FAIL_COND(all_remove_files.has(file)); //We are deleting a directory which is contained in a directory we are deleting...
  265. 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.
  266. }
  267. }
  268. void DependencyRemoveDialog::_find_all_removed_dependencies(EditorFileSystemDirectory *efsd, Vector<RemovedDependency> &p_removed) {
  269. if (!efsd)
  270. return;
  271. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  272. _find_all_removed_dependencies(efsd->get_subdir(i), p_removed);
  273. }
  274. for (int i = 0; i < efsd->get_file_count(); i++) {
  275. const String path = efsd->get_file_path(i);
  276. //It doesn't matter if a file we are about to delete will have some of its dependencies removed too
  277. if (all_remove_files.has(path))
  278. continue;
  279. Vector<String> all_deps = efsd->get_file_deps(i);
  280. for (int j = 0; j < all_deps.size(); ++j) {
  281. if (all_remove_files.has(all_deps[j])) {
  282. RemovedDependency dep;
  283. dep.file = path;
  284. dep.file_type = efsd->get_file_type(i);
  285. dep.dependency = all_deps[j];
  286. dep.dependency_folder = all_remove_files[all_deps[j]];
  287. p_removed.push_back(dep);
  288. }
  289. }
  290. }
  291. }
  292. void DependencyRemoveDialog::_build_removed_dependency_tree(const Vector<RemovedDependency> &p_removed) {
  293. owners->clear();
  294. owners->create_item(); // root
  295. Map<String, TreeItem *> tree_items;
  296. for (int i = 0; i < p_removed.size(); i++) {
  297. RemovedDependency rd = p_removed[i];
  298. //Ensure that the dependency is already in the tree
  299. if (!tree_items.has(rd.dependency)) {
  300. if (rd.dependency_folder.length() > 0) {
  301. //Ensure the ancestor folder is already in the tree
  302. if (!tree_items.has(rd.dependency_folder)) {
  303. TreeItem *folder_item = owners->create_item(owners->get_root());
  304. folder_item->set_text(0, rd.dependency_folder);
  305. folder_item->set_icon(0, get_icon("Folder", "EditorIcons"));
  306. tree_items[rd.dependency_folder] = folder_item;
  307. }
  308. TreeItem *dependency_item = owners->create_item(tree_items[rd.dependency_folder]);
  309. dependency_item->set_text(0, rd.dependency);
  310. dependency_item->set_icon(0, get_icon("Warning", "EditorIcons"));
  311. tree_items[rd.dependency] = dependency_item;
  312. } else {
  313. TreeItem *dependency_item = owners->create_item(owners->get_root());
  314. dependency_item->set_text(0, rd.dependency);
  315. dependency_item->set_icon(0, get_icon("Warning", "EditorIcons"));
  316. tree_items[rd.dependency] = dependency_item;
  317. }
  318. }
  319. //List this file under this dependency
  320. Ref<Texture> icon = has_icon(rd.file_type, "EditorIcons") ? get_icon(rd.file_type, "EditorIcons") : get_icon("Object", "EditorIcons");
  321. TreeItem *file_item = owners->create_item(tree_items[rd.dependency]);
  322. file_item->set_text(0, rd.file);
  323. file_item->set_icon(0, icon);
  324. }
  325. }
  326. void DependencyRemoveDialog::show(const Vector<String> &p_folders, const Vector<String> &p_files) {
  327. all_remove_files.clear();
  328. to_delete.clear();
  329. owners->clear();
  330. for (int i = 0; i < p_folders.size(); ++i) {
  331. String folder = p_folders[i].ends_with("/") ? p_folders[i] : (p_folders[i] + "/");
  332. _find_files_in_removed_folder(EditorFileSystem::get_singleton()->get_filesystem_path(folder), folder);
  333. to_delete.push_back(folder);
  334. }
  335. for (int i = 0; i < p_files.size(); ++i) {
  336. all_remove_files[p_files[i]] = String();
  337. to_delete.push_back(p_files[i]);
  338. }
  339. Vector<RemovedDependency> removed_deps;
  340. _find_all_removed_dependencies(EditorFileSystem::get_singleton()->get_filesystem(), removed_deps);
  341. removed_deps.sort();
  342. if (removed_deps.empty()) {
  343. owners->hide();
  344. text->set_text(TTR("Remove selected files from the project? (no undo)"));
  345. popup_centered_minsize(Size2(400, 100));
  346. } else {
  347. _build_removed_dependency_tree(removed_deps);
  348. owners->show();
  349. text->set_text(TTR("The files being removed are required by other resources in order for them to work.\nRemove them anyway? (no undo)"));
  350. popup_centered_minsize(Size2(500, 350));
  351. }
  352. }
  353. void DependencyRemoveDialog::ok_pressed() {
  354. bool files_only = true;
  355. for (int i = 0; i < to_delete.size(); ++i) {
  356. if (to_delete[i].ends_with("/")) {
  357. files_only = false;
  358. } else if (ResourceCache::has(to_delete[i])) {
  359. Resource *res = ResourceCache::get(to_delete[i]);
  360. res->set_path(""); //clear reference to path
  361. }
  362. String path = OS::get_singleton()->get_resource_dir() + to_delete[i].replace_first("res://", "/");
  363. print_line("Moving to trash: " + path);
  364. Error err = OS::get_singleton()->move_to_trash(path);
  365. if (err != OK) {
  366. EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:\n") + to_delete[i] + "\n");
  367. }
  368. }
  369. if (files_only) {
  370. //If we only deleted files we should only need to tell the file system about the files we touched.
  371. for (int i = 0; i < to_delete.size(); ++i) {
  372. EditorFileSystem::get_singleton()->update_file(to_delete[i]);
  373. }
  374. } else {
  375. EditorFileSystem::get_singleton()->scan_changes();
  376. }
  377. }
  378. DependencyRemoveDialog::DependencyRemoveDialog() {
  379. VBoxContainer *vb = memnew(VBoxContainer);
  380. add_child(vb);
  381. text = memnew(Label);
  382. vb->add_child(text);
  383. owners = memnew(Tree);
  384. owners->set_hide_root(true);
  385. vb->add_child(owners);
  386. owners->set_v_size_flags(SIZE_EXPAND_FILL);
  387. get_ok()->set_text(TTR("Remove"));
  388. }
  389. //////////////
  390. void DependencyErrorDialog::show(const String &p_for_file, const Vector<String> &report) {
  391. for_file = p_for_file;
  392. set_title(TTR("Error loading:") + " " + p_for_file.get_file());
  393. files->clear();
  394. TreeItem *root = files->create_item(NULL);
  395. for (int i = 0; i < report.size(); i++) {
  396. String dep;
  397. String type = "Object";
  398. dep = report[i].get_slice("::", 0);
  399. if (report[i].get_slice_count("::") > 0)
  400. type = report[i].get_slice("::", 1);
  401. Ref<Texture> icon;
  402. if (!has_icon(type, "EditorIcons")) {
  403. icon = get_icon("Object", "EditorIcons");
  404. } else {
  405. icon = get_icon(type, "EditorIcons");
  406. }
  407. TreeItem *ti = files->create_item(root);
  408. ti->set_text(0, dep);
  409. ti->set_icon(0, icon);
  410. }
  411. popup_centered_minsize(Size2(500, 220));
  412. }
  413. void DependencyErrorDialog::ok_pressed() {
  414. EditorNode::get_singleton()->load_scene(for_file, true);
  415. }
  416. void DependencyErrorDialog::custom_action(const String &) {
  417. EditorNode::get_singleton()->fix_dependencies(for_file);
  418. }
  419. DependencyErrorDialog::DependencyErrorDialog() {
  420. VBoxContainer *vb = memnew(VBoxContainer);
  421. add_child(vb);
  422. files = memnew(Tree);
  423. files->set_hide_root(true);
  424. vb->add_margin_child(TTR("Scene failed to load due to missing dependencies:"), files, true);
  425. files->set_v_size_flags(SIZE_EXPAND_FILL);
  426. get_ok()->set_text(TTR("Open Anyway"));
  427. get_cancel()->set_text(TTR("Close"));
  428. text = memnew(Label);
  429. vb->add_child(text);
  430. text->set_text(TTR("Which action should be taken?"));
  431. fdep = add_button(TTR("Fix Dependencies"), true, "fixdeps");
  432. set_title(TTR("Errors loading!"));
  433. }
  434. //////////////////////////////////////////////////////////////////////
  435. void OrphanResourcesDialog::ok_pressed() {
  436. paths.clear();
  437. _find_to_delete(files->get_root(), paths);
  438. if (paths.empty())
  439. return;
  440. delete_confirm->set_text(vformat(TTR("Permanently delete %d item(s)? (No undo!)"), paths.size()));
  441. delete_confirm->popup_centered_minsize();
  442. }
  443. bool OrphanResourcesDialog::_fill_owners(EditorFileSystemDirectory *efsd, HashMap<String, int> &refs, TreeItem *p_parent) {
  444. if (!efsd)
  445. return false;
  446. bool has_childs = false;
  447. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  448. TreeItem *dir_item = NULL;
  449. if (p_parent) {
  450. dir_item = files->create_item(p_parent);
  451. dir_item->set_text(0, efsd->get_subdir(i)->get_name());
  452. dir_item->set_icon(0, get_icon("folder", "FileDialog"));
  453. }
  454. bool children = _fill_owners(efsd->get_subdir(i), refs, dir_item);
  455. if (p_parent) {
  456. if (!children) {
  457. memdelete(dir_item);
  458. } else {
  459. has_childs = true;
  460. }
  461. }
  462. }
  463. for (int i = 0; i < efsd->get_file_count(); i++) {
  464. if (!p_parent) {
  465. Vector<String> deps = efsd->get_file_deps(i);
  466. //print_line(":::"+efsd->get_file_path(i));
  467. for (int j = 0; j < deps.size(); j++) {
  468. if (!refs.has(deps[j])) {
  469. refs[deps[j]] = 1;
  470. }
  471. }
  472. } else {
  473. String path = efsd->get_file_path(i);
  474. if (!refs.has(path)) {
  475. TreeItem *ti = files->create_item(p_parent);
  476. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  477. ti->set_text(0, efsd->get_file(i));
  478. ti->set_editable(0, true);
  479. String type = efsd->get_file_type(i);
  480. Ref<Texture> icon;
  481. if (has_icon(type, "EditorIcons")) {
  482. icon = get_icon(type, "EditorIcons");
  483. } else {
  484. icon = get_icon("Object", "EditorIcons");
  485. }
  486. ti->set_icon(0, icon);
  487. int ds = efsd->get_file_deps(i).size();
  488. ti->set_text(1, itos(ds));
  489. if (ds) {
  490. ti->add_button(1, get_icon("Visible", "EditorIcons"));
  491. }
  492. ti->set_metadata(0, path);
  493. has_childs = true;
  494. }
  495. }
  496. }
  497. return has_childs;
  498. }
  499. void OrphanResourcesDialog::refresh() {
  500. HashMap<String, int> refs;
  501. _fill_owners(EditorFileSystem::get_singleton()->get_filesystem(), refs, NULL);
  502. files->clear();
  503. TreeItem *root = files->create_item();
  504. _fill_owners(EditorFileSystem::get_singleton()->get_filesystem(), refs, root);
  505. }
  506. void OrphanResourcesDialog::show() {
  507. refresh();
  508. popup_centered_ratio();
  509. }
  510. void OrphanResourcesDialog::_find_to_delete(TreeItem *p_item, List<String> &paths) {
  511. while (p_item) {
  512. if (p_item->get_cell_mode(0) == TreeItem::CELL_MODE_CHECK && p_item->is_checked(0)) {
  513. paths.push_back(p_item->get_metadata(0));
  514. }
  515. if (p_item->get_children()) {
  516. _find_to_delete(p_item->get_children(), paths);
  517. }
  518. p_item = p_item->get_next();
  519. }
  520. }
  521. void OrphanResourcesDialog::_delete_confirm() {
  522. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  523. for (List<String>::Element *E = paths.front(); E; E = E->next()) {
  524. da->remove(E->get());
  525. EditorFileSystem::get_singleton()->update_file(E->get());
  526. }
  527. memdelete(da);
  528. refresh();
  529. }
  530. void OrphanResourcesDialog::_button_pressed(Object *p_item, int p_column, int p_id) {
  531. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  532. String path = ti->get_metadata(0);
  533. dep_edit->edit(path);
  534. }
  535. void OrphanResourcesDialog::_bind_methods() {
  536. ClassDB::bind_method(D_METHOD("_delete_confirm"), &OrphanResourcesDialog::_delete_confirm);
  537. ClassDB::bind_method(D_METHOD("_button_pressed"), &OrphanResourcesDialog::_button_pressed);
  538. }
  539. OrphanResourcesDialog::OrphanResourcesDialog() {
  540. VBoxContainer *vbc = memnew(VBoxContainer);
  541. add_child(vbc);
  542. files = memnew(Tree);
  543. files->set_columns(2);
  544. files->set_column_titles_visible(true);
  545. files->set_column_min_width(1, 100);
  546. files->set_column_expand(0, true);
  547. files->set_column_expand(1, false);
  548. files->set_column_title(0, TTR("Resource"));
  549. files->set_column_title(1, TTR("Owns"));
  550. files->set_hide_root(true);
  551. vbc->add_margin_child(TTR("Resources Without Explicit Ownership:"), files, true);
  552. set_title(TTR("Orphan Resource Explorer"));
  553. delete_confirm = memnew(ConfirmationDialog);
  554. delete_confirm->set_text(TTR("Delete selected files?"));
  555. get_ok()->set_text(TTR("Delete"));
  556. add_child(delete_confirm);
  557. dep_edit = memnew(DependencyEditor);
  558. add_child(dep_edit);
  559. files->connect("button_pressed", this, "_button_pressed");
  560. delete_confirm->connect("confirmed", this, "_delete_confirm");
  561. set_hide_on_ok(false);
  562. }