dependency_editor.cpp 25 KB

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