filesystem_dock.cpp 94 KB

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