filesystem_dock.cpp 53 KB

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