filesystem_dock.cpp 49 KB

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