filesystem_dock.cpp 66 KB

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