filesystem_dock.cpp 85 KB

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