filesystem_dock.cpp 49 KB

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