filesystem_dock.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. /*************************************************************************/
  2. /* filesystem_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_node.h"
  32. #include "editor_settings.h"
  33. #include "io/resource_loader.h"
  34. #include "os/dir_access.h"
  35. #include "os/file_access.h"
  36. #include "os/os.h"
  37. #include "project_settings.h"
  38. #include "scene/main/viewport.h"
  39. bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory *p_dir, Vector<String> &uncollapsed_paths) {
  40. TreeItem *item = tree->create_item(p_parent);
  41. String dname = p_dir->get_name();
  42. if (dname == "")
  43. dname = "res://";
  44. item->set_text(0, dname);
  45. item->set_icon(0, get_icon("Folder", "EditorIcons"));
  46. item->set_selectable(0, true);
  47. String lpath = p_dir->get_path();
  48. if (lpath != "res://" && lpath.ends_with("/")) {
  49. lpath = lpath.substr(0, lpath.length() - 1);
  50. }
  51. item->set_metadata(0, lpath);
  52. if (lpath == path) {
  53. item->select(0);
  54. }
  55. if ((path.begins_with(lpath) && path != lpath)) {
  56. item->set_collapsed(false);
  57. } else {
  58. bool is_collapsed = true;
  59. for (int i = 0; i < uncollapsed_paths.size(); i++) {
  60. if (lpath == uncollapsed_paths[i]) {
  61. is_collapsed = false;
  62. break;
  63. }
  64. }
  65. item->set_collapsed(is_collapsed);
  66. }
  67. for (int i = 0; i < p_dir->get_subdir_count(); i++)
  68. _create_tree(item, p_dir->get_subdir(i), uncollapsed_paths);
  69. return true;
  70. }
  71. void FileSystemDock::_update_tree(bool keep_collapse_state) {
  72. Vector<String> uncollapsed_paths;
  73. if (keep_collapse_state) {
  74. TreeItem *root = tree->get_root();
  75. if (root) {
  76. TreeItem *resTree = root->get_children()->get_next();
  77. Vector<TreeItem *> needs_check;
  78. needs_check.push_back(resTree);
  79. while (needs_check.size()) {
  80. if (!needs_check[0]->is_collapsed()) {
  81. uncollapsed_paths.push_back(needs_check[0]->get_metadata(0));
  82. TreeItem *child = needs_check[0]->get_children();
  83. while (child) {
  84. needs_check.push_back(child);
  85. child = child->get_next();
  86. }
  87. }
  88. needs_check.remove(0);
  89. }
  90. }
  91. }
  92. tree->clear();
  93. updating_tree = true;
  94. TreeItem *root = tree->create_item();
  95. TreeItem *favorites = tree->create_item(root);
  96. favorites->set_icon(0, get_icon("Favorites", "EditorIcons"));
  97. favorites->set_text(0, TTR("Favorites:"));
  98. favorites->set_selectable(0, false);
  99. Vector<String> favorite_paths = EditorSettings::get_singleton()->get_favorite_dirs();
  100. String res_path = "res://";
  101. Ref<Texture> folder_icon = get_icon("Folder", "EditorIcons");
  102. for (int i = 0; i < favorite_paths.size(); i++) {
  103. String fave = favorite_paths[i];
  104. if (!fave.begins_with(res_path))
  105. continue;
  106. TreeItem *ti = tree->create_item(favorites);
  107. if (fave == res_path)
  108. ti->set_text(0, "/");
  109. else
  110. ti->set_text(0, fave.get_file());
  111. ti->set_icon(0, folder_icon);
  112. ti->set_selectable(0, true);
  113. ti->set_metadata(0, fave);
  114. }
  115. _create_tree(root, EditorFileSystem::get_singleton()->get_filesystem(), uncollapsed_paths);
  116. updating_tree = false;
  117. }
  118. void FileSystemDock::_notification(int p_what) {
  119. switch (p_what) {
  120. case NOTIFICATION_RESIZED: {
  121. bool new_mode = get_size().height < get_viewport_rect().size.height / 2;
  122. if (new_mode != low_height_mode) {
  123. low_height_mode = new_mode;
  124. if (low_height_mode) {
  125. file_list_vb->hide();
  126. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  127. button_tree->show();
  128. } else {
  129. tree->set_v_size_flags(SIZE_FILL);
  130. if (!tree->is_visible()) {
  131. tree->show();
  132. button_favorite->show();
  133. _update_tree(true);
  134. }
  135. if (!file_list_vb->is_visible()) {
  136. file_list_vb->show();
  137. button_tree->hide();
  138. _update_files(true);
  139. }
  140. }
  141. }
  142. } break;
  143. case NOTIFICATION_ENTER_TREE: {
  144. if (initialized)
  145. return;
  146. initialized = true;
  147. EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "_fs_changed");
  148. EditorResourcePreview::get_singleton()->connect("preview_invalidated", this, "_preview_invalidated");
  149. String ei = "EditorIcons";
  150. button_reload->set_icon(get_icon("Reload", ei));
  151. button_favorite->set_icon(get_icon("Favorites", ei));
  152. //button_instance->set_icon(get_icon("Add", ei));
  153. //button_open->set_icon(get_icon("Folder", ei));
  154. button_tree->set_icon(get_icon("Filesystem", ei));
  155. _update_file_display_toggle_button();
  156. button_display_mode->connect("pressed", this, "_change_file_display");
  157. //file_options->set_icon( get_icon("Tools","ei"));
  158. files->connect("item_activated", this, "_select_file");
  159. button_hist_next->connect("pressed", this, "_fw_history");
  160. button_hist_prev->connect("pressed", this, "_bw_history");
  161. search_box->add_icon_override("right_icon", get_icon("Search", ei));
  162. button_hist_next->set_icon(get_icon("Forward", ei));
  163. button_hist_prev->set_icon(get_icon("Back", ei));
  164. file_options->connect("id_pressed", this, "_file_option");
  165. folder_options->connect("id_pressed", this, "_folder_option");
  166. button_tree->connect("pressed", this, "_go_to_tree", varray(), CONNECT_DEFERRED);
  167. current_path->connect("text_entered", this, "navigate_to_path");
  168. if (EditorFileSystem::get_singleton()->is_scanning()) {
  169. _set_scanning_mode();
  170. } else {
  171. _update_tree(false);
  172. }
  173. } break;
  174. case NOTIFICATION_PROCESS: {
  175. if (EditorFileSystem::get_singleton()->is_scanning()) {
  176. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
  177. }
  178. } break;
  179. case NOTIFICATION_EXIT_TREE: {
  180. } break;
  181. case NOTIFICATION_DRAG_BEGIN: {
  182. Dictionary dd = get_viewport()->gui_get_drag_data();
  183. if (tree->is_visible_in_tree() && dd.has("type")) {
  184. if ((String(dd["type"]) == "files") || (String(dd["type"]) == "files_and_dirs") || (String(dd["type"]) == "resource")) {
  185. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  186. } else if ((String(dd["type"]) == "favorite")) {
  187. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  188. }
  189. }
  190. } break;
  191. case NOTIFICATION_DRAG_END: {
  192. tree->set_drop_mode_flags(0);
  193. } break;
  194. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  195. String ei = "EditorIcons";
  196. int new_mode = int(EditorSettings::get_singleton()->get("docks/filesystem/display_mode"));
  197. //_update_icons
  198. button_reload->set_icon(get_icon("Reload", ei));
  199. button_favorite->set_icon(get_icon("Favorites", ei));
  200. button_tree->set_icon(get_icon("Filesystem", ei));
  201. button_hist_next->set_icon(get_icon("Forward", ei));
  202. button_hist_prev->set_icon(get_icon("Back", ei));
  203. search_box->add_icon_override("right_icon", get_icon("Search", ei));
  204. if (new_mode != display_mode) {
  205. set_display_mode(new_mode);
  206. } else {
  207. _update_file_display_toggle_button();
  208. _update_files(true);
  209. }
  210. _update_tree(true);
  211. } break;
  212. }
  213. }
  214. void FileSystemDock::_dir_selected() {
  215. TreeItem *sel = tree->get_selected();
  216. if (!sel)
  217. return;
  218. path = sel->get_metadata(0);
  219. bool found = false;
  220. Vector<String> favorites = EditorSettings::get_singleton()->get_favorite_dirs();
  221. for (int i = 0; i < favorites.size(); i++) {
  222. if (favorites[i] == path) {
  223. found = true;
  224. break;
  225. }
  226. }
  227. button_favorite->set_pressed(found);
  228. current_path->set_text(path);
  229. _push_to_history();
  230. if (!low_height_mode) {
  231. _update_files(false);
  232. }
  233. }
  234. void FileSystemDock::_favorites_pressed() {
  235. TreeItem *sel = tree->get_selected();
  236. if (!sel)
  237. return;
  238. path = sel->get_metadata(0);
  239. int idx = -1;
  240. Vector<String> favorites = EditorSettings::get_singleton()->get_favorite_dirs();
  241. for (int i = 0; i < favorites.size(); i++) {
  242. if (favorites[i] == path) {
  243. idx = i;
  244. break;
  245. }
  246. }
  247. if (idx == -1) {
  248. favorites.push_back(path);
  249. } else {
  250. favorites.remove(idx);
  251. }
  252. EditorSettings::get_singleton()->set_favorite_dirs(favorites);
  253. _update_tree(true);
  254. }
  255. String FileSystemDock::get_selected_path() const {
  256. TreeItem *sel = tree->get_selected();
  257. if (!sel)
  258. return "";
  259. return sel->get_metadata(0);
  260. }
  261. String FileSystemDock::get_current_path() const {
  262. return path;
  263. }
  264. void FileSystemDock::navigate_to_path(const String &p_path) {
  265. // If the path is a file, do not only go to the directory in the tree, also select the file in the file list.
  266. String file_name = "";
  267. DirAccess *dirAccess = DirAccess::open("res://");
  268. if (dirAccess->file_exists(p_path)) {
  269. path = p_path.get_base_dir();
  270. file_name = p_path.get_file();
  271. } else if (dirAccess->dir_exists(p_path)) {
  272. path = p_path;
  273. } else {
  274. ERR_EXPLAIN(TTR("Cannot navigate to '" + p_path + "' as it has not been found in the file system!"));
  275. ERR_FAIL();
  276. }
  277. current_path->set_text(path);
  278. _push_to_history();
  279. if (!low_height_mode) {
  280. _update_tree(true);
  281. _update_files(false);
  282. } else {
  283. if (file_name.empty()) {
  284. _go_to_tree();
  285. } else {
  286. _go_to_file_list();
  287. }
  288. }
  289. if (!file_name.empty()) {
  290. for (int i = 0; i < files->get_item_count(); i++) {
  291. if (files->get_item_text(i) == file_name) {
  292. files->select(i, true);
  293. files->ensure_current_is_visible();
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. void FileSystemDock::_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata) {
  300. if ((file_list_vb->is_visible_in_tree() || path == p_path.get_base_dir()) && p_preview.is_valid()) {
  301. Array uarr = p_udata;
  302. int idx = uarr[0];
  303. String file = uarr[1];
  304. if (idx < files->get_item_count() && files->get_item_text(idx) == file && files->get_item_metadata(idx) == p_path)
  305. files->set_item_icon(idx, p_preview);
  306. }
  307. }
  308. void FileSystemDock::_update_file_display_toggle_button() {
  309. if (button_display_mode->is_pressed()) {
  310. display_mode = DISPLAY_LIST;
  311. button_display_mode->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  312. button_display_mode->set_tooltip(TTR("View items as a grid of thumbnails"));
  313. } else {
  314. display_mode = DISPLAY_THUMBNAILS;
  315. button_display_mode->set_icon(get_icon("FileList", "EditorIcons"));
  316. button_display_mode->set_tooltip(TTR("View items as a list"));
  317. }
  318. }
  319. void FileSystemDock::_change_file_display() {
  320. _update_file_display_toggle_button();
  321. EditorSettings::get_singleton()->set("docks/filesystem/display_mode", display_mode);
  322. _update_files(true);
  323. }
  324. void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *matches, int p_max_items) {
  325. if (matches->size() > p_max_items)
  326. return;
  327. for (int i = 0; i < p_path->get_subdir_count(); i++) {
  328. _search(p_path->get_subdir(i), matches, p_max_items);
  329. }
  330. String match = search_box->get_text();
  331. for (int i = 0; i < p_path->get_file_count(); i++) {
  332. String file = p_path->get_file(i);
  333. if (file.find(match) != -1) {
  334. FileInfo fi;
  335. fi.name = file;
  336. fi.type = p_path->get_file_type(i);
  337. fi.path = p_path->get_file_path(i);
  338. fi.import_broken = !p_path->get_file_import_is_valid(i);
  339. fi.import_status = 0;
  340. matches->push_back(fi);
  341. if (matches->size() > p_max_items)
  342. return;
  343. }
  344. }
  345. }
  346. void FileSystemDock::_update_files(bool p_keep_selection) {
  347. Set<String> cselection;
  348. if (p_keep_selection) {
  349. for (int i = 0; i < files->get_item_count(); i++) {
  350. if (files->is_selected(i))
  351. cselection.insert(files->get_item_text(i));
  352. }
  353. }
  354. files->clear();
  355. current_path->set_text(path);
  356. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(path);
  357. if (!efd)
  358. return;
  359. String ei = "EditorIcons";
  360. int thumbnail_size = EditorSettings::get_singleton()->get("docks/filesystem/thumbnail_size");
  361. thumbnail_size *= EDSCALE;
  362. Ref<Texture> folder_thumbnail;
  363. Ref<Texture> file_thumbnail;
  364. Ref<Texture> file_thumbnail_broken;
  365. bool always_show_folders = EditorSettings::get_singleton()->get("docks/filesystem/always_show_folders");
  366. bool use_thumbnails = (display_mode == DISPLAY_THUMBNAILS);
  367. bool use_folders = search_box->get_text().length() == 0 && (low_height_mode || always_show_folders);
  368. if (use_thumbnails) {
  369. files->set_max_columns(0);
  370. files->set_icon_mode(ItemList::ICON_MODE_TOP);
  371. files->set_fixed_column_width(thumbnail_size * 3 / 2);
  372. files->set_max_text_lines(2);
  373. files->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  374. if (thumbnail_size < 64) {
  375. folder_thumbnail = get_icon("FolderMediumThumb", ei);
  376. file_thumbnail = get_icon("FileMediumThumb", ei);
  377. file_thumbnail_broken = get_icon("FileDeadMediumThumb", ei);
  378. } else {
  379. folder_thumbnail = get_icon("FolderBigThumb", ei);
  380. file_thumbnail = get_icon("FileBigThumb", ei);
  381. file_thumbnail_broken = get_icon("FileDeadBigThumb", ei);
  382. }
  383. } else {
  384. files->set_icon_mode(ItemList::ICON_MODE_LEFT);
  385. files->set_max_columns(1);
  386. files->set_max_text_lines(1);
  387. files->set_fixed_column_width(0);
  388. files->set_fixed_icon_size(Size2());
  389. }
  390. if (use_folders) {
  391. Ref<Texture> folderIcon = (use_thumbnails) ? folder_thumbnail : get_icon("folder", "FileDialog");
  392. if (path != "res://") {
  393. files->add_item("..", folderIcon, false);
  394. String bd = path.get_base_dir();
  395. if (bd != "res://" && !bd.ends_with("/"))
  396. bd += "/";
  397. files->set_item_metadata(files->get_item_count() - 1, bd);
  398. }
  399. for (int i = 0; i < efd->get_subdir_count(); i++) {
  400. String dname = efd->get_subdir(i)->get_name();
  401. files->add_item(dname, folderIcon, true);
  402. files->set_item_metadata(files->get_item_count() - 1, path.plus_file(dname) + "/");
  403. if (cselection.has(dname))
  404. files->select(files->get_item_count() - 1, false);
  405. }
  406. }
  407. List<FileInfo> filelist;
  408. if (search_box->get_text().length() > 0) {
  409. _search(EditorFileSystem::get_singleton()->get_filesystem(), &filelist, 128);
  410. filelist.sort();
  411. } else {
  412. for (int i = 0; i < efd->get_file_count(); i++) {
  413. FileInfo fi;
  414. fi.name = efd->get_file(i);
  415. fi.path = path.plus_file(fi.name);
  416. fi.type = efd->get_file_type(i);
  417. fi.import_broken = !efd->get_file_import_is_valid(i);
  418. fi.import_status = 0;
  419. filelist.push_back(fi);
  420. }
  421. }
  422. String oi = "Object";
  423. for (List<FileInfo>::Element *E = filelist.front(); E; E = E->next()) {
  424. FileInfo *finfo = &(E->get());
  425. String fname = finfo->name;
  426. String fpath = finfo->path;
  427. String ftype = finfo->type;
  428. Ref<Texture> type_icon;
  429. Ref<Texture> big_icon;
  430. String tooltip = fname;
  431. if (!finfo->import_broken) {
  432. type_icon = (has_icon(ftype, ei)) ? get_icon(ftype, ei) : get_icon(oi, ei);
  433. big_icon = file_thumbnail;
  434. } else {
  435. type_icon = get_icon("ImportFail", ei);
  436. big_icon = file_thumbnail_broken;
  437. tooltip += TTR("\nStatus: Import of file failed. Please fix file and reimport manually.");
  438. }
  439. int item_index;
  440. if (use_thumbnails) {
  441. files->add_item(fname, big_icon, true);
  442. item_index = files->get_item_count() - 1;
  443. files->set_item_metadata(item_index, fpath);
  444. files->set_item_tag_icon(item_index, type_icon);
  445. if (!finfo->import_broken) {
  446. Array udata;
  447. udata.resize(2);
  448. udata[0] = item_index;
  449. udata[1] = fname;
  450. EditorResourcePreview::get_singleton()->queue_resource_preview(fpath, this, "_thumbnail_done", udata);
  451. }
  452. } else {
  453. files->add_item(fname, type_icon, true);
  454. item_index = files->get_item_count() - 1;
  455. files->set_item_metadata(item_index, fpath);
  456. }
  457. if (cselection.has(fname))
  458. files->select(item_index, false);
  459. if (finfo->sources.size()) {
  460. for (int j = 0; j < finfo->sources.size(); j++) {
  461. tooltip += "\nSource: " + finfo->sources[j];
  462. }
  463. }
  464. files->set_item_tooltip(item_index, tooltip);
  465. }
  466. }
  467. void FileSystemDock::_select_file(int p_idx) {
  468. String fpath = files->get_item_metadata(p_idx);
  469. if (fpath.ends_with("/")) {
  470. if (fpath != "res://") {
  471. fpath = fpath.substr(0, fpath.length() - 1);
  472. }
  473. path = fpath;
  474. _update_files(false);
  475. current_path->set_text(path);
  476. _push_to_history();
  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_files_in_dir(EditorFileSystemDirectory *efsd, Vector<String> &files) const {
  575. if (efsd == NULL)
  576. return;
  577. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  578. _get_all_files_in_dir(efsd->get_subdir(i), files);
  579. }
  580. for (int i = 0; i < efsd->get_file_count(); i++) {
  581. files.push_back(efsd->get_file_path(i));
  582. }
  583. }
  584. void FileSystemDock::_find_remaps(EditorFileSystemDirectory *efsd, const Map<String, String> &renames, Vector<String> &to_remaps) const {
  585. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  586. _find_remaps(efsd->get_subdir(i), renames, to_remaps);
  587. }
  588. for (int i = 0; i < efsd->get_file_count(); i++) {
  589. Vector<String> deps = efsd->get_file_deps(i);
  590. for (int j = 0; j < deps.size(); j++) {
  591. if (renames.has(deps[j])) {
  592. to_remaps.push_back(efsd->get_file_path(i));
  593. break;
  594. }
  595. }
  596. }
  597. }
  598. void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_new_path, Map<String, String> &p_renames) const {
  599. //Ensure folder paths end with "/"
  600. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  601. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  602. if (new_path == old_path) {
  603. return;
  604. } else if (old_path == "res://") {
  605. EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
  606. return;
  607. } else if (!p_item.is_file && new_path.begins_with(old_path)) {
  608. //This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/"
  609. EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.\n") + old_path + "\n");
  610. return;
  611. }
  612. //Build a list of files which will have new paths as a result of this operation
  613. Vector<String> changed_paths;
  614. if (p_item.is_file) {
  615. changed_paths.push_back(old_path);
  616. } else {
  617. _get_all_files_in_dir(EditorFileSystem::get_singleton()->get_filesystem_path(old_path), changed_paths);
  618. }
  619. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  620. print_line("Moving " + old_path + " -> " + new_path);
  621. Error err = da->rename(old_path, new_path);
  622. if (err == OK) {
  623. //Move/Rename any corresponding import settings too
  624. if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
  625. err = da->rename(old_path + ".import", new_path + ".import");
  626. if (err != OK) {
  627. EditorNode::get_singleton()->add_io_error(TTR("Error moving:\n") + old_path + ".import\n");
  628. }
  629. }
  630. //Only treat as a changed dependency if it was successfully moved
  631. for (int i = 0; i < changed_paths.size(); ++i) {
  632. p_renames[changed_paths[i]] = changed_paths[i].replace_first(old_path, new_path);
  633. print_line(" Remap: " + changed_paths[i] + " -> " + p_renames[changed_paths[i]]);
  634. }
  635. } else {
  636. EditorNode::get_singleton()->add_io_error(TTR("Error moving:\n") + old_path + "\n");
  637. }
  638. memdelete(da);
  639. }
  640. void FileSystemDock::_update_dependencies_after_move(const Map<String, String> &p_renames) const {
  641. //The following code assumes that the following holds:
  642. // 1) EditorFileSystem contains the old paths/folder structure from before the rename/move.
  643. // 2) ResourceLoader can use the new paths without needing to call rescan.
  644. Vector<String> remaps;
  645. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(), p_renames, remaps);
  646. for (int i = 0; i < remaps.size(); ++i) {
  647. //Because we haven't called a rescan yet the found remap might still be an old path itself.
  648. String file = p_renames.has(remaps[i]) ? p_renames[remaps[i]] : remaps[i];
  649. print_line("Remapping dependencies for: " + file);
  650. Error err = ResourceLoader::rename_dependencies(file, p_renames);
  651. if (err != OK) {
  652. EditorNode::get_singleton()->add_io_error(TTR("Unable to update dependencies:\n") + remaps[i] + "\n");
  653. }
  654. }
  655. }
  656. void FileSystemDock::_make_dir_confirm() {
  657. String dir_name = make_dir_dialog_text->get_text().strip_edges();
  658. if (dir_name.length() == 0) {
  659. EditorNode::get_singleton()->show_warning(TTR("No name provided"));
  660. return;
  661. } else if (dir_name.find("/") != -1 || dir_name.find("\\") != -1 || dir_name.find(":") != -1) {
  662. EditorNode::get_singleton()->show_warning(TTR("Provided name contains invalid characters"));
  663. return;
  664. }
  665. print_line("Making folder " + dir_name + " in " + path);
  666. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  667. Error err = da->change_dir(path);
  668. if (err == OK) {
  669. err = da->make_dir(dir_name);
  670. }
  671. memdelete(da);
  672. if (err == OK) {
  673. print_line("call rescan!");
  674. _rescan();
  675. } else {
  676. EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
  677. }
  678. }
  679. void FileSystemDock::_rename_operation_confirm() {
  680. String new_name = rename_dialog_text->get_text().strip_edges();
  681. if (new_name.length() == 0) {
  682. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  683. return;
  684. } else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
  685. EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
  686. return;
  687. }
  688. String old_path = to_rename.path.ends_with("/") ? to_rename.path.substr(0, to_rename.path.length() - 1) : to_rename.path;
  689. String new_path = old_path.get_base_dir().plus_file(new_name);
  690. if (old_path == new_path) {
  691. return;
  692. }
  693. //Present a more user friendly warning for name conflict
  694. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  695. if (da->file_exists(new_path) || da->dir_exists(new_path)) {
  696. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  697. memdelete(da);
  698. return;
  699. }
  700. memdelete(da);
  701. Map<String, String> renames;
  702. _try_move_item(to_rename, new_path, renames);
  703. _update_dependencies_after_move(renames);
  704. //Rescan everything
  705. print_line("call rescan!");
  706. _rescan();
  707. }
  708. void FileSystemDock::_move_operation_confirm(const String &p_to_path) {
  709. Map<String, String> renames;
  710. for (int i = 0; i < to_move.size(); i++) {
  711. 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;
  712. String new_path = p_to_path.plus_file(old_path.get_file());
  713. _try_move_item(to_move[i], new_path, renames);
  714. }
  715. _update_dependencies_after_move(renames);
  716. print_line("call rescan!");
  717. _rescan();
  718. }
  719. void FileSystemDock::_file_option(int p_option) {
  720. switch (p_option) {
  721. case FILE_SHOW_IN_EXPLORER: {
  722. String dir = ProjectSettings::get_singleton()->globalize_path(this->path);
  723. OS::get_singleton()->shell_open(String("file://") + dir);
  724. } break;
  725. case FILE_OPEN: {
  726. int idx = files->get_current();
  727. if (idx < 0 || idx >= files->get_item_count())
  728. break;
  729. _select_file(idx);
  730. } break;
  731. case FILE_INSTANCE: {
  732. Vector<String> paths;
  733. for (int i = 0; i < files->get_item_count(); i++) {
  734. if (!files->is_selected(i))
  735. continue;
  736. String fpath = files->get_item_metadata(i);
  737. if (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene") {
  738. paths.push_back(fpath);
  739. }
  740. }
  741. if (!paths.empty()) {
  742. emit_signal("instance", paths);
  743. }
  744. } break;
  745. case FILE_DEPENDENCIES: {
  746. int idx = files->get_current();
  747. if (idx < 0 || idx >= files->get_item_count())
  748. break;
  749. String fpath = files->get_item_metadata(idx);
  750. deps_editor->edit(fpath);
  751. } break;
  752. case FILE_OWNERS: {
  753. int idx = files->get_current();
  754. if (idx < 0 || idx >= files->get_item_count())
  755. break;
  756. String fpath = files->get_item_metadata(idx);
  757. owners_editor->show(fpath);
  758. } break;
  759. case FILE_MOVE: {
  760. to_move.clear();
  761. for (int i = 0; i < files->get_item_count(); i++) {
  762. if (!files->is_selected(i))
  763. continue;
  764. String fpath = files->get_item_metadata(i);
  765. to_move.push_back(FileOrFolder(fpath, !fpath.ends_with("/")));
  766. }
  767. if (to_move.size() > 0) {
  768. move_dialog->popup_centered_ratio();
  769. }
  770. } break;
  771. case FILE_RENAME: {
  772. int idx = files->get_current();
  773. if (idx < 0 || idx >= files->get_item_count())
  774. break;
  775. to_rename.path = files->get_item_metadata(idx);
  776. to_rename.is_file = !to_rename.path.ends_with("/");
  777. if (to_rename.is_file) {
  778. String name = to_rename.path.get_file();
  779. rename_dialog->set_title(TTR("Renaming file:") + " " + name);
  780. rename_dialog_text->set_text(name);
  781. rename_dialog_text->select(0, name.find_last("."));
  782. } else {
  783. String name = to_rename.path.substr(0, to_rename.path.length() - 1).get_file();
  784. rename_dialog->set_title(TTR("Renaming folder:") + " " + name);
  785. rename_dialog_text->set_text(name);
  786. rename_dialog_text->select(0, name.length());
  787. }
  788. rename_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  789. rename_dialog_text->grab_focus();
  790. } break;
  791. case FILE_REMOVE: {
  792. Vector<String> remove_files;
  793. Vector<String> remove_folders;
  794. for (int i = 0; i < files->get_item_count(); i++) {
  795. String fpath = files->get_item_metadata(i);
  796. if (files->is_selected(i) && fpath != "res://") {
  797. if (fpath.ends_with("/")) {
  798. remove_folders.push_back(fpath);
  799. } else {
  800. remove_files.push_back(fpath);
  801. }
  802. }
  803. }
  804. if (remove_files.size() + remove_folders.size() > 0) {
  805. remove_dialog->show(remove_folders, remove_files);
  806. //1) find if used
  807. //2) warn
  808. }
  809. } break;
  810. case FILE_INFO: {
  811. } break;
  812. case FILE_REIMPORT: {
  813. Vector<String> reimport;
  814. for (int i = 0; i < files->get_item_count(); i++) {
  815. if (!files->is_selected(i))
  816. continue;
  817. String fpath = files->get_item_metadata(i);
  818. reimport.push_back(fpath);
  819. }
  820. ERR_FAIL_COND(reimport.size() == 0);
  821. /*
  822. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(reimport[0]);
  823. ERR_FAIL_COND(!rimd.is_valid());
  824. String editor=rimd->get_editor();
  825. if (editor.begins_with("texture_")) { //compatibility fix for old texture format
  826. editor="texture";
  827. }
  828. Ref<EditorImportPlugin> rimp = EditorImportExport::get_singleton()->get_import_plugin_by_name(editor);
  829. ERR_FAIL_COND(!rimp.is_valid());
  830. if (reimport.size()==1) {
  831. rimp->import_dialog(reimport[0]);
  832. } else {
  833. rimp->reimport_multiple_files(reimport);
  834. }
  835. */
  836. } break;
  837. case FILE_NEW_FOLDER: {
  838. make_dir_dialog_text->set_text("new folder");
  839. make_dir_dialog_text->select_all();
  840. make_dir_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  841. make_dir_dialog_text->grab_focus();
  842. } break;
  843. case FILE_COPY_PATH: {
  844. int idx = files->get_current();
  845. if (idx < 0 || idx >= files->get_item_count())
  846. break;
  847. String fpath = files->get_item_metadata(idx);
  848. OS::get_singleton()->set_clipboard(fpath);
  849. } break;
  850. }
  851. }
  852. void FileSystemDock::_folder_option(int p_option) {
  853. TreeItem *selected = tree->get_selected();
  854. switch (p_option) {
  855. case FOLDER_EXPAND_ALL:
  856. case FOLDER_COLLAPSE_ALL: {
  857. bool is_collapsed = (p_option == FOLDER_COLLAPSE_ALL);
  858. Vector<TreeItem *> needs_check;
  859. needs_check.push_back(selected);
  860. while (needs_check.size()) {
  861. needs_check[0]->set_collapsed(is_collapsed);
  862. TreeItem *child = needs_check[0]->get_children();
  863. while (child) {
  864. needs_check.push_back(child);
  865. child = child->get_next();
  866. }
  867. needs_check.remove(0);
  868. }
  869. } break;
  870. case FOLDER_MOVE: {
  871. to_move.clear();
  872. String fpath = selected->get_metadata(tree->get_selected_column());
  873. if (fpath != "res://") {
  874. fpath = fpath.ends_with("/") ? fpath.substr(0, fpath.length() - 1) : fpath;
  875. to_move.push_back(FileOrFolder(fpath, false));
  876. move_dialog->popup_centered_ratio();
  877. }
  878. } break;
  879. case FOLDER_RENAME: {
  880. to_rename.path = selected->get_metadata(tree->get_selected_column());
  881. to_rename.is_file = false;
  882. if (to_rename.path != "res://") {
  883. String name = to_rename.path.ends_with("/") ? to_rename.path.substr(0, to_rename.path.length() - 1).get_file() : to_rename.path.get_file();
  884. rename_dialog->set_title(TTR("Renaming folder:") + " " + name);
  885. rename_dialog_text->set_text(name);
  886. rename_dialog_text->select(0, name.length());
  887. rename_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  888. rename_dialog_text->grab_focus();
  889. }
  890. } break;
  891. case FOLDER_REMOVE: {
  892. Vector<String> remove_folders;
  893. Vector<String> remove_files;
  894. String fpath = selected->get_metadata(tree->get_selected_column());
  895. if (fpath != "res://") {
  896. remove_folders.push_back(fpath);
  897. remove_dialog->show(remove_folders, remove_files);
  898. }
  899. } break;
  900. case FOLDER_NEW_FOLDER: {
  901. make_dir_dialog_text->set_text("new folder");
  902. make_dir_dialog_text->select_all();
  903. make_dir_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  904. make_dir_dialog_text->grab_focus();
  905. } break;
  906. case FOLDER_COPY_PATH: {
  907. String fpath = selected->get_metadata(tree->get_selected_column());
  908. OS::get_singleton()->set_clipboard(fpath);
  909. } break;
  910. case FOLDER_SHOW_IN_EXPLORER: {
  911. String fpath = selected->get_metadata(tree->get_selected_column());
  912. String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
  913. OS::get_singleton()->shell_open(String("file://") + dir);
  914. } break;
  915. }
  916. }
  917. void FileSystemDock::_go_to_file_list() {
  918. if (low_height_mode) {
  919. tree->hide();
  920. file_list_vb->show();
  921. button_favorite->hide();
  922. }
  923. //file_options->show();
  924. _update_files(false);
  925. //emit_signal("open",path);
  926. }
  927. void FileSystemDock::_dir_rmb_pressed(const Vector2 &p_pos) {
  928. folder_options->clear();
  929. folder_options->set_size(Size2(1, 1));
  930. folder_options->add_item(TTR("Expand all"), FOLDER_EXPAND_ALL);
  931. folder_options->add_item(TTR("Collapse all"), FOLDER_COLLAPSE_ALL);
  932. TreeItem *item = tree->get_selected();
  933. if (item) {
  934. String fpath = item->get_metadata(tree->get_selected_column());
  935. folder_options->add_separator();
  936. folder_options->add_item(TTR("Copy Path"), FOLDER_COPY_PATH);
  937. if (fpath != "res://") {
  938. folder_options->add_item(TTR("Rename.."), FOLDER_RENAME);
  939. folder_options->add_item(TTR("Move To.."), FOLDER_MOVE);
  940. folder_options->add_item(TTR("Delete"), FOLDER_REMOVE);
  941. }
  942. folder_options->add_separator();
  943. folder_options->add_item(TTR("New Folder.."), FOLDER_NEW_FOLDER);
  944. folder_options->add_item(TTR("Show In File Manager"), FOLDER_SHOW_IN_EXPLORER);
  945. }
  946. folder_options->set_position(tree->get_global_position() + p_pos);
  947. folder_options->popup();
  948. }
  949. void FileSystemDock::_search_changed(const String &p_text) {
  950. if (file_list_vb->is_visible())
  951. _update_files(false);
  952. }
  953. void FileSystemDock::_rescan() {
  954. _set_scanning_mode();
  955. EditorFileSystem::get_singleton()->scan();
  956. }
  957. void FileSystemDock::fix_dependencies(const String &p_for_file) {
  958. deps_editor->edit(p_for_file);
  959. }
  960. void FileSystemDock::focus_on_filter() {
  961. if (low_height_mode && tree->is_visible()) {
  962. // Tree mode, switch to files list with search box
  963. tree->hide();
  964. file_list_vb->show();
  965. button_favorite->hide();
  966. }
  967. search_box->grab_focus();
  968. }
  969. void FileSystemDock::set_display_mode(int p_mode) {
  970. if (p_mode == display_mode)
  971. return;
  972. button_display_mode->set_pressed(p_mode == DISPLAY_LIST);
  973. _change_file_display();
  974. }
  975. Variant FileSystemDock::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  976. if (p_from == tree) {
  977. TreeItem *selected = tree->get_selected();
  978. if (!selected)
  979. return Variant();
  980. String fpath = selected->get_metadata(0);
  981. if (fpath == String())
  982. return Variant();
  983. if (!fpath.ends_with("/"))
  984. fpath = fpath + "/";
  985. Vector<String> paths;
  986. paths.push_back(fpath);
  987. Dictionary d = EditorNode::get_singleton()->drag_files(paths, p_from);
  988. if (selected->get_parent() && tree->get_root()->get_children() == selected->get_parent()) {
  989. //a favorite.. treat as such
  990. d["type"] = "favorite";
  991. }
  992. return d;
  993. }
  994. if (p_from == files) {
  995. List<int> seldirs;
  996. List<int> selfiles;
  997. for (int i = 0; i < files->get_item_count(); i++) {
  998. if (files->is_selected(i)) {
  999. String fpath = files->get_item_metadata(i);
  1000. if (fpath.ends_with("/"))
  1001. seldirs.push_back(i);
  1002. else
  1003. selfiles.push_back(i);
  1004. }
  1005. }
  1006. if (seldirs.empty() && selfiles.empty())
  1007. return Variant();
  1008. /*
  1009. if (seldirs.size() && selfiles.size())
  1010. return Variant(); //can't really mix files and dirs (i think?) - yes you can, commenting
  1011. */
  1012. /*if (selfiles.size()==1) {
  1013. Ref<Resource> resource = ResourceLoader::load(files->get_item_metadata(selfiles.front()->get()));
  1014. if (resource.is_valid()) {
  1015. return EditorNode::get_singleton()->drag_resource(resource,p_from);
  1016. }
  1017. }*/
  1018. Vector<String> fnames;
  1019. if (selfiles.size() > 0 || seldirs.size() > 0) {
  1020. if (selfiles.size() > 0) {
  1021. for (List<int>::Element *E = selfiles.front(); E; E = E->next()) {
  1022. fnames.push_back(files->get_item_metadata(E->get()));
  1023. }
  1024. if (seldirs.size() == 0)
  1025. return EditorNode::get_singleton()->drag_files(fnames, p_from);
  1026. }
  1027. for (List<int>::Element *E = seldirs.front(); E; E = E->next()) {
  1028. fnames.push_back(files->get_item_metadata(E->get()));
  1029. }
  1030. return EditorNode::get_singleton()->drag_files_and_dirs(fnames, p_from);
  1031. }
  1032. }
  1033. return Variant();
  1034. }
  1035. bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1036. Dictionary drag_data = p_data;
  1037. if (drag_data.has("type") && String(drag_data["type"]) == "favorite") {
  1038. //moving favorite around
  1039. TreeItem *ti = tree->get_item_at_position(p_point);
  1040. if (!ti)
  1041. return false;
  1042. int what = tree->get_drop_section_at_position(p_point);
  1043. if (ti == tree->get_root()->get_children()) {
  1044. return (what == 1); //the parent, first fav
  1045. }
  1046. if (ti->get_parent() && tree->get_root()->get_children() == ti->get_parent()) {
  1047. return true; // a favorite
  1048. }
  1049. if (ti == tree->get_root()->get_children()->get_next()) {
  1050. return (what == -1); //the tree, last fav
  1051. }
  1052. return false;
  1053. }
  1054. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1055. return true;
  1056. }
  1057. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1058. Vector<String> fnames = drag_data["files"];
  1059. if (p_from == files) {
  1060. int at_pos = files->get_item_at_position(p_point);
  1061. if (at_pos != -1) {
  1062. String dir = files->get_item_metadata(at_pos);
  1063. if (dir.ends_with("/"))
  1064. return true;
  1065. }
  1066. }
  1067. if (p_from == tree) {
  1068. TreeItem *ti = tree->get_item_at_position(p_point);
  1069. if (!ti)
  1070. return false;
  1071. String fpath = ti->get_metadata(0);
  1072. if (fpath == String())
  1073. return false;
  1074. return true;
  1075. }
  1076. }
  1077. return false;
  1078. }
  1079. void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1080. if (!can_drop_data_fw(p_point, p_data, p_from))
  1081. return;
  1082. Dictionary drag_data = p_data;
  1083. if (drag_data.has("type") && String(drag_data["type"]) == "favorite") {
  1084. //moving favorite around
  1085. TreeItem *ti = tree->get_item_at_position(p_point);
  1086. if (!ti)
  1087. return;
  1088. Vector<String> files = drag_data["files"];
  1089. ERR_FAIL_COND(files.size() != 1);
  1090. String swap = files[0];
  1091. if (swap != "res://" && swap.ends_with("/")) {
  1092. swap = swap.substr(0, swap.length() - 1);
  1093. }
  1094. int what = tree->get_drop_section_at_position(p_point);
  1095. TreeItem *swap_item = NULL;
  1096. if (ti == tree->get_root()->get_children()) {
  1097. swap_item = tree->get_root()->get_children()->get_children();
  1098. } else if (ti->get_parent() && tree->get_root()->get_children() == ti->get_parent()) {
  1099. if (what == -1) {
  1100. swap_item = ti;
  1101. } else {
  1102. swap_item = ti->get_next();
  1103. }
  1104. }
  1105. String swap_with;
  1106. if (swap_item) {
  1107. swap_with = swap_item->get_metadata(0);
  1108. if (swap_with != "res://" && swap_with.ends_with("/")) {
  1109. swap_with = swap_with.substr(0, swap_with.length() - 1);
  1110. }
  1111. }
  1112. if (swap == swap_with)
  1113. return;
  1114. Vector<String> dirs = EditorSettings::get_singleton()->get_favorite_dirs();
  1115. ERR_FAIL_COND(dirs.find(swap) == -1);
  1116. ERR_FAIL_COND(swap_with != String() && dirs.find(swap_with) == -1);
  1117. dirs.erase(swap);
  1118. if (swap_with == String()) {
  1119. dirs.push_back(swap);
  1120. } else {
  1121. int idx = dirs.find(swap_with);
  1122. dirs.insert(idx, swap);
  1123. }
  1124. EditorSettings::get_singleton()->set_favorite_dirs(dirs);
  1125. _update_tree(true);
  1126. return;
  1127. }
  1128. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1129. Ref<Resource> res = drag_data["resource"];
  1130. if (!res.is_valid()) {
  1131. return;
  1132. }
  1133. if (p_from == tree) {
  1134. TreeItem *ti = tree->get_item_at_position(p_point);
  1135. if (!ti)
  1136. return;
  1137. String fpath = ti->get_metadata(0);
  1138. if (fpath == String())
  1139. return;
  1140. EditorNode::get_singleton()->save_resource_as(res, fpath);
  1141. return;
  1142. }
  1143. if (p_from == files) {
  1144. String save_path = path;
  1145. int at_pos = files->get_item_at_position(p_point);
  1146. if (at_pos != -1) {
  1147. String to_dir = files->get_item_metadata(at_pos);
  1148. if (to_dir.ends_with("/")) {
  1149. save_path = to_dir;
  1150. if (save_path != "res://")
  1151. save_path = save_path.substr(0, save_path.length() - 1);
  1152. }
  1153. }
  1154. EditorNode::get_singleton()->save_resource_as(res, save_path);
  1155. return;
  1156. }
  1157. }
  1158. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1159. if (p_from == files || p_from == tree) {
  1160. String to_dir;
  1161. if (p_from == files) {
  1162. int at_pos = files->get_item_at_position(p_point);
  1163. ERR_FAIL_COND(at_pos == -1);
  1164. to_dir = files->get_item_metadata(at_pos);
  1165. } else {
  1166. TreeItem *ti = tree->get_item_at_position(p_point);
  1167. if (!ti)
  1168. return;
  1169. to_dir = ti->get_metadata(0);
  1170. ERR_FAIL_COND(to_dir == String());
  1171. }
  1172. if (to_dir != "res://" && to_dir.ends_with("/")) {
  1173. to_dir = to_dir.substr(0, to_dir.length() - 1);
  1174. }
  1175. Vector<String> fnames = drag_data["files"];
  1176. to_move.clear();
  1177. for (int i = 0; i < fnames.size(); i++) {
  1178. to_move.push_back(FileOrFolder(fnames[i], !fnames[i].ends_with("/")));
  1179. }
  1180. _move_operation_confirm(to_dir);
  1181. }
  1182. }
  1183. }
  1184. void FileSystemDock::_files_list_rmb_select(int p_item, const Vector2 &p_pos) {
  1185. //Right clicking ".." should clear current selection
  1186. if (files->get_item_text(p_item) == "..") {
  1187. for (int i = 0; i < files->get_item_count(); i++) {
  1188. files->unselect(i);
  1189. }
  1190. }
  1191. Vector<String> filenames;
  1192. Vector<String> foldernames;
  1193. bool all_files = true;
  1194. bool all_files_scenes = true;
  1195. bool all_folders = true;
  1196. for (int i = 0; i < files->get_item_count(); i++) {
  1197. if (!files->is_selected(i)) {
  1198. continue;
  1199. }
  1200. String fpath = files->get_item_metadata(i);
  1201. if (fpath.ends_with("/")) {
  1202. foldernames.push_back(fpath);
  1203. all_files = false;
  1204. } else {
  1205. filenames.push_back(fpath);
  1206. all_folders = false;
  1207. all_files_scenes &= (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene");
  1208. }
  1209. }
  1210. file_options->clear();
  1211. file_options->set_size(Size2(1, 1));
  1212. if (all_files && filenames.size() > 0) {
  1213. file_options->add_item(TTR("Open"), FILE_OPEN);
  1214. if (all_files_scenes) {
  1215. file_options->add_item(TTR("Instance"), FILE_INSTANCE);
  1216. }
  1217. if (filenames.size() == 1) {
  1218. file_options->add_separator();
  1219. file_options->add_item(TTR("Edit Dependencies.."), FILE_DEPENDENCIES);
  1220. file_options->add_item(TTR("View Owners.."), FILE_OWNERS);
  1221. }
  1222. } else if (all_folders && foldernames.size() > 0) {
  1223. file_options->add_item(TTR("Open"), FILE_OPEN);
  1224. }
  1225. file_options->add_separator();
  1226. int num_items = filenames.size() + foldernames.size();
  1227. if (num_items >= 1) {
  1228. if (num_items == 1) {
  1229. file_options->add_item(TTR("Copy Path"), FILE_COPY_PATH);
  1230. file_options->add_item(TTR("Rename.."), FILE_RENAME);
  1231. }
  1232. file_options->add_item(TTR("Move To.."), FILE_MOVE);
  1233. file_options->add_item(TTR("Delete"), FILE_REMOVE);
  1234. file_options->add_separator();
  1235. }
  1236. file_options->add_item(TTR("New Folder.."), FILE_NEW_FOLDER);
  1237. file_options->add_item(TTR("Show In File Manager"), FILE_SHOW_IN_EXPLORER);
  1238. file_options->set_position(files->get_global_position() + p_pos);
  1239. file_options->popup();
  1240. }
  1241. void FileSystemDock::select_file(const String &p_file) {
  1242. navigate_to_path(p_file);
  1243. }
  1244. void FileSystemDock::_file_multi_selected(int p_index, bool p_selected) {
  1245. _file_selected();
  1246. }
  1247. void FileSystemDock::_file_selected() {
  1248. //check import
  1249. Vector<String> imports;
  1250. String import_type;
  1251. for (int i = 0; i < files->get_item_count(); i++) {
  1252. if (!files->is_selected(i))
  1253. continue;
  1254. String fpath = files->get_item_metadata(i);
  1255. if (!FileAccess::exists(fpath + ".import")) {
  1256. imports.clear();
  1257. break;
  1258. }
  1259. Ref<ConfigFile> cf;
  1260. cf.instance();
  1261. Error err = cf->load(fpath + ".import");
  1262. if (err != OK) {
  1263. imports.clear();
  1264. break;
  1265. }
  1266. String type = cf->get_value("remap", "type");
  1267. if (import_type == "") {
  1268. import_type = type;
  1269. } else if (import_type != type) {
  1270. //all should be the same type
  1271. imports.clear();
  1272. break;
  1273. }
  1274. imports.push_back(fpath);
  1275. }
  1276. if (imports.size() == 0) {
  1277. EditorNode::get_singleton()->get_import_dock()->clear();
  1278. } else if (imports.size() == 1) {
  1279. EditorNode::get_singleton()->get_import_dock()->set_edit_path(imports[0]);
  1280. } else {
  1281. EditorNode::get_singleton()->get_import_dock()->set_edit_multiple_paths(imports);
  1282. }
  1283. }
  1284. void FileSystemDock::_bind_methods() {
  1285. ClassDB::bind_method(D_METHOD("_update_tree"), &FileSystemDock::_update_tree);
  1286. ClassDB::bind_method(D_METHOD("_rescan"), &FileSystemDock::_rescan);
  1287. ClassDB::bind_method(D_METHOD("_favorites_pressed"), &FileSystemDock::_favorites_pressed);
  1288. //ClassDB::bind_method(D_METHOD("_instance_pressed"),&ScenesDock::_instance_pressed);
  1289. ClassDB::bind_method(D_METHOD("_go_to_file_list"), &FileSystemDock::_go_to_file_list);
  1290. ClassDB::bind_method(D_METHOD("_dir_rmb_pressed"), &FileSystemDock::_dir_rmb_pressed);
  1291. ClassDB::bind_method(D_METHOD("_thumbnail_done"), &FileSystemDock::_thumbnail_done);
  1292. ClassDB::bind_method(D_METHOD("_select_file"), &FileSystemDock::_select_file);
  1293. ClassDB::bind_method(D_METHOD("_go_to_tree"), &FileSystemDock::_go_to_tree);
  1294. ClassDB::bind_method(D_METHOD("navigate_to_path"), &FileSystemDock::navigate_to_path);
  1295. ClassDB::bind_method(D_METHOD("_change_file_display"), &FileSystemDock::_change_file_display);
  1296. ClassDB::bind_method(D_METHOD("_fw_history"), &FileSystemDock::_fw_history);
  1297. ClassDB::bind_method(D_METHOD("_bw_history"), &FileSystemDock::_bw_history);
  1298. ClassDB::bind_method(D_METHOD("_fs_changed"), &FileSystemDock::_fs_changed);
  1299. ClassDB::bind_method(D_METHOD("_dir_selected"), &FileSystemDock::_dir_selected);
  1300. ClassDB::bind_method(D_METHOD("_file_option"), &FileSystemDock::_file_option);
  1301. ClassDB::bind_method(D_METHOD("_folder_option"), &FileSystemDock::_folder_option);
  1302. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileSystemDock::_make_dir_confirm);
  1303. ClassDB::bind_method(D_METHOD("_move_operation_confirm"), &FileSystemDock::_move_operation_confirm);
  1304. ClassDB::bind_method(D_METHOD("_rename_operation_confirm"), &FileSystemDock::_rename_operation_confirm);
  1305. ClassDB::bind_method(D_METHOD("_search_changed"), &FileSystemDock::_search_changed);
  1306. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &FileSystemDock::get_drag_data_fw);
  1307. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &FileSystemDock::can_drop_data_fw);
  1308. ClassDB::bind_method(D_METHOD("drop_data_fw"), &FileSystemDock::drop_data_fw);
  1309. ClassDB::bind_method(D_METHOD("_files_list_rmb_select"), &FileSystemDock::_files_list_rmb_select);
  1310. ClassDB::bind_method(D_METHOD("_preview_invalidated"), &FileSystemDock::_preview_invalidated);
  1311. ClassDB::bind_method(D_METHOD("_file_selected"), &FileSystemDock::_file_selected);
  1312. ClassDB::bind_method(D_METHOD("_file_multi_selected"), &FileSystemDock::_file_multi_selected);
  1313. ADD_SIGNAL(MethodInfo("instance", PropertyInfo(Variant::POOL_STRING_ARRAY, "files")));
  1314. ADD_SIGNAL(MethodInfo("open"));
  1315. }
  1316. FileSystemDock::FileSystemDock(EditorNode *p_editor) {
  1317. editor = p_editor;
  1318. path = "res://";
  1319. HBoxContainer *toolbar_hbc = memnew(HBoxContainer);
  1320. add_child(toolbar_hbc);
  1321. button_hist_prev = memnew(ToolButton);
  1322. button_hist_prev->set_disabled(true);
  1323. button_hist_prev->set_focus_mode(FOCUS_NONE);
  1324. button_hist_prev->set_tooltip(TTR("Previous Directory"));
  1325. toolbar_hbc->add_child(button_hist_prev);
  1326. button_hist_next = memnew(ToolButton);
  1327. button_hist_next->set_disabled(true);
  1328. button_hist_next->set_focus_mode(FOCUS_NONE);
  1329. button_hist_next->set_tooltip(TTR("Next Directory"));
  1330. toolbar_hbc->add_child(button_hist_next);
  1331. current_path = memnew(LineEdit);
  1332. current_path->set_h_size_flags(SIZE_EXPAND_FILL);
  1333. toolbar_hbc->add_child(current_path);
  1334. button_reload = memnew(Button);
  1335. button_reload->set_flat(true);
  1336. button_reload->connect("pressed", this, "_rescan");
  1337. button_reload->set_focus_mode(FOCUS_NONE);
  1338. button_reload->set_tooltip(TTR("Re-Scan Filesystem"));
  1339. button_reload->hide();
  1340. toolbar_hbc->add_child(button_reload);
  1341. //toolbar_hbc->add_spacer();
  1342. button_favorite = memnew(Button);
  1343. button_favorite->set_flat(true);
  1344. button_favorite->set_toggle_mode(true);
  1345. button_favorite->connect("pressed", this, "_favorites_pressed");
  1346. button_favorite->set_tooltip(TTR("Toggle folder status as Favorite"));
  1347. button_favorite->set_focus_mode(FOCUS_NONE);
  1348. toolbar_hbc->add_child(button_favorite);
  1349. //Control *spacer = memnew( Control);
  1350. /*
  1351. button_open = memnew( Button );
  1352. button_open->set_flat(true);
  1353. button_open->connect("pressed",this,"_go_to_file_list");
  1354. toolbar_hbc->add_child(button_open);
  1355. button_open->hide();
  1356. button_open->set_focus_mode(FOCUS_NONE);
  1357. button_open->set_tooltip("Open the selected file.\nOpen as scene if a scene, or as resource otherwise.");
  1358. button_instance = memnew( Button );
  1359. button_instance->set_flat(true);
  1360. button_instance->connect("pressed",this,"_instance_pressed");
  1361. toolbar_hbc->add_child(button_instance);
  1362. button_instance->hide();
  1363. button_instance->set_focus_mode(FOCUS_NONE);
  1364. button_instance->set_tooltip(TTR("Instance the selected scene(s) as child of the selected node."));
  1365. */
  1366. file_options = memnew(PopupMenu);
  1367. add_child(file_options);
  1368. folder_options = memnew(PopupMenu);
  1369. add_child(folder_options);
  1370. split_box = memnew(VSplitContainer);
  1371. split_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1372. add_child(split_box);
  1373. tree = memnew(Tree);
  1374. tree->set_hide_root(true);
  1375. tree->set_drag_forwarding(this);
  1376. tree->set_allow_rmb_select(true);
  1377. tree->set_custom_minimum_size(Size2(0, 200 * EDSCALE));
  1378. split_box->add_child(tree);
  1379. tree->connect("item_edited", this, "_favorite_toggled");
  1380. tree->connect("item_activated", this, "_go_to_file_list");
  1381. tree->connect("cell_selected", this, "_dir_selected");
  1382. tree->connect("item_rmb_selected", this, "_dir_rmb_pressed");
  1383. file_list_vb = memnew(VBoxContainer);
  1384. file_list_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1385. split_box->add_child(file_list_vb);
  1386. path_hb = memnew(HBoxContainer);
  1387. file_list_vb->add_child(path_hb);
  1388. button_tree = memnew(ToolButton);
  1389. button_tree->hide();
  1390. path_hb->add_child(button_tree);
  1391. search_box = memnew(LineEdit);
  1392. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1393. search_box->connect("text_changed", this, "_search_changed");
  1394. path_hb->add_child(search_box);
  1395. button_display_mode = memnew(ToolButton);
  1396. button_display_mode->set_toggle_mode(true);
  1397. path_hb->add_child(button_display_mode);
  1398. files = memnew(ItemList);
  1399. files->set_v_size_flags(SIZE_EXPAND_FILL);
  1400. files->set_select_mode(ItemList::SELECT_MULTI);
  1401. files->set_drag_forwarding(this);
  1402. files->connect("item_rmb_selected", this, "_files_list_rmb_select");
  1403. files->connect("item_selected", this, "_file_selected");
  1404. files->connect("multi_selected", this, "_file_multi_selected");
  1405. files->set_allow_rmb_select(true);
  1406. file_list_vb->add_child(files);
  1407. scanning_vb = memnew(VBoxContainer);
  1408. scanning_vb->hide();
  1409. add_child(scanning_vb);
  1410. Label *slabel = memnew(Label);
  1411. slabel->set_text(TTR("Scanning Files,\nPlease Wait.."));
  1412. slabel->set_align(Label::ALIGN_CENTER);
  1413. scanning_vb->add_child(slabel);
  1414. scanning_progress = memnew(ProgressBar);
  1415. scanning_vb->add_child(scanning_progress);
  1416. deps_editor = memnew(DependencyEditor);
  1417. add_child(deps_editor);
  1418. owners_editor = memnew(DependencyEditorOwners);
  1419. add_child(owners_editor);
  1420. remove_dialog = memnew(DependencyRemoveDialog);
  1421. add_child(remove_dialog);
  1422. move_dialog = memnew(EditorDirDialog);
  1423. move_dialog->get_ok()->set_text(TTR("Move"));
  1424. add_child(move_dialog);
  1425. move_dialog->connect("dir_selected", this, "_move_operation_confirm");
  1426. rename_dialog = memnew(ConfirmationDialog);
  1427. VBoxContainer *rename_dialog_vb = memnew(VBoxContainer);
  1428. rename_dialog->add_child(rename_dialog_vb);
  1429. rename_dialog_text = memnew(LineEdit);
  1430. rename_dialog_vb->add_margin_child(TTR("Name:"), rename_dialog_text);
  1431. rename_dialog->get_ok()->set_text(TTR("Rename"));
  1432. add_child(rename_dialog);
  1433. rename_dialog->register_text_enter(rename_dialog_text);
  1434. rename_dialog->connect("confirmed", this, "_rename_operation_confirm");
  1435. make_dir_dialog = memnew(ConfirmationDialog);
  1436. make_dir_dialog->set_title(TTR("Create Folder"));
  1437. VBoxContainer *make_folder_dialog_vb = memnew(VBoxContainer);
  1438. make_dir_dialog->add_child(make_folder_dialog_vb);
  1439. make_dir_dialog_text = memnew(LineEdit);
  1440. make_folder_dialog_vb->add_margin_child(TTR("Name:"), make_dir_dialog_text);
  1441. add_child(make_dir_dialog);
  1442. make_dir_dialog->register_text_enter(make_dir_dialog_text);
  1443. make_dir_dialog->connect("confirmed", this, "_make_dir_confirm");
  1444. updating_tree = false;
  1445. initialized = false;
  1446. history_pos = 0;
  1447. history_max_size = 20;
  1448. history.push_back("res://");
  1449. low_height_mode = false;
  1450. display_mode = DISPLAY_THUMBNAILS;
  1451. }
  1452. FileSystemDock::~FileSystemDock() {
  1453. }