filesystem_dock.cpp 82 KB

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