filesystem_dock.cpp 48 KB

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