filesystem_dock.cpp 75 KB

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