filesystem_dock.cpp 49 KB

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