filesystem_dock.cpp 62 KB

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