filesystem_dock.cpp 96 KB

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