filesystem_dock.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  1. /*************************************************************************/
  2. /* filesystem_dock.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 "filesystem_dock.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/dir_access.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "editor_node.h"
  38. #include "editor_settings.h"
  39. #include "scene/main/viewport.h"
  40. Ref<Texture> FileSystemDock::_get_tree_item_icon(EditorFileSystemDirectory *p_dir, int p_idx) {
  41. Ref<Texture> file_icon;
  42. if (!p_dir->get_file_import_is_valid(p_idx)) {
  43. file_icon = get_icon("ImportFail", "EditorIcons");
  44. } else {
  45. String file_type = p_dir->get_file_type(p_idx);
  46. file_icon = (has_icon(file_type, "EditorIcons")) ? get_icon(file_type, "EditorIcons") : get_icon("File", "EditorIcons");
  47. }
  48. return file_icon;
  49. }
  50. bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory *p_dir, Vector<String> &uncollapsed_paths, bool p_select_in_favorites) {
  51. bool parent_should_expand = false;
  52. // Create a tree item for the subdirectory
  53. TreeItem *subdirectory_item = tree->create_item(p_parent);
  54. String dname = p_dir->get_name();
  55. if (dname == "")
  56. dname = "res://";
  57. subdirectory_item->set_text(0, dname);
  58. subdirectory_item->set_icon(0, get_icon("Folder", "EditorIcons"));
  59. subdirectory_item->set_selectable(0, true);
  60. String lpath = p_dir->get_path();
  61. subdirectory_item->set_metadata(0, lpath);
  62. if (!p_select_in_favorites && (path == lpath || ((display_mode == DISPLAY_MODE_SPLIT) && path.get_base_dir() == lpath))) {
  63. subdirectory_item->select(0);
  64. }
  65. if ((path.begins_with(lpath) && path != lpath)) {
  66. subdirectory_item->set_collapsed(false);
  67. } else {
  68. subdirectory_item->set_collapsed(uncollapsed_paths.find(lpath) < 0);
  69. }
  70. if (searched_string.length() > 0 && dname.to_lower().find(searched_string) >= 0) {
  71. parent_should_expand = true;
  72. }
  73. // Create items for all subdirectories
  74. for (int i = 0; i < p_dir->get_subdir_count(); i++)
  75. parent_should_expand = (_create_tree(subdirectory_item, p_dir->get_subdir(i), uncollapsed_paths, p_select_in_favorites) || parent_should_expand);
  76. // Create all items for the files in the subdirectory
  77. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  78. for (int i = 0; i < p_dir->get_file_count(); i++) {
  79. String file_name = p_dir->get_file(i);
  80. if (searched_string.length() > 0) {
  81. if (file_name.to_lower().find(searched_string) < 0) {
  82. // The searched string is not in the file name, we skip it
  83. continue;
  84. } else {
  85. // We expand all parents
  86. parent_should_expand = true;
  87. }
  88. }
  89. TreeItem *file_item = tree->create_item(subdirectory_item);
  90. file_item->set_text(0, file_name);
  91. file_item->set_icon(0, _get_tree_item_icon(p_dir, i));
  92. String file_metadata = lpath.plus_file(file_name);
  93. file_item->set_metadata(0, file_metadata);
  94. if (!p_select_in_favorites && path == file_metadata) {
  95. file_item->select(0);
  96. file_item->set_as_cursor(0);
  97. }
  98. Array udata;
  99. udata.push_back(tree_update_id);
  100. udata.push_back(file_item);
  101. EditorResourcePreview::get_singleton()->queue_resource_preview(file_metadata, this, "_tree_thumbnail_done", udata);
  102. }
  103. }
  104. if (searched_string.length() > 0) {
  105. if (parent_should_expand) {
  106. subdirectory_item->set_collapsed(false);
  107. } else if (dname != "res://") {
  108. subdirectory_item->get_parent()->remove_child(subdirectory_item);
  109. }
  110. }
  111. return parent_should_expand;
  112. }
  113. Vector<String> FileSystemDock::_compute_uncollapsed_paths() {
  114. // Register currently collapsed paths
  115. Vector<String> uncollapsed_paths;
  116. TreeItem *root = tree->get_root();
  117. if (root) {
  118. TreeItem *favorites_item = root->get_children();
  119. if (!favorites_item->is_collapsed()) {
  120. uncollapsed_paths.push_back(favorites_item->get_metadata(0));
  121. }
  122. TreeItem *resTree = root->get_children()->get_next();
  123. if (resTree) {
  124. Vector<TreeItem *> needs_check;
  125. needs_check.push_back(resTree);
  126. while (needs_check.size()) {
  127. if (!needs_check[0]->is_collapsed()) {
  128. uncollapsed_paths.push_back(needs_check[0]->get_metadata(0));
  129. TreeItem *child = needs_check[0]->get_children();
  130. while (child) {
  131. needs_check.push_back(child);
  132. child = child->get_next();
  133. }
  134. }
  135. needs_check.remove(0);
  136. }
  137. }
  138. }
  139. return uncollapsed_paths;
  140. }
  141. void FileSystemDock::_update_tree(const Vector<String> p_uncollapsed_paths, bool p_uncollapse_root, bool p_select_in_favorites) {
  142. // Recreate the tree
  143. tree->clear();
  144. tree_update_id++;
  145. updating_tree = true;
  146. TreeItem *root = tree->create_item();
  147. // Handles the favorites
  148. TreeItem *favorites = tree->create_item(root);
  149. favorites->set_icon(0, get_icon("Favorites", "EditorIcons"));
  150. favorites->set_text(0, TTR("Favorites:"));
  151. favorites->set_metadata(0, "Favorites");
  152. favorites->set_collapsed(p_uncollapsed_paths.find("Favorites") < 0);
  153. Vector<String> favorite_paths = EditorSettings::get_singleton()->get_favorites();
  154. for (int i = 0; i < favorite_paths.size(); i++) {
  155. String fave = favorite_paths[i];
  156. if (!fave.begins_with("res://"))
  157. continue;
  158. Ref<Texture> folder_icon = get_icon("Folder", "EditorIcons");
  159. String text;
  160. Ref<Texture> icon;
  161. if (fave == "res://") {
  162. text = "/";
  163. icon = folder_icon;
  164. } else if (fave.ends_with("/")) {
  165. text = fave.substr(0, fave.length() - 1).get_file();
  166. icon = folder_icon;
  167. } else {
  168. text = fave.get_file();
  169. int index;
  170. EditorFileSystemDirectory *dir = EditorFileSystem::get_singleton()->find_file(fave, &index);
  171. if (dir) {
  172. icon = _get_tree_item_icon(dir, index);
  173. } else {
  174. icon = get_icon("File", "EditorIcons");
  175. }
  176. }
  177. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  178. TreeItem *ti = tree->create_item(favorites);
  179. ti->set_text(0, text);
  180. ti->set_icon(0, icon);
  181. ti->set_tooltip(0, fave);
  182. ti->set_selectable(0, true);
  183. ti->set_metadata(0, fave);
  184. if (p_select_in_favorites && fave == path) {
  185. ti->select(0);
  186. ti->set_as_cursor(0);
  187. }
  188. if (!fave.ends_with("/")) {
  189. Array udata;
  190. udata.push_back(tree_update_id);
  191. udata.push_back(ti);
  192. EditorResourcePreview::get_singleton()->queue_resource_preview(fave, this, "_tree_thumbnail_done", udata);
  193. }
  194. }
  195. }
  196. Vector<String> uncollapsed_paths = p_uncollapsed_paths;
  197. if (p_uncollapse_root) {
  198. uncollapsed_paths.push_back("res://");
  199. }
  200. // Create the remaining of the tree
  201. _create_tree(root, EditorFileSystem::get_singleton()->get_filesystem(), uncollapsed_paths, p_select_in_favorites);
  202. tree->ensure_cursor_is_visible();
  203. updating_tree = false;
  204. }
  205. void FileSystemDock::set_display_mode(DisplayMode p_display_mode) {
  206. display_mode = p_display_mode;
  207. _update_display_mode(false);
  208. }
  209. void FileSystemDock::_update_display_mode(bool p_force) {
  210. // Compute the new display mode
  211. if (p_force || old_display_mode != display_mode) {
  212. button_toggle_display_mode->set_pressed(display_mode == DISPLAY_MODE_SPLIT ? true : false);
  213. switch (display_mode) {
  214. case DISPLAY_MODE_TREE_ONLY:
  215. tree->show();
  216. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  217. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  218. tree_search_box->show();
  219. } else {
  220. tree_search_box->hide();
  221. }
  222. _update_tree(_compute_uncollapsed_paths());
  223. file_list_vb->hide();
  224. break;
  225. case DISPLAY_MODE_SPLIT:
  226. tree->show();
  227. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  228. tree->ensure_cursor_is_visible();
  229. tree_search_box->hide();
  230. _update_tree(_compute_uncollapsed_paths());
  231. file_list_vb->show();
  232. _update_file_list(true);
  233. break;
  234. }
  235. old_display_mode = display_mode;
  236. }
  237. }
  238. void FileSystemDock::_notification(int p_what) {
  239. switch (p_what) {
  240. case NOTIFICATION_ENTER_TREE: {
  241. if (initialized)
  242. return;
  243. initialized = true;
  244. EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "_fs_changed");
  245. EditorResourcePreview::get_singleton()->connect("preview_invalidated", this, "_preview_invalidated");
  246. String ei = "EditorIcons";
  247. button_reload->set_icon(get_icon("Reload", ei));
  248. button_toggle_display_mode->set_icon(get_icon("Panels2", ei));
  249. button_file_list_display_mode->connect("pressed", this, "_toggle_file_display");
  250. files->connect("item_activated", this, "_file_list_activate_file");
  251. button_hist_next->connect("pressed", this, "_fw_history");
  252. button_hist_prev->connect("pressed", this, "_bw_history");
  253. tree_search_box->set_right_icon(get_icon("Search", ei));
  254. tree_search_box->set_clear_button_enabled(true);
  255. file_list_search_box->set_right_icon(get_icon("Search", ei));
  256. file_list_search_box->set_clear_button_enabled(true);
  257. button_hist_next->set_icon(get_icon("Forward", ei));
  258. button_hist_prev->set_icon(get_icon("Back", ei));
  259. file_list_popup->connect("id_pressed", this, "_file_list_rmb_option");
  260. tree_popup->connect("id_pressed", this, "_tree_rmb_option");
  261. current_path->connect("text_entered", this, "_navigate_to_path");
  262. always_show_folders = bool(EditorSettings::get_singleton()->get("docks/filesystem/always_show_folders"));
  263. set_file_list_display_mode(FileSystemDock::FILE_LIST_DISPLAY_LIST);
  264. _update_display_mode();
  265. if (EditorFileSystem::get_singleton()->is_scanning()) {
  266. _set_scanning_mode();
  267. } else {
  268. _update_tree(Vector<String>(), true);
  269. }
  270. } break;
  271. case NOTIFICATION_PROCESS: {
  272. if (EditorFileSystem::get_singleton()->is_scanning()) {
  273. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
  274. }
  275. } break;
  276. case NOTIFICATION_EXIT_TREE: {
  277. } break;
  278. case NOTIFICATION_DRAG_BEGIN: {
  279. Dictionary dd = get_viewport()->gui_get_drag_data();
  280. if (tree->is_visible_in_tree() && dd.has("type")) {
  281. if ((String(dd["type"]) == "files") || (String(dd["type"]) == "files_and_dirs") || (String(dd["type"]) == "resource")) {
  282. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM | Tree::DROP_MODE_INBETWEEN);
  283. } else if ((String(dd["type"]) == "favorite")) {
  284. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  285. }
  286. }
  287. } break;
  288. case NOTIFICATION_DRAG_END: {
  289. tree->set_drop_mode_flags(0);
  290. } break;
  291. case NOTIFICATION_THEME_CHANGED: {
  292. if (is_visible_in_tree()) {
  293. _update_display_mode(true);
  294. }
  295. } break;
  296. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  297. // Update icons
  298. String ei = "EditorIcons";
  299. button_reload->set_icon(get_icon("Reload", ei));
  300. button_toggle_display_mode->set_icon(get_icon("Panels2", ei));
  301. button_hist_next->set_icon(get_icon("Forward", ei));
  302. button_hist_prev->set_icon(get_icon("Back", ei));
  303. if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
  304. button_file_list_display_mode->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  305. } else {
  306. button_file_list_display_mode->set_icon(get_icon("FileList", "EditorIcons"));
  307. }
  308. tree_search_box->set_right_icon(get_icon("Search", ei));
  309. tree_search_box->set_clear_button_enabled(true);
  310. file_list_search_box->set_right_icon(get_icon("Search", ei));
  311. file_list_search_box->set_clear_button_enabled(true);
  312. // Update always showfolders
  313. bool new_always_show_folders = bool(EditorSettings::get_singleton()->get("docks/filesystem/always_show_folders"));
  314. if (new_always_show_folders != always_show_folders) {
  315. always_show_folders = new_always_show_folders;
  316. _update_file_list(true);
  317. }
  318. // Change full tree mode
  319. _update_display_mode();
  320. } break;
  321. }
  322. }
  323. void FileSystemDock::_tree_multi_selected(Object *p_item, int p_column, bool p_selected) {
  324. // Update the import dock
  325. import_dock_needs_update = true;
  326. call_deferred("_update_import_dock");
  327. // Return if we don't select something new
  328. if (!p_selected)
  329. return;
  330. // Tree item selected
  331. TreeItem *selected = tree->get_selected();
  332. if (!selected)
  333. return;
  334. TreeItem *favorites_item = tree->get_root()->get_children();
  335. if (selected->get_parent() == favorites_item && !String(selected->get_metadata(0)).ends_with("/")) {
  336. // Go to the favorites if we click in the favorites and the path has changed
  337. path = "Favorites";
  338. } else {
  339. path = selected->get_metadata(0);
  340. // Note: the "Favorites" item also leads to this path
  341. }
  342. // Set the current path
  343. _set_current_path_text(path);
  344. _push_to_history();
  345. // Update the file list
  346. if (!updating_tree && display_mode == DISPLAY_MODE_SPLIT) {
  347. _update_file_list(false);
  348. }
  349. }
  350. String FileSystemDock::get_selected_path() const {
  351. if (path.ends_with("/"))
  352. return path;
  353. else
  354. return path.get_base_dir();
  355. }
  356. String FileSystemDock::get_current_path() const {
  357. return path;
  358. }
  359. void FileSystemDock::_set_current_path_text(const String &p_path) {
  360. if (p_path == "Favorites") {
  361. current_path->set_text(TTR("Favorites"));
  362. } else {
  363. current_path->set_text(path);
  364. }
  365. }
  366. void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_favorites) {
  367. if (p_path == "Favorites") {
  368. path = p_path;
  369. } else {
  370. String target_path = p_path;
  371. // If the path is a file, do not only go to the directory in the tree, also select the file in the file list.
  372. if (target_path.ends_with("/")) {
  373. target_path = target_path.substr(0, target_path.length() - 1);
  374. }
  375. DirAccess *dirAccess = DirAccess::open("res://");
  376. if (dirAccess->file_exists(p_path)) {
  377. path = target_path;
  378. } else if (dirAccess->dir_exists(p_path)) {
  379. path = target_path + "/";
  380. } else {
  381. ERR_EXPLAIN(vformat(TTR("Cannot navigate to '%s' as it has not been found in the file system!"), p_path));
  382. ERR_FAIL();
  383. }
  384. }
  385. _set_current_path_text(path);
  386. _push_to_history();
  387. _update_tree(_compute_uncollapsed_paths(), false, p_select_in_favorites);
  388. if (display_mode == DISPLAY_MODE_SPLIT) {
  389. _update_file_list(false);
  390. }
  391. String file_name = p_path.get_file();
  392. if (!file_name.empty()) {
  393. for (int i = 0; i < files->get_item_count(); i++) {
  394. if (files->get_item_text(i) == file_name) {
  395. files->select(i, true);
  396. files->ensure_current_is_visible();
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. void FileSystemDock::navigate_to_path(const String &p_path) {
  403. _navigate_to_path(p_path);
  404. }
  405. void FileSystemDock::_file_list_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, const Variant &p_udata) {
  406. if ((file_list_vb->is_visible_in_tree() || path == p_path.get_base_dir()) && p_preview.is_valid()) {
  407. Array uarr = p_udata;
  408. int idx = uarr[0];
  409. String file = uarr[1];
  410. if (idx < files->get_item_count() && files->get_item_text(idx) == file && files->get_item_metadata(idx) == p_path) {
  411. if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
  412. if (p_small_preview.is_valid())
  413. files->set_item_icon(idx, p_small_preview);
  414. } else {
  415. files->set_item_icon(idx, p_preview);
  416. }
  417. }
  418. }
  419. }
  420. void FileSystemDock::_tree_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, const Variant &p_udata) {
  421. if (p_small_preview.is_valid()) {
  422. Array uarr = p_udata;
  423. if (tree_update_id == (int)uarr[0]) {
  424. TreeItem *file_item = Object::cast_to<TreeItem>(uarr[1]);
  425. if (file_item) {
  426. file_item->set_icon(0, p_small_preview);
  427. }
  428. }
  429. }
  430. }
  431. void FileSystemDock::_toggle_file_display() {
  432. _set_file_display(file_list_display_mode != FILE_LIST_DISPLAY_LIST);
  433. emit_signal("display_mode_changed");
  434. }
  435. void FileSystemDock::_set_file_display(bool p_active) {
  436. if (p_active) {
  437. file_list_display_mode = FILE_LIST_DISPLAY_LIST;
  438. button_file_list_display_mode->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  439. button_file_list_display_mode->set_tooltip(TTR("View items as a grid of thumbnails."));
  440. } else {
  441. file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
  442. button_file_list_display_mode->set_icon(get_icon("FileList", "EditorIcons"));
  443. button_file_list_display_mode->set_tooltip(TTR("View items as a list."));
  444. }
  445. _update_file_list(true);
  446. }
  447. void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *matches, int p_max_items) {
  448. if (matches->size() > p_max_items)
  449. return;
  450. for (int i = 0; i < p_path->get_subdir_count(); i++) {
  451. _search(p_path->get_subdir(i), matches, p_max_items);
  452. }
  453. for (int i = 0; i < p_path->get_file_count(); i++) {
  454. String file = p_path->get_file(i);
  455. if (file.to_lower().find(searched_string) != -1) {
  456. FileInfo fi;
  457. fi.name = file;
  458. fi.type = p_path->get_file_type(i);
  459. fi.path = p_path->get_file_path(i);
  460. fi.import_broken = !p_path->get_file_import_is_valid(i);
  461. fi.import_status = 0;
  462. matches->push_back(fi);
  463. if (matches->size() > p_max_items)
  464. return;
  465. }
  466. }
  467. }
  468. void FileSystemDock::_update_file_list(bool p_keep_selection) {
  469. // Register the previously selected items
  470. Set<String> cselection;
  471. if (p_keep_selection) {
  472. for (int i = 0; i < files->get_item_count(); i++) {
  473. if (files->is_selected(i))
  474. cselection.insert(files->get_item_text(i));
  475. }
  476. }
  477. files->clear();
  478. _set_current_path_text(path);
  479. String directory = path;
  480. String file = "";
  481. String ei = "EditorIcons";
  482. int thumbnail_size = EditorSettings::get_singleton()->get("docks/filesystem/thumbnail_size");
  483. thumbnail_size *= EDSCALE;
  484. Ref<Texture> folder_thumbnail;
  485. Ref<Texture> file_thumbnail;
  486. Ref<Texture> file_thumbnail_broken;
  487. bool use_thumbnails = (file_list_display_mode == FILE_LIST_DISPLAY_THUMBNAILS);
  488. if (use_thumbnails) {
  489. // Thumbnails mode
  490. files->set_max_columns(0);
  491. files->set_icon_mode(ItemList::ICON_MODE_TOP);
  492. files->set_fixed_column_width(thumbnail_size * 3 / 2);
  493. files->set_max_text_lines(2);
  494. files->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  495. if (thumbnail_size < 64) {
  496. folder_thumbnail = get_icon("FolderMediumThumb", ei);
  497. file_thumbnail = get_icon("FileMediumThumb", ei);
  498. file_thumbnail_broken = get_icon("FileDeadMediumThumb", ei);
  499. } else {
  500. folder_thumbnail = get_icon("FolderBigThumb", ei);
  501. file_thumbnail = get_icon("FileBigThumb", ei);
  502. file_thumbnail_broken = get_icon("FileDeadBigThumb", ei);
  503. }
  504. } else {
  505. // No thumbnails
  506. files->set_icon_mode(ItemList::ICON_MODE_LEFT);
  507. files->set_max_columns(1);
  508. files->set_max_text_lines(1);
  509. files->set_fixed_column_width(0);
  510. files->set_fixed_icon_size(Size2());
  511. }
  512. Ref<Texture> folder_icon = (use_thumbnails) ? folder_thumbnail : get_icon("folder", "FileDialog");
  513. // Build the FileInfo list
  514. List<FileInfo> filelist;
  515. if (path == "Favorites") {
  516. // Display the favorites
  517. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  518. for (int i = 0; i < favorites.size(); i++) {
  519. String favorite = favorites[i];
  520. String text;
  521. Ref<Texture> icon;
  522. if (favorite == "res://") {
  523. text = "/";
  524. icon = folder_icon;
  525. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  526. files->add_item(text, icon, true);
  527. files->set_item_metadata(files->get_item_count() - 1, favorite);
  528. }
  529. } else if (favorite.ends_with("/")) {
  530. text = favorite.substr(0, favorite.length() - 1).get_file();
  531. icon = folder_icon;
  532. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  533. files->add_item(text, icon, true);
  534. files->set_item_metadata(files->get_item_count() - 1, favorite);
  535. }
  536. } else {
  537. int index;
  538. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->find_file(favorite, &index);
  539. FileInfo fi;
  540. fi.name = favorite.get_file();
  541. fi.path = favorite;
  542. if (efd) {
  543. fi.type = efd->get_file_type(index);
  544. fi.import_broken = !efd->get_file_import_is_valid(index);
  545. } else {
  546. fi.type = "";
  547. fi.import_broken = true;
  548. }
  549. fi.import_status = 0;
  550. if (searched_string.length() == 0 || fi.name.to_lower().find(searched_string) >= 0) {
  551. filelist.push_back(fi);
  552. }
  553. }
  554. }
  555. } else {
  556. // Get infos on the directory + file
  557. if (directory.ends_with("/") && directory != "res://") {
  558. directory = directory.substr(0, directory.length() - 1);
  559. }
  560. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
  561. if (!efd) {
  562. directory = path.get_base_dir();
  563. file = path.get_file();
  564. efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
  565. }
  566. if (!efd)
  567. return;
  568. if (searched_string.length() > 0) {
  569. // Display the search results
  570. _search(EditorFileSystem::get_singleton()->get_filesystem(), &filelist, 128);
  571. } else {
  572. if (display_mode == DISPLAY_MODE_TREE_ONLY || always_show_folders) {
  573. // Display folders in the list
  574. if (directory != "res://") {
  575. files->add_item("..", folder_icon, true);
  576. String bd = directory.get_base_dir();
  577. if (bd != "res://" && !bd.ends_with("/"))
  578. bd += "/";
  579. files->set_item_metadata(files->get_item_count() - 1, bd);
  580. files->set_item_selectable(files->get_item_count() - 1, false);
  581. }
  582. for (int i = 0; i < efd->get_subdir_count(); i++) {
  583. String dname = efd->get_subdir(i)->get_name();
  584. files->add_item(dname, folder_icon, true);
  585. files->set_item_metadata(files->get_item_count() - 1, directory.plus_file(dname) + "/");
  586. if (cselection.has(dname)) {
  587. files->select(files->get_item_count() - 1, false);
  588. }
  589. }
  590. }
  591. // Display the folder content
  592. for (int i = 0; i < efd->get_file_count(); i++) {
  593. FileInfo fi;
  594. fi.name = efd->get_file(i);
  595. fi.path = directory.plus_file(fi.name);
  596. fi.type = efd->get_file_type(i);
  597. fi.import_broken = !efd->get_file_import_is_valid(i);
  598. fi.import_status = 0;
  599. filelist.push_back(fi);
  600. }
  601. }
  602. filelist.sort();
  603. }
  604. // Fills the ItemList control node from the FileInfos
  605. String oi = "Object";
  606. for (List<FileInfo>::Element *E = filelist.front(); E; E = E->next()) {
  607. FileInfo *finfo = &(E->get());
  608. String fname = finfo->name;
  609. String fpath = finfo->path;
  610. String ftype = finfo->type;
  611. Ref<Texture> type_icon;
  612. Ref<Texture> big_icon;
  613. String tooltip = fname;
  614. // Select the icons
  615. if (!finfo->import_broken) {
  616. type_icon = (has_icon(ftype, ei)) ? get_icon(ftype, ei) : get_icon(oi, ei);
  617. big_icon = file_thumbnail;
  618. } else {
  619. type_icon = get_icon("ImportFail", ei);
  620. big_icon = file_thumbnail_broken;
  621. tooltip += "\n" + TTR("Status: Import of file failed. Please fix file and reimport manually.");
  622. }
  623. // Add the item to the ItemList
  624. int item_index;
  625. if (use_thumbnails) {
  626. files->add_item(fname, big_icon, true);
  627. item_index = files->get_item_count() - 1;
  628. files->set_item_metadata(item_index, fpath);
  629. files->set_item_tag_icon(item_index, type_icon);
  630. } else {
  631. files->add_item(fname, type_icon, true);
  632. item_index = files->get_item_count() - 1;
  633. files->set_item_metadata(item_index, fpath);
  634. }
  635. // Generate the preview
  636. if (!finfo->import_broken) {
  637. Array udata;
  638. udata.resize(2);
  639. udata[0] = item_index;
  640. udata[1] = fname;
  641. EditorResourcePreview::get_singleton()->queue_resource_preview(fpath, this, "_file_list_thumbnail_done", udata);
  642. }
  643. // Select the items
  644. if (cselection.has(fname))
  645. files->select(item_index, false);
  646. if (!p_keep_selection && file != "" && fname == file) {
  647. files->select(item_index, true);
  648. files->ensure_current_is_visible();
  649. }
  650. // Tooltip
  651. if (finfo->sources.size()) {
  652. for (int j = 0; j < finfo->sources.size(); j++) {
  653. tooltip += "\nSource: " + finfo->sources[j];
  654. }
  655. }
  656. files->set_item_tooltip(item_index, tooltip);
  657. }
  658. }
  659. void FileSystemDock::_select_file(const String p_path, bool p_select_in_favorites) {
  660. String fpath = p_path;
  661. if (fpath.ends_with("/")) {
  662. if (fpath != "res://") {
  663. fpath = fpath.substr(0, fpath.length() - 1);
  664. }
  665. } else if (fpath != "Favorites") {
  666. if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
  667. editor->open_request(fpath);
  668. } else {
  669. editor->load_resource(fpath);
  670. }
  671. }
  672. _navigate_to_path(fpath, p_select_in_favorites);
  673. }
  674. void FileSystemDock::_tree_activate_file() {
  675. TreeItem *selected = tree->get_selected();
  676. if (selected) {
  677. String path = selected->get_metadata(0);
  678. TreeItem *parent = selected->get_parent();
  679. bool is_favorite = parent != NULL && parent->get_metadata(0) == "Favorites";
  680. if ((!is_favorite && path.ends_with("/")) || path == "Favorites") {
  681. bool collapsed = selected->is_collapsed();
  682. selected->set_collapsed(!collapsed);
  683. } else {
  684. _select_file(path, is_favorite && !path.ends_with("/"));
  685. }
  686. }
  687. }
  688. void FileSystemDock::_file_list_activate_file(int p_idx) {
  689. _select_file(files->get_item_metadata(p_idx));
  690. }
  691. void FileSystemDock::_preview_invalidated(const String &p_path) {
  692. if (file_list_display_mode == FILE_LIST_DISPLAY_THUMBNAILS && p_path.get_base_dir() == path && searched_string.length() == 0 && file_list_vb->is_visible_in_tree()) {
  693. for (int i = 0; i < files->get_item_count(); i++) {
  694. if (files->get_item_metadata(i) == p_path) {
  695. //re-request preview
  696. Array udata;
  697. udata.resize(2);
  698. udata[0] = i;
  699. udata[1] = files->get_item_text(i);
  700. EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, this, "_file_list_thumbnail_done", udata);
  701. break;
  702. }
  703. }
  704. }
  705. }
  706. void FileSystemDock::_fs_changed() {
  707. button_hist_prev->set_disabled(history_pos == 0);
  708. button_hist_next->set_disabled(history_pos == history.size() - 1);
  709. scanning_vb->hide();
  710. split_box->show();
  711. if (tree->is_visible()) {
  712. _update_tree(_compute_uncollapsed_paths());
  713. }
  714. if (file_list_vb->is_visible()) {
  715. _update_file_list(true);
  716. }
  717. set_process(false);
  718. }
  719. void FileSystemDock::_set_scanning_mode() {
  720. button_hist_prev->set_disabled(true);
  721. button_hist_next->set_disabled(true);
  722. split_box->hide();
  723. scanning_vb->show();
  724. set_process(true);
  725. if (EditorFileSystem::get_singleton()->is_scanning()) {
  726. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
  727. } else {
  728. scanning_progress->set_value(0);
  729. }
  730. }
  731. void FileSystemDock::_fw_history() {
  732. if (history_pos < history.size() - 1)
  733. history_pos++;
  734. _update_history();
  735. }
  736. void FileSystemDock::_bw_history() {
  737. if (history_pos > 0)
  738. history_pos--;
  739. _update_history();
  740. }
  741. void FileSystemDock::_update_history() {
  742. path = history[history_pos];
  743. _set_current_path_text(path);
  744. if (tree->is_visible()) {
  745. _update_tree(_compute_uncollapsed_paths());
  746. tree->grab_focus();
  747. tree->ensure_cursor_is_visible();
  748. }
  749. if (file_list_vb->is_visible()) {
  750. _update_file_list(false);
  751. }
  752. button_hist_prev->set_disabled(history_pos == 0);
  753. button_hist_next->set_disabled(history_pos == history.size() - 1);
  754. }
  755. void FileSystemDock::_push_to_history() {
  756. if (history[history_pos] != path) {
  757. history.resize(history_pos + 1);
  758. history.push_back(path);
  759. history_pos++;
  760. if (history.size() > history_max_size) {
  761. history.remove(0);
  762. history_pos = history_max_size - 1;
  763. }
  764. }
  765. button_hist_prev->set_disabled(history_pos == 0);
  766. button_hist_next->set_disabled(history_pos == history.size() - 1);
  767. }
  768. void FileSystemDock::_get_all_items_in_dir(EditorFileSystemDirectory *efsd, Vector<String> &files, Vector<String> &folders) const {
  769. if (efsd == NULL)
  770. return;
  771. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  772. folders.push_back(efsd->get_subdir(i)->get_path());
  773. _get_all_items_in_dir(efsd->get_subdir(i), files, folders);
  774. }
  775. for (int i = 0; i < efsd->get_file_count(); i++) {
  776. files.push_back(efsd->get_file_path(i));
  777. }
  778. }
  779. void FileSystemDock::_find_remaps(EditorFileSystemDirectory *efsd, const Map<String, String> &renames, Vector<String> &to_remaps) const {
  780. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  781. _find_remaps(efsd->get_subdir(i), renames, to_remaps);
  782. }
  783. for (int i = 0; i < efsd->get_file_count(); i++) {
  784. Vector<String> deps = efsd->get_file_deps(i);
  785. for (int j = 0; j < deps.size(); j++) {
  786. if (renames.has(deps[j])) {
  787. to_remaps.push_back(efsd->get_file_path(i));
  788. break;
  789. }
  790. }
  791. }
  792. }
  793. void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_new_path,
  794. Map<String, String> &p_file_renames, Map<String, String> &p_folder_renames) {
  795. //Ensure folder paths end with "/"
  796. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  797. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  798. if (new_path == old_path) {
  799. return;
  800. } else if (old_path == "res://") {
  801. EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
  802. return;
  803. } else if (!p_item.is_file && new_path.begins_with(old_path)) {
  804. //This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/"
  805. EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
  806. return;
  807. }
  808. //Build a list of files which will have new paths as a result of this operation
  809. Vector<String> file_changed_paths;
  810. Vector<String> folder_changed_paths;
  811. if (p_item.is_file) {
  812. file_changed_paths.push_back(old_path);
  813. } else {
  814. folder_changed_paths.push_back(old_path);
  815. _get_all_items_in_dir(EditorFileSystem::get_singleton()->get_filesystem_path(old_path), file_changed_paths, folder_changed_paths);
  816. }
  817. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  818. print_verbose("Moving " + old_path + " -> " + new_path);
  819. Error err = da->rename(old_path, new_path);
  820. if (err == OK) {
  821. //Move/Rename any corresponding import settings too
  822. if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
  823. err = da->rename(old_path + ".import", new_path + ".import");
  824. if (err != OK) {
  825. EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + ".import\n");
  826. }
  827. }
  828. // update scene if it is open
  829. for (int i = 0; i < file_changed_paths.size(); ++i) {
  830. String new_item_path = p_item.is_file ? new_path : file_changed_paths[i].replace_first(old_path, new_path);
  831. if (ResourceLoader::get_resource_type(new_item_path) == "PackedScene" && editor->is_scene_open(file_changed_paths[i])) {
  832. EditorData *ed = &editor->get_editor_data();
  833. for (int j = 0; j < ed->get_edited_scene_count(); j++) {
  834. if (ed->get_scene_path(j) == file_changed_paths[i]) {
  835. ed->get_edited_scene_root(j)->set_filename(new_item_path);
  836. break;
  837. }
  838. }
  839. }
  840. }
  841. //Only treat as a changed dependency if it was successfully moved
  842. for (int i = 0; i < file_changed_paths.size(); ++i) {
  843. p_file_renames[file_changed_paths[i]] = file_changed_paths[i].replace_first(old_path, new_path);
  844. print_verbose(" Remap: " + file_changed_paths[i] + " -> " + p_file_renames[file_changed_paths[i]]);
  845. emit_signal("files_moved", file_changed_paths[i], p_file_renames[file_changed_paths[i]]);
  846. }
  847. for (int i = 0; i < folder_changed_paths.size(); ++i) {
  848. p_folder_renames[folder_changed_paths[i]] = folder_changed_paths[i].replace_first(old_path, new_path);
  849. emit_signal("folder_moved", folder_changed_paths[i], p_folder_renames[folder_changed_paths[i]].substr(0, p_folder_renames[folder_changed_paths[i]].length() - 1));
  850. }
  851. } else {
  852. EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + "\n");
  853. }
  854. memdelete(da);
  855. }
  856. void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const {
  857. //Ensure folder paths end with "/"
  858. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  859. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  860. if (new_path == old_path) {
  861. return;
  862. } else if (old_path == "res://") {
  863. EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
  864. return;
  865. } else if (!p_item.is_file && new_path.begins_with(old_path)) {
  866. //This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/"
  867. EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
  868. return;
  869. }
  870. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  871. print_verbose("Duplicating " + old_path + " -> " + new_path);
  872. Error err = p_item.is_file ? da->copy(old_path, new_path) : da->copy_dir(old_path, new_path);
  873. if (err == OK) {
  874. //Move/Rename any corresponding import settings too
  875. if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
  876. err = da->copy(old_path + ".import", new_path + ".import");
  877. if (err != OK) {
  878. EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + ".import\n");
  879. }
  880. }
  881. } else {
  882. EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + "\n");
  883. }
  884. memdelete(da);
  885. }
  886. void FileSystemDock::_update_resource_paths_after_move(const Map<String, String> &p_renames) const {
  887. //Rename all resources loaded, be it subresources or actual resources
  888. List<Ref<Resource> > cached;
  889. ResourceCache::get_cached_resources(&cached);
  890. for (List<Ref<Resource> >::Element *E = cached.front(); E; E = E->next()) {
  891. Ref<Resource> r = E->get();
  892. String base_path = r->get_path();
  893. String extra_path;
  894. int sep_pos = r->get_path().find("::");
  895. if (sep_pos >= 0) {
  896. extra_path = base_path.substr(sep_pos, base_path.length());
  897. base_path = base_path.substr(0, sep_pos);
  898. }
  899. if (p_renames.has(base_path)) {
  900. base_path = p_renames[base_path];
  901. }
  902. r->set_path(base_path + extra_path);
  903. }
  904. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
  905. String path;
  906. if (i == EditorNode::get_editor_data().get_edited_scene()) {
  907. if (!get_tree()->get_edited_scene_root())
  908. continue;
  909. path = get_tree()->get_edited_scene_root()->get_filename();
  910. } else {
  911. path = EditorNode::get_editor_data().get_scene_path(i);
  912. }
  913. if (p_renames.has(path)) {
  914. path = p_renames[path];
  915. }
  916. if (i == EditorNode::get_editor_data().get_edited_scene()) {
  917. get_tree()->get_edited_scene_root()->set_filename(path);
  918. } else {
  919. EditorNode::get_editor_data().set_scene_path(i, path);
  920. }
  921. }
  922. }
  923. void FileSystemDock::_update_dependencies_after_move(const Map<String, String> &p_renames) const {
  924. //The following code assumes that the following holds:
  925. // 1) EditorFileSystem contains the old paths/folder structure from before the rename/move.
  926. // 2) ResourceLoader can use the new paths without needing to call rescan.
  927. Vector<String> remaps;
  928. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(), p_renames, remaps);
  929. for (int i = 0; i < remaps.size(); ++i) {
  930. //Because we haven't called a rescan yet the found remap might still be an old path itself.
  931. String file = p_renames.has(remaps[i]) ? p_renames[remaps[i]] : remaps[i];
  932. print_verbose("Remapping dependencies for: " + file);
  933. Error err = ResourceLoader::rename_dependencies(file, p_renames);
  934. if (err == OK) {
  935. if (ResourceLoader::get_resource_type(file) == "PackedScene")
  936. editor->reload_scene(file);
  937. } else {
  938. EditorNode::get_singleton()->add_io_error(TTR("Unable to update dependencies:") + "\n" + remaps[i] + "\n");
  939. }
  940. }
  941. }
  942. void FileSystemDock::_update_project_settings_after_move(const Map<String, String> &p_renames) const {
  943. // Find all project settings of type FILE and replace them if needed
  944. const Map<StringName, PropertyInfo> prop_info = ProjectSettings::get_singleton()->get_custom_property_info();
  945. for (const Map<StringName, PropertyInfo>::Element *E = prop_info.front(); E; E = E->next()) {
  946. if (E->get().hint == PROPERTY_HINT_FILE) {
  947. String old_path = GLOBAL_GET(E->key());
  948. if (p_renames.has(old_path)) {
  949. ProjectSettings::get_singleton()->set_setting(E->key(), p_renames[old_path]);
  950. }
  951. };
  952. }
  953. // Also search for the file in autoload, as they are stored differently from normal files.
  954. List<PropertyInfo> property_list;
  955. ProjectSettings::get_singleton()->get_property_list(&property_list);
  956. for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  957. if (E->get().name.begins_with("autoload/")) {
  958. // If the autoload resource paths has a leading "*", it indicates that it is a Singleton,
  959. // so we have to handle both cases when updating.
  960. String autoload = GLOBAL_GET(E->get().name);
  961. String autoload_singleton = autoload.substr(1, autoload.length());
  962. if (p_renames.has(autoload)) {
  963. ProjectSettings::get_singleton()->set_setting(E->get().name, p_renames[autoload]);
  964. } else if (autoload.begins_with("*") && p_renames.has(autoload_singleton)) {
  965. ProjectSettings::get_singleton()->set_setting(E->get().name, "*" + p_renames[autoload_singleton]);
  966. }
  967. }
  968. }
  969. ProjectSettings::get_singleton()->save();
  970. }
  971. void FileSystemDock::_update_favorites_list_after_move(const Map<String, String> &p_files_renames, const Map<String, String> &p_folders_renames) const {
  972. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  973. Vector<String> new_favorites;
  974. for (int i = 0; i < favorites.size(); i++) {
  975. String old_path = favorites[i];
  976. if (p_folders_renames.has(old_path)) {
  977. new_favorites.push_back(p_folders_renames[old_path]);
  978. } else if (p_files_renames.has(old_path)) {
  979. new_favorites.push_back(p_files_renames[old_path]);
  980. } else {
  981. new_favorites.push_back(old_path);
  982. }
  983. }
  984. EditorSettings::get_singleton()->set_favorites(new_favorites);
  985. }
  986. void FileSystemDock::_make_dir_confirm() {
  987. String dir_name = make_dir_dialog_text->get_text().strip_edges();
  988. if (dir_name.length() == 0) {
  989. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  990. return;
  991. } else if (dir_name.find("/") != -1 || dir_name.find("\\") != -1 || dir_name.find(":") != -1 || dir_name.find("*") != -1 ||
  992. dir_name.find("|") != -1 || dir_name.find(">") != -1 || dir_name.ends_with(".") || dir_name.ends_with(" ")) {
  993. EditorNode::get_singleton()->show_warning(TTR("Provided name contains invalid characters"));
  994. return;
  995. }
  996. String directory = path;
  997. if (!directory.ends_with("/")) {
  998. directory = directory.get_base_dir();
  999. }
  1000. print_verbose("Making folder " + dir_name + " in " + directory);
  1001. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1002. Error err = da->change_dir(directory);
  1003. if (err == OK) {
  1004. err = da->make_dir(dir_name);
  1005. }
  1006. memdelete(da);
  1007. if (err == OK) {
  1008. print_verbose("FileSystem: calling rescan.");
  1009. _rescan();
  1010. } else {
  1011. EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
  1012. }
  1013. }
  1014. void FileSystemDock::_file_deleted(String p_file) {
  1015. emit_signal("file_deleted", p_file);
  1016. }
  1017. void FileSystemDock::_folder_deleted(String p_folder) {
  1018. emit_signal("folder_deleted", p_folder);
  1019. }
  1020. void FileSystemDock::_rename_operation_confirm() {
  1021. String new_name = rename_dialog_text->get_text().strip_edges();
  1022. if (new_name.length() == 0) {
  1023. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1024. return;
  1025. } else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
  1026. EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
  1027. return;
  1028. }
  1029. String old_path = to_rename.path.ends_with("/") ? to_rename.path.substr(0, to_rename.path.length() - 1) : to_rename.path;
  1030. String new_path = old_path.get_base_dir().plus_file(new_name);
  1031. if (old_path == new_path) {
  1032. return;
  1033. }
  1034. //Present a more user friendly warning for name conflict
  1035. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1036. #if defined(WINDOWS_ENABLED) || defined(UWP_ENABLED)
  1037. // Workaround case insensitivity on Windows
  1038. if ((da->file_exists(new_path) || da->dir_exists(new_path)) && new_path.to_lower() != old_path.to_lower()) {
  1039. #else
  1040. if (da->file_exists(new_path) || da->dir_exists(new_path)) {
  1041. #endif
  1042. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  1043. memdelete(da);
  1044. return;
  1045. }
  1046. memdelete(da);
  1047. Map<String, String> file_renames;
  1048. Map<String, String> folder_renames;
  1049. _try_move_item(to_rename, new_path, file_renames, folder_renames);
  1050. _update_dependencies_after_move(file_renames);
  1051. _update_resource_paths_after_move(file_renames);
  1052. _update_project_settings_after_move(file_renames);
  1053. _update_favorites_list_after_move(file_renames, folder_renames);
  1054. //Rescan everything
  1055. print_verbose("FileSystem: calling rescan.");
  1056. _rescan();
  1057. }
  1058. void FileSystemDock::_duplicate_operation_confirm() {
  1059. String new_name = duplicate_dialog_text->get_text().strip_edges();
  1060. if (new_name.length() == 0) {
  1061. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1062. return;
  1063. } else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
  1064. EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
  1065. return;
  1066. }
  1067. String new_path;
  1068. String base_dir = to_duplicate.path.get_base_dir();
  1069. if (to_duplicate.is_file) {
  1070. new_path = base_dir.plus_file(new_name);
  1071. } else {
  1072. new_path = base_dir.substr(0, base_dir.find_last("/")) + "/" + new_name;
  1073. }
  1074. //Present a more user friendly warning for name conflict
  1075. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1076. if (da->file_exists(new_path) || da->dir_exists(new_path)) {
  1077. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  1078. memdelete(da);
  1079. return;
  1080. }
  1081. memdelete(da);
  1082. _try_duplicate_item(to_duplicate, new_path);
  1083. //Rescan everything
  1084. print_verbose("FileSystem: calling rescan.");
  1085. _rescan();
  1086. }
  1087. void FileSystemDock::_move_with_overwrite() {
  1088. _move_operation_confirm(to_move_path, true);
  1089. }
  1090. bool FileSystemDock::_check_existing() {
  1091. String &p_to_path = to_move_path;
  1092. for (int i = 0; i < to_move.size(); i++) {
  1093. String ol_pth = to_move[i].path.ends_with("/") ? to_move[i].path.substr(0, to_move[i].path.length() - 1) : to_move[i].path;
  1094. String p_new_path = p_to_path.plus_file(ol_pth.get_file());
  1095. FileOrFolder p_item = to_move[i];
  1096. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  1097. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  1098. if (p_item.is_file && FileAccess::exists(new_path)) {
  1099. return false;
  1100. } else if (!p_item.is_file && DirAccess::exists(new_path)) {
  1101. return false;
  1102. }
  1103. }
  1104. return true;
  1105. }
  1106. void FileSystemDock::_move_operation_confirm(const String &p_to_path, bool overwrite) {
  1107. if (!overwrite) {
  1108. to_move_path = p_to_path;
  1109. bool can_move = _check_existing();
  1110. if (!can_move) {
  1111. //ask to do something
  1112. overwrite_dialog->popup_centered_minsize();
  1113. overwrite_dialog->grab_focus();
  1114. return;
  1115. }
  1116. }
  1117. Map<String, String> file_renames;
  1118. Map<String, String> folder_renames;
  1119. bool is_moved = false;
  1120. for (int i = 0; i < to_move.size(); i++) {
  1121. String old_path = to_move[i].path.ends_with("/") ? to_move[i].path.substr(0, to_move[i].path.length() - 1) : to_move[i].path;
  1122. String new_path = p_to_path.plus_file(old_path.get_file());
  1123. if (old_path != new_path) {
  1124. _try_move_item(to_move[i], new_path, file_renames, folder_renames);
  1125. is_moved = true;
  1126. }
  1127. }
  1128. if (is_moved) {
  1129. _update_dependencies_after_move(file_renames);
  1130. _update_resource_paths_after_move(file_renames);
  1131. _update_project_settings_after_move(file_renames);
  1132. _update_favorites_list_after_move(file_renames, folder_renames);
  1133. print_verbose("FileSystem: calling rescan.");
  1134. _rescan();
  1135. }
  1136. }
  1137. Vector<String> FileSystemDock::_tree_get_selected(bool remove_self_inclusion) {
  1138. // Build a list of selected items with the active one at the first position
  1139. Vector<String> selected_strings;
  1140. TreeItem *favorites_item = tree->get_root()->get_children();
  1141. TreeItem *active_selected = tree->get_selected();
  1142. if (active_selected && active_selected != favorites_item) {
  1143. selected_strings.push_back(active_selected->get_metadata(0));
  1144. }
  1145. TreeItem *selected = tree->get_root();
  1146. selected = tree->get_next_selected(selected);
  1147. while (selected) {
  1148. if (selected != active_selected && selected != favorites_item) {
  1149. selected_strings.push_back(selected->get_metadata(0));
  1150. }
  1151. selected = tree->get_next_selected(selected);
  1152. }
  1153. // Remove paths or files that are included into another
  1154. if (remove_self_inclusion && selected_strings.size() > 1) {
  1155. selected_strings.sort_custom<NaturalNoCaseComparator>();
  1156. String last_path = "";
  1157. for (int i = 0; i < selected_strings.size(); i++) {
  1158. if (last_path != "" && selected_strings[i].begins_with(last_path)) {
  1159. selected_strings.remove(i);
  1160. i--;
  1161. }
  1162. if (selected_strings[i].ends_with("/")) {
  1163. last_path = selected_strings[i];
  1164. }
  1165. }
  1166. }
  1167. return selected_strings;
  1168. }
  1169. void FileSystemDock::_tree_rmb_option(int p_option) {
  1170. Vector<String> selected_strings = _tree_get_selected();
  1171. // Execute the current option
  1172. switch (p_option) {
  1173. case FOLDER_EXPAND_ALL:
  1174. case FOLDER_COLLAPSE_ALL: {
  1175. // Expand or collapse the folder
  1176. if (selected_strings.size() == 1) {
  1177. bool is_collapsed = (p_option == FOLDER_COLLAPSE_ALL);
  1178. Vector<TreeItem *> needs_check;
  1179. needs_check.push_back(tree->get_selected());
  1180. while (needs_check.size()) {
  1181. needs_check[0]->set_collapsed(is_collapsed);
  1182. TreeItem *child = needs_check[0]->get_children();
  1183. while (child) {
  1184. needs_check.push_back(child);
  1185. child = child->get_next();
  1186. }
  1187. needs_check.remove(0);
  1188. }
  1189. }
  1190. } break;
  1191. default: {
  1192. _file_option(p_option, selected_strings);
  1193. } break;
  1194. }
  1195. }
  1196. void FileSystemDock::_file_list_rmb_option(int p_option) {
  1197. Vector<int> selected_id = files->get_selected_items();
  1198. Vector<String> selected;
  1199. for (int i = 0; i < selected_id.size(); i++) {
  1200. selected.push_back(files->get_item_metadata(selected_id[i]));
  1201. }
  1202. _file_option(p_option, selected);
  1203. }
  1204. void FileSystemDock::_file_option(int p_option, const Vector<String> p_selected) {
  1205. // The first one should be the active item
  1206. switch (p_option) {
  1207. case FILE_SHOW_IN_EXPLORER: {
  1208. // Show the file / folder in the OS explorer
  1209. String fpath = path;
  1210. if (!fpath.ends_with("/")) {
  1211. fpath = fpath.get_base_dir();
  1212. }
  1213. String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
  1214. OS::get_singleton()->shell_open(String("file://") + dir);
  1215. } break;
  1216. case FILE_OPEN: {
  1217. // Open the file
  1218. for (int i = 0; i < p_selected.size(); i++) {
  1219. _select_file(p_selected[i]);
  1220. }
  1221. } break;
  1222. case FILE_INSTANCE: {
  1223. // Instance all selected scenes
  1224. Vector<String> paths;
  1225. for (int i = 0; i < p_selected.size(); i++) {
  1226. String fpath = p_selected[i];
  1227. if (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene") {
  1228. paths.push_back(fpath);
  1229. }
  1230. }
  1231. if (!paths.empty()) {
  1232. emit_signal("instance", paths);
  1233. }
  1234. } break;
  1235. case FILE_ADD_FAVORITE: {
  1236. // Add the files from favorites
  1237. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1238. for (int i = 0; i < p_selected.size(); i++) {
  1239. if (favorites.find(p_selected[i]) == -1) {
  1240. favorites.push_back(p_selected[i]);
  1241. }
  1242. }
  1243. EditorSettings::get_singleton()->set_favorites(favorites);
  1244. _update_tree(_compute_uncollapsed_paths());
  1245. } break;
  1246. case FILE_REMOVE_FAVORITE: {
  1247. // Remove the files from favorites
  1248. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1249. for (int i = 0; i < p_selected.size(); i++) {
  1250. favorites.erase(p_selected[i]);
  1251. }
  1252. EditorSettings::get_singleton()->set_favorites(favorites);
  1253. _update_tree(_compute_uncollapsed_paths());
  1254. if (path == "Favorites")
  1255. _update_file_list(true);
  1256. } break;
  1257. case FILE_DEPENDENCIES: {
  1258. // Checkout the file dependencies
  1259. if (!p_selected.empty()) {
  1260. String fpath = p_selected[0];
  1261. deps_editor->edit(fpath);
  1262. }
  1263. } break;
  1264. case FILE_OWNERS: {
  1265. // Checkout the file owners
  1266. if (!p_selected.empty()) {
  1267. String fpath = p_selected[0];
  1268. owners_editor->show(fpath);
  1269. }
  1270. } break;
  1271. case FILE_MOVE: {
  1272. // Move the files to a given location
  1273. to_move.clear();
  1274. for (int i = 0; i < p_selected.size(); i++) {
  1275. String fpath = p_selected[i];
  1276. if (fpath != "res://") {
  1277. to_move.push_back(FileOrFolder(fpath, !fpath.ends_with("/")));
  1278. }
  1279. }
  1280. if (to_move.size() > 0) {
  1281. move_dialog->popup_centered_ratio();
  1282. }
  1283. } break;
  1284. case FILE_RENAME: {
  1285. // Rename the active file
  1286. if (!p_selected.empty()) {
  1287. to_rename.path = p_selected[0];
  1288. if (to_rename.path != "res://") {
  1289. to_rename.is_file = !to_rename.path.ends_with("/");
  1290. if (to_rename.is_file) {
  1291. String name = to_rename.path.get_file();
  1292. rename_dialog->set_title(TTR("Renaming file:") + " " + name);
  1293. rename_dialog_text->set_text(name);
  1294. rename_dialog_text->select(0, name.find_last("."));
  1295. } else {
  1296. String name = to_rename.path.substr(0, to_rename.path.length() - 1).get_file();
  1297. rename_dialog->set_title(TTR("Renaming folder:") + " " + name);
  1298. rename_dialog_text->set_text(name);
  1299. rename_dialog_text->select(0, name.length());
  1300. }
  1301. rename_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  1302. rename_dialog_text->grab_focus();
  1303. }
  1304. }
  1305. } break;
  1306. case FILE_REMOVE: {
  1307. // Remove the selected files
  1308. Vector<String> remove_files;
  1309. Vector<String> remove_folders;
  1310. for (int i = 0; i < p_selected.size(); i++) {
  1311. String fpath = p_selected[i];
  1312. if (fpath != "res://") {
  1313. if (fpath.ends_with("/")) {
  1314. remove_folders.push_back(fpath);
  1315. } else {
  1316. remove_files.push_back(fpath);
  1317. }
  1318. }
  1319. }
  1320. if (remove_files.size() + remove_folders.size() > 0) {
  1321. remove_dialog->show(remove_folders, remove_files);
  1322. }
  1323. } break;
  1324. case FILE_DUPLICATE: {
  1325. // Duplicate the selected files
  1326. for (int i = 0; i < p_selected.size(); i++) {
  1327. to_duplicate.path = p_selected[i];
  1328. to_duplicate.is_file = !to_duplicate.path.ends_with("/");
  1329. if (to_duplicate.is_file) {
  1330. String name = to_duplicate.path.get_file();
  1331. duplicate_dialog->set_title(TTR("Duplicating file:") + " " + name);
  1332. duplicate_dialog_text->set_text(name);
  1333. duplicate_dialog_text->select(0, name.find_last("."));
  1334. } else {
  1335. String name = to_duplicate.path.substr(0, to_duplicate.path.length() - 1).get_file();
  1336. duplicate_dialog->set_title(TTR("Duplicating folder:") + " " + name);
  1337. duplicate_dialog_text->set_text(name);
  1338. duplicate_dialog_text->select(0, name.length());
  1339. }
  1340. duplicate_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  1341. duplicate_dialog_text->grab_focus();
  1342. }
  1343. } break;
  1344. case FILE_INFO: {
  1345. } break;
  1346. case FILE_REIMPORT: {
  1347. // Reimport all selected files
  1348. Vector<String> reimport;
  1349. for (int i = 0; i < p_selected.size(); i++) {
  1350. reimport.push_back(p_selected[i]);
  1351. }
  1352. ERR_FAIL_COND(reimport.size() == 0);
  1353. } break;
  1354. case FILE_NEW_FOLDER: {
  1355. // Create a new folder
  1356. make_dir_dialog_text->set_text("new folder");
  1357. make_dir_dialog_text->select_all();
  1358. make_dir_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  1359. make_dir_dialog_text->grab_focus();
  1360. } break;
  1361. case FILE_NEW_SCRIPT: {
  1362. // Create a new script
  1363. String fpath = path;
  1364. if (!fpath.ends_with("/")) {
  1365. fpath = fpath.get_base_dir();
  1366. }
  1367. make_script_dialog_text->config("Node", fpath.plus_file("new_script.gd"), false);
  1368. make_script_dialog_text->popup_centered(Size2(300, 300) * EDSCALE);
  1369. } break;
  1370. case FILE_COPY_PATH: {
  1371. // Copy the file path
  1372. if (!p_selected.empty()) {
  1373. String fpath = p_selected[0];
  1374. OS::get_singleton()->set_clipboard(fpath);
  1375. }
  1376. } break;
  1377. case FILE_NEW_RESOURCE: {
  1378. // Create a new resource
  1379. new_resource_dialog->popup_create(true);
  1380. } break;
  1381. }
  1382. }
  1383. void FileSystemDock::_resource_created() const {
  1384. Object *c = new_resource_dialog->instance_selected();
  1385. ERR_FAIL_COND(!c);
  1386. Resource *r = Object::cast_to<Resource>(c);
  1387. ERR_FAIL_COND(!r);
  1388. REF res(r);
  1389. editor->push_item(c);
  1390. RES current_res = RES(r);
  1391. String fpath = path;
  1392. if (!fpath.ends_with("/")) {
  1393. fpath = fpath.get_base_dir();
  1394. }
  1395. editor->save_resource_as(current_res, fpath);
  1396. }
  1397. void FileSystemDock::_search_changed(const String &p_text, const Control *p_from) {
  1398. if (searched_string.length() == 0 && p_text.length() > 0) {
  1399. // Register the uncollapsed paths before they change
  1400. uncollapsed_paths_before_search = _compute_uncollapsed_paths();
  1401. }
  1402. searched_string = p_text.to_lower();
  1403. if (p_from == tree_search_box)
  1404. file_list_search_box->set_text(searched_string);
  1405. else // file_list_search_box
  1406. tree_search_box->set_text(searched_string);
  1407. switch (display_mode) {
  1408. case DISPLAY_MODE_TREE_ONLY: {
  1409. _update_tree(searched_string.length() == 0 ? uncollapsed_paths_before_search : Vector<String>());
  1410. } break;
  1411. case DISPLAY_MODE_SPLIT: {
  1412. _update_file_list(false);
  1413. _update_tree(searched_string.length() == 0 ? uncollapsed_paths_before_search : Vector<String>());
  1414. } break;
  1415. }
  1416. }
  1417. void FileSystemDock::_rescan() {
  1418. _set_scanning_mode();
  1419. EditorFileSystem::get_singleton()->scan();
  1420. }
  1421. void FileSystemDock::_toggle_split_mode(bool p_active) {
  1422. set_display_mode(p_active ? DISPLAY_MODE_SPLIT : DISPLAY_MODE_TREE_ONLY);
  1423. emit_signal("display_mode_changed");
  1424. }
  1425. void FileSystemDock::fix_dependencies(const String &p_for_file) {
  1426. deps_editor->edit(p_for_file);
  1427. }
  1428. void FileSystemDock::focus_on_filter() {
  1429. file_list_search_box->grab_focus();
  1430. }
  1431. void FileSystemDock::set_file_list_display_mode(FileListDisplayMode p_mode) {
  1432. if (p_mode == file_list_display_mode)
  1433. return;
  1434. _toggle_file_display();
  1435. }
  1436. Variant FileSystemDock::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1437. bool all_favorites = true;
  1438. bool all_not_favorites = true;
  1439. Vector<String> paths;
  1440. if (p_from == tree) {
  1441. // Check if the first selected is in favorite
  1442. TreeItem *selected = tree->get_next_selected(tree->get_root());
  1443. while (selected) {
  1444. TreeItem *favorites_item = tree->get_root()->get_children();
  1445. if (selected == favorites_item) {
  1446. // The "Favorites" item is not draggable
  1447. return Variant();
  1448. }
  1449. bool is_favorite = selected->get_parent() != NULL && tree->get_root()->get_children() == selected->get_parent();
  1450. all_favorites &= is_favorite;
  1451. all_not_favorites &= !is_favorite;
  1452. selected = tree->get_next_selected(selected);
  1453. }
  1454. if (all_favorites) {
  1455. paths = _tree_get_selected(false);
  1456. } else {
  1457. paths = _tree_get_selected();
  1458. }
  1459. } else if (p_from == files) {
  1460. for (int i = 0; i < files->get_item_count(); i++) {
  1461. if (files->is_selected(i)) {
  1462. paths.push_back(files->get_item_metadata(i));
  1463. }
  1464. }
  1465. all_favorites = false;
  1466. all_not_favorites = true;
  1467. }
  1468. if (paths.empty())
  1469. return Variant();
  1470. if (!all_favorites && !all_not_favorites)
  1471. return Variant();
  1472. Dictionary drag_data = EditorNode::get_singleton()->drag_files_and_dirs(paths, p_from);
  1473. if (all_favorites) {
  1474. drag_data["type"] = "favorite";
  1475. }
  1476. return drag_data;
  1477. }
  1478. bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1479. Dictionary drag_data = p_data;
  1480. if (drag_data.has("type") && String(drag_data["type"]) == "favorite") {
  1481. // Moving favorite around
  1482. TreeItem *ti = tree->get_item_at_position(p_point);
  1483. if (!ti)
  1484. return false;
  1485. int drop_section = tree->get_drop_section_at_position(p_point);
  1486. TreeItem *favorites_item = tree->get_root()->get_children();
  1487. TreeItem *resources_item = favorites_item->get_next();
  1488. if (ti == favorites_item) {
  1489. return (drop_section == 1); // The parent, first fav
  1490. }
  1491. if (ti->get_parent() && favorites_item == ti->get_parent()) {
  1492. return true; // A favorite
  1493. }
  1494. if (ti == resources_item) {
  1495. return (drop_section == -1); // The tree, last fav
  1496. }
  1497. return false;
  1498. }
  1499. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1500. // Move resources
  1501. String to_dir;
  1502. bool favorite;
  1503. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1504. return !to_dir.empty();
  1505. }
  1506. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1507. // Move files or dir
  1508. String to_dir;
  1509. bool favorite;
  1510. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1511. if (favorite)
  1512. return true;
  1513. if (to_dir.empty())
  1514. return false;
  1515. //Attempting to move a folder into itself will fail later
  1516. //Rather than bring up a message don't try to do it in the first place
  1517. to_dir = to_dir.ends_with("/") ? to_dir : (to_dir + "/");
  1518. Vector<String> fnames = drag_data["files"];
  1519. for (int i = 0; i < fnames.size(); ++i) {
  1520. if (fnames[i].ends_with("/") && to_dir.begins_with(fnames[i]))
  1521. return false;
  1522. }
  1523. return true;
  1524. }
  1525. return false;
  1526. }
  1527. void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1528. if (!can_drop_data_fw(p_point, p_data, p_from))
  1529. return;
  1530. Dictionary drag_data = p_data;
  1531. Vector<String> dirs = EditorSettings::get_singleton()->get_favorites();
  1532. if (drag_data.has("type") && String(drag_data["type"]) == "favorite") {
  1533. // Moving favorite around
  1534. TreeItem *ti = tree->get_item_at_position(p_point);
  1535. if (!ti)
  1536. return;
  1537. int drop_section = tree->get_drop_section_at_position(p_point);
  1538. int drop_position;
  1539. Vector<String> files = drag_data["files"];
  1540. TreeItem *favorites_item = tree->get_root()->get_children();
  1541. TreeItem *resources_item = favorites_item->get_next();
  1542. if (ti == favorites_item) {
  1543. // Drop on the favorite folder
  1544. drop_position = 0;
  1545. } else if (ti == resources_item) {
  1546. // Drop on the resource item
  1547. drop_position = dirs.size();
  1548. } else {
  1549. // Drop in the list
  1550. drop_position = dirs.find(ti->get_metadata(0));
  1551. if (drop_section == 1) {
  1552. drop_position++;
  1553. }
  1554. }
  1555. // Remove dragged favorites
  1556. Vector<int> to_remove;
  1557. int offset = 0;
  1558. for (int i = 0; i < files.size(); i++) {
  1559. int to_remove_pos = dirs.find(files[i]);
  1560. to_remove.push_back(to_remove_pos);
  1561. if (to_remove_pos < drop_position) {
  1562. offset++;
  1563. }
  1564. }
  1565. drop_position -= offset;
  1566. to_remove.sort();
  1567. for (int i = 0; i < to_remove.size(); i++) {
  1568. dirs.remove(to_remove[i] - i);
  1569. }
  1570. // Re-add them at the right position
  1571. for (int i = 0; i < files.size(); i++) {
  1572. dirs.insert(drop_position, files[i]);
  1573. drop_position++;
  1574. }
  1575. EditorSettings::get_singleton()->set_favorites(dirs);
  1576. _update_tree(_compute_uncollapsed_paths());
  1577. if (display_mode == DISPLAY_MODE_SPLIT && path == "Favorites")
  1578. _update_file_list(true);
  1579. return;
  1580. }
  1581. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1582. // Moving resource
  1583. Ref<Resource> res = drag_data["resource"];
  1584. String to_dir;
  1585. bool favorite;
  1586. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1587. if (res.is_valid() && !to_dir.empty()) {
  1588. EditorNode::get_singleton()->push_item(res.ptr());
  1589. EditorNode::get_singleton()->save_resource_as(res, to_dir);
  1590. }
  1591. }
  1592. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1593. // Move files or add to favorites
  1594. String to_dir;
  1595. bool favorite;
  1596. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1597. if (!to_dir.empty()) {
  1598. Vector<String> fnames = drag_data["files"];
  1599. to_move.clear();
  1600. for (int i = 0; i < fnames.size(); i++) {
  1601. to_move.push_back(FileOrFolder(fnames[i], !fnames[i].ends_with("/")));
  1602. }
  1603. _move_operation_confirm(to_dir);
  1604. } else if (favorite) {
  1605. // Add the files from favorites
  1606. Vector<String> fnames = drag_data["files"];
  1607. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1608. for (int i = 0; i < fnames.size(); i++) {
  1609. if (favorites.find(fnames[i]) == -1) {
  1610. favorites.push_back(fnames[i]);
  1611. }
  1612. }
  1613. EditorSettings::get_singleton()->set_favorites(favorites);
  1614. _update_tree(_compute_uncollapsed_paths());
  1615. }
  1616. }
  1617. }
  1618. void FileSystemDock::_get_drag_target_folder(String &target, bool &target_favorites, const Point2 &p_point, Control *p_from) const {
  1619. target = String();
  1620. target_favorites = false;
  1621. // In the file list
  1622. if (p_from == files) {
  1623. int pos = files->get_item_at_position(p_point, true);
  1624. if (pos == -1) {
  1625. return;
  1626. }
  1627. String ltarget = files->get_item_metadata(pos);
  1628. target = ltarget.ends_with("/") ? ltarget : path.get_base_dir();
  1629. return;
  1630. }
  1631. // In the tree
  1632. if (p_from == tree) {
  1633. TreeItem *ti = tree->get_item_at_position(p_point);
  1634. int section = tree->get_drop_section_at_position(p_point);
  1635. if (ti) {
  1636. // Check the favorites first
  1637. if (ti == tree->get_root()->get_children() && section >= 0) {
  1638. target_favorites = true;
  1639. return;
  1640. } else if (ti->get_parent() == tree->get_root()->get_children()) {
  1641. target_favorites = true;
  1642. return;
  1643. } else {
  1644. String fpath = ti->get_metadata(0);
  1645. if (section == 0) {
  1646. if (fpath.ends_with("/")) {
  1647. // We drop on a folder
  1648. target = fpath;
  1649. return;
  1650. }
  1651. } else {
  1652. if (ti->get_parent() != tree->get_root()->get_children()) {
  1653. // Not in the favorite section
  1654. if (fpath != "res://") {
  1655. // We drop between two files
  1656. if (fpath.ends_with("/")) {
  1657. fpath = fpath.substr(0, fpath.length() - 1);
  1658. }
  1659. target = fpath.get_base_dir();
  1660. return;
  1661. }
  1662. }
  1663. }
  1664. }
  1665. }
  1666. }
  1667. return;
  1668. }
  1669. void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<String> p_paths, bool p_display_path_dependent_options) {
  1670. // Add options for files and folders
  1671. ERR_FAIL_COND(p_paths.empty())
  1672. Vector<String> filenames;
  1673. Vector<String> foldernames;
  1674. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1675. bool all_files = true;
  1676. bool all_files_scenes = true;
  1677. bool all_folders = true;
  1678. bool all_favorites = true;
  1679. bool all_not_favorites = true;
  1680. for (int i = 0; i < p_paths.size(); i++) {
  1681. String fpath = p_paths[i];
  1682. if (fpath.ends_with("/")) {
  1683. foldernames.push_back(fpath);
  1684. all_files = false;
  1685. } else {
  1686. filenames.push_back(fpath);
  1687. all_folders = false;
  1688. all_files_scenes &= (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene");
  1689. }
  1690. // Check if in favorites
  1691. bool found = false;
  1692. for (int j = 0; j < favorites.size(); j++) {
  1693. if (favorites[j] == fpath) {
  1694. found = true;
  1695. break;
  1696. }
  1697. }
  1698. if (found) {
  1699. all_not_favorites = false;
  1700. } else {
  1701. all_favorites = false;
  1702. }
  1703. }
  1704. if (all_files) {
  1705. if (all_files_scenes && filenames.size() >= 1) {
  1706. p_popup->add_item(TTR("Open Scene(s)"), FILE_OPEN);
  1707. p_popup->add_item(TTR("Instance"), FILE_INSTANCE);
  1708. p_popup->add_separator();
  1709. }
  1710. if (!all_files_scenes && filenames.size() == 1) {
  1711. p_popup->add_item(TTR("Open"), FILE_OPEN);
  1712. p_popup->add_separator();
  1713. }
  1714. }
  1715. if (p_paths.size() >= 1) {
  1716. if (!all_favorites) {
  1717. p_popup->add_item(TTR("Add to favorites"), FILE_ADD_FAVORITE);
  1718. }
  1719. if (!all_not_favorites) {
  1720. p_popup->add_item(TTR("Remove from favorites"), FILE_REMOVE_FAVORITE);
  1721. }
  1722. p_popup->add_separator();
  1723. }
  1724. if (all_files) {
  1725. if (filenames.size() == 1) {
  1726. p_popup->add_item(TTR("Edit Dependencies..."), FILE_DEPENDENCIES);
  1727. p_popup->add_item(TTR("View Owners..."), FILE_OWNERS);
  1728. p_popup->add_separator();
  1729. }
  1730. } else if (all_folders && foldernames.size() > 0) {
  1731. p_popup->add_item(TTR("Open"), FILE_OPEN);
  1732. p_popup->add_separator();
  1733. }
  1734. if (p_paths.size() == 1) {
  1735. p_popup->add_item(TTR("Copy Path"), FILE_COPY_PATH);
  1736. p_popup->add_item(TTR("Rename..."), FILE_RENAME);
  1737. p_popup->add_item(TTR("Duplicate..."), FILE_DUPLICATE);
  1738. }
  1739. p_popup->add_item(TTR("Move To..."), FILE_MOVE);
  1740. p_popup->add_item(TTR("Delete"), FILE_REMOVE);
  1741. if (p_paths.size() == 1) {
  1742. p_popup->add_separator();
  1743. if (p_display_path_dependent_options) {
  1744. p_popup->add_item(TTR("New Folder..."), FILE_NEW_FOLDER);
  1745. p_popup->add_item(TTR("New Script..."), FILE_NEW_SCRIPT);
  1746. p_popup->add_item(TTR("New Resource..."), FILE_NEW_RESOURCE);
  1747. }
  1748. String fpath = p_paths[0];
  1749. String item_text = fpath.ends_with("/") ? TTR("Open in File Manager") : TTR("Show in File Manager");
  1750. p_popup->add_item(item_text, FILE_SHOW_IN_EXPLORER);
  1751. }
  1752. }
  1753. void FileSystemDock::_tree_rmb_select(const Vector2 &p_pos) {
  1754. // Right click is pressed in the tree
  1755. Vector<String> paths = _tree_get_selected();
  1756. if (paths.size() == 1) {
  1757. if (paths[0].ends_with("/")) {
  1758. tree_popup->add_item(TTR("Expand All"), FOLDER_EXPAND_ALL);
  1759. tree_popup->add_item(TTR("Collapse All"), FOLDER_COLLAPSE_ALL);
  1760. tree_popup->add_separator();
  1761. }
  1762. }
  1763. // Popup
  1764. if (!paths.empty()) {
  1765. tree_popup->clear();
  1766. tree_popup->set_size(Size2(1, 1));
  1767. _file_and_folders_fill_popup(tree_popup, paths);
  1768. tree_popup->set_position(tree->get_global_position() + p_pos);
  1769. tree_popup->popup();
  1770. }
  1771. }
  1772. void FileSystemDock::_file_list_rmb_select(int p_item, const Vector2 &p_pos) {
  1773. // Right click is pressed in the file list
  1774. Vector<String> paths;
  1775. for (int i = 0; i < files->get_item_count(); i++) {
  1776. if (!files->is_selected(i))
  1777. continue;
  1778. if (files->get_item_text(p_item) == "..") {
  1779. files->unselect(i);
  1780. continue;
  1781. }
  1782. paths.push_back(files->get_item_metadata(i));
  1783. }
  1784. // Popup
  1785. if (!paths.empty()) {
  1786. file_list_popup->clear();
  1787. file_list_popup->set_size(Size2(1, 1));
  1788. _file_and_folders_fill_popup(file_list_popup, paths, searched_string.length() == 0);
  1789. file_list_popup->set_position(files->get_global_position() + p_pos);
  1790. file_list_popup->popup();
  1791. }
  1792. }
  1793. void FileSystemDock::_file_list_rmb_pressed(const Vector2 &p_pos) {
  1794. // Right click on empty space for file list
  1795. if (searched_string.length() > 0)
  1796. return;
  1797. file_list_popup->clear();
  1798. file_list_popup->set_size(Size2(1, 1));
  1799. file_list_popup->add_item(TTR("New Folder..."), FILE_NEW_FOLDER);
  1800. file_list_popup->add_item(TTR("New Script..."), FILE_NEW_SCRIPT);
  1801. file_list_popup->add_item(TTR("New Resource..."), FILE_NEW_RESOURCE);
  1802. file_list_popup->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_EXPLORER);
  1803. file_list_popup->set_position(files->get_global_position() + p_pos);
  1804. file_list_popup->popup();
  1805. }
  1806. void FileSystemDock::select_file(const String &p_file) {
  1807. _navigate_to_path(p_file);
  1808. }
  1809. void FileSystemDock::_file_multi_selected(int p_index, bool p_selected) {
  1810. // Set the path to the current focused item
  1811. int current = files->get_current();
  1812. if (current == p_index) {
  1813. String fpath = files->get_item_metadata(current);
  1814. if (!fpath.ends_with("/")) {
  1815. path = fpath;
  1816. if (display_mode == DISPLAY_MODE_SPLIT) {
  1817. _update_tree(_compute_uncollapsed_paths());
  1818. }
  1819. }
  1820. }
  1821. // Update the import dock
  1822. import_dock_needs_update = true;
  1823. call_deferred("_update_import_dock");
  1824. }
  1825. void FileSystemDock::_tree_gui_input(Ref<InputEvent> p_event) {
  1826. if (get_viewport()->get_modal_stack_top())
  1827. return; //ignore because of modal window
  1828. Ref<InputEventKey> key = p_event;
  1829. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1830. if (ED_IS_SHORTCUT("filesystem_dock/duplicate", p_event)) {
  1831. _tree_rmb_option(FILE_DUPLICATE);
  1832. } else if (ED_IS_SHORTCUT("filesystem_dock/copy_path", p_event)) {
  1833. _tree_rmb_option(FILE_COPY_PATH);
  1834. } else if (ED_IS_SHORTCUT("filesystem_dock/delete", p_event)) {
  1835. _tree_rmb_option(FILE_REMOVE);
  1836. } else if (ED_IS_SHORTCUT("filesystem_dock/rename", p_event)) {
  1837. _tree_rmb_option(FILE_RENAME);
  1838. }
  1839. }
  1840. }
  1841. void FileSystemDock::_file_list_gui_input(Ref<InputEvent> p_event) {
  1842. if (get_viewport()->get_modal_stack_top())
  1843. return; //ignore because of modal window
  1844. Ref<InputEventKey> key = p_event;
  1845. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1846. if (ED_IS_SHORTCUT("filesystem_dock/duplicate", p_event)) {
  1847. _file_list_rmb_option(FILE_DUPLICATE);
  1848. } else if (ED_IS_SHORTCUT("filesystem_dock/copy_path", p_event)) {
  1849. _file_list_rmb_option(FILE_COPY_PATH);
  1850. } else if (ED_IS_SHORTCUT("filesystem_dock/delete", p_event)) {
  1851. _file_list_rmb_option(FILE_REMOVE);
  1852. } else if (ED_IS_SHORTCUT("filesystem_dock/rename", p_event)) {
  1853. _file_list_rmb_option(FILE_RENAME);
  1854. }
  1855. }
  1856. }
  1857. void FileSystemDock::_update_import_dock() {
  1858. if (!import_dock_needs_update)
  1859. return;
  1860. // List selected
  1861. Vector<String> selected;
  1862. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  1863. // Use the tree
  1864. selected = _tree_get_selected();
  1865. } else {
  1866. // Use the file list
  1867. for (int i = 0; i < files->get_item_count(); i++) {
  1868. if (!files->is_selected(i))
  1869. continue;
  1870. selected.push_back(files->get_item_metadata(i));
  1871. }
  1872. }
  1873. // Check import
  1874. Vector<String> imports;
  1875. String import_type;
  1876. for (int i = 0; i < selected.size(); i++) {
  1877. String fpath = selected[i];
  1878. if (fpath.ends_with("/")) {
  1879. imports.clear();
  1880. break;
  1881. }
  1882. if (!FileAccess::exists(fpath + ".import")) {
  1883. imports.clear();
  1884. break;
  1885. }
  1886. Ref<ConfigFile> cf;
  1887. cf.instance();
  1888. Error err = cf->load(fpath + ".import");
  1889. if (err != OK) {
  1890. imports.clear();
  1891. break;
  1892. }
  1893. String type = cf->get_value("remap", "type");
  1894. if (import_type == "") {
  1895. import_type = type;
  1896. } else if (import_type != type) {
  1897. //all should be the same type
  1898. imports.clear();
  1899. break;
  1900. }
  1901. imports.push_back(fpath);
  1902. }
  1903. if (imports.size() == 0) {
  1904. EditorNode::get_singleton()->get_import_dock()->clear();
  1905. } else if (imports.size() == 1) {
  1906. EditorNode::get_singleton()->get_import_dock()->set_edit_path(imports[0]);
  1907. } else {
  1908. EditorNode::get_singleton()->get_import_dock()->set_edit_multiple_paths(imports);
  1909. }
  1910. import_dock_needs_update = false;
  1911. }
  1912. void FileSystemDock::_bind_methods() {
  1913. ClassDB::bind_method(D_METHOD("_file_list_gui_input"), &FileSystemDock::_file_list_gui_input);
  1914. ClassDB::bind_method(D_METHOD("_tree_gui_input"), &FileSystemDock::_tree_gui_input);
  1915. ClassDB::bind_method(D_METHOD("_update_tree"), &FileSystemDock::_update_tree);
  1916. ClassDB::bind_method(D_METHOD("_rescan"), &FileSystemDock::_rescan);
  1917. ClassDB::bind_method(D_METHOD("_toggle_split_mode"), &FileSystemDock::_toggle_split_mode);
  1918. ClassDB::bind_method(D_METHOD("_tree_rmb_option", "option"), &FileSystemDock::_tree_rmb_option);
  1919. ClassDB::bind_method(D_METHOD("_file_list_rmb_option", "option"), &FileSystemDock::_file_list_rmb_option);
  1920. ClassDB::bind_method(D_METHOD("_tree_rmb_select"), &FileSystemDock::_tree_rmb_select);
  1921. ClassDB::bind_method(D_METHOD("_file_list_rmb_select"), &FileSystemDock::_file_list_rmb_select);
  1922. ClassDB::bind_method(D_METHOD("_file_list_rmb_pressed"), &FileSystemDock::_file_list_rmb_pressed);
  1923. ClassDB::bind_method(D_METHOD("_file_deleted"), &FileSystemDock::_file_deleted);
  1924. ClassDB::bind_method(D_METHOD("_folder_deleted"), &FileSystemDock::_folder_deleted);
  1925. ClassDB::bind_method(D_METHOD("_file_list_thumbnail_done"), &FileSystemDock::_file_list_thumbnail_done);
  1926. ClassDB::bind_method(D_METHOD("_tree_thumbnail_done"), &FileSystemDock::_tree_thumbnail_done);
  1927. ClassDB::bind_method(D_METHOD("_file_list_activate_file"), &FileSystemDock::_file_list_activate_file);
  1928. ClassDB::bind_method(D_METHOD("_tree_activate_file"), &FileSystemDock::_tree_activate_file);
  1929. ClassDB::bind_method(D_METHOD("_select_file"), &FileSystemDock::_select_file);
  1930. ClassDB::bind_method(D_METHOD("_navigate_to_path"), &FileSystemDock::_navigate_to_path);
  1931. ClassDB::bind_method(D_METHOD("_toggle_file_display"), &FileSystemDock::_toggle_file_display);
  1932. ClassDB::bind_method(D_METHOD("_fw_history"), &FileSystemDock::_fw_history);
  1933. ClassDB::bind_method(D_METHOD("_bw_history"), &FileSystemDock::_bw_history);
  1934. ClassDB::bind_method(D_METHOD("_fs_changed"), &FileSystemDock::_fs_changed);
  1935. ClassDB::bind_method(D_METHOD("_tree_multi_selected"), &FileSystemDock::_tree_multi_selected);
  1936. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileSystemDock::_make_dir_confirm);
  1937. ClassDB::bind_method(D_METHOD("_resource_created"), &FileSystemDock::_resource_created);
  1938. ClassDB::bind_method(D_METHOD("_move_operation_confirm", "to_path", "overwrite"), &FileSystemDock::_move_operation_confirm, DEFVAL(false));
  1939. ClassDB::bind_method(D_METHOD("_move_with_overwrite"), &FileSystemDock::_move_with_overwrite);
  1940. ClassDB::bind_method(D_METHOD("_rename_operation_confirm"), &FileSystemDock::_rename_operation_confirm);
  1941. ClassDB::bind_method(D_METHOD("_duplicate_operation_confirm"), &FileSystemDock::_duplicate_operation_confirm);
  1942. ClassDB::bind_method(D_METHOD("_search_changed"), &FileSystemDock::_search_changed);
  1943. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &FileSystemDock::get_drag_data_fw);
  1944. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &FileSystemDock::can_drop_data_fw);
  1945. ClassDB::bind_method(D_METHOD("drop_data_fw"), &FileSystemDock::drop_data_fw);
  1946. ClassDB::bind_method(D_METHOD("navigate_to_path"), &FileSystemDock::navigate_to_path);
  1947. ClassDB::bind_method(D_METHOD("_preview_invalidated"), &FileSystemDock::_preview_invalidated);
  1948. ClassDB::bind_method(D_METHOD("_file_multi_selected"), &FileSystemDock::_file_multi_selected);
  1949. ClassDB::bind_method(D_METHOD("_update_import_dock"), &FileSystemDock::_update_import_dock);
  1950. ADD_SIGNAL(MethodInfo("instance", PropertyInfo(Variant::POOL_STRING_ARRAY, "files")));
  1951. ADD_SIGNAL(MethodInfo("open"));
  1952. ADD_SIGNAL(MethodInfo("file_removed", PropertyInfo(Variant::STRING, "file")));
  1953. ADD_SIGNAL(MethodInfo("folder_removed", PropertyInfo(Variant::STRING, "folder")));
  1954. ADD_SIGNAL(MethodInfo("files_moved", PropertyInfo(Variant::STRING, "old_file"), PropertyInfo(Variant::STRING, "new_file")));
  1955. ADD_SIGNAL(MethodInfo("folder_moved", PropertyInfo(Variant::STRING, "old_folder"), PropertyInfo(Variant::STRING, "new_file")));
  1956. ADD_SIGNAL(MethodInfo("display_mode_changed"));
  1957. }
  1958. FileSystemDock::FileSystemDock(EditorNode *p_editor) {
  1959. set_name("FileSystem");
  1960. editor = p_editor;
  1961. path = "res://";
  1962. ED_SHORTCUT("filesystem_dock/copy_path", TTR("Copy Path"), KEY_MASK_CMD | KEY_C);
  1963. ED_SHORTCUT("filesystem_dock/duplicate", TTR("Duplicate..."), KEY_MASK_CMD | KEY_D);
  1964. ED_SHORTCUT("filesystem_dock/delete", TTR("Delete"), KEY_DELETE);
  1965. ED_SHORTCUT("filesystem_dock/rename", TTR("Rename"));
  1966. VBoxContainer *top_vbc = memnew(VBoxContainer);
  1967. add_child(top_vbc);
  1968. HBoxContainer *toolbar_hbc = memnew(HBoxContainer);
  1969. toolbar_hbc->add_constant_override("separation", 0);
  1970. top_vbc->add_child(toolbar_hbc);
  1971. button_hist_prev = memnew(ToolButton);
  1972. button_hist_prev->set_disabled(true);
  1973. button_hist_prev->set_focus_mode(FOCUS_NONE);
  1974. button_hist_prev->set_tooltip(TTR("Previous Directory"));
  1975. toolbar_hbc->add_child(button_hist_prev);
  1976. button_hist_next = memnew(ToolButton);
  1977. button_hist_next->set_disabled(true);
  1978. button_hist_next->set_focus_mode(FOCUS_NONE);
  1979. button_hist_next->set_tooltip(TTR("Next Directory"));
  1980. toolbar_hbc->add_child(button_hist_next);
  1981. current_path = memnew(LineEdit);
  1982. current_path->set_h_size_flags(SIZE_EXPAND_FILL);
  1983. _set_current_path_text(path);
  1984. toolbar_hbc->add_child(current_path);
  1985. button_reload = memnew(Button);
  1986. button_reload->set_flat(true);
  1987. button_reload->connect("pressed", this, "_rescan");
  1988. button_reload->set_focus_mode(FOCUS_NONE);
  1989. button_reload->set_tooltip(TTR("Re-Scan Filesystem"));
  1990. button_reload->hide();
  1991. toolbar_hbc->add_child(button_reload);
  1992. button_toggle_display_mode = memnew(Button);
  1993. button_toggle_display_mode->set_flat(true);
  1994. button_toggle_display_mode->set_toggle_mode(true);
  1995. button_toggle_display_mode->connect("toggled", this, "_toggle_split_mode");
  1996. button_toggle_display_mode->set_focus_mode(FOCUS_NONE);
  1997. button_toggle_display_mode->set_tooltip(TTR("Toggle split mode"));
  1998. toolbar_hbc->add_child(button_toggle_display_mode);
  1999. HBoxContainer *toolbar2_hbc = memnew(HBoxContainer);
  2000. toolbar2_hbc->add_constant_override("separation", 0);
  2001. top_vbc->add_child(toolbar2_hbc);
  2002. tree_search_box = memnew(LineEdit);
  2003. tree_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2004. tree_search_box->set_placeholder(TTR("Search files"));
  2005. tree_search_box->connect("text_changed", this, "_search_changed", varray(tree_search_box));
  2006. toolbar2_hbc->add_child(tree_search_box);
  2007. file_list_popup = memnew(PopupMenu);
  2008. file_list_popup->set_hide_on_window_lose_focus(true);
  2009. add_child(file_list_popup);
  2010. tree_popup = memnew(PopupMenu);
  2011. tree_popup->set_hide_on_window_lose_focus(true);
  2012. add_child(tree_popup);
  2013. split_box = memnew(VSplitContainer);
  2014. split_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2015. add_child(split_box);
  2016. tree = memnew(Tree);
  2017. tree->set_hide_root(true);
  2018. tree->set_drag_forwarding(this);
  2019. tree->set_allow_rmb_select(true);
  2020. tree->set_select_mode(Tree::SELECT_MULTI);
  2021. tree->set_custom_minimum_size(Size2(0, 15 * EDSCALE));
  2022. split_box->add_child(tree);
  2023. tree->connect("item_edited", this, "_favorite_toggled");
  2024. tree->connect("item_activated", this, "_tree_activate_file");
  2025. tree->connect("multi_selected", this, "_tree_multi_selected");
  2026. tree->connect("item_rmb_selected", this, "_tree_rmb_select");
  2027. tree->connect("gui_input", this, "_tree_gui_input");
  2028. file_list_vb = memnew(VBoxContainer);
  2029. file_list_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2030. split_box->add_child(file_list_vb);
  2031. path_hb = memnew(HBoxContainer);
  2032. file_list_vb->add_child(path_hb);
  2033. file_list_search_box = memnew(LineEdit);
  2034. file_list_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2035. file_list_search_box->set_placeholder(TTR("Search files"));
  2036. file_list_search_box->connect("text_changed", this, "_search_changed", varray(file_list_search_box));
  2037. path_hb->add_child(file_list_search_box);
  2038. button_file_list_display_mode = memnew(ToolButton);
  2039. path_hb->add_child(button_file_list_display_mode);
  2040. files = memnew(ItemList);
  2041. files->set_v_size_flags(SIZE_EXPAND_FILL);
  2042. files->set_select_mode(ItemList::SELECT_MULTI);
  2043. files->set_drag_forwarding(this);
  2044. files->connect("item_rmb_selected", this, "_file_list_rmb_select");
  2045. files->connect("gui_input", this, "_file_list_gui_input");
  2046. files->connect("multi_selected", this, "_file_multi_selected");
  2047. files->connect("rmb_clicked", this, "_file_list_rmb_pressed");
  2048. files->set_custom_minimum_size(Size2(0, 15 * EDSCALE));
  2049. files->set_allow_rmb_select(true);
  2050. file_list_vb->add_child(files);
  2051. scanning_vb = memnew(VBoxContainer);
  2052. scanning_vb->hide();
  2053. add_child(scanning_vb);
  2054. Label *slabel = memnew(Label);
  2055. slabel->set_text(TTR("Scanning Files,\nPlease Wait..."));
  2056. slabel->set_align(Label::ALIGN_CENTER);
  2057. scanning_vb->add_child(slabel);
  2058. scanning_progress = memnew(ProgressBar);
  2059. scanning_vb->add_child(scanning_progress);
  2060. deps_editor = memnew(DependencyEditor);
  2061. add_child(deps_editor);
  2062. owners_editor = memnew(DependencyEditorOwners(editor));
  2063. add_child(owners_editor);
  2064. remove_dialog = memnew(DependencyRemoveDialog);
  2065. remove_dialog->connect("file_removed", this, "_file_deleted");
  2066. remove_dialog->connect("folder_removed", this, "_folder_deleted");
  2067. add_child(remove_dialog);
  2068. move_dialog = memnew(EditorDirDialog);
  2069. move_dialog->get_ok()->set_text(TTR("Move"));
  2070. add_child(move_dialog);
  2071. move_dialog->connect("dir_selected", this, "_move_operation_confirm");
  2072. rename_dialog = memnew(ConfirmationDialog);
  2073. VBoxContainer *rename_dialog_vb = memnew(VBoxContainer);
  2074. rename_dialog->add_child(rename_dialog_vb);
  2075. rename_dialog_text = memnew(LineEdit);
  2076. rename_dialog_vb->add_margin_child(TTR("Name:"), rename_dialog_text);
  2077. rename_dialog->get_ok()->set_text(TTR("Rename"));
  2078. add_child(rename_dialog);
  2079. rename_dialog->register_text_enter(rename_dialog_text);
  2080. rename_dialog->connect("confirmed", this, "_rename_operation_confirm");
  2081. overwrite_dialog = memnew(ConfirmationDialog);
  2082. overwrite_dialog->set_text(TTR("There is already file or folder with the same name in this location."));
  2083. overwrite_dialog->get_ok()->set_text(TTR("Overwrite"));
  2084. add_child(overwrite_dialog);
  2085. overwrite_dialog->connect("confirmed", this, "_move_with_overwrite");
  2086. duplicate_dialog = memnew(ConfirmationDialog);
  2087. VBoxContainer *duplicate_dialog_vb = memnew(VBoxContainer);
  2088. duplicate_dialog->add_child(duplicate_dialog_vb);
  2089. duplicate_dialog_text = memnew(LineEdit);
  2090. duplicate_dialog_vb->add_margin_child(TTR("Name:"), duplicate_dialog_text);
  2091. duplicate_dialog->get_ok()->set_text(TTR("Duplicate"));
  2092. add_child(duplicate_dialog);
  2093. duplicate_dialog->register_text_enter(duplicate_dialog_text);
  2094. duplicate_dialog->connect("confirmed", this, "_duplicate_operation_confirm");
  2095. make_dir_dialog = memnew(ConfirmationDialog);
  2096. make_dir_dialog->set_title(TTR("Create Folder"));
  2097. VBoxContainer *make_folder_dialog_vb = memnew(VBoxContainer);
  2098. make_dir_dialog->add_child(make_folder_dialog_vb);
  2099. make_dir_dialog_text = memnew(LineEdit);
  2100. make_folder_dialog_vb->add_margin_child(TTR("Name:"), make_dir_dialog_text);
  2101. add_child(make_dir_dialog);
  2102. make_dir_dialog->register_text_enter(make_dir_dialog_text);
  2103. make_dir_dialog->connect("confirmed", this, "_make_dir_confirm");
  2104. make_script_dialog_text = memnew(ScriptCreateDialog);
  2105. make_script_dialog_text->set_title(TTR("Create Script"));
  2106. add_child(make_script_dialog_text);
  2107. new_resource_dialog = memnew(CreateDialog);
  2108. add_child(new_resource_dialog);
  2109. new_resource_dialog->set_base_type("Resource");
  2110. new_resource_dialog->connect("create", this, "_resource_created");
  2111. searched_string = String();
  2112. uncollapsed_paths_before_search = Vector<String>();
  2113. updating_tree = false;
  2114. tree_update_id = 0;
  2115. initialized = false;
  2116. import_dock_needs_update = false;
  2117. history_pos = 0;
  2118. history_max_size = 20;
  2119. history.push_back("res://");
  2120. display_mode = DISPLAY_MODE_TREE_ONLY;
  2121. old_display_mode = DISPLAY_MODE_TREE_ONLY;
  2122. file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
  2123. always_show_folders = false;
  2124. }
  2125. FileSystemDock::~FileSystemDock() {
  2126. }