filesystem_dock.cpp 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019
  1. /*************************************************************************/
  2. /* filesystem_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/os/os.h"
  37. #include "core/templates/list.h"
  38. #include "editor_feature_profile.h"
  39. #include "editor_node.h"
  40. #include "editor_resource_preview.h"
  41. #include "editor_scale.h"
  42. #include "editor_settings.h"
  43. #include "import_dock.h"
  44. #include "scene/main/window.h"
  45. #include "scene/resources/packed_scene.h"
  46. #include "servers/display_server.h"
  47. Ref<Texture2D> FileSystemDock::_get_tree_item_icon(bool p_is_valid, String p_file_type) {
  48. Ref<Texture2D> file_icon;
  49. if (!p_is_valid) {
  50. file_icon = get_theme_icon("ImportFail", "EditorIcons");
  51. } else {
  52. file_icon = (has_theme_icon(p_file_type, "EditorIcons")) ? get_theme_icon(p_file_type, "EditorIcons") : get_theme_icon("File", "EditorIcons");
  53. }
  54. return file_icon;
  55. }
  56. bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory *p_dir, Vector<String> &uncollapsed_paths, bool p_select_in_favorites, bool p_unfold_path) {
  57. bool parent_should_expand = false;
  58. // Create a tree item for the subdirectory.
  59. TreeItem *subdirectory_item = tree->create_item(p_parent);
  60. String dname = p_dir->get_name();
  61. if (dname == "") {
  62. dname = "res://";
  63. }
  64. subdirectory_item->set_text(0, dname);
  65. subdirectory_item->set_structured_text_bidi_override(0, STRUCTURED_TEXT_FILE);
  66. subdirectory_item->set_icon(0, get_theme_icon("Folder", "EditorIcons"));
  67. subdirectory_item->set_icon_modulate(0, get_theme_color("folder_icon_modulate", "FileDialog"));
  68. subdirectory_item->set_selectable(0, true);
  69. String lpath = p_dir->get_path();
  70. subdirectory_item->set_metadata(0, lpath);
  71. if (!p_select_in_favorites && (path == lpath || ((display_mode == DISPLAY_MODE_SPLIT) && path.get_base_dir() == lpath))) {
  72. subdirectory_item->select(0);
  73. // Keep select an item when re-created a tree
  74. // To prevent crashing when nothing is selected.
  75. subdirectory_item->set_as_cursor(0);
  76. }
  77. if (p_unfold_path && path.begins_with(lpath) && path != lpath) {
  78. subdirectory_item->set_collapsed(false);
  79. } else {
  80. subdirectory_item->set_collapsed(uncollapsed_paths.find(lpath) < 0);
  81. }
  82. if (searched_string.length() > 0 && dname.to_lower().find(searched_string) >= 0) {
  83. parent_should_expand = true;
  84. }
  85. // Create items for all subdirectories.
  86. bool reversed = file_sort == FILE_SORT_NAME_REVERSE;
  87. for (int i = reversed ? p_dir->get_subdir_count() - 1 : 0;
  88. reversed ? i >= 0 : i < p_dir->get_subdir_count();
  89. reversed ? i-- : i++) {
  90. parent_should_expand = (_create_tree(subdirectory_item, p_dir->get_subdir(i), uncollapsed_paths, p_select_in_favorites, p_unfold_path) || parent_should_expand);
  91. }
  92. // Create all items for the files in the subdirectory.
  93. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  94. String main_scene = ProjectSettings::get_singleton()->get("application/run/main_scene");
  95. // Build the list of the files to display.
  96. List<FileInfo> file_list;
  97. for (int i = 0; i < p_dir->get_file_count(); i++) {
  98. String file_type = p_dir->get_file_type(i);
  99. if (_is_file_type_disabled_by_feature_profile(file_type)) {
  100. // If type is disabled, file won't be displayed.
  101. continue;
  102. }
  103. String file_name = p_dir->get_file(i);
  104. if (searched_string.length() > 0) {
  105. if (file_name.to_lower().find(searched_string) < 0) {
  106. // The searched string is not in the file name, we skip it.
  107. continue;
  108. } else {
  109. // We expand all parents.
  110. parent_should_expand = true;
  111. }
  112. }
  113. FileInfo fi;
  114. fi.name = p_dir->get_file(i);
  115. fi.type = p_dir->get_file_type(i);
  116. fi.import_broken = !p_dir->get_file_import_is_valid(i);
  117. fi.modified_time = p_dir->get_file_modified_time(i);
  118. file_list.push_back(fi);
  119. }
  120. // Sort the file list if needed.
  121. _sort_file_info_list(file_list);
  122. // Build the tree.
  123. for (List<FileInfo>::Element *E = file_list.front(); E; E = E->next()) {
  124. FileInfo fi = E->get();
  125. TreeItem *file_item = tree->create_item(subdirectory_item);
  126. file_item->set_text(0, fi.name);
  127. file_item->set_structured_text_bidi_override(0, STRUCTURED_TEXT_FILE);
  128. file_item->set_icon(0, _get_tree_item_icon(!fi.import_broken, fi.type));
  129. String file_metadata = lpath.plus_file(fi.name);
  130. file_item->set_metadata(0, file_metadata);
  131. if (!p_select_in_favorites && path == file_metadata) {
  132. file_item->select(0);
  133. file_item->set_as_cursor(0);
  134. }
  135. if (main_scene == file_metadata) {
  136. file_item->set_custom_color(0, get_theme_color("accent_color", "Editor"));
  137. }
  138. Array udata;
  139. udata.push_back(tree_update_id);
  140. udata.push_back(file_item);
  141. EditorResourcePreview::get_singleton()->queue_resource_preview(file_metadata, this, "_tree_thumbnail_done", udata);
  142. }
  143. } else if (display_mode == DISPLAY_MODE_SPLIT) {
  144. if (lpath.get_base_dir() == path.get_base_dir()) {
  145. subdirectory_item->select(0);
  146. subdirectory_item->set_as_cursor(0);
  147. }
  148. }
  149. if (searched_string.length() > 0) {
  150. if (parent_should_expand) {
  151. subdirectory_item->set_collapsed(false);
  152. } else if (dname != "res://") {
  153. subdirectory_item->get_parent()->remove_child(subdirectory_item);
  154. memdelete(subdirectory_item);
  155. }
  156. }
  157. return parent_should_expand;
  158. }
  159. Vector<String> FileSystemDock::_compute_uncollapsed_paths() {
  160. // Register currently collapsed paths.
  161. Vector<String> uncollapsed_paths;
  162. TreeItem *root = tree->get_root();
  163. if (root) {
  164. TreeItem *favorites_item = root->get_first_child();
  165. if (!favorites_item->is_collapsed()) {
  166. uncollapsed_paths.push_back(favorites_item->get_metadata(0));
  167. }
  168. TreeItem *resTree = root->get_first_child()->get_next();
  169. if (resTree) {
  170. Vector<TreeItem *> needs_check;
  171. needs_check.push_back(resTree);
  172. while (needs_check.size()) {
  173. if (!needs_check[0]->is_collapsed()) {
  174. uncollapsed_paths.push_back(needs_check[0]->get_metadata(0));
  175. TreeItem *child = needs_check[0]->get_first_child();
  176. while (child) {
  177. needs_check.push_back(child);
  178. child = child->get_next();
  179. }
  180. }
  181. needs_check.remove(0);
  182. }
  183. }
  184. }
  185. return uncollapsed_paths;
  186. }
  187. void FileSystemDock::_update_tree(const Vector<String> &p_uncollapsed_paths, bool p_uncollapse_root, bool p_select_in_favorites, bool p_unfold_path) {
  188. // Recreate the tree.
  189. tree->clear();
  190. tree_update_id++;
  191. updating_tree = true;
  192. TreeItem *root = tree->create_item();
  193. // Handles the favorites.
  194. TreeItem *favorites = tree->create_item(root);
  195. favorites->set_icon(0, get_theme_icon("Favorites", "EditorIcons"));
  196. favorites->set_text(0, TTR("Favorites:"));
  197. favorites->set_metadata(0, "Favorites");
  198. favorites->set_collapsed(p_uncollapsed_paths.find("Favorites") < 0);
  199. Vector<String> favorite_paths = EditorSettings::get_singleton()->get_favorites();
  200. for (int i = 0; i < favorite_paths.size(); i++) {
  201. String fave = favorite_paths[i];
  202. if (!fave.begins_with("res://")) {
  203. continue;
  204. }
  205. Ref<Texture2D> folder_icon = get_theme_icon("Folder", "EditorIcons");
  206. const Color folder_color = get_theme_color("folder_icon_modulate", "FileDialog");
  207. String text;
  208. Ref<Texture2D> icon;
  209. Color color;
  210. if (fave == "res://") {
  211. text = "/";
  212. icon = folder_icon;
  213. color = folder_color;
  214. } else if (fave.ends_with("/")) {
  215. text = fave.substr(0, fave.length() - 1).get_file();
  216. icon = folder_icon;
  217. color = folder_color;
  218. } else {
  219. text = fave.get_file();
  220. int index;
  221. EditorFileSystemDirectory *dir = EditorFileSystem::get_singleton()->find_file(fave, &index);
  222. if (dir) {
  223. icon = _get_tree_item_icon(dir->get_file_import_is_valid(index), dir->get_file_type(index));
  224. } else {
  225. icon = get_theme_icon("File", "EditorIcons");
  226. }
  227. color = Color(1, 1, 1);
  228. }
  229. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  230. TreeItem *ti = tree->create_item(favorites);
  231. ti->set_text(0, text);
  232. ti->set_icon(0, icon);
  233. ti->set_icon_modulate(0, color);
  234. ti->set_tooltip(0, fave);
  235. ti->set_selectable(0, true);
  236. ti->set_metadata(0, fave);
  237. if (p_select_in_favorites && fave == path) {
  238. ti->select(0);
  239. ti->set_as_cursor(0);
  240. }
  241. if (!fave.ends_with("/")) {
  242. Array udata;
  243. udata.push_back(tree_update_id);
  244. udata.push_back(ti);
  245. EditorResourcePreview::get_singleton()->queue_resource_preview(fave, this, "_tree_thumbnail_done", udata);
  246. }
  247. }
  248. }
  249. Vector<String> uncollapsed_paths = p_uncollapsed_paths;
  250. if (p_uncollapse_root) {
  251. uncollapsed_paths.push_back("res://");
  252. }
  253. // Create the remaining of the tree.
  254. _create_tree(root, EditorFileSystem::get_singleton()->get_filesystem(), uncollapsed_paths, p_select_in_favorites, p_unfold_path);
  255. tree->ensure_cursor_is_visible();
  256. updating_tree = false;
  257. }
  258. void FileSystemDock::set_display_mode(DisplayMode p_display_mode) {
  259. display_mode = p_display_mode;
  260. _update_display_mode(false);
  261. }
  262. void FileSystemDock::_update_display_mode(bool p_force) {
  263. // Compute the new display mode.
  264. if (p_force || old_display_mode != display_mode) {
  265. button_toggle_display_mode->set_pressed(display_mode == DISPLAY_MODE_SPLIT);
  266. switch (display_mode) {
  267. case DISPLAY_MODE_TREE_ONLY:
  268. tree->show();
  269. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  270. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  271. toolbar2_hbc->show();
  272. } else {
  273. toolbar2_hbc->hide();
  274. }
  275. _update_tree(_compute_uncollapsed_paths());
  276. file_list_vb->hide();
  277. break;
  278. case DISPLAY_MODE_SPLIT:
  279. tree->show();
  280. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  281. tree->ensure_cursor_is_visible();
  282. toolbar2_hbc->hide();
  283. _update_tree(_compute_uncollapsed_paths());
  284. file_list_vb->show();
  285. _update_file_list(true);
  286. break;
  287. }
  288. old_display_mode = display_mode;
  289. }
  290. }
  291. void FileSystemDock::_notification(int p_what) {
  292. switch (p_what) {
  293. case NOTIFICATION_TRANSLATION_CHANGED:
  294. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  295. case NOTIFICATION_ENTER_TREE: {
  296. if (initialized) {
  297. return;
  298. }
  299. initialized = true;
  300. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &FileSystemDock::_feature_profile_changed));
  301. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &FileSystemDock::_fs_changed));
  302. EditorResourcePreview::get_singleton()->connect("preview_invalidated", callable_mp(this, &FileSystemDock::_preview_invalidated));
  303. String ei = "EditorIcons";
  304. button_reload->set_icon(get_theme_icon("Reload", ei));
  305. button_toggle_display_mode->set_icon(get_theme_icon("Panels2", ei));
  306. button_file_list_display_mode->connect("pressed", callable_mp(this, &FileSystemDock::_toggle_file_display));
  307. files->connect("item_activated", callable_mp(this, &FileSystemDock::_file_list_activate_file));
  308. button_hist_next->connect("pressed", callable_mp(this, &FileSystemDock::_fw_history));
  309. button_hist_prev->connect("pressed", callable_mp(this, &FileSystemDock::_bw_history));
  310. tree_search_box->set_right_icon(get_theme_icon("Search", ei));
  311. tree_search_box->set_clear_button_enabled(true);
  312. tree_button_sort->set_icon(get_theme_icon("Sort", ei));
  313. file_list_search_box->set_right_icon(get_theme_icon("Search", ei));
  314. file_list_search_box->set_clear_button_enabled(true);
  315. file_list_button_sort->set_icon(get_theme_icon("Sort", ei));
  316. if (is_layout_rtl()) {
  317. button_hist_next->set_icon(get_theme_icon("Back", ei));
  318. button_hist_prev->set_icon(get_theme_icon("Forward", ei));
  319. } else {
  320. button_hist_next->set_icon(get_theme_icon("Forward", ei));
  321. button_hist_prev->set_icon(get_theme_icon("Back", ei));
  322. }
  323. file_list_popup->connect("id_pressed", callable_mp(this, &FileSystemDock::_file_list_rmb_option));
  324. tree_popup->connect("id_pressed", callable_mp(this, &FileSystemDock::_tree_rmb_option));
  325. current_path->connect("text_entered", callable_mp(this, &FileSystemDock::_navigate_to_path), make_binds(false));
  326. always_show_folders = bool(EditorSettings::get_singleton()->get("docks/filesystem/always_show_folders"));
  327. set_file_list_display_mode(FileSystemDock::FILE_LIST_DISPLAY_LIST);
  328. _update_display_mode();
  329. if (EditorFileSystem::get_singleton()->is_scanning()) {
  330. _set_scanning_mode();
  331. } else {
  332. _update_tree(Vector<String>(), true);
  333. }
  334. } break;
  335. case NOTIFICATION_PROCESS: {
  336. if (EditorFileSystem::get_singleton()->is_scanning()) {
  337. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
  338. }
  339. } break;
  340. case NOTIFICATION_DRAG_BEGIN: {
  341. Dictionary dd = get_viewport()->gui_get_drag_data();
  342. if (tree->is_visible_in_tree() && dd.has("type")) {
  343. if (dd.has("favorite")) {
  344. if ((String(dd["favorite"]) == "all")) {
  345. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  346. }
  347. } else if ((String(dd["type"]) == "files") || (String(dd["type"]) == "files_and_dirs") || (String(dd["type"]) == "resource")) {
  348. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM | Tree::DROP_MODE_INBETWEEN);
  349. }
  350. }
  351. } break;
  352. case NOTIFICATION_DRAG_END: {
  353. tree->set_drop_mode_flags(0);
  354. } break;
  355. case NOTIFICATION_THEME_CHANGED: {
  356. if (is_visible_in_tree()) {
  357. _update_display_mode(true);
  358. }
  359. } break;
  360. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  361. // Update icons.
  362. String ei = "EditorIcons";
  363. button_reload->set_icon(get_theme_icon("Reload", ei));
  364. button_toggle_display_mode->set_icon(get_theme_icon("Panels2", ei));
  365. if (is_layout_rtl()) {
  366. button_hist_next->set_icon(get_theme_icon("Back", ei));
  367. button_hist_prev->set_icon(get_theme_icon("Forward", ei));
  368. } else {
  369. button_hist_next->set_icon(get_theme_icon("Forward", ei));
  370. button_hist_prev->set_icon(get_theme_icon("Back", ei));
  371. }
  372. if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
  373. button_file_list_display_mode->set_icon(get_theme_icon("FileThumbnail", "EditorIcons"));
  374. } else {
  375. button_file_list_display_mode->set_icon(get_theme_icon("FileList", "EditorIcons"));
  376. }
  377. tree_search_box->set_right_icon(get_theme_icon("Search", ei));
  378. tree_search_box->set_clear_button_enabled(true);
  379. tree_button_sort->set_icon(get_theme_icon("Sort", ei));
  380. file_list_search_box->set_right_icon(get_theme_icon("Search", ei));
  381. file_list_search_box->set_clear_button_enabled(true);
  382. file_list_button_sort->set_icon(get_theme_icon("Sort", ei));
  383. // Update always show folders.
  384. bool new_always_show_folders = bool(EditorSettings::get_singleton()->get("docks/filesystem/always_show_folders"));
  385. if (new_always_show_folders != always_show_folders) {
  386. always_show_folders = new_always_show_folders;
  387. _update_file_list(true);
  388. }
  389. // Change full tree mode.
  390. _update_display_mode();
  391. } break;
  392. }
  393. }
  394. void FileSystemDock::_tree_multi_selected(Object *p_item, int p_column, bool p_selected) {
  395. // Update the import dock.
  396. import_dock_needs_update = true;
  397. call_deferred("_update_import_dock");
  398. // Return if we don't select something new.
  399. if (!p_selected) {
  400. return;
  401. }
  402. // Tree item selected.
  403. TreeItem *selected = tree->get_selected();
  404. if (!selected) {
  405. return;
  406. }
  407. TreeItem *favorites_item = tree->get_root()->get_first_child();
  408. if (selected->get_parent() == favorites_item && !String(selected->get_metadata(0)).ends_with("/")) {
  409. // Go to the favorites if we click in the favorites and the path has changed.
  410. path = "Favorites";
  411. } else {
  412. path = selected->get_metadata(0);
  413. // Note: the "Favorites" item also leads to this path.
  414. }
  415. // Set the current path.
  416. _set_current_path_text(path);
  417. _push_to_history();
  418. // Update the file list.
  419. if (!updating_tree && display_mode == DISPLAY_MODE_SPLIT) {
  420. _update_file_list(false);
  421. }
  422. }
  423. String FileSystemDock::get_selected_path() const {
  424. if (path.ends_with("/")) {
  425. return path;
  426. } else {
  427. return path.get_base_dir();
  428. }
  429. }
  430. String FileSystemDock::get_current_path() const {
  431. return path;
  432. }
  433. void FileSystemDock::_set_current_path_text(const String &p_path) {
  434. if (p_path == "Favorites") {
  435. current_path->set_text(TTR("Favorites"));
  436. } else {
  437. current_path->set_text(path);
  438. }
  439. }
  440. void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_favorites) {
  441. if (p_path == "Favorites") {
  442. path = p_path;
  443. } else {
  444. String target_path = p_path;
  445. // If the path is a file, do not only go to the directory in the tree, also select the file in the file list.
  446. if (target_path.ends_with("/")) {
  447. target_path = target_path.substr(0, target_path.length() - 1);
  448. }
  449. DirAccess *dirAccess = DirAccess::open("res://");
  450. if (dirAccess->file_exists(p_path)) {
  451. path = target_path;
  452. } else if (dirAccess->dir_exists(p_path)) {
  453. path = target_path + "/";
  454. } else {
  455. memdelete(dirAccess);
  456. ERR_FAIL_MSG(vformat("Cannot navigate to '%s' as it has not been found in the file system!", p_path));
  457. }
  458. memdelete(dirAccess);
  459. }
  460. _set_current_path_text(path);
  461. _push_to_history();
  462. _update_tree(_compute_uncollapsed_paths(), false, p_select_in_favorites, true);
  463. if (display_mode == DISPLAY_MODE_SPLIT) {
  464. _update_file_list(false);
  465. files->get_v_scroll()->set_value(0);
  466. }
  467. String file_name = p_path.get_file();
  468. if (!file_name.is_empty()) {
  469. for (int i = 0; i < files->get_item_count(); i++) {
  470. if (files->get_item_text(i) == file_name) {
  471. files->select(i, true);
  472. files->ensure_current_is_visible();
  473. break;
  474. }
  475. }
  476. }
  477. }
  478. void FileSystemDock::navigate_to_path(const String &p_path) {
  479. file_list_search_box->clear();
  480. _navigate_to_path(p_path);
  481. }
  482. void FileSystemDock::_file_list_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) {
  483. if ((file_list_vb->is_visible_in_tree() || path == p_path.get_base_dir()) && p_preview.is_valid()) {
  484. Array uarr = p_udata;
  485. int idx = uarr[0];
  486. String file = uarr[1];
  487. if (idx < files->get_item_count() && files->get_item_text(idx) == file && files->get_item_metadata(idx) == p_path) {
  488. if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
  489. if (p_small_preview.is_valid()) {
  490. files->set_item_icon(idx, p_small_preview);
  491. }
  492. } else {
  493. files->set_item_icon(idx, p_preview);
  494. }
  495. }
  496. }
  497. }
  498. void FileSystemDock::_tree_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) {
  499. if (p_small_preview.is_valid()) {
  500. Array uarr = p_udata;
  501. if (tree_update_id == (int)uarr[0]) {
  502. TreeItem *file_item = Object::cast_to<TreeItem>(uarr[1]);
  503. if (file_item) {
  504. file_item->set_icon(0, p_small_preview);
  505. }
  506. }
  507. }
  508. }
  509. void FileSystemDock::_toggle_file_display() {
  510. _set_file_display(file_list_display_mode != FILE_LIST_DISPLAY_LIST);
  511. emit_signal("display_mode_changed");
  512. }
  513. void FileSystemDock::_set_file_display(bool p_active) {
  514. if (p_active) {
  515. file_list_display_mode = FILE_LIST_DISPLAY_LIST;
  516. button_file_list_display_mode->set_icon(get_theme_icon("FileThumbnail", "EditorIcons"));
  517. button_file_list_display_mode->set_tooltip(TTR("View items as a grid of thumbnails."));
  518. } else {
  519. file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
  520. button_file_list_display_mode->set_icon(get_theme_icon("FileList", "EditorIcons"));
  521. button_file_list_display_mode->set_tooltip(TTR("View items as a list."));
  522. }
  523. _update_file_list(true);
  524. }
  525. bool FileSystemDock::_is_file_type_disabled_by_feature_profile(const StringName &p_class) {
  526. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  527. if (profile.is_null()) {
  528. return false;
  529. }
  530. StringName class_name = p_class;
  531. while (class_name != StringName()) {
  532. if (profile->is_class_disabled(class_name)) {
  533. return true;
  534. }
  535. class_name = ClassDB::get_parent_class(class_name);
  536. }
  537. return false;
  538. }
  539. void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *matches, int p_max_items) {
  540. if (matches->size() > p_max_items) {
  541. return;
  542. }
  543. for (int i = 0; i < p_path->get_subdir_count(); i++) {
  544. _search(p_path->get_subdir(i), matches, p_max_items);
  545. }
  546. for (int i = 0; i < p_path->get_file_count(); i++) {
  547. String file = p_path->get_file(i);
  548. if (file.to_lower().find(searched_string) != -1) {
  549. FileInfo fi;
  550. fi.name = file;
  551. fi.type = p_path->get_file_type(i);
  552. fi.path = p_path->get_file_path(i);
  553. fi.import_broken = !p_path->get_file_import_is_valid(i);
  554. fi.modified_time = p_path->get_file_modified_time(i);
  555. if (_is_file_type_disabled_by_feature_profile(fi.type)) {
  556. // This type is disabled, will not appear here.
  557. continue;
  558. }
  559. matches->push_back(fi);
  560. if (matches->size() > p_max_items) {
  561. return;
  562. }
  563. }
  564. }
  565. }
  566. struct FileSystemDock::FileInfoTypeComparator {
  567. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  568. // Uses the extension, then the icon name to distinguish file types.
  569. String icon_path_a = "";
  570. String icon_path_b = "";
  571. Ref<Texture2D> icon_a = EditorNode::get_singleton()->get_class_icon(p_a.type);
  572. if (icon_a.is_valid()) {
  573. icon_path_a = icon_a->get_name();
  574. }
  575. Ref<Texture2D> icon_b = EditorNode::get_singleton()->get_class_icon(p_b.type);
  576. if (icon_b.is_valid()) {
  577. icon_path_b = icon_b->get_name();
  578. }
  579. return NaturalNoCaseComparator()(p_a.name.get_extension() + icon_path_a + p_a.name.get_basename(), p_b.name.get_extension() + icon_path_b + p_b.name.get_basename());
  580. }
  581. };
  582. struct FileSystemDock::FileInfoModifiedTimeComparator {
  583. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  584. return p_a.modified_time > p_b.modified_time;
  585. }
  586. };
  587. void FileSystemDock::_sort_file_info_list(List<FileSystemDock::FileInfo> &r_file_list) {
  588. // Sort the file list if needed.
  589. switch (file_sort) {
  590. case FILE_SORT_TYPE:
  591. r_file_list.sort_custom<FileInfoTypeComparator>();
  592. break;
  593. case FILE_SORT_TYPE_REVERSE:
  594. r_file_list.sort_custom<FileInfoTypeComparator>();
  595. r_file_list.reverse();
  596. break;
  597. case FILE_SORT_MODIFIED_TIME:
  598. r_file_list.sort_custom<FileInfoModifiedTimeComparator>();
  599. break;
  600. case FILE_SORT_MODIFIED_TIME_REVERSE:
  601. r_file_list.sort_custom<FileInfoModifiedTimeComparator>();
  602. r_file_list.reverse();
  603. break;
  604. case FILE_SORT_NAME_REVERSE:
  605. r_file_list.reverse();
  606. break;
  607. default: // FILE_SORT_NAME
  608. break;
  609. }
  610. }
  611. void FileSystemDock::_update_file_list(bool p_keep_selection) {
  612. // Register the previously selected items.
  613. Set<String> cselection;
  614. if (p_keep_selection) {
  615. for (int i = 0; i < files->get_item_count(); i++) {
  616. if (files->is_selected(i)) {
  617. cselection.insert(files->get_item_text(i));
  618. }
  619. }
  620. }
  621. files->clear();
  622. _set_current_path_text(path);
  623. String directory = path;
  624. String file = "";
  625. String ei = "EditorIcons";
  626. int thumbnail_size = EditorSettings::get_singleton()->get("docks/filesystem/thumbnail_size");
  627. thumbnail_size *= EDSCALE;
  628. Ref<Texture2D> folder_thumbnail;
  629. Ref<Texture2D> file_thumbnail;
  630. Ref<Texture2D> file_thumbnail_broken;
  631. bool use_thumbnails = (file_list_display_mode == FILE_LIST_DISPLAY_THUMBNAILS);
  632. if (use_thumbnails) {
  633. // Thumbnails mode.
  634. files->set_max_columns(0);
  635. files->set_icon_mode(ItemList::ICON_MODE_TOP);
  636. files->set_fixed_column_width(thumbnail_size * 3 / 2);
  637. files->set_max_text_lines(2);
  638. files->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  639. if (thumbnail_size < 64) {
  640. folder_thumbnail = get_theme_icon("FolderMediumThumb", ei);
  641. file_thumbnail = get_theme_icon("FileMediumThumb", ei);
  642. file_thumbnail_broken = get_theme_icon("FileDeadMediumThumb", ei);
  643. } else {
  644. folder_thumbnail = get_theme_icon("FolderBigThumb", ei);
  645. file_thumbnail = get_theme_icon("FileBigThumb", ei);
  646. file_thumbnail_broken = get_theme_icon("FileDeadBigThumb", ei);
  647. }
  648. } else {
  649. // No thumbnails.
  650. files->set_icon_mode(ItemList::ICON_MODE_LEFT);
  651. files->set_max_columns(1);
  652. files->set_max_text_lines(1);
  653. files->set_fixed_column_width(0);
  654. files->set_fixed_icon_size(Size2());
  655. }
  656. Ref<Texture2D> folder_icon = (use_thumbnails) ? folder_thumbnail : get_theme_icon("folder", "FileDialog");
  657. const Color folder_color = get_theme_color("folder_icon_modulate", "FileDialog");
  658. // Build the FileInfo list.
  659. List<FileInfo> file_list;
  660. if (path == "Favorites") {
  661. // Display the favorites.
  662. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  663. for (int i = 0; i < favorites.size(); i++) {
  664. String favorite = favorites[i];
  665. String text;
  666. Ref<Texture2D> icon;
  667. if (favorite == "res://") {
  668. text = "/";
  669. icon = folder_icon;
  670. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  671. files->add_item(text, icon, true);
  672. files->set_item_metadata(files->get_item_count() - 1, favorite);
  673. }
  674. } else if (favorite.ends_with("/")) {
  675. text = favorite.substr(0, favorite.length() - 1).get_file();
  676. icon = folder_icon;
  677. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  678. files->add_item(text, icon, true);
  679. files->set_item_metadata(files->get_item_count() - 1, favorite);
  680. }
  681. } else {
  682. int index;
  683. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->find_file(favorite, &index);
  684. FileInfo fi;
  685. fi.name = favorite.get_file();
  686. fi.path = favorite;
  687. if (efd) {
  688. fi.type = efd->get_file_type(index);
  689. fi.import_broken = !efd->get_file_import_is_valid(index);
  690. fi.modified_time = efd->get_file_modified_time(index);
  691. } else {
  692. fi.type = "";
  693. fi.import_broken = true;
  694. fi.modified_time = 0;
  695. }
  696. if (searched_string.length() == 0 || fi.name.to_lower().find(searched_string) >= 0) {
  697. file_list.push_back(fi);
  698. }
  699. }
  700. }
  701. } else {
  702. // Get infos on the directory + file.
  703. if (directory.ends_with("/") && directory != "res://") {
  704. directory = directory.substr(0, directory.length() - 1);
  705. }
  706. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
  707. if (!efd) {
  708. directory = path.get_base_dir();
  709. file = path.get_file();
  710. efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
  711. }
  712. if (!efd) {
  713. return;
  714. }
  715. if (searched_string.length() > 0) {
  716. // Display the search results.
  717. _search(EditorFileSystem::get_singleton()->get_filesystem(), &file_list, 128);
  718. } else {
  719. if (display_mode == DISPLAY_MODE_TREE_ONLY || always_show_folders) {
  720. // Display folders in the list.
  721. if (directory != "res://") {
  722. files->add_item("..", folder_icon, true);
  723. String bd = directory.get_base_dir();
  724. if (bd != "res://" && !bd.ends_with("/")) {
  725. bd += "/";
  726. }
  727. files->set_item_metadata(files->get_item_count() - 1, bd);
  728. files->set_item_selectable(files->get_item_count() - 1, false);
  729. files->set_item_icon_modulate(files->get_item_count() - 1, folder_color);
  730. }
  731. bool reversed = file_sort == FILE_SORT_NAME_REVERSE;
  732. for (int i = reversed ? efd->get_subdir_count() - 1 : 0;
  733. reversed ? i >= 0 : i < efd->get_subdir_count();
  734. reversed ? i-- : i++) {
  735. String dname = efd->get_subdir(i)->get_name();
  736. files->add_item(dname, folder_icon, true);
  737. files->set_item_metadata(files->get_item_count() - 1, directory.plus_file(dname) + "/");
  738. files->set_item_icon_modulate(files->get_item_count() - 1, folder_color);
  739. if (cselection.has(dname)) {
  740. files->select(files->get_item_count() - 1, false);
  741. }
  742. }
  743. }
  744. // Display the folder content.
  745. for (int i = 0; i < efd->get_file_count(); i++) {
  746. FileInfo fi;
  747. fi.name = efd->get_file(i);
  748. fi.path = directory.plus_file(fi.name);
  749. fi.type = efd->get_file_type(i);
  750. fi.import_broken = !efd->get_file_import_is_valid(i);
  751. fi.modified_time = efd->get_file_modified_time(i);
  752. file_list.push_back(fi);
  753. }
  754. }
  755. }
  756. // Sort the file list if needed.
  757. _sort_file_info_list(file_list);
  758. // Fills the ItemList control node from the FileInfos.
  759. String main_scene = ProjectSettings::get_singleton()->get("application/run/main_scene");
  760. String oi = "Object";
  761. for (List<FileInfo>::Element *E = file_list.front(); E; E = E->next()) {
  762. FileInfo *finfo = &(E->get());
  763. String fname = finfo->name;
  764. String fpath = finfo->path;
  765. String ftype = finfo->type;
  766. Ref<Texture2D> type_icon;
  767. Ref<Texture2D> big_icon;
  768. String tooltip = fpath;
  769. // Select the icons.
  770. if (!finfo->import_broken) {
  771. type_icon = (has_theme_icon(ftype, ei)) ? get_theme_icon(ftype, ei) : get_theme_icon(oi, ei);
  772. big_icon = file_thumbnail;
  773. } else {
  774. type_icon = get_theme_icon("ImportFail", ei);
  775. big_icon = file_thumbnail_broken;
  776. tooltip += "\n" + TTR("Status: Import of file failed. Please fix file and reimport manually.");
  777. }
  778. // Add the item to the ItemList.
  779. int item_index;
  780. if (use_thumbnails) {
  781. files->add_item(fname, big_icon, true);
  782. item_index = files->get_item_count() - 1;
  783. files->set_item_metadata(item_index, fpath);
  784. files->set_item_tag_icon(item_index, type_icon);
  785. } else {
  786. files->add_item(fname, type_icon, true);
  787. item_index = files->get_item_count() - 1;
  788. files->set_item_metadata(item_index, fpath);
  789. }
  790. if (fpath == main_scene) {
  791. files->set_item_custom_fg_color(item_index, get_theme_color("accent_color", "Editor"));
  792. }
  793. // Generate the preview.
  794. if (!finfo->import_broken) {
  795. Array udata;
  796. udata.resize(2);
  797. udata[0] = item_index;
  798. udata[1] = fname;
  799. EditorResourcePreview::get_singleton()->queue_resource_preview(fpath, this, "_file_list_thumbnail_done", udata);
  800. }
  801. // Select the items.
  802. if (cselection.has(fname)) {
  803. files->select(item_index, false);
  804. }
  805. if (!p_keep_selection && file != "" && fname == file) {
  806. files->select(item_index, true);
  807. files->ensure_current_is_visible();
  808. }
  809. // Tooltip.
  810. if (finfo->sources.size()) {
  811. for (int j = 0; j < finfo->sources.size(); j++) {
  812. tooltip += "\nSource: " + finfo->sources[j];
  813. }
  814. }
  815. files->set_item_tooltip(item_index, tooltip);
  816. }
  817. }
  818. void FileSystemDock::_select_file(const String &p_path, bool p_select_in_favorites) {
  819. String fpath = p_path;
  820. if (fpath.ends_with("/")) {
  821. if (fpath != "res://") {
  822. fpath = fpath.substr(0, fpath.length() - 1);
  823. }
  824. } else if (fpath != "Favorites") {
  825. if (FileAccess::exists(fpath + ".import")) {
  826. Ref<ConfigFile> config;
  827. config.instance();
  828. Error err = config->load(fpath + ".import");
  829. if (err == OK) {
  830. if (config->has_section_key("remap", "importer")) {
  831. String importer = config->get_value("remap", "importer");
  832. if (importer == "keep") {
  833. EditorNode::get_singleton()->show_warning(TTR("Importing has been disabled for this file, so it can't be opened for editing."));
  834. return;
  835. }
  836. }
  837. }
  838. }
  839. if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
  840. bool is_imported = false;
  841. {
  842. List<String> importer_exts;
  843. ResourceImporterScene::get_singleton()->get_recognized_extensions(&importer_exts);
  844. String extension = fpath.get_extension();
  845. for (List<String>::Element *E = importer_exts.front(); E; E = E->next()) {
  846. if (extension.nocasecmp_to(E->get()) == 0) {
  847. is_imported = true;
  848. break;
  849. }
  850. }
  851. }
  852. if (is_imported) {
  853. ResourceImporterScene::get_singleton()->show_advanced_options(fpath);
  854. } else {
  855. editor->open_request(fpath);
  856. }
  857. } else {
  858. editor->load_resource(fpath);
  859. }
  860. }
  861. _navigate_to_path(fpath, p_select_in_favorites);
  862. }
  863. void FileSystemDock::_tree_activate_file() {
  864. TreeItem *selected = tree->get_selected();
  865. if (selected) {
  866. String path = selected->get_metadata(0);
  867. TreeItem *parent = selected->get_parent();
  868. bool is_favorite = parent != nullptr && parent->get_metadata(0) == "Favorites";
  869. if ((!is_favorite && path.ends_with("/")) || path == "Favorites") {
  870. bool collapsed = selected->is_collapsed();
  871. selected->set_collapsed(!collapsed);
  872. } else {
  873. _select_file(path, is_favorite && !path.ends_with("/"));
  874. }
  875. }
  876. }
  877. void FileSystemDock::_file_list_activate_file(int p_idx) {
  878. _select_file(files->get_item_metadata(p_idx));
  879. }
  880. void FileSystemDock::_preview_invalidated(const String &p_path) {
  881. 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()) {
  882. for (int i = 0; i < files->get_item_count(); i++) {
  883. if (files->get_item_metadata(i) == p_path) {
  884. // Re-request preview.
  885. Array udata;
  886. udata.resize(2);
  887. udata[0] = i;
  888. udata[1] = files->get_item_text(i);
  889. EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, this, "_file_list_thumbnail_done", udata);
  890. break;
  891. }
  892. }
  893. }
  894. }
  895. void FileSystemDock::_fs_changed() {
  896. button_hist_prev->set_disabled(history_pos == 0);
  897. button_hist_next->set_disabled(history_pos == history.size() - 1);
  898. scanning_vb->hide();
  899. split_box->show();
  900. if (tree->is_visible()) {
  901. _update_tree(_compute_uncollapsed_paths());
  902. }
  903. if (file_list_vb->is_visible()) {
  904. _update_file_list(true);
  905. }
  906. set_process(false);
  907. }
  908. void FileSystemDock::_set_scanning_mode() {
  909. button_hist_prev->set_disabled(true);
  910. button_hist_next->set_disabled(true);
  911. split_box->hide();
  912. scanning_vb->show();
  913. set_process(true);
  914. if (EditorFileSystem::get_singleton()->is_scanning()) {
  915. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
  916. } else {
  917. scanning_progress->set_value(0);
  918. }
  919. }
  920. void FileSystemDock::_fw_history() {
  921. if (history_pos < history.size() - 1) {
  922. history_pos++;
  923. }
  924. _update_history();
  925. }
  926. void FileSystemDock::_bw_history() {
  927. if (history_pos > 0) {
  928. history_pos--;
  929. }
  930. _update_history();
  931. }
  932. void FileSystemDock::_update_history() {
  933. path = history[history_pos];
  934. _set_current_path_text(path);
  935. if (tree->is_visible()) {
  936. _update_tree(_compute_uncollapsed_paths());
  937. tree->grab_focus();
  938. tree->ensure_cursor_is_visible();
  939. }
  940. if (file_list_vb->is_visible()) {
  941. _update_file_list(false);
  942. }
  943. button_hist_prev->set_disabled(history_pos == 0);
  944. button_hist_next->set_disabled(history_pos == history.size() - 1);
  945. }
  946. void FileSystemDock::_push_to_history() {
  947. if (history[history_pos] != path) {
  948. history.resize(history_pos + 1);
  949. history.push_back(path);
  950. history_pos++;
  951. if (history.size() > history_max_size) {
  952. history.remove(0);
  953. history_pos = history_max_size - 1;
  954. }
  955. }
  956. button_hist_prev->set_disabled(history_pos == 0);
  957. button_hist_next->set_disabled(history_pos == history.size() - 1);
  958. }
  959. void FileSystemDock::_get_all_items_in_dir(EditorFileSystemDirectory *efsd, Vector<String> &files, Vector<String> &folders) const {
  960. if (efsd == nullptr) {
  961. return;
  962. }
  963. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  964. folders.push_back(efsd->get_subdir(i)->get_path());
  965. _get_all_items_in_dir(efsd->get_subdir(i), files, folders);
  966. }
  967. for (int i = 0; i < efsd->get_file_count(); i++) {
  968. files.push_back(efsd->get_file_path(i));
  969. }
  970. }
  971. void FileSystemDock::_find_remaps(EditorFileSystemDirectory *efsd, const Map<String, String> &renames, Vector<String> &to_remaps) const {
  972. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  973. _find_remaps(efsd->get_subdir(i), renames, to_remaps);
  974. }
  975. for (int i = 0; i < efsd->get_file_count(); i++) {
  976. Vector<String> deps = efsd->get_file_deps(i);
  977. for (int j = 0; j < deps.size(); j++) {
  978. if (renames.has(deps[j])) {
  979. to_remaps.push_back(efsd->get_file_path(i));
  980. break;
  981. }
  982. }
  983. }
  984. }
  985. void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_new_path,
  986. Map<String, String> &p_file_renames, Map<String, String> &p_folder_renames) {
  987. // Ensure folder paths end with "/".
  988. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  989. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  990. if (new_path == old_path) {
  991. return;
  992. } else if (old_path == "res://") {
  993. EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
  994. return;
  995. } else if (!p_item.is_file && new_path.begins_with(old_path)) {
  996. // This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/".
  997. EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
  998. return;
  999. }
  1000. // Build a list of files which will have new paths as a result of this operation.
  1001. Vector<String> file_changed_paths;
  1002. Vector<String> folder_changed_paths;
  1003. if (p_item.is_file) {
  1004. file_changed_paths.push_back(old_path);
  1005. } else {
  1006. folder_changed_paths.push_back(old_path);
  1007. _get_all_items_in_dir(EditorFileSystem::get_singleton()->get_filesystem_path(old_path), file_changed_paths, folder_changed_paths);
  1008. }
  1009. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1010. print_verbose("Moving " + old_path + " -> " + new_path);
  1011. Error err = da->rename(old_path, new_path);
  1012. if (err == OK) {
  1013. // Move/Rename any corresponding import settings too.
  1014. if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
  1015. err = da->rename(old_path + ".import", new_path + ".import");
  1016. if (err != OK) {
  1017. EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + ".import\n");
  1018. }
  1019. }
  1020. // Update scene if it is open.
  1021. for (int i = 0; i < file_changed_paths.size(); ++i) {
  1022. String new_item_path = p_item.is_file ? new_path : file_changed_paths[i].replace_first(old_path, new_path);
  1023. if (ResourceLoader::get_resource_type(new_item_path) == "PackedScene" && editor->is_scene_open(file_changed_paths[i])) {
  1024. EditorData *ed = &editor->get_editor_data();
  1025. for (int j = 0; j < ed->get_edited_scene_count(); j++) {
  1026. if (ed->get_scene_path(j) == file_changed_paths[i]) {
  1027. ed->get_edited_scene_root(j)->set_filename(new_item_path);
  1028. editor->save_layout();
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. // Only treat as a changed dependency if it was successfully moved.
  1035. for (int i = 0; i < file_changed_paths.size(); ++i) {
  1036. p_file_renames[file_changed_paths[i]] = file_changed_paths[i].replace_first(old_path, new_path);
  1037. print_verbose(" Remap: " + file_changed_paths[i] + " -> " + p_file_renames[file_changed_paths[i]]);
  1038. emit_signal("files_moved", file_changed_paths[i], p_file_renames[file_changed_paths[i]]);
  1039. }
  1040. for (int i = 0; i < folder_changed_paths.size(); ++i) {
  1041. p_folder_renames[folder_changed_paths[i]] = folder_changed_paths[i].replace_first(old_path, new_path);
  1042. emit_signal("folder_moved", folder_changed_paths[i], p_folder_renames[folder_changed_paths[i]].substr(0, p_folder_renames[folder_changed_paths[i]].length() - 1));
  1043. }
  1044. } else {
  1045. EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + "\n");
  1046. }
  1047. memdelete(da);
  1048. }
  1049. void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const {
  1050. // Ensure folder paths end with "/".
  1051. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  1052. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  1053. if (new_path == old_path) {
  1054. return;
  1055. } else if (old_path == "res://") {
  1056. EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
  1057. return;
  1058. } else if (!p_item.is_file && new_path.begins_with(old_path)) {
  1059. // This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/".
  1060. EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
  1061. return;
  1062. }
  1063. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1064. print_verbose("Duplicating " + old_path + " -> " + new_path);
  1065. Error err = p_item.is_file ? da->copy(old_path, new_path) : da->copy_dir(old_path, new_path);
  1066. if (err == OK) {
  1067. // Move/Rename any corresponding import settings too.
  1068. if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
  1069. err = da->copy(old_path + ".import", new_path + ".import");
  1070. if (err != OK) {
  1071. EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + ".import\n");
  1072. }
  1073. }
  1074. } else {
  1075. EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + "\n");
  1076. }
  1077. memdelete(da);
  1078. }
  1079. void FileSystemDock::_update_resource_paths_after_move(const Map<String, String> &p_renames) const {
  1080. // Rename all resources loaded, be it subresources or actual resources.
  1081. List<Ref<Resource>> cached;
  1082. ResourceCache::get_cached_resources(&cached);
  1083. for (List<Ref<Resource>>::Element *E = cached.front(); E; E = E->next()) {
  1084. Ref<Resource> r = E->get();
  1085. String base_path = r->get_path();
  1086. String extra_path;
  1087. int sep_pos = r->get_path().find("::");
  1088. if (sep_pos >= 0) {
  1089. extra_path = base_path.substr(sep_pos, base_path.length());
  1090. base_path = base_path.substr(0, sep_pos);
  1091. }
  1092. if (p_renames.has(base_path)) {
  1093. base_path = p_renames[base_path];
  1094. }
  1095. r->set_path(base_path + extra_path);
  1096. }
  1097. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
  1098. String path;
  1099. if (i == EditorNode::get_editor_data().get_edited_scene()) {
  1100. if (!get_tree()->get_edited_scene_root()) {
  1101. continue;
  1102. }
  1103. path = get_tree()->get_edited_scene_root()->get_filename();
  1104. } else {
  1105. path = EditorNode::get_editor_data().get_scene_path(i);
  1106. }
  1107. if (p_renames.has(path)) {
  1108. path = p_renames[path];
  1109. }
  1110. if (i == EditorNode::get_editor_data().get_edited_scene()) {
  1111. get_tree()->get_edited_scene_root()->set_filename(path);
  1112. } else {
  1113. EditorNode::get_editor_data().set_scene_path(i, path);
  1114. }
  1115. }
  1116. }
  1117. void FileSystemDock::_update_dependencies_after_move(const Map<String, String> &p_renames) const {
  1118. // The following code assumes that the following holds:
  1119. // 1) EditorFileSystem contains the old paths/folder structure from before the rename/move.
  1120. // 2) ResourceLoader can use the new paths without needing to call rescan.
  1121. Vector<String> remaps;
  1122. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(), p_renames, remaps);
  1123. for (int i = 0; i < remaps.size(); ++i) {
  1124. // Because we haven't called a rescan yet the found remap might still be an old path itself.
  1125. String file = p_renames.has(remaps[i]) ? p_renames[remaps[i]] : remaps[i];
  1126. print_verbose("Remapping dependencies for: " + file);
  1127. Error err = ResourceLoader::rename_dependencies(file, p_renames);
  1128. if (err == OK) {
  1129. if (ResourceLoader::get_resource_type(file) == "PackedScene") {
  1130. editor->reload_scene(file);
  1131. }
  1132. } else {
  1133. EditorNode::get_singleton()->add_io_error(TTR("Unable to update dependencies:") + "\n" + remaps[i] + "\n");
  1134. }
  1135. }
  1136. }
  1137. void FileSystemDock::_update_project_settings_after_move(const Map<String, String> &p_renames) const {
  1138. // Find all project settings of type FILE and replace them if needed.
  1139. const Map<StringName, PropertyInfo> prop_info = ProjectSettings::get_singleton()->get_custom_property_info();
  1140. for (const Map<StringName, PropertyInfo>::Element *E = prop_info.front(); E; E = E->next()) {
  1141. if (E->get().hint == PROPERTY_HINT_FILE) {
  1142. String old_path = GLOBAL_GET(E->key());
  1143. if (p_renames.has(old_path)) {
  1144. ProjectSettings::get_singleton()->set_setting(E->key(), p_renames[old_path]);
  1145. }
  1146. };
  1147. }
  1148. // Also search for the file in autoload, as they are stored differently from normal files.
  1149. List<PropertyInfo> property_list;
  1150. ProjectSettings::get_singleton()->get_property_list(&property_list);
  1151. for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  1152. if (E->get().name.begins_with("autoload/")) {
  1153. // If the autoload resource paths has a leading "*", it indicates that it is a Singleton,
  1154. // so we have to handle both cases when updating.
  1155. String autoload = GLOBAL_GET(E->get().name);
  1156. String autoload_singleton = autoload.substr(1, autoload.length());
  1157. if (p_renames.has(autoload)) {
  1158. ProjectSettings::get_singleton()->set_setting(E->get().name, p_renames[autoload]);
  1159. } else if (autoload.begins_with("*") && p_renames.has(autoload_singleton)) {
  1160. ProjectSettings::get_singleton()->set_setting(E->get().name, "*" + p_renames[autoload_singleton]);
  1161. }
  1162. }
  1163. }
  1164. ProjectSettings::get_singleton()->save();
  1165. }
  1166. void FileSystemDock::_update_favorites_list_after_move(const Map<String, String> &p_files_renames, const Map<String, String> &p_folders_renames) const {
  1167. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1168. Vector<String> new_favorites;
  1169. for (int i = 0; i < favorites.size(); i++) {
  1170. String old_path = favorites[i];
  1171. if (p_folders_renames.has(old_path)) {
  1172. new_favorites.push_back(p_folders_renames[old_path]);
  1173. } else if (p_files_renames.has(old_path)) {
  1174. new_favorites.push_back(p_files_renames[old_path]);
  1175. } else {
  1176. new_favorites.push_back(old_path);
  1177. }
  1178. }
  1179. EditorSettings::get_singleton()->set_favorites(new_favorites);
  1180. }
  1181. void FileSystemDock::_save_scenes_after_move(const Map<String, String> &p_renames) const {
  1182. Vector<String> remaps;
  1183. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(), p_renames, remaps);
  1184. Vector<String> new_filenames;
  1185. for (int i = 0; i < remaps.size(); ++i) {
  1186. String file = p_renames.has(remaps[i]) ? p_renames[remaps[i]] : remaps[i];
  1187. if (ResourceLoader::get_resource_type(file) == "PackedScene") {
  1188. new_filenames.push_back(file);
  1189. }
  1190. }
  1191. editor->save_scene_list(new_filenames);
  1192. }
  1193. void FileSystemDock::_make_dir_confirm() {
  1194. String dir_name = make_dir_dialog_text->get_text().strip_edges();
  1195. if (dir_name.length() == 0) {
  1196. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1197. return;
  1198. } else if (dir_name.find("/") != -1 || dir_name.find("\\") != -1 || dir_name.find(":") != -1 || dir_name.find("*") != -1 ||
  1199. dir_name.find("|") != -1 || dir_name.find(">") != -1 || dir_name.ends_with(".") || dir_name.ends_with(" ")) {
  1200. EditorNode::get_singleton()->show_warning(TTR("Provided name contains invalid characters."));
  1201. return;
  1202. }
  1203. String directory = path;
  1204. if (!directory.ends_with("/")) {
  1205. directory = directory.get_base_dir();
  1206. }
  1207. print_verbose("Making folder " + dir_name + " in " + directory);
  1208. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1209. Error err = da->change_dir(directory);
  1210. if (err == OK) {
  1211. err = da->make_dir(dir_name);
  1212. }
  1213. memdelete(da);
  1214. if (err == OK) {
  1215. print_verbose("FileSystem: calling rescan.");
  1216. _rescan();
  1217. } else {
  1218. EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
  1219. }
  1220. }
  1221. void FileSystemDock::_make_scene_confirm() {
  1222. String scene_name = make_scene_dialog_text->get_text().strip_edges();
  1223. if (scene_name.length() == 0) {
  1224. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1225. return;
  1226. }
  1227. String directory = path;
  1228. if (!directory.ends_with("/")) {
  1229. directory = directory.get_base_dir();
  1230. }
  1231. String extension = scene_name.get_extension();
  1232. List<String> extensions;
  1233. Ref<PackedScene> sd = memnew(PackedScene);
  1234. ResourceSaver::get_recognized_extensions(sd, &extensions);
  1235. bool extension_correct = false;
  1236. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  1237. if (E->get() == extension) {
  1238. extension_correct = true;
  1239. break;
  1240. }
  1241. }
  1242. if (!extension_correct) {
  1243. scene_name = scene_name.get_basename() + ".tscn";
  1244. }
  1245. scene_name = directory.plus_file(scene_name);
  1246. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1247. if (da->file_exists(scene_name)) {
  1248. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  1249. memdelete(da);
  1250. return;
  1251. }
  1252. memdelete(da);
  1253. int idx = editor->new_scene();
  1254. editor->get_editor_data().set_scene_path(idx, scene_name);
  1255. }
  1256. void FileSystemDock::_file_removed(String p_file) {
  1257. emit_signal("file_removed", p_file);
  1258. // Find the closest parent directory available, in case multiple items were deleted along the same path.
  1259. path = p_file.get_base_dir();
  1260. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1261. while (!da->dir_exists(path)) {
  1262. path = path.get_base_dir();
  1263. }
  1264. current_path->set_text(path);
  1265. }
  1266. void FileSystemDock::_folder_removed(String p_folder) {
  1267. emit_signal("folder_removed", p_folder);
  1268. // Find the closest parent directory available, in case multiple items were deleted along the same path.
  1269. path = p_folder.get_base_dir();
  1270. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1271. while (!da->dir_exists(path)) {
  1272. path = path.get_base_dir();
  1273. }
  1274. current_path->set_text(path);
  1275. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(path);
  1276. if (efd) {
  1277. efd->force_update();
  1278. }
  1279. }
  1280. void FileSystemDock::_rename_operation_confirm() {
  1281. String new_name = rename_dialog_text->get_text().strip_edges();
  1282. if (new_name.length() == 0) {
  1283. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1284. return;
  1285. } else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
  1286. EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
  1287. return;
  1288. }
  1289. String old_path = to_rename.path.ends_with("/") ? to_rename.path.substr(0, to_rename.path.length() - 1) : to_rename.path;
  1290. String new_path = old_path.get_base_dir().plus_file(new_name);
  1291. if (old_path == new_path) {
  1292. return;
  1293. }
  1294. if (EditorFileSystem::get_singleton()->is_group_file(old_path)) {
  1295. EditorFileSystem::get_singleton()->move_group_file(old_path, new_path);
  1296. }
  1297. // Present a more user friendly warning for name conflict.
  1298. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1299. #if defined(WINDOWS_ENABLED) || defined(UWP_ENABLED)
  1300. // Workaround case insensitivity on Windows.
  1301. if ((da->file_exists(new_path) || da->dir_exists(new_path)) && new_path.to_lower() != old_path.to_lower()) {
  1302. #else
  1303. if (da->file_exists(new_path) || da->dir_exists(new_path)) {
  1304. #endif
  1305. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  1306. memdelete(da);
  1307. return;
  1308. }
  1309. memdelete(da);
  1310. Map<String, String> file_renames;
  1311. Map<String, String> folder_renames;
  1312. _try_move_item(to_rename, new_path, file_renames, folder_renames);
  1313. int current_tab = editor->get_current_tab();
  1314. _save_scenes_after_move(file_renames); // save scenes before updating
  1315. _update_dependencies_after_move(file_renames);
  1316. _update_resource_paths_after_move(file_renames);
  1317. _update_project_settings_after_move(file_renames);
  1318. _update_favorites_list_after_move(file_renames, folder_renames);
  1319. editor->set_current_tab(current_tab);
  1320. print_verbose("FileSystem: calling rescan.");
  1321. _rescan();
  1322. print_verbose("FileSystem: saving moved scenes.");
  1323. _save_scenes_after_move(file_renames);
  1324. path = new_path;
  1325. current_path->set_text(path);
  1326. }
  1327. void FileSystemDock::_duplicate_operation_confirm() {
  1328. String new_name = duplicate_dialog_text->get_text().strip_edges();
  1329. if (new_name.length() == 0) {
  1330. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1331. return;
  1332. } else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
  1333. EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
  1334. return;
  1335. }
  1336. String base_dir = to_duplicate.path.get_base_dir();
  1337. // get_base_dir() returns "some/path" if the original path was "some/path/", so work it around.
  1338. if (to_duplicate.path.ends_with("/")) {
  1339. base_dir = base_dir.get_base_dir();
  1340. }
  1341. String new_path = base_dir.plus_file(new_name);
  1342. // Present a more user friendly warning for name conflict
  1343. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1344. if (da->file_exists(new_path) || da->dir_exists(new_path)) {
  1345. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  1346. memdelete(da);
  1347. return;
  1348. }
  1349. memdelete(da);
  1350. _try_duplicate_item(to_duplicate, new_path);
  1351. // Rescan everything.
  1352. print_verbose("FileSystem: calling rescan.");
  1353. _rescan();
  1354. }
  1355. void FileSystemDock::_move_with_overwrite() {
  1356. _move_operation_confirm(to_move_path, true);
  1357. }
  1358. Vector<String> FileSystemDock::_check_existing() {
  1359. Vector<String> conflicting_items;
  1360. String &p_to_path = to_move_path;
  1361. for (int i = 0; i < to_move.size(); i++) {
  1362. 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;
  1363. String p_new_path = p_to_path.plus_file(ol_pth.get_file());
  1364. FileOrFolder p_item = to_move[i];
  1365. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  1366. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  1367. if ((p_item.is_file && FileAccess::exists(new_path)) ||
  1368. (!p_item.is_file && DirAccess::exists(new_path))) {
  1369. conflicting_items.push_back(old_path);
  1370. }
  1371. }
  1372. return conflicting_items;
  1373. }
  1374. void FileSystemDock::_move_operation_confirm(const String &p_to_path, bool p_overwrite) {
  1375. if (!p_overwrite) {
  1376. to_move_path = p_to_path;
  1377. Vector<String> conflicting_items = _check_existing();
  1378. if (!conflicting_items.is_empty()) {
  1379. // Ask to do something.
  1380. overwrite_dialog->set_text(vformat(
  1381. TTR("The following files or folders conflict with items in the target location '%s':\n\n%s\n\nDo you wish to overwrite them?"),
  1382. to_move_path,
  1383. String("\n").join(conflicting_items)));
  1384. overwrite_dialog->popup_centered();
  1385. return;
  1386. }
  1387. }
  1388. // Check groups.
  1389. for (int i = 0; i < to_move.size(); i++) {
  1390. if (to_move[i].is_file && EditorFileSystem::get_singleton()->is_group_file(to_move[i].path)) {
  1391. EditorFileSystem::get_singleton()->move_group_file(to_move[i].path, p_to_path.plus_file(to_move[i].path.get_file()));
  1392. }
  1393. }
  1394. Map<String, String> file_renames;
  1395. Map<String, String> folder_renames;
  1396. bool is_moved = false;
  1397. for (int i = 0; i < to_move.size(); i++) {
  1398. 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;
  1399. String new_path = p_to_path.plus_file(old_path.get_file());
  1400. if (old_path != new_path) {
  1401. _try_move_item(to_move[i], new_path, file_renames, folder_renames);
  1402. is_moved = true;
  1403. }
  1404. }
  1405. if (is_moved) {
  1406. int current_tab = editor->get_current_tab();
  1407. _save_scenes_after_move(file_renames); // Save scenes before updating.
  1408. _update_dependencies_after_move(file_renames);
  1409. _update_resource_paths_after_move(file_renames);
  1410. _update_project_settings_after_move(file_renames);
  1411. _update_favorites_list_after_move(file_renames, folder_renames);
  1412. editor->set_current_tab(current_tab);
  1413. print_verbose("FileSystem: calling rescan.");
  1414. _rescan();
  1415. print_verbose("FileSystem: saving moved scenes.");
  1416. _save_scenes_after_move(file_renames);
  1417. path = p_to_path;
  1418. current_path->set_text(path);
  1419. }
  1420. }
  1421. Vector<String> FileSystemDock::_tree_get_selected(bool remove_self_inclusion) {
  1422. // Build a list of selected items with the active one at the first position.
  1423. Vector<String> selected_strings;
  1424. TreeItem *favorites_item = tree->get_root()->get_first_child();
  1425. TreeItem *active_selected = tree->get_selected();
  1426. if (active_selected && active_selected != favorites_item) {
  1427. selected_strings.push_back(active_selected->get_metadata(0));
  1428. }
  1429. TreeItem *selected = tree->get_root();
  1430. selected = tree->get_next_selected(selected);
  1431. while (selected) {
  1432. if (selected != active_selected && selected != favorites_item) {
  1433. selected_strings.push_back(selected->get_metadata(0));
  1434. }
  1435. selected = tree->get_next_selected(selected);
  1436. }
  1437. if (remove_self_inclusion) {
  1438. selected_strings = _remove_self_included_paths(selected_strings);
  1439. }
  1440. return selected_strings;
  1441. }
  1442. Vector<String> FileSystemDock::_remove_self_included_paths(Vector<String> selected_strings) {
  1443. // Remove paths or files that are included into another.
  1444. if (selected_strings.size() > 1) {
  1445. selected_strings.sort_custom<NaturalNoCaseComparator>();
  1446. String last_path = "";
  1447. for (int i = 0; i < selected_strings.size(); i++) {
  1448. if (last_path != "" && selected_strings[i].begins_with(last_path)) {
  1449. selected_strings.remove(i);
  1450. i--;
  1451. }
  1452. if (selected_strings[i].ends_with("/")) {
  1453. last_path = selected_strings[i];
  1454. }
  1455. }
  1456. }
  1457. return selected_strings;
  1458. }
  1459. void FileSystemDock::_tree_rmb_option(int p_option) {
  1460. Vector<String> selected_strings = _tree_get_selected(false);
  1461. // Execute the current option.
  1462. switch (p_option) {
  1463. case FOLDER_EXPAND_ALL:
  1464. case FOLDER_COLLAPSE_ALL: {
  1465. // Expand or collapse the folder
  1466. if (selected_strings.size() == 1) {
  1467. bool is_collapsed = (p_option == FOLDER_COLLAPSE_ALL);
  1468. Vector<TreeItem *> needs_check;
  1469. needs_check.push_back(tree->get_selected());
  1470. while (needs_check.size()) {
  1471. needs_check[0]->set_collapsed(is_collapsed);
  1472. TreeItem *child = needs_check[0]->get_first_child();
  1473. while (child) {
  1474. needs_check.push_back(child);
  1475. child = child->get_next();
  1476. }
  1477. needs_check.remove(0);
  1478. }
  1479. }
  1480. } break;
  1481. default: {
  1482. _file_option(p_option, selected_strings);
  1483. } break;
  1484. }
  1485. }
  1486. void FileSystemDock::_file_list_rmb_option(int p_option) {
  1487. Vector<int> selected_id = files->get_selected_items();
  1488. Vector<String> selected;
  1489. for (int i = 0; i < selected_id.size(); i++) {
  1490. selected.push_back(files->get_item_metadata(selected_id[i]));
  1491. }
  1492. _file_option(p_option, selected);
  1493. }
  1494. void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected) {
  1495. // The first one should be the active item.
  1496. switch (p_option) {
  1497. case FILE_SHOW_IN_EXPLORER: {
  1498. // Show the file/folder in the OS explorer.
  1499. String fpath = path;
  1500. if (path == "Favorites") {
  1501. fpath = p_selected[0];
  1502. }
  1503. if (!fpath.ends_with("/")) {
  1504. fpath = fpath.get_base_dir();
  1505. }
  1506. String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
  1507. OS::get_singleton()->shell_open(String("file://") + dir);
  1508. } break;
  1509. case FILE_OPEN: {
  1510. // Open folders.
  1511. TreeItem *selected = tree->get_root();
  1512. selected = tree->get_next_selected(selected);
  1513. while (selected) {
  1514. if (p_selected.find(selected->get_metadata(0)) >= 0) {
  1515. selected->set_collapsed(false);
  1516. }
  1517. selected = tree->get_next_selected(selected);
  1518. }
  1519. // Open the file.
  1520. for (int i = 0; i < p_selected.size(); i++) {
  1521. _select_file(p_selected[i]);
  1522. }
  1523. } break;
  1524. case FILE_INHERIT: {
  1525. // Create a new scene inherited from the selected one.
  1526. if (p_selected.size() == 1) {
  1527. emit_signal("inherit", p_selected[0]);
  1528. }
  1529. } break;
  1530. case FILE_MAIN_SCENE: {
  1531. // Set as main scene with selected scene file.
  1532. if (p_selected.size() == 1) {
  1533. ProjectSettings::get_singleton()->set("application/run/main_scene", p_selected[0]);
  1534. ProjectSettings::get_singleton()->save();
  1535. _update_tree(_compute_uncollapsed_paths());
  1536. _update_file_list(true);
  1537. }
  1538. } break;
  1539. case FILE_INSTANCE: {
  1540. // Instance all selected scenes.
  1541. Vector<String> paths;
  1542. for (int i = 0; i < p_selected.size(); i++) {
  1543. String fpath = p_selected[i];
  1544. if (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene") {
  1545. paths.push_back(fpath);
  1546. }
  1547. }
  1548. if (!paths.is_empty()) {
  1549. emit_signal("instance", paths);
  1550. }
  1551. } break;
  1552. case FILE_ADD_FAVORITE: {
  1553. // Add the files from favorites.
  1554. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1555. for (int i = 0; i < p_selected.size(); i++) {
  1556. if (favorites.find(p_selected[i]) == -1) {
  1557. favorites.push_back(p_selected[i]);
  1558. }
  1559. }
  1560. EditorSettings::get_singleton()->set_favorites(favorites);
  1561. _update_tree(_compute_uncollapsed_paths());
  1562. } break;
  1563. case FILE_REMOVE_FAVORITE: {
  1564. // Remove the files from favorites.
  1565. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1566. for (int i = 0; i < p_selected.size(); i++) {
  1567. favorites.erase(p_selected[i]);
  1568. }
  1569. EditorSettings::get_singleton()->set_favorites(favorites);
  1570. _update_tree(_compute_uncollapsed_paths());
  1571. if (path == "Favorites") {
  1572. _update_file_list(true);
  1573. }
  1574. } break;
  1575. case FILE_DEPENDENCIES: {
  1576. // Checkout the file dependencies.
  1577. if (!p_selected.is_empty()) {
  1578. String fpath = p_selected[0];
  1579. deps_editor->edit(fpath);
  1580. }
  1581. } break;
  1582. case FILE_OWNERS: {
  1583. // Checkout the file owners.
  1584. if (!p_selected.is_empty()) {
  1585. String fpath = p_selected[0];
  1586. owners_editor->show(fpath);
  1587. }
  1588. } break;
  1589. case FILE_MOVE: {
  1590. // Move the files to a given location.
  1591. to_move.clear();
  1592. Vector<String> collapsed_paths = _remove_self_included_paths(p_selected);
  1593. for (int i = collapsed_paths.size() - 1; i >= 0; i--) {
  1594. String fpath = collapsed_paths[i];
  1595. if (fpath != "res://") {
  1596. to_move.push_back(FileOrFolder(fpath, !fpath.ends_with("/")));
  1597. }
  1598. }
  1599. if (to_move.size() > 0) {
  1600. move_dialog->popup_centered_ratio();
  1601. }
  1602. } break;
  1603. case FILE_RENAME: {
  1604. // Rename the active file.
  1605. if (!p_selected.is_empty()) {
  1606. to_rename.path = p_selected[0];
  1607. if (to_rename.path != "res://") {
  1608. to_rename.is_file = !to_rename.path.ends_with("/");
  1609. if (to_rename.is_file) {
  1610. String name = to_rename.path.get_file();
  1611. rename_dialog->set_title(TTR("Renaming file:") + " " + name);
  1612. rename_dialog_text->set_text(name);
  1613. rename_dialog_text->select(0, name.rfind("."));
  1614. } else {
  1615. String name = to_rename.path.substr(0, to_rename.path.length() - 1).get_file();
  1616. rename_dialog->set_title(TTR("Renaming folder:") + " " + name);
  1617. rename_dialog_text->set_text(name);
  1618. rename_dialog_text->select(0, name.length());
  1619. }
  1620. rename_dialog->popup_centered(Size2(250, 80) * EDSCALE);
  1621. rename_dialog_text->grab_focus();
  1622. }
  1623. }
  1624. } break;
  1625. case FILE_REMOVE: {
  1626. // Remove the selected files.
  1627. Vector<String> remove_files;
  1628. Vector<String> remove_folders;
  1629. Vector<String> collapsed_paths = _remove_self_included_paths(p_selected);
  1630. for (int i = 0; i < collapsed_paths.size(); i++) {
  1631. String fpath = collapsed_paths[i];
  1632. if (fpath != "res://") {
  1633. if (fpath.ends_with("/")) {
  1634. remove_folders.push_back(fpath);
  1635. } else {
  1636. remove_files.push_back(fpath);
  1637. }
  1638. }
  1639. }
  1640. if (remove_files.size() + remove_folders.size() > 0) {
  1641. remove_dialog->show(remove_folders, remove_files);
  1642. }
  1643. } break;
  1644. case FILE_DUPLICATE: {
  1645. // Duplicate the selected files.
  1646. for (int i = 0; i < p_selected.size(); i++) {
  1647. to_duplicate.path = p_selected[i];
  1648. to_duplicate.is_file = !to_duplicate.path.ends_with("/");
  1649. if (to_duplicate.is_file) {
  1650. String name = to_duplicate.path.get_file();
  1651. duplicate_dialog->set_title(TTR("Duplicating file:") + " " + name);
  1652. duplicate_dialog_text->set_text(name);
  1653. duplicate_dialog_text->select(0, name.rfind("."));
  1654. } else {
  1655. String name = to_duplicate.path.substr(0, to_duplicate.path.length() - 1).get_file();
  1656. duplicate_dialog->set_title(TTR("Duplicating folder:") + " " + name);
  1657. duplicate_dialog_text->set_text(name);
  1658. duplicate_dialog_text->select(0, name.length());
  1659. }
  1660. duplicate_dialog->popup_centered(Size2(250, 80) * EDSCALE);
  1661. duplicate_dialog_text->grab_focus();
  1662. }
  1663. } break;
  1664. case FILE_INFO: {
  1665. } break;
  1666. case FILE_REIMPORT: {
  1667. // Reimport all selected files.
  1668. Vector<String> reimport;
  1669. for (int i = 0; i < p_selected.size(); i++) {
  1670. reimport.push_back(p_selected[i]);
  1671. }
  1672. ERR_FAIL_COND_MSG(reimport.size() == 0, "You need to select files to reimport them.");
  1673. } break;
  1674. case FILE_NEW_FOLDER: {
  1675. make_dir_dialog_text->set_text("new folder");
  1676. make_dir_dialog_text->select_all();
  1677. make_dir_dialog->popup_centered(Size2(250, 80) * EDSCALE);
  1678. make_dir_dialog_text->grab_focus();
  1679. } break;
  1680. case FILE_NEW_SCENE: {
  1681. make_scene_dialog_text->set_text("new scene");
  1682. make_scene_dialog_text->select_all();
  1683. make_scene_dialog->popup_centered(Size2(250, 80) * EDSCALE);
  1684. make_scene_dialog_text->grab_focus();
  1685. } break;
  1686. case FILE_NEW_SCRIPT: {
  1687. String fpath = path;
  1688. if (!fpath.ends_with("/")) {
  1689. fpath = fpath.get_base_dir();
  1690. }
  1691. make_script_dialog->config("Node", fpath.plus_file("new_script.gd"), false, false);
  1692. make_script_dialog->popup_centered();
  1693. } break;
  1694. case FILE_COPY_PATH: {
  1695. if (!p_selected.is_empty()) {
  1696. String fpath = p_selected[0];
  1697. DisplayServer::get_singleton()->clipboard_set(fpath);
  1698. }
  1699. } break;
  1700. case FILE_NEW_RESOURCE: {
  1701. new_resource_dialog->popup_create(true);
  1702. } break;
  1703. }
  1704. }
  1705. void FileSystemDock::_resource_created() {
  1706. Variant c = new_resource_dialog->instance_selected();
  1707. ERR_FAIL_COND(!c);
  1708. Resource *r = Object::cast_to<Resource>(c);
  1709. ERR_FAIL_COND(!r);
  1710. PackedScene *scene = Object::cast_to<PackedScene>(r);
  1711. if (scene) {
  1712. Node *node = memnew(Node);
  1713. node->set_name("Node");
  1714. scene->pack(node);
  1715. memdelete(node);
  1716. }
  1717. editor->push_item(r);
  1718. String fpath = path;
  1719. if (!fpath.ends_with("/")) {
  1720. fpath = fpath.get_base_dir();
  1721. }
  1722. editor->save_resource_as(RES(r), fpath);
  1723. }
  1724. void FileSystemDock::_focus_current_search_box() {
  1725. LineEdit *current_search_box = nullptr;
  1726. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  1727. current_search_box = tree_search_box;
  1728. } else if (display_mode == DISPLAY_MODE_SPLIT) {
  1729. current_search_box = file_list_search_box;
  1730. }
  1731. if (current_search_box) {
  1732. current_search_box->grab_focus();
  1733. current_search_box->select_all();
  1734. }
  1735. }
  1736. void FileSystemDock::_search_changed(const String &p_text, const Control *p_from) {
  1737. if (searched_string.length() == 0) {
  1738. // Register the uncollapsed paths before they change.
  1739. uncollapsed_paths_before_search = _compute_uncollapsed_paths();
  1740. }
  1741. searched_string = p_text.to_lower();
  1742. if (p_from == tree_search_box) {
  1743. file_list_search_box->set_text(searched_string);
  1744. } else { // File_list_search_box.
  1745. tree_search_box->set_text(searched_string);
  1746. }
  1747. bool unfold_path = (p_text == String() && path != String());
  1748. switch (display_mode) {
  1749. case DISPLAY_MODE_TREE_ONLY: {
  1750. _update_tree(searched_string.length() == 0 ? uncollapsed_paths_before_search : Vector<String>(), false, false, unfold_path);
  1751. } break;
  1752. case DISPLAY_MODE_SPLIT: {
  1753. _update_file_list(false);
  1754. _update_tree(searched_string.length() == 0 ? uncollapsed_paths_before_search : Vector<String>(), false, false, unfold_path);
  1755. } break;
  1756. }
  1757. }
  1758. void FileSystemDock::_rescan() {
  1759. _set_scanning_mode();
  1760. EditorFileSystem::get_singleton()->scan();
  1761. }
  1762. void FileSystemDock::_toggle_split_mode(bool p_active) {
  1763. set_display_mode(p_active ? DISPLAY_MODE_SPLIT : DISPLAY_MODE_TREE_ONLY);
  1764. emit_signal("display_mode_changed");
  1765. }
  1766. void FileSystemDock::fix_dependencies(const String &p_for_file) {
  1767. deps_editor->edit(p_for_file);
  1768. }
  1769. void FileSystemDock::focus_on_filter() {
  1770. file_list_search_box->grab_focus();
  1771. }
  1772. void FileSystemDock::set_file_list_display_mode(FileListDisplayMode p_mode) {
  1773. if (p_mode == file_list_display_mode) {
  1774. return;
  1775. }
  1776. _toggle_file_display();
  1777. }
  1778. Variant FileSystemDock::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1779. bool all_favorites = true;
  1780. bool all_not_favorites = true;
  1781. Vector<String> paths;
  1782. if (p_from == tree) {
  1783. // Check if the first selected is in favorite.
  1784. TreeItem *selected = tree->get_next_selected(tree->get_root());
  1785. while (selected) {
  1786. TreeItem *favorites_item = tree->get_root()->get_first_child();
  1787. if (selected == favorites_item) {
  1788. // The "Favorites" item is not draggable.
  1789. return Variant();
  1790. }
  1791. bool is_favorite = selected->get_parent() != nullptr && tree->get_root()->get_first_child() == selected->get_parent();
  1792. all_favorites &= is_favorite;
  1793. all_not_favorites &= !is_favorite;
  1794. selected = tree->get_next_selected(selected);
  1795. }
  1796. if (!all_not_favorites) {
  1797. paths = _tree_get_selected(false);
  1798. } else {
  1799. paths = _tree_get_selected();
  1800. }
  1801. } else if (p_from == files) {
  1802. for (int i = 0; i < files->get_item_count(); i++) {
  1803. if (files->is_selected(i)) {
  1804. paths.push_back(files->get_item_metadata(i));
  1805. }
  1806. }
  1807. all_favorites = false;
  1808. all_not_favorites = true;
  1809. }
  1810. if (paths.is_empty()) {
  1811. return Variant();
  1812. }
  1813. Dictionary drag_data = EditorNode::get_singleton()->drag_files_and_dirs(paths, p_from);
  1814. if (!all_not_favorites) {
  1815. drag_data["favorite"] = all_favorites ? "all" : "mixed";
  1816. }
  1817. return drag_data;
  1818. }
  1819. bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1820. Dictionary drag_data = p_data;
  1821. if (drag_data.has("favorite")) {
  1822. if (String(drag_data["favorite"]) != "all") {
  1823. return false;
  1824. }
  1825. // Moving favorite around.
  1826. TreeItem *ti = tree->get_item_at_position(p_point);
  1827. if (!ti) {
  1828. return false;
  1829. }
  1830. int drop_section = tree->get_drop_section_at_position(p_point);
  1831. TreeItem *favorites_item = tree->get_root()->get_first_child();
  1832. TreeItem *resources_item = favorites_item->get_next();
  1833. if (ti == favorites_item) {
  1834. return (drop_section == 1); // The parent, first fav.
  1835. }
  1836. if (ti->get_parent() && favorites_item == ti->get_parent()) {
  1837. return true; // A favorite
  1838. }
  1839. if (ti == resources_item) {
  1840. return (drop_section == -1); // The tree, last fav.
  1841. }
  1842. return false;
  1843. }
  1844. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1845. // Move resources.
  1846. String to_dir;
  1847. bool favorite;
  1848. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1849. return !to_dir.is_empty();
  1850. }
  1851. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1852. // Move files or dir.
  1853. String to_dir;
  1854. bool favorite;
  1855. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1856. if (favorite) {
  1857. return true;
  1858. }
  1859. if (to_dir.is_empty()) {
  1860. return false;
  1861. }
  1862. // Attempting to move a folder into itself will fail later,
  1863. // rather than bring up a message don't try to do it in the first place.
  1864. to_dir = to_dir.ends_with("/") ? to_dir : (to_dir + "/");
  1865. Vector<String> fnames = drag_data["files"];
  1866. for (int i = 0; i < fnames.size(); ++i) {
  1867. if (fnames[i].ends_with("/") && to_dir.begins_with(fnames[i])) {
  1868. return false;
  1869. }
  1870. }
  1871. return true;
  1872. }
  1873. return false;
  1874. }
  1875. void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1876. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1877. return;
  1878. }
  1879. Dictionary drag_data = p_data;
  1880. Vector<String> dirs = EditorSettings::get_singleton()->get_favorites();
  1881. if (drag_data.has("favorite")) {
  1882. if (String(drag_data["favorite"]) != "all") {
  1883. return;
  1884. }
  1885. // Moving favorite around.
  1886. TreeItem *ti = tree->get_item_at_position(p_point);
  1887. if (!ti) {
  1888. return;
  1889. }
  1890. int drop_section = tree->get_drop_section_at_position(p_point);
  1891. int drop_position;
  1892. Vector<String> files = drag_data["files"];
  1893. TreeItem *favorites_item = tree->get_root()->get_first_child();
  1894. TreeItem *resources_item = favorites_item->get_next();
  1895. if (ti == favorites_item) {
  1896. // Drop on the favorite folder.
  1897. drop_position = 0;
  1898. } else if (ti == resources_item) {
  1899. // Drop on the resource item.
  1900. drop_position = dirs.size();
  1901. } else {
  1902. // Drop in the list.
  1903. drop_position = dirs.find(ti->get_metadata(0));
  1904. if (drop_section == 1) {
  1905. drop_position++;
  1906. }
  1907. }
  1908. // Remove dragged favorites.
  1909. Vector<int> to_remove;
  1910. int offset = 0;
  1911. for (int i = 0; i < files.size(); i++) {
  1912. int to_remove_pos = dirs.find(files[i]);
  1913. to_remove.push_back(to_remove_pos);
  1914. if (to_remove_pos < drop_position) {
  1915. offset++;
  1916. }
  1917. }
  1918. drop_position -= offset;
  1919. to_remove.sort();
  1920. for (int i = 0; i < to_remove.size(); i++) {
  1921. dirs.remove(to_remove[i] - i);
  1922. }
  1923. // Re-add them at the right position.
  1924. for (int i = 0; i < files.size(); i++) {
  1925. dirs.insert(drop_position, files[i]);
  1926. drop_position++;
  1927. }
  1928. EditorSettings::get_singleton()->set_favorites(dirs);
  1929. _update_tree(_compute_uncollapsed_paths());
  1930. if (display_mode == DISPLAY_MODE_SPLIT && path == "Favorites") {
  1931. _update_file_list(true);
  1932. }
  1933. return;
  1934. }
  1935. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1936. // Moving resource.
  1937. Ref<Resource> res = drag_data["resource"];
  1938. String to_dir;
  1939. bool favorite;
  1940. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1941. if (res.is_valid() && !to_dir.is_empty()) {
  1942. EditorNode::get_singleton()->push_item(res.ptr());
  1943. EditorNode::get_singleton()->save_resource_as(res, to_dir);
  1944. }
  1945. }
  1946. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1947. // Move files or add to favorites.
  1948. String to_dir;
  1949. bool favorite;
  1950. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1951. if (!to_dir.is_empty()) {
  1952. Vector<String> fnames = drag_data["files"];
  1953. to_move.clear();
  1954. for (int i = 0; i < fnames.size(); i++) {
  1955. if (fnames[i].get_base_dir() != to_dir) {
  1956. to_move.push_back(FileOrFolder(fnames[i], !fnames[i].ends_with("/")));
  1957. }
  1958. }
  1959. if (!to_move.is_empty()) {
  1960. if (Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  1961. for (int i = 0; i < to_move.size(); i++) {
  1962. String new_path;
  1963. String new_path_base;
  1964. if (to_move[i].is_file) {
  1965. new_path = to_dir.plus_file(to_move[i].path.get_file());
  1966. new_path_base = new_path.get_basename() + " (%d)." + new_path.get_extension();
  1967. } else {
  1968. PackedStringArray path_split = to_move[i].path.split("/");
  1969. new_path = to_dir.plus_file(path_split[path_split.size() - 2]);
  1970. new_path_base = new_path + " (%d)";
  1971. }
  1972. int exist_counter = 1;
  1973. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1974. while (da->file_exists(new_path) || da->dir_exists(new_path)) {
  1975. exist_counter++;
  1976. new_path = vformat(new_path_base, exist_counter);
  1977. }
  1978. _try_duplicate_item(to_move[i], new_path);
  1979. }
  1980. _rescan();
  1981. } else {
  1982. _move_operation_confirm(to_dir);
  1983. }
  1984. }
  1985. } else if (favorite) {
  1986. // Add the files from favorites.
  1987. Vector<String> fnames = drag_data["files"];
  1988. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1989. for (int i = 0; i < fnames.size(); i++) {
  1990. if (favorites.find(fnames[i]) == -1) {
  1991. favorites.push_back(fnames[i]);
  1992. }
  1993. }
  1994. EditorSettings::get_singleton()->set_favorites(favorites);
  1995. _update_tree(_compute_uncollapsed_paths());
  1996. }
  1997. }
  1998. }
  1999. void FileSystemDock::_get_drag_target_folder(String &target, bool &target_favorites, const Point2 &p_point, Control *p_from) const {
  2000. target = String();
  2001. target_favorites = false;
  2002. // In the file list.
  2003. if (p_from == files) {
  2004. int pos = files->get_item_at_position(p_point, true);
  2005. if (pos == -1) {
  2006. return;
  2007. }
  2008. String ltarget = files->get_item_metadata(pos);
  2009. target = ltarget.ends_with("/") ? ltarget : path.get_base_dir();
  2010. return;
  2011. }
  2012. // In the tree.
  2013. if (p_from == tree) {
  2014. TreeItem *ti = tree->get_item_at_position(p_point);
  2015. int section = tree->get_drop_section_at_position(p_point);
  2016. if (ti) {
  2017. // Check the favorites first.
  2018. if (ti == tree->get_root()->get_first_child() && section >= 0) {
  2019. target_favorites = true;
  2020. return;
  2021. } else if (ti->get_parent() == tree->get_root()->get_first_child()) {
  2022. target_favorites = true;
  2023. return;
  2024. } else {
  2025. String fpath = ti->get_metadata(0);
  2026. if (section == 0) {
  2027. if (fpath.ends_with("/")) {
  2028. // We drop on a folder.
  2029. target = fpath;
  2030. return;
  2031. } else {
  2032. // We drop on the folder that the target file is in.
  2033. target = fpath.get_base_dir();
  2034. return;
  2035. }
  2036. } else {
  2037. if (ti->get_parent() != tree->get_root()->get_first_child()) {
  2038. // Not in the favorite section.
  2039. if (fpath != "res://") {
  2040. // We drop between two files
  2041. if (fpath.ends_with("/")) {
  2042. fpath = fpath.substr(0, fpath.length() - 1);
  2043. }
  2044. target = fpath.get_base_dir();
  2045. return;
  2046. }
  2047. }
  2048. }
  2049. }
  2050. }
  2051. }
  2052. }
  2053. void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<String> p_paths, bool p_display_path_dependent_options) {
  2054. // Add options for files and folders.
  2055. ERR_FAIL_COND_MSG(p_paths.is_empty(), "Path cannot be empty.");
  2056. Vector<String> filenames;
  2057. Vector<String> foldernames;
  2058. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  2059. bool all_files = true;
  2060. bool all_files_scenes = true;
  2061. bool all_folders = true;
  2062. bool all_favorites = true;
  2063. bool all_not_favorites = true;
  2064. for (int i = 0; i < p_paths.size(); i++) {
  2065. String fpath = p_paths[i];
  2066. if (fpath.ends_with("/")) {
  2067. foldernames.push_back(fpath);
  2068. all_files = false;
  2069. } else {
  2070. filenames.push_back(fpath);
  2071. all_folders = false;
  2072. all_files_scenes &= (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene");
  2073. }
  2074. // Check if in favorites.
  2075. bool found = false;
  2076. for (int j = 0; j < favorites.size(); j++) {
  2077. if (favorites[j] == fpath) {
  2078. found = true;
  2079. break;
  2080. }
  2081. }
  2082. if (found) {
  2083. all_not_favorites = false;
  2084. } else {
  2085. all_favorites = false;
  2086. }
  2087. }
  2088. if (all_files) {
  2089. if (all_files_scenes) {
  2090. if (filenames.size() == 1) {
  2091. p_popup->add_icon_item(get_theme_icon("Load", "EditorIcons"), TTR("Open Scene"), FILE_OPEN);
  2092. p_popup->add_icon_item(get_theme_icon("CreateNewSceneFrom", "EditorIcons"), TTR("New Inherited Scene"), FILE_INHERIT);
  2093. if (ProjectSettings::get_singleton()->get("application/run/main_scene") != filenames[0]) {
  2094. p_popup->add_icon_item(get_theme_icon("PlayScene", "EditorIcons"), TTR("Set As Main Scene"), FILE_MAIN_SCENE);
  2095. }
  2096. } else {
  2097. p_popup->add_icon_item(get_theme_icon("Load", "EditorIcons"), TTR("Open Scenes"), FILE_OPEN);
  2098. }
  2099. p_popup->add_icon_item(get_theme_icon("Instance", "EditorIcons"), TTR("Instance"), FILE_INSTANCE);
  2100. p_popup->add_separator();
  2101. } else if (filenames.size() == 1) {
  2102. p_popup->add_icon_item(get_theme_icon("Load", "EditorIcons"), TTR("Open"), FILE_OPEN);
  2103. p_popup->add_separator();
  2104. }
  2105. }
  2106. if (p_paths.size() >= 1) {
  2107. if (!all_favorites) {
  2108. p_popup->add_icon_item(get_theme_icon("Favorites", "EditorIcons"), TTR("Add to Favorites"), FILE_ADD_FAVORITE);
  2109. }
  2110. if (!all_not_favorites) {
  2111. p_popup->add_icon_item(get_theme_icon("NonFavorite", "EditorIcons"), TTR("Remove from Favorites"), FILE_REMOVE_FAVORITE);
  2112. }
  2113. p_popup->add_separator();
  2114. }
  2115. if (all_files) {
  2116. if (filenames.size() == 1) {
  2117. p_popup->add_item(TTR("Edit Dependencies..."), FILE_DEPENDENCIES);
  2118. p_popup->add_item(TTR("View Owners..."), FILE_OWNERS);
  2119. p_popup->add_separator();
  2120. }
  2121. } else if (all_folders && foldernames.size() > 0) {
  2122. p_popup->add_icon_item(get_theme_icon("Load", "EditorIcons"), TTR("Open"), FILE_OPEN);
  2123. p_popup->add_separator();
  2124. }
  2125. if (p_paths.size() == 1) {
  2126. p_popup->add_icon_shortcut(get_theme_icon("ActionCopy", "EditorIcons"), ED_GET_SHORTCUT("filesystem_dock/copy_path"), FILE_COPY_PATH);
  2127. if (p_paths[0] != "res://") {
  2128. p_popup->add_icon_shortcut(get_theme_icon("Rename", "EditorIcons"), ED_GET_SHORTCUT("filesystem_dock/rename"), FILE_RENAME);
  2129. p_popup->add_icon_shortcut(get_theme_icon("Duplicate", "EditorIcons"), ED_GET_SHORTCUT("filesystem_dock/duplicate"), FILE_DUPLICATE);
  2130. }
  2131. }
  2132. if (p_paths.size() > 1 || p_paths[0] != "res://") {
  2133. p_popup->add_icon_item(get_theme_icon("MoveUp", "EditorIcons"), TTR("Move To..."), FILE_MOVE);
  2134. p_popup->add_icon_shortcut(get_theme_icon("Remove", "EditorIcons"), ED_GET_SHORTCUT("filesystem_dock/delete"), FILE_REMOVE);
  2135. }
  2136. if (p_paths.size() == 1) {
  2137. p_popup->add_separator();
  2138. if (p_display_path_dependent_options) {
  2139. p_popup->add_icon_item(get_theme_icon("Folder", "EditorIcons"), TTR("New Folder..."), FILE_NEW_FOLDER);
  2140. p_popup->add_icon_item(get_theme_icon("PackedScene", "EditorIcons"), TTR("New Scene..."), FILE_NEW_SCENE);
  2141. p_popup->add_icon_item(get_theme_icon("Script", "EditorIcons"), TTR("New Script..."), FILE_NEW_SCRIPT);
  2142. p_popup->add_icon_item(get_theme_icon("Object", "EditorIcons"), TTR("New Resource..."), FILE_NEW_RESOURCE);
  2143. p_popup->add_separator();
  2144. }
  2145. String fpath = p_paths[0];
  2146. String item_text = fpath.ends_with("/") ? TTR("Open in File Manager") : TTR("Show in File Manager");
  2147. p_popup->add_icon_item(get_theme_icon("Filesystem", "EditorIcons"), item_text, FILE_SHOW_IN_EXPLORER);
  2148. }
  2149. }
  2150. void FileSystemDock::_tree_rmb_select(const Vector2 &p_pos) {
  2151. // Right click is pressed in the tree.
  2152. Vector<String> paths = _tree_get_selected(false);
  2153. tree_popup->clear();
  2154. if (paths.size() == 1) {
  2155. if (paths[0].ends_with("/")) {
  2156. tree_popup->add_icon_item(get_theme_icon("GuiTreeArrowDown", "EditorIcons"), TTR("Expand All"), FOLDER_EXPAND_ALL);
  2157. tree_popup->add_icon_item(get_theme_icon("GuiTreeArrowRight", "EditorIcons"), TTR("Collapse All"), FOLDER_COLLAPSE_ALL);
  2158. tree_popup->add_separator();
  2159. }
  2160. }
  2161. // Popup.
  2162. if (!paths.is_empty()) {
  2163. tree_popup->set_size(Size2(1, 1));
  2164. _file_and_folders_fill_popup(tree_popup, paths);
  2165. tree_popup->set_position(tree->get_screen_position() + p_pos);
  2166. tree_popup->popup();
  2167. }
  2168. }
  2169. void FileSystemDock::_tree_rmb_empty(const Vector2 &p_pos) {
  2170. // Right click is pressed in the empty space of the tree.
  2171. path = "res://";
  2172. tree_popup->clear();
  2173. tree_popup->set_size(Size2(1, 1));
  2174. tree_popup->add_icon_item(get_theme_icon("Folder", "EditorIcons"), TTR("New Folder..."), FILE_NEW_FOLDER);
  2175. tree_popup->add_icon_item(get_theme_icon("PackedScene", "EditorIcons"), TTR("New Scene..."), FILE_NEW_SCENE);
  2176. tree_popup->add_icon_item(get_theme_icon("Script", "EditorIcons"), TTR("New Script..."), FILE_NEW_SCRIPT);
  2177. tree_popup->add_icon_item(get_theme_icon("Object", "EditorIcons"), TTR("New Resource..."), FILE_NEW_RESOURCE);
  2178. tree_popup->set_position(tree->get_global_position() + p_pos);
  2179. tree_popup->popup();
  2180. }
  2181. void FileSystemDock::_tree_empty_selected() {
  2182. tree->deselect_all();
  2183. }
  2184. void FileSystemDock::_file_list_rmb_select(int p_item, const Vector2 &p_pos) {
  2185. // Right click is pressed in the file list.
  2186. Vector<String> paths;
  2187. for (int i = 0; i < files->get_item_count(); i++) {
  2188. if (!files->is_selected(i)) {
  2189. continue;
  2190. }
  2191. if (files->get_item_text(p_item) == "..") {
  2192. files->deselect(i);
  2193. continue;
  2194. }
  2195. paths.push_back(files->get_item_metadata(i));
  2196. }
  2197. // Popup.
  2198. if (!paths.is_empty()) {
  2199. file_list_popup->clear();
  2200. file_list_popup->set_size(Size2(1, 1));
  2201. _file_and_folders_fill_popup(file_list_popup, paths, searched_string.length() == 0);
  2202. file_list_popup->set_position(files->get_global_position() + p_pos);
  2203. file_list_popup->popup();
  2204. }
  2205. }
  2206. void FileSystemDock::_file_list_rmb_pressed(const Vector2 &p_pos) {
  2207. // Right click on empty space for file list.
  2208. if (searched_string.length() > 0) {
  2209. return;
  2210. }
  2211. file_list_popup->clear();
  2212. file_list_popup->set_size(Size2(1, 1));
  2213. file_list_popup->add_icon_item(get_theme_icon("Folder", "EditorIcons"), TTR("New Folder..."), FILE_NEW_FOLDER);
  2214. file_list_popup->add_icon_item(get_theme_icon("PackedScene", "EditorIcons"), TTR("New Scene..."), FILE_NEW_SCENE);
  2215. file_list_popup->add_icon_item(get_theme_icon("Script", "EditorIcons"), TTR("New Script..."), FILE_NEW_SCRIPT);
  2216. file_list_popup->add_icon_item(get_theme_icon("Object", "EditorIcons"), TTR("New Resource..."), FILE_NEW_RESOURCE);
  2217. file_list_popup->add_separator();
  2218. file_list_popup->add_icon_item(get_theme_icon("Filesystem", "EditorIcons"), TTR("Open in File Manager"), FILE_SHOW_IN_EXPLORER);
  2219. file_list_popup->set_position(files->get_global_position() + p_pos);
  2220. file_list_popup->popup();
  2221. }
  2222. void FileSystemDock::select_file(const String &p_file) {
  2223. _navigate_to_path(p_file);
  2224. }
  2225. void FileSystemDock::_file_multi_selected(int p_index, bool p_selected) {
  2226. // Set the path to the current focused item.
  2227. int current = files->get_current();
  2228. if (current == p_index) {
  2229. String fpath = files->get_item_metadata(current);
  2230. if (!fpath.ends_with("/")) {
  2231. path = fpath;
  2232. if (display_mode == DISPLAY_MODE_SPLIT) {
  2233. _update_tree(_compute_uncollapsed_paths());
  2234. }
  2235. }
  2236. }
  2237. // Update the import dock.
  2238. import_dock_needs_update = true;
  2239. call_deferred("_update_import_dock");
  2240. }
  2241. void FileSystemDock::_tree_gui_input(Ref<InputEvent> p_event) {
  2242. Ref<InputEventKey> key = p_event;
  2243. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  2244. if (ED_IS_SHORTCUT("filesystem_dock/duplicate", p_event)) {
  2245. _tree_rmb_option(FILE_DUPLICATE);
  2246. } else if (ED_IS_SHORTCUT("filesystem_dock/copy_path", p_event)) {
  2247. _tree_rmb_option(FILE_COPY_PATH);
  2248. } else if (ED_IS_SHORTCUT("filesystem_dock/delete", p_event)) {
  2249. _tree_rmb_option(FILE_REMOVE);
  2250. } else if (ED_IS_SHORTCUT("filesystem_dock/rename", p_event)) {
  2251. _tree_rmb_option(FILE_RENAME);
  2252. } else if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
  2253. _focus_current_search_box();
  2254. } else {
  2255. return;
  2256. }
  2257. accept_event();
  2258. }
  2259. }
  2260. void FileSystemDock::_file_list_gui_input(Ref<InputEvent> p_event) {
  2261. Ref<InputEventKey> key = p_event;
  2262. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  2263. if (ED_IS_SHORTCUT("filesystem_dock/duplicate", p_event)) {
  2264. _file_list_rmb_option(FILE_DUPLICATE);
  2265. } else if (ED_IS_SHORTCUT("filesystem_dock/copy_path", p_event)) {
  2266. _file_list_rmb_option(FILE_COPY_PATH);
  2267. } else if (ED_IS_SHORTCUT("filesystem_dock/delete", p_event)) {
  2268. _file_list_rmb_option(FILE_REMOVE);
  2269. } else if (ED_IS_SHORTCUT("filesystem_dock/rename", p_event)) {
  2270. _file_list_rmb_option(FILE_RENAME);
  2271. } else if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
  2272. _focus_current_search_box();
  2273. } else {
  2274. return;
  2275. }
  2276. accept_event();
  2277. }
  2278. }
  2279. void FileSystemDock::_get_imported_files(const String &p_path, Vector<String> &files) const {
  2280. if (!p_path.ends_with("/")) {
  2281. if (FileAccess::exists(p_path + ".import")) {
  2282. files.push_back(p_path);
  2283. }
  2284. return;
  2285. }
  2286. DirAccess *da = DirAccess::open(p_path);
  2287. da->list_dir_begin();
  2288. String n = da->get_next();
  2289. while (n != String()) {
  2290. if (n != "." && n != ".." && !n.ends_with(".import")) {
  2291. String npath = p_path + n + (da->current_is_dir() ? "/" : "");
  2292. _get_imported_files(npath, files);
  2293. }
  2294. n = da->get_next();
  2295. }
  2296. da->list_dir_end();
  2297. }
  2298. void FileSystemDock::_update_import_dock() {
  2299. if (!import_dock_needs_update) {
  2300. return;
  2301. }
  2302. // List selected.
  2303. Vector<String> selected;
  2304. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  2305. // Use the tree
  2306. selected = _tree_get_selected();
  2307. } else {
  2308. // Use the file list.
  2309. for (int i = 0; i < files->get_item_count(); i++) {
  2310. if (!files->is_selected(i)) {
  2311. continue;
  2312. }
  2313. selected.push_back(files->get_item_metadata(i));
  2314. }
  2315. }
  2316. // Expand directory selection
  2317. Vector<String> efiles;
  2318. for (int i = 0; i < selected.size(); i++) {
  2319. _get_imported_files(selected[i], efiles);
  2320. }
  2321. // Check import.
  2322. Vector<String> imports;
  2323. String import_type;
  2324. for (int i = 0; i < efiles.size(); i++) {
  2325. String fpath = efiles[i];
  2326. Ref<ConfigFile> cf;
  2327. cf.instance();
  2328. Error err = cf->load(fpath + ".import");
  2329. if (err != OK) {
  2330. imports.clear();
  2331. break;
  2332. }
  2333. String type;
  2334. if (cf->has_section_key("remap", "type")) {
  2335. type = cf->get_value("remap", "type");
  2336. }
  2337. if (import_type == "") {
  2338. import_type = type;
  2339. } else if (import_type != type) {
  2340. // All should be the same type.
  2341. imports.clear();
  2342. break;
  2343. }
  2344. imports.push_back(fpath);
  2345. }
  2346. if (imports.size() == 0) {
  2347. EditorNode::get_singleton()->get_import_dock()->clear();
  2348. } else if (imports.size() == 1) {
  2349. EditorNode::get_singleton()->get_import_dock()->set_edit_path(imports[0]);
  2350. } else {
  2351. EditorNode::get_singleton()->get_import_dock()->set_edit_multiple_paths(imports);
  2352. }
  2353. import_dock_needs_update = false;
  2354. }
  2355. void FileSystemDock::_feature_profile_changed() {
  2356. _update_display_mode(true);
  2357. }
  2358. void FileSystemDock::set_file_sort(FileSortOption p_file_sort) {
  2359. for (int i = 0; i != FILE_SORT_MAX; i++) {
  2360. tree_button_sort->get_popup()->set_item_checked(i, (i == (int)p_file_sort));
  2361. file_list_button_sort->get_popup()->set_item_checked(i, (i == (int)p_file_sort));
  2362. }
  2363. file_sort = p_file_sort;
  2364. // Update everything needed.
  2365. _update_tree(_compute_uncollapsed_paths());
  2366. _update_file_list(true);
  2367. }
  2368. void FileSystemDock::_file_sort_popup(int p_id) {
  2369. set_file_sort((FileSortOption)p_id);
  2370. }
  2371. MenuButton *FileSystemDock::_create_file_menu_button() {
  2372. MenuButton *button = memnew(MenuButton);
  2373. button->set_flat(true);
  2374. button->set_tooltip(TTR("Sort files"));
  2375. PopupMenu *p = button->get_popup();
  2376. p->connect("id_pressed", callable_mp(this, &FileSystemDock::_file_sort_popup));
  2377. p->add_radio_check_item(TTR("Sort by Name (Ascending)"), FILE_SORT_NAME);
  2378. p->add_radio_check_item(TTR("Sort by Name (Descending)"), FILE_SORT_NAME_REVERSE);
  2379. p->add_radio_check_item(TTR("Sort by Type (Ascending)"), FILE_SORT_TYPE);
  2380. p->add_radio_check_item(TTR("Sort by Type (Descending)"), FILE_SORT_TYPE_REVERSE);
  2381. p->add_radio_check_item(TTR("Sort by Last Modified"), FILE_SORT_MODIFIED_TIME);
  2382. p->add_radio_check_item(TTR("Sort by First Modified"), FILE_SORT_MODIFIED_TIME_REVERSE);
  2383. p->set_item_checked(file_sort, true);
  2384. return button;
  2385. }
  2386. void FileSystemDock::_bind_methods() {
  2387. ClassDB::bind_method(D_METHOD("_update_tree"), &FileSystemDock::_update_tree);
  2388. ClassDB::bind_method(D_METHOD("_file_list_thumbnail_done"), &FileSystemDock::_file_list_thumbnail_done);
  2389. ClassDB::bind_method(D_METHOD("_tree_thumbnail_done"), &FileSystemDock::_tree_thumbnail_done);
  2390. ClassDB::bind_method(D_METHOD("_select_file"), &FileSystemDock::_select_file);
  2391. ClassDB::bind_method(D_METHOD("get_drag_data_fw", "position", "from"), &FileSystemDock::get_drag_data_fw);
  2392. ClassDB::bind_method(D_METHOD("can_drop_data_fw", "position", "data", "from"), &FileSystemDock::can_drop_data_fw);
  2393. ClassDB::bind_method(D_METHOD("drop_data_fw", "position", "data", "from"), &FileSystemDock::drop_data_fw);
  2394. ClassDB::bind_method(D_METHOD("navigate_to_path", "path"), &FileSystemDock::navigate_to_path);
  2395. ClassDB::bind_method(D_METHOD("_update_import_dock"), &FileSystemDock::_update_import_dock);
  2396. ADD_SIGNAL(MethodInfo("inherit", PropertyInfo(Variant::STRING, "file")));
  2397. ADD_SIGNAL(MethodInfo("instance", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
  2398. ADD_SIGNAL(MethodInfo("file_removed", PropertyInfo(Variant::STRING, "file")));
  2399. ADD_SIGNAL(MethodInfo("folder_removed", PropertyInfo(Variant::STRING, "folder")));
  2400. ADD_SIGNAL(MethodInfo("files_moved", PropertyInfo(Variant::STRING, "old_file"), PropertyInfo(Variant::STRING, "new_file")));
  2401. ADD_SIGNAL(MethodInfo("folder_moved", PropertyInfo(Variant::STRING, "old_folder"), PropertyInfo(Variant::STRING, "new_file")));
  2402. ADD_SIGNAL(MethodInfo("display_mode_changed"));
  2403. }
  2404. FileSystemDock::FileSystemDock(EditorNode *p_editor) {
  2405. set_name("FileSystem");
  2406. editor = p_editor;
  2407. path = "res://";
  2408. // `KEY_MASK_CMD | KEY_C` conflicts with other editor shortcuts.
  2409. ED_SHORTCUT("filesystem_dock/copy_path", TTR("Copy Path"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_C);
  2410. ED_SHORTCUT("filesystem_dock/duplicate", TTR("Duplicate..."), KEY_MASK_CMD | KEY_D);
  2411. #if defined(WINDOWS_ENABLED)
  2412. // TRANSLATORS: This string is only used on Windows, as it refers to the system trash
  2413. // as "Recycle Bin" instead of "Trash". Make sure to use the translation of "Recycle Bin"
  2414. // recommended by Microsoft for the target language.
  2415. ED_SHORTCUT("filesystem_dock/delete", TTR("Move to Recycle Bin"), KEY_DELETE);
  2416. #elif defined(OSX_ENABLED)
  2417. // TRANSLATORS: This string is only used on macOS, as it refers to the system trash
  2418. // as "Bin" instead of "Trash". Make sure to use the translation of "Bin"
  2419. // recommended by Apple for the target language.
  2420. ED_SHORTCUT("filesystem_dock/delete", TTR("Move to Bin"), KEY_DELETE);
  2421. #else
  2422. // TRANSLATORS: This string is only used on platforms other than Windows and macOS.
  2423. ED_SHORTCUT("filesystem_dock/delete", TTR("Move to Trash"), KEY_DELETE);
  2424. #endif
  2425. ED_SHORTCUT("filesystem_dock/rename", TTR("Rename..."), KEY_F2);
  2426. VBoxContainer *top_vbc = memnew(VBoxContainer);
  2427. add_child(top_vbc);
  2428. HBoxContainer *toolbar_hbc = memnew(HBoxContainer);
  2429. toolbar_hbc->add_theme_constant_override("separation", 0);
  2430. top_vbc->add_child(toolbar_hbc);
  2431. button_hist_prev = memnew(Button);
  2432. button_hist_prev->set_flat(true);
  2433. button_hist_prev->set_disabled(true);
  2434. button_hist_prev->set_focus_mode(FOCUS_NONE);
  2435. button_hist_prev->set_tooltip(TTR("Previous Folder/File"));
  2436. toolbar_hbc->add_child(button_hist_prev);
  2437. button_hist_next = memnew(Button);
  2438. button_hist_next->set_flat(true);
  2439. button_hist_next->set_disabled(true);
  2440. button_hist_next->set_focus_mode(FOCUS_NONE);
  2441. button_hist_next->set_tooltip(TTR("Next Folder/File"));
  2442. toolbar_hbc->add_child(button_hist_next);
  2443. current_path = memnew(LineEdit);
  2444. current_path->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  2445. current_path->set_h_size_flags(SIZE_EXPAND_FILL);
  2446. _set_current_path_text(path);
  2447. toolbar_hbc->add_child(current_path);
  2448. button_reload = memnew(Button);
  2449. button_reload->connect("pressed", callable_mp(this, &FileSystemDock::_rescan));
  2450. button_reload->set_focus_mode(FOCUS_NONE);
  2451. button_reload->set_tooltip(TTR("Re-Scan Filesystem"));
  2452. button_reload->hide();
  2453. toolbar_hbc->add_child(button_reload);
  2454. button_toggle_display_mode = memnew(Button);
  2455. button_toggle_display_mode->set_toggle_mode(true);
  2456. button_toggle_display_mode->connect("toggled", callable_mp(this, &FileSystemDock::_toggle_split_mode));
  2457. button_toggle_display_mode->set_focus_mode(FOCUS_NONE);
  2458. button_toggle_display_mode->set_tooltip(TTR("Toggle Split Mode"));
  2459. button_toggle_display_mode->set_flat(true);
  2460. toolbar_hbc->add_child(button_toggle_display_mode);
  2461. toolbar2_hbc = memnew(HBoxContainer);
  2462. toolbar2_hbc->add_theme_constant_override("separation", 0);
  2463. top_vbc->add_child(toolbar2_hbc);
  2464. tree_search_box = memnew(LineEdit);
  2465. tree_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2466. tree_search_box->set_placeholder(TTR("Search files"));
  2467. tree_search_box->connect("text_changed", callable_mp(this, &FileSystemDock::_search_changed), varray(tree_search_box));
  2468. toolbar2_hbc->add_child(tree_search_box);
  2469. tree_button_sort = _create_file_menu_button();
  2470. toolbar2_hbc->add_child(tree_button_sort);
  2471. file_list_popup = memnew(PopupMenu);
  2472. add_child(file_list_popup);
  2473. tree_popup = memnew(PopupMenu);
  2474. add_child(tree_popup);
  2475. split_box = memnew(VSplitContainer);
  2476. split_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2477. add_child(split_box);
  2478. tree = memnew(Tree);
  2479. tree->set_hide_root(true);
  2480. tree->set_drag_forwarding(this);
  2481. tree->set_allow_rmb_select(true);
  2482. tree->set_select_mode(Tree::SELECT_MULTI);
  2483. tree->set_custom_minimum_size(Size2(0, 15 * EDSCALE));
  2484. split_box->add_child(tree);
  2485. tree->connect("item_activated", callable_mp(this, &FileSystemDock::_tree_activate_file));
  2486. tree->connect("multi_selected", callable_mp(this, &FileSystemDock::_tree_multi_selected));
  2487. tree->connect("item_rmb_selected", callable_mp(this, &FileSystemDock::_tree_rmb_select));
  2488. tree->connect("empty_rmb", callable_mp(this, &FileSystemDock::_tree_rmb_empty));
  2489. tree->connect("nothing_selected", callable_mp(this, &FileSystemDock::_tree_empty_selected));
  2490. tree->connect("gui_input", callable_mp(this, &FileSystemDock::_tree_gui_input));
  2491. file_list_vb = memnew(VBoxContainer);
  2492. file_list_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2493. split_box->add_child(file_list_vb);
  2494. path_hb = memnew(HBoxContainer);
  2495. file_list_vb->add_child(path_hb);
  2496. file_list_search_box = memnew(LineEdit);
  2497. file_list_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2498. file_list_search_box->set_placeholder(TTR("Search files"));
  2499. file_list_search_box->connect("text_changed", callable_mp(this, &FileSystemDock::_search_changed), varray(file_list_search_box));
  2500. path_hb->add_child(file_list_search_box);
  2501. file_list_button_sort = _create_file_menu_button();
  2502. path_hb->add_child(file_list_button_sort);
  2503. button_file_list_display_mode = memnew(Button);
  2504. button_file_list_display_mode->set_flat(true);
  2505. path_hb->add_child(button_file_list_display_mode);
  2506. files = memnew(ItemList);
  2507. files->set_v_size_flags(SIZE_EXPAND_FILL);
  2508. files->set_select_mode(ItemList::SELECT_MULTI);
  2509. files->set_drag_forwarding(this);
  2510. files->connect("item_rmb_selected", callable_mp(this, &FileSystemDock::_file_list_rmb_select));
  2511. files->connect("gui_input", callable_mp(this, &FileSystemDock::_file_list_gui_input));
  2512. files->connect("multi_selected", callable_mp(this, &FileSystemDock::_file_multi_selected));
  2513. files->connect("rmb_clicked", callable_mp(this, &FileSystemDock::_file_list_rmb_pressed));
  2514. files->set_custom_minimum_size(Size2(0, 15 * EDSCALE));
  2515. files->set_allow_rmb_select(true);
  2516. file_list_vb->add_child(files);
  2517. scanning_vb = memnew(VBoxContainer);
  2518. scanning_vb->hide();
  2519. add_child(scanning_vb);
  2520. Label *slabel = memnew(Label);
  2521. slabel->set_text(TTR("Scanning Files,\nPlease Wait..."));
  2522. slabel->set_align(Label::ALIGN_CENTER);
  2523. scanning_vb->add_child(slabel);
  2524. scanning_progress = memnew(ProgressBar);
  2525. scanning_vb->add_child(scanning_progress);
  2526. deps_editor = memnew(DependencyEditor);
  2527. add_child(deps_editor);
  2528. owners_editor = memnew(DependencyEditorOwners(editor));
  2529. add_child(owners_editor);
  2530. remove_dialog = memnew(DependencyRemoveDialog);
  2531. remove_dialog->connect("file_removed", callable_mp(this, &FileSystemDock::_file_removed));
  2532. remove_dialog->connect("folder_removed", callable_mp(this, &FileSystemDock::_folder_removed));
  2533. add_child(remove_dialog);
  2534. move_dialog = memnew(EditorDirDialog);
  2535. move_dialog->get_ok_button()->set_text(TTR("Move"));
  2536. add_child(move_dialog);
  2537. move_dialog->connect("dir_selected", callable_mp(this, &FileSystemDock::_move_operation_confirm), make_binds(false));
  2538. rename_dialog = memnew(ConfirmationDialog);
  2539. VBoxContainer *rename_dialog_vb = memnew(VBoxContainer);
  2540. rename_dialog->add_child(rename_dialog_vb);
  2541. rename_dialog_text = memnew(LineEdit);
  2542. rename_dialog_vb->add_margin_child(TTR("Name:"), rename_dialog_text);
  2543. rename_dialog->get_ok_button()->set_text(TTR("Rename"));
  2544. add_child(rename_dialog);
  2545. rename_dialog->register_text_enter(rename_dialog_text);
  2546. rename_dialog->connect("confirmed", callable_mp(this, &FileSystemDock::_rename_operation_confirm));
  2547. overwrite_dialog = memnew(ConfirmationDialog);
  2548. overwrite_dialog->get_ok_button()->set_text(TTR("Overwrite"));
  2549. add_child(overwrite_dialog);
  2550. overwrite_dialog->connect("confirmed", callable_mp(this, &FileSystemDock::_move_with_overwrite));
  2551. duplicate_dialog = memnew(ConfirmationDialog);
  2552. VBoxContainer *duplicate_dialog_vb = memnew(VBoxContainer);
  2553. duplicate_dialog->add_child(duplicate_dialog_vb);
  2554. duplicate_dialog_text = memnew(LineEdit);
  2555. duplicate_dialog_vb->add_margin_child(TTR("Name:"), duplicate_dialog_text);
  2556. duplicate_dialog->get_ok_button()->set_text(TTR("Duplicate"));
  2557. add_child(duplicate_dialog);
  2558. duplicate_dialog->register_text_enter(duplicate_dialog_text);
  2559. duplicate_dialog->connect("confirmed", callable_mp(this, &FileSystemDock::_duplicate_operation_confirm));
  2560. make_dir_dialog = memnew(ConfirmationDialog);
  2561. make_dir_dialog->set_title(TTR("Create Folder"));
  2562. VBoxContainer *make_folder_dialog_vb = memnew(VBoxContainer);
  2563. make_dir_dialog->add_child(make_folder_dialog_vb);
  2564. make_dir_dialog_text = memnew(LineEdit);
  2565. make_folder_dialog_vb->add_margin_child(TTR("Name:"), make_dir_dialog_text);
  2566. add_child(make_dir_dialog);
  2567. make_dir_dialog->register_text_enter(make_dir_dialog_text);
  2568. make_dir_dialog->connect("confirmed", callable_mp(this, &FileSystemDock::_make_dir_confirm));
  2569. make_scene_dialog = memnew(ConfirmationDialog);
  2570. make_scene_dialog->set_title(TTR("Create Scene"));
  2571. VBoxContainer *make_scene_dialog_vb = memnew(VBoxContainer);
  2572. make_scene_dialog->add_child(make_scene_dialog_vb);
  2573. make_scene_dialog_text = memnew(LineEdit);
  2574. make_scene_dialog_vb->add_margin_child(TTR("Name:"), make_scene_dialog_text);
  2575. add_child(make_scene_dialog);
  2576. make_scene_dialog->register_text_enter(make_scene_dialog_text);
  2577. make_scene_dialog->connect("confirmed", callable_mp(this, &FileSystemDock::_make_scene_confirm));
  2578. make_script_dialog = memnew(ScriptCreateDialog);
  2579. make_script_dialog->set_title(TTR("Create Script"));
  2580. add_child(make_script_dialog);
  2581. new_resource_dialog = memnew(CreateDialog);
  2582. add_child(new_resource_dialog);
  2583. new_resource_dialog->set_base_type("Resource");
  2584. new_resource_dialog->connect("create", callable_mp(this, &FileSystemDock::_resource_created));
  2585. searched_string = String();
  2586. uncollapsed_paths_before_search = Vector<String>();
  2587. updating_tree = false;
  2588. tree_update_id = 0;
  2589. initialized = false;
  2590. import_dock_needs_update = false;
  2591. history_pos = 0;
  2592. history_max_size = 20;
  2593. history.push_back("res://");
  2594. display_mode = DISPLAY_MODE_TREE_ONLY;
  2595. old_display_mode = DISPLAY_MODE_TREE_ONLY;
  2596. file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
  2597. always_show_folders = false;
  2598. }
  2599. FileSystemDock::~FileSystemDock() {
  2600. }