filesystem_dock.cpp 97 KB

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