filesystem_dock.cpp 83 KB

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