filesystem_dock.cpp 49 KB

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