filesystem_dock.cpp 82 KB

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