filesystem_dock.cpp 82 KB

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