filesystem_dock.cpp 50 KB

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