filesystem_dock.cpp 49 KB

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