filesystem_dock.cpp 79 KB

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