filesystem_dock.cpp 101 KB

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