filesystem_dock.cpp 100 KB

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