filesystem_dock.cpp 99 KB

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