filesystem_dock.cpp 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. /*************************************************************************/
  2. /* filesystem_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/io/resource_loader.h"
  32. #include "core/os/dir_access.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "editor_node.h"
  38. #include "editor_settings.h"
  39. #include "scene/main/viewport.h"
  40. Ref<Texture> FileSystemDock::_get_tree_item_icon(EditorFileSystemDirectory *p_dir, int p_idx) {
  41. Ref<Texture> file_icon;
  42. if (!p_dir->get_file_import_is_valid(p_idx)) {
  43. file_icon = get_icon("ImportFail", "EditorIcons");
  44. } else {
  45. String file_type = p_dir->get_file_type(p_idx);
  46. file_icon = (has_icon(file_type, "EditorIcons")) ? get_icon(file_type, "EditorIcons") : get_icon("File", "EditorIcons");
  47. }
  48. return file_icon;
  49. }
  50. bool FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory *p_dir, Vector<String> &uncollapsed_paths, bool p_select_in_favorites) {
  51. bool parent_should_expand = false;
  52. // Create a tree item for the subdirectory
  53. TreeItem *subdirectory_item = tree->create_item(p_parent);
  54. String dname = p_dir->get_name();
  55. if (dname == "")
  56. dname = "res://";
  57. subdirectory_item->set_text(0, dname);
  58. subdirectory_item->set_icon(0, get_icon("Folder", "EditorIcons"));
  59. subdirectory_item->set_selectable(0, true);
  60. String lpath = p_dir->get_path();
  61. subdirectory_item->set_metadata(0, lpath);
  62. if (!p_select_in_favorites && (path == lpath || ((display_mode == DISPLAY_MODE_SPLIT) && path.get_base_dir() == lpath))) {
  63. subdirectory_item->select(0);
  64. }
  65. if ((path.begins_with(lpath) && path != lpath)) {
  66. subdirectory_item->set_collapsed(false);
  67. } else {
  68. subdirectory_item->set_collapsed(uncollapsed_paths.find(lpath) < 0);
  69. }
  70. if (searched_string.length() > 0 && dname.to_lower().find(searched_string) >= 0) {
  71. parent_should_expand = true;
  72. }
  73. // Create items for all subdirectories
  74. for (int i = 0; i < p_dir->get_subdir_count(); i++)
  75. parent_should_expand = (_create_tree(subdirectory_item, p_dir->get_subdir(i), uncollapsed_paths, p_select_in_favorites) || parent_should_expand);
  76. // Create all items for the files in the subdirectory
  77. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  78. for (int i = 0; i < p_dir->get_file_count(); i++) {
  79. String file_name = p_dir->get_file(i);
  80. if (searched_string.length() > 0) {
  81. if (file_name.to_lower().find(searched_string) < 0) {
  82. // The searched string is not in the file name, we skip it
  83. continue;
  84. } else {
  85. // We expand all parents
  86. parent_should_expand = true;
  87. }
  88. }
  89. TreeItem *file_item = tree->create_item(subdirectory_item);
  90. file_item->set_text(0, file_name);
  91. file_item->set_icon(0, _get_tree_item_icon(p_dir, i));
  92. String file_metadata = lpath.plus_file(file_name);
  93. file_item->set_metadata(0, file_metadata);
  94. if (!p_select_in_favorites && path == file_metadata) {
  95. file_item->select(0);
  96. file_item->set_as_cursor(0);
  97. }
  98. Array udata;
  99. udata.push_back(tree_update_id);
  100. udata.push_back(file_item);
  101. EditorResourcePreview::get_singleton()->queue_resource_preview(file_metadata, this, "_tree_thumbnail_done", udata);
  102. }
  103. }
  104. if (searched_string.length() > 0) {
  105. if (parent_should_expand) {
  106. subdirectory_item->set_collapsed(false);
  107. } else if (dname != "res://") {
  108. subdirectory_item->get_parent()->remove_child(subdirectory_item);
  109. }
  110. }
  111. return parent_should_expand;
  112. }
  113. Vector<String> FileSystemDock::_compute_uncollapsed_paths() {
  114. // Register currently collapsed paths
  115. Vector<String> uncollapsed_paths;
  116. TreeItem *root = tree->get_root();
  117. if (root) {
  118. TreeItem *favorites_item = root->get_children();
  119. if (!favorites_item->is_collapsed()) {
  120. uncollapsed_paths.push_back(favorites_item->get_metadata(0));
  121. }
  122. TreeItem *resTree = root->get_children()->get_next();
  123. if (resTree) {
  124. Vector<TreeItem *> needs_check;
  125. needs_check.push_back(resTree);
  126. while (needs_check.size()) {
  127. if (!needs_check[0]->is_collapsed()) {
  128. uncollapsed_paths.push_back(needs_check[0]->get_metadata(0));
  129. TreeItem *child = needs_check[0]->get_children();
  130. while (child) {
  131. needs_check.push_back(child);
  132. child = child->get_next();
  133. }
  134. }
  135. needs_check.remove(0);
  136. }
  137. }
  138. }
  139. return uncollapsed_paths;
  140. }
  141. void FileSystemDock::_update_tree(const Vector<String> p_uncollapsed_paths, bool p_uncollapse_root, bool p_select_in_favorites) {
  142. // Recreate the tree
  143. tree->clear();
  144. tree_update_id++;
  145. updating_tree = true;
  146. TreeItem *root = tree->create_item();
  147. // Handles the favorites
  148. TreeItem *favorites = tree->create_item(root);
  149. favorites->set_icon(0, get_icon("Favorites", "EditorIcons"));
  150. favorites->set_text(0, TTR("Favorites:"));
  151. favorites->set_metadata(0, "Favorites");
  152. favorites->set_collapsed(p_uncollapsed_paths.find("Favorites") < 0);
  153. Vector<String> favorite_paths = EditorSettings::get_singleton()->get_favorites();
  154. for (int i = 0; i < favorite_paths.size(); i++) {
  155. String fave = favorite_paths[i];
  156. if (!fave.begins_with("res://"))
  157. continue;
  158. Ref<Texture> folder_icon = get_icon("Folder", "EditorIcons");
  159. String text;
  160. Ref<Texture> icon;
  161. if (fave == "res://") {
  162. text = "/";
  163. icon = folder_icon;
  164. } else if (fave.ends_with("/")) {
  165. text = fave.substr(0, fave.length() - 1).get_file();
  166. icon = folder_icon;
  167. } else {
  168. text = fave.get_file();
  169. int index;
  170. EditorFileSystemDirectory *dir = EditorFileSystem::get_singleton()->find_file(fave, &index);
  171. if (dir) {
  172. icon = _get_tree_item_icon(dir, index);
  173. } else {
  174. icon = get_icon("File", "EditorIcons");
  175. }
  176. }
  177. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  178. TreeItem *ti = tree->create_item(favorites);
  179. ti->set_text(0, text);
  180. ti->set_icon(0, icon);
  181. ti->set_tooltip(0, fave);
  182. ti->set_selectable(0, true);
  183. ti->set_metadata(0, fave);
  184. if (p_select_in_favorites && fave == path) {
  185. ti->select(0);
  186. ti->set_as_cursor(0);
  187. }
  188. if (!fave.ends_with("/")) {
  189. Array udata;
  190. udata.push_back(tree_update_id);
  191. udata.push_back(ti);
  192. EditorResourcePreview::get_singleton()->queue_resource_preview(fave, this, "_tree_thumbnail_done", udata);
  193. }
  194. }
  195. }
  196. Vector<String> uncollapsed_paths = p_uncollapsed_paths;
  197. if (p_uncollapse_root) {
  198. uncollapsed_paths.push_back("res://");
  199. }
  200. // Create the remaining of the tree
  201. _create_tree(root, EditorFileSystem::get_singleton()->get_filesystem(), uncollapsed_paths, p_select_in_favorites);
  202. tree->ensure_cursor_is_visible();
  203. updating_tree = false;
  204. }
  205. void FileSystemDock::set_display_mode(DisplayMode p_display_mode) {
  206. display_mode = p_display_mode;
  207. _update_display_mode(false);
  208. }
  209. void FileSystemDock::_update_display_mode(bool p_force) {
  210. // Compute the new display mode
  211. if (p_force || old_display_mode != display_mode) {
  212. button_toggle_display_mode->set_pressed(display_mode == DISPLAY_MODE_SPLIT ? true : false);
  213. switch (display_mode) {
  214. case DISPLAY_MODE_TREE_ONLY:
  215. tree->show();
  216. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  217. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  218. tree_search_box->show();
  219. } else {
  220. tree_search_box->hide();
  221. }
  222. _update_tree(_compute_uncollapsed_paths());
  223. file_list_vb->hide();
  224. break;
  225. case DISPLAY_MODE_SPLIT:
  226. tree->show();
  227. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  228. tree->ensure_cursor_is_visible();
  229. tree_search_box->hide();
  230. _update_tree(_compute_uncollapsed_paths());
  231. file_list_vb->show();
  232. _update_file_list(true);
  233. break;
  234. }
  235. old_display_mode = display_mode;
  236. }
  237. }
  238. void FileSystemDock::_notification(int p_what) {
  239. switch (p_what) {
  240. case NOTIFICATION_ENTER_TREE: {
  241. if (initialized)
  242. return;
  243. initialized = true;
  244. EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "_fs_changed");
  245. EditorResourcePreview::get_singleton()->connect("preview_invalidated", this, "_preview_invalidated");
  246. String ei = "EditorIcons";
  247. button_reload->set_icon(get_icon("Reload", ei));
  248. button_toggle_display_mode->set_icon(get_icon("Panels2", ei));
  249. button_file_list_display_mode->connect("pressed", this, "_toggle_file_display");
  250. files->connect("item_activated", this, "_file_list_activate_file");
  251. button_hist_next->connect("pressed", this, "_fw_history");
  252. button_hist_prev->connect("pressed", this, "_bw_history");
  253. tree_search_box->set_right_icon(get_icon("Search", ei));
  254. tree_search_box->set_clear_button_enabled(true);
  255. file_list_search_box->set_right_icon(get_icon("Search", ei));
  256. file_list_search_box->set_clear_button_enabled(true);
  257. button_hist_next->set_icon(get_icon("Forward", ei));
  258. button_hist_prev->set_icon(get_icon("Back", ei));
  259. file_list_popup->connect("id_pressed", this, "_file_list_rmb_option");
  260. tree_popup->connect("id_pressed", this, "_tree_rmb_option");
  261. current_path->connect("text_entered", this, "_navigate_to_path");
  262. always_show_folders = bool(EditorSettings::get_singleton()->get("docks/filesystem/always_show_folders"));
  263. set_file_list_display_mode(FileSystemDock::FILE_LIST_DISPLAY_LIST);
  264. _update_display_mode();
  265. if (EditorFileSystem::get_singleton()->is_scanning()) {
  266. _set_scanning_mode();
  267. } else {
  268. _update_tree(Vector<String>(), true);
  269. }
  270. } break;
  271. case NOTIFICATION_PROCESS: {
  272. if (EditorFileSystem::get_singleton()->is_scanning()) {
  273. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
  274. }
  275. } break;
  276. case NOTIFICATION_EXIT_TREE: {
  277. } break;
  278. case NOTIFICATION_DRAG_BEGIN: {
  279. Dictionary dd = get_viewport()->gui_get_drag_data();
  280. if (tree->is_visible_in_tree() && dd.has("type")) {
  281. if ((String(dd["type"]) == "files") || (String(dd["type"]) == "files_and_dirs") || (String(dd["type"]) == "resource")) {
  282. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM | Tree::DROP_MODE_INBETWEEN);
  283. } else if ((String(dd["type"]) == "favorite")) {
  284. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  285. }
  286. }
  287. } break;
  288. case NOTIFICATION_DRAG_END: {
  289. tree->set_drop_mode_flags(0);
  290. } break;
  291. case NOTIFICATION_THEME_CHANGED: {
  292. if (is_visible_in_tree()) {
  293. _update_display_mode(true);
  294. }
  295. } break;
  296. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  297. // Update icons
  298. String ei = "EditorIcons";
  299. button_reload->set_icon(get_icon("Reload", ei));
  300. button_toggle_display_mode->set_icon(get_icon("Panels2", ei));
  301. button_hist_next->set_icon(get_icon("Forward", ei));
  302. button_hist_prev->set_icon(get_icon("Back", ei));
  303. if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
  304. button_file_list_display_mode->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  305. } else {
  306. button_file_list_display_mode->set_icon(get_icon("FileList", "EditorIcons"));
  307. }
  308. tree_search_box->set_right_icon(get_icon("Search", ei));
  309. tree_search_box->set_clear_button_enabled(true);
  310. file_list_search_box->set_right_icon(get_icon("Search", ei));
  311. file_list_search_box->set_clear_button_enabled(true);
  312. // Update always showfolders
  313. bool new_always_show_folders = bool(EditorSettings::get_singleton()->get("docks/filesystem/always_show_folders"));
  314. if (new_always_show_folders != always_show_folders) {
  315. always_show_folders = new_always_show_folders;
  316. _update_file_list(true);
  317. }
  318. // Change full tree mode
  319. _update_display_mode();
  320. } break;
  321. }
  322. }
  323. void FileSystemDock::_tree_multi_selected(Object *p_item, int p_column, bool p_selected) {
  324. // Update the import dock
  325. import_dock_needs_update = true;
  326. call_deferred("_update_import_dock");
  327. // Return if we don't select something new
  328. if (!p_selected)
  329. return;
  330. // Tree item selected
  331. TreeItem *selected = tree->get_selected();
  332. if (!selected)
  333. return;
  334. TreeItem *favorites_item = tree->get_root()->get_children();
  335. if (selected->get_parent() == favorites_item && !String(selected->get_metadata(0)).ends_with("/")) {
  336. // Go to the favorites if we click in the favorites and the path has changed
  337. path = "Favorites";
  338. } else {
  339. path = selected->get_metadata(0);
  340. // Note: the "Favorites" item also leads to this path
  341. }
  342. // Set the current path
  343. _set_current_path_text(path);
  344. _push_to_history();
  345. // Update the file list
  346. if (!updating_tree && display_mode == DISPLAY_MODE_SPLIT) {
  347. _update_file_list(false);
  348. }
  349. }
  350. String FileSystemDock::get_selected_path() const {
  351. if (path.ends_with("/"))
  352. return path;
  353. else
  354. return path.get_base_dir();
  355. }
  356. String FileSystemDock::get_current_path() const {
  357. return path;
  358. }
  359. void FileSystemDock::_set_current_path_text(const String &p_path) {
  360. if (p_path == "Favorites") {
  361. current_path->set_text(TTR("Favorites"));
  362. } else {
  363. current_path->set_text(path);
  364. }
  365. }
  366. void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_favorites) {
  367. if (p_path == "Favorites") {
  368. path = p_path;
  369. } else {
  370. String target_path = p_path;
  371. // If the path is a file, do not only go to the directory in the tree, also select the file in the file list.
  372. if (target_path.ends_with("/")) {
  373. target_path = target_path.substr(0, target_path.length() - 1);
  374. }
  375. DirAccess *dirAccess = DirAccess::open("res://");
  376. if (dirAccess->file_exists(p_path)) {
  377. path = target_path;
  378. } else if (dirAccess->dir_exists(p_path)) {
  379. path = target_path + "/";
  380. } else {
  381. ERR_EXPLAIN(vformat(TTR("Cannot navigate to '%s' as it has not been found in the file system!"), p_path));
  382. ERR_FAIL();
  383. }
  384. }
  385. _set_current_path_text(path);
  386. _push_to_history();
  387. _update_tree(_compute_uncollapsed_paths(), false, p_select_in_favorites);
  388. if (display_mode == DISPLAY_MODE_SPLIT) {
  389. _update_file_list(false);
  390. }
  391. String file_name = p_path.get_file();
  392. if (!file_name.empty()) {
  393. for (int i = 0; i < files->get_item_count(); i++) {
  394. if (files->get_item_text(i) == file_name) {
  395. files->select(i, true);
  396. files->ensure_current_is_visible();
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. void FileSystemDock::navigate_to_path(const String &p_path) {
  403. _navigate_to_path(p_path);
  404. }
  405. void FileSystemDock::_file_list_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, const Variant &p_udata) {
  406. if ((file_list_vb->is_visible_in_tree() || path == p_path.get_base_dir()) && p_preview.is_valid()) {
  407. Array uarr = p_udata;
  408. int idx = uarr[0];
  409. String file = uarr[1];
  410. if (idx < files->get_item_count() && files->get_item_text(idx) == file && files->get_item_metadata(idx) == p_path) {
  411. if (file_list_display_mode == FILE_LIST_DISPLAY_LIST) {
  412. if (p_small_preview.is_valid())
  413. files->set_item_icon(idx, p_small_preview);
  414. } else {
  415. files->set_item_icon(idx, p_preview);
  416. }
  417. }
  418. }
  419. }
  420. void FileSystemDock::_tree_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, const Variant &p_udata) {
  421. if (p_small_preview.is_valid()) {
  422. Array uarr = p_udata;
  423. if (tree_update_id == (int)uarr[0]) {
  424. TreeItem *file_item = Object::cast_to<TreeItem>(uarr[1]);
  425. if (file_item) {
  426. file_item->set_icon(0, p_small_preview);
  427. }
  428. }
  429. }
  430. }
  431. void FileSystemDock::_toggle_file_display() {
  432. _set_file_display(file_list_display_mode != FILE_LIST_DISPLAY_LIST);
  433. emit_signal("display_mode_changed");
  434. }
  435. void FileSystemDock::_set_file_display(bool p_active) {
  436. if (p_active) {
  437. file_list_display_mode = FILE_LIST_DISPLAY_LIST;
  438. button_file_list_display_mode->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  439. button_file_list_display_mode->set_tooltip(TTR("View items as a grid of thumbnails."));
  440. } else {
  441. file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
  442. button_file_list_display_mode->set_icon(get_icon("FileList", "EditorIcons"));
  443. button_file_list_display_mode->set_tooltip(TTR("View items as a list."));
  444. }
  445. _update_file_list(true);
  446. }
  447. void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *matches, int p_max_items) {
  448. if (matches->size() > p_max_items)
  449. return;
  450. for (int i = 0; i < p_path->get_subdir_count(); i++) {
  451. _search(p_path->get_subdir(i), matches, p_max_items);
  452. }
  453. for (int i = 0; i < p_path->get_file_count(); i++) {
  454. String file = p_path->get_file(i);
  455. if (file.to_lower().find(searched_string) != -1) {
  456. FileInfo fi;
  457. fi.name = file;
  458. fi.type = p_path->get_file_type(i);
  459. fi.path = p_path->get_file_path(i);
  460. fi.import_broken = !p_path->get_file_import_is_valid(i);
  461. fi.import_status = 0;
  462. matches->push_back(fi);
  463. if (matches->size() > p_max_items)
  464. return;
  465. }
  466. }
  467. }
  468. void FileSystemDock::_update_file_list(bool p_keep_selection) {
  469. // Register the previously selected items
  470. Set<String> cselection;
  471. if (p_keep_selection) {
  472. for (int i = 0; i < files->get_item_count(); i++) {
  473. if (files->is_selected(i))
  474. cselection.insert(files->get_item_text(i));
  475. }
  476. }
  477. files->clear();
  478. _set_current_path_text(path);
  479. String directory = path;
  480. String file = "";
  481. String ei = "EditorIcons";
  482. int thumbnail_size = EditorSettings::get_singleton()->get("docks/filesystem/thumbnail_size");
  483. thumbnail_size *= EDSCALE;
  484. Ref<Texture> folder_thumbnail;
  485. Ref<Texture> file_thumbnail;
  486. Ref<Texture> file_thumbnail_broken;
  487. bool use_thumbnails = (file_list_display_mode == FILE_LIST_DISPLAY_THUMBNAILS);
  488. if (use_thumbnails) {
  489. // Thumbnails mode
  490. files->set_max_columns(0);
  491. files->set_icon_mode(ItemList::ICON_MODE_TOP);
  492. files->set_fixed_column_width(thumbnail_size * 3 / 2);
  493. files->set_max_text_lines(2);
  494. files->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  495. if (thumbnail_size < 64) {
  496. folder_thumbnail = get_icon("FolderMediumThumb", ei);
  497. file_thumbnail = get_icon("FileMediumThumb", ei);
  498. file_thumbnail_broken = get_icon("FileDeadMediumThumb", ei);
  499. } else {
  500. folder_thumbnail = get_icon("FolderBigThumb", ei);
  501. file_thumbnail = get_icon("FileBigThumb", ei);
  502. file_thumbnail_broken = get_icon("FileDeadBigThumb", ei);
  503. }
  504. } else {
  505. // No thumbnails
  506. files->set_icon_mode(ItemList::ICON_MODE_LEFT);
  507. files->set_max_columns(1);
  508. files->set_max_text_lines(1);
  509. files->set_fixed_column_width(0);
  510. files->set_fixed_icon_size(Size2());
  511. }
  512. Ref<Texture> folder_icon = (use_thumbnails) ? folder_thumbnail : get_icon("folder", "FileDialog");
  513. // Build the FileInfo list
  514. List<FileInfo> filelist;
  515. if (path == "Favorites") {
  516. // Display the favorites
  517. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  518. for (int i = 0; i < favorites.size(); i++) {
  519. String favorite = favorites[i];
  520. String text;
  521. Ref<Texture> icon;
  522. if (favorite == "res://") {
  523. text = "/";
  524. icon = folder_icon;
  525. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  526. files->add_item(text, icon, true);
  527. files->set_item_metadata(files->get_item_count() - 1, favorite);
  528. }
  529. } else if (favorite.ends_with("/")) {
  530. text = favorite.substr(0, favorite.length() - 1).get_file();
  531. icon = folder_icon;
  532. if (searched_string.length() == 0 || text.to_lower().find(searched_string) >= 0) {
  533. files->add_item(text, icon, true);
  534. files->set_item_metadata(files->get_item_count() - 1, favorite);
  535. }
  536. } else {
  537. int index;
  538. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->find_file(favorite, &index);
  539. FileInfo fi;
  540. fi.name = favorite.get_file();
  541. fi.path = favorite;
  542. if (efd) {
  543. fi.type = efd->get_file_type(index);
  544. fi.import_broken = !efd->get_file_import_is_valid(index);
  545. } else {
  546. fi.type = "";
  547. fi.import_broken = true;
  548. }
  549. fi.import_status = 0;
  550. if (searched_string.length() == 0 || fi.name.to_lower().find(searched_string) >= 0) {
  551. filelist.push_back(fi);
  552. }
  553. }
  554. }
  555. } else {
  556. // Get infos on the directory + file
  557. if (directory.ends_with("/") && directory != "res://") {
  558. directory = directory.substr(0, directory.length() - 1);
  559. }
  560. EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
  561. if (!efd) {
  562. directory = path.get_base_dir();
  563. file = path.get_file();
  564. efd = EditorFileSystem::get_singleton()->get_filesystem_path(directory);
  565. }
  566. if (!efd)
  567. return;
  568. if (searched_string.length() > 0) {
  569. // Display the search results
  570. _search(EditorFileSystem::get_singleton()->get_filesystem(), &filelist, 128);
  571. } else {
  572. if (display_mode == DISPLAY_MODE_TREE_ONLY || always_show_folders) {
  573. // Display folders in the list
  574. if (directory != "res://") {
  575. files->add_item("..", folder_icon, true);
  576. String bd = directory.get_base_dir();
  577. if (bd != "res://" && !bd.ends_with("/"))
  578. bd += "/";
  579. files->set_item_metadata(files->get_item_count() - 1, bd);
  580. files->set_item_selectable(files->get_item_count() - 1, false);
  581. }
  582. for (int i = 0; i < efd->get_subdir_count(); i++) {
  583. String dname = efd->get_subdir(i)->get_name();
  584. files->add_item(dname, folder_icon, true);
  585. files->set_item_metadata(files->get_item_count() - 1, directory.plus_file(dname) + "/");
  586. if (cselection.has(dname)) {
  587. files->select(files->get_item_count() - 1, false);
  588. }
  589. }
  590. }
  591. // Display the folder content
  592. for (int i = 0; i < efd->get_file_count(); i++) {
  593. FileInfo fi;
  594. fi.name = efd->get_file(i);
  595. fi.path = directory.plus_file(fi.name);
  596. fi.type = efd->get_file_type(i);
  597. fi.import_broken = !efd->get_file_import_is_valid(i);
  598. fi.import_status = 0;
  599. filelist.push_back(fi);
  600. }
  601. }
  602. filelist.sort();
  603. }
  604. // Fills the ItemList control node from the FileInfos
  605. String oi = "Object";
  606. for (List<FileInfo>::Element *E = filelist.front(); E; E = E->next()) {
  607. FileInfo *finfo = &(E->get());
  608. String fname = finfo->name;
  609. String fpath = finfo->path;
  610. String ftype = finfo->type;
  611. Ref<Texture> type_icon;
  612. Ref<Texture> big_icon;
  613. String tooltip = fname;
  614. // Select the icons
  615. if (!finfo->import_broken) {
  616. type_icon = (has_icon(ftype, ei)) ? get_icon(ftype, ei) : get_icon(oi, ei);
  617. big_icon = file_thumbnail;
  618. } else {
  619. type_icon = get_icon("ImportFail", ei);
  620. big_icon = file_thumbnail_broken;
  621. tooltip += "\n" + TTR("Status: Import of file failed. Please fix file and reimport manually.");
  622. }
  623. // Add the item to the ItemList
  624. int item_index;
  625. if (use_thumbnails) {
  626. files->add_item(fname, big_icon, true);
  627. item_index = files->get_item_count() - 1;
  628. files->set_item_metadata(item_index, fpath);
  629. files->set_item_tag_icon(item_index, type_icon);
  630. } else {
  631. files->add_item(fname, type_icon, true);
  632. item_index = files->get_item_count() - 1;
  633. files->set_item_metadata(item_index, fpath);
  634. }
  635. // Generate the preview
  636. if (!finfo->import_broken) {
  637. Array udata;
  638. udata.resize(2);
  639. udata[0] = item_index;
  640. udata[1] = fname;
  641. EditorResourcePreview::get_singleton()->queue_resource_preview(fpath, this, "_file_list_thumbnail_done", udata);
  642. }
  643. // Select the items
  644. if (cselection.has(fname))
  645. files->select(item_index, false);
  646. if (!p_keep_selection && file != "" && fname == file) {
  647. files->select(item_index, true);
  648. files->ensure_current_is_visible();
  649. }
  650. // Tooltip
  651. if (finfo->sources.size()) {
  652. for (int j = 0; j < finfo->sources.size(); j++) {
  653. tooltip += "\nSource: " + finfo->sources[j];
  654. }
  655. }
  656. files->set_item_tooltip(item_index, tooltip);
  657. }
  658. }
  659. void FileSystemDock::_select_file(const String p_path, bool p_select_in_favorites) {
  660. String fpath = p_path;
  661. if (fpath.ends_with("/")) {
  662. if (fpath != "res://") {
  663. fpath = fpath.substr(0, fpath.length() - 1);
  664. }
  665. } else if (fpath != "Favorites") {
  666. if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
  667. editor->open_request(fpath);
  668. } else {
  669. editor->load_resource(fpath);
  670. }
  671. }
  672. _navigate_to_path(fpath, p_select_in_favorites);
  673. }
  674. void FileSystemDock::_tree_activate_file() {
  675. TreeItem *selected = tree->get_selected();
  676. if (selected) {
  677. String path = selected->get_metadata(0);
  678. TreeItem *parent = selected->get_parent();
  679. bool is_favorite = parent != NULL && parent->get_metadata(0) == "Favorites";
  680. if ((!is_favorite && path.ends_with("/")) || path == "Favorites") {
  681. bool collapsed = selected->is_collapsed();
  682. selected->set_collapsed(!collapsed);
  683. } else {
  684. _select_file(path, is_favorite && !path.ends_with("/"));
  685. }
  686. }
  687. }
  688. void FileSystemDock::_file_list_activate_file(int p_idx) {
  689. _select_file(files->get_item_metadata(p_idx));
  690. }
  691. void FileSystemDock::_preview_invalidated(const String &p_path) {
  692. if (file_list_display_mode == FILE_LIST_DISPLAY_THUMBNAILS && p_path.get_base_dir() == path && searched_string.length() == 0 && file_list_vb->is_visible_in_tree()) {
  693. for (int i = 0; i < files->get_item_count(); i++) {
  694. if (files->get_item_metadata(i) == p_path) {
  695. //re-request preview
  696. Array udata;
  697. udata.resize(2);
  698. udata[0] = i;
  699. udata[1] = files->get_item_text(i);
  700. EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, this, "_file_list_thumbnail_done", udata);
  701. break;
  702. }
  703. }
  704. }
  705. }
  706. void FileSystemDock::_fs_changed() {
  707. button_hist_prev->set_disabled(history_pos == 0);
  708. button_hist_next->set_disabled(history_pos == history.size() - 1);
  709. scanning_vb->hide();
  710. split_box->show();
  711. if (tree->is_visible()) {
  712. _update_tree(_compute_uncollapsed_paths());
  713. }
  714. if (file_list_vb->is_visible()) {
  715. _update_file_list(true);
  716. }
  717. set_process(false);
  718. }
  719. void FileSystemDock::_set_scanning_mode() {
  720. button_hist_prev->set_disabled(true);
  721. button_hist_next->set_disabled(true);
  722. split_box->hide();
  723. scanning_vb->show();
  724. set_process(true);
  725. if (EditorFileSystem::get_singleton()->is_scanning()) {
  726. scanning_progress->set_value(EditorFileSystem::get_singleton()->get_scanning_progress() * 100);
  727. } else {
  728. scanning_progress->set_value(0);
  729. }
  730. }
  731. void FileSystemDock::_fw_history() {
  732. if (history_pos < history.size() - 1)
  733. history_pos++;
  734. _update_history();
  735. }
  736. void FileSystemDock::_bw_history() {
  737. if (history_pos > 0)
  738. history_pos--;
  739. _update_history();
  740. }
  741. void FileSystemDock::_update_history() {
  742. path = history[history_pos];
  743. _set_current_path_text(path);
  744. if (tree->is_visible()) {
  745. _update_tree(_compute_uncollapsed_paths());
  746. tree->grab_focus();
  747. tree->ensure_cursor_is_visible();
  748. }
  749. if (file_list_vb->is_visible()) {
  750. _update_file_list(false);
  751. }
  752. button_hist_prev->set_disabled(history_pos == 0);
  753. button_hist_next->set_disabled(history_pos == history.size() - 1);
  754. }
  755. void FileSystemDock::_push_to_history() {
  756. if (history[history_pos] != path) {
  757. history.resize(history_pos + 1);
  758. history.push_back(path);
  759. history_pos++;
  760. if (history.size() > history_max_size) {
  761. history.remove(0);
  762. history_pos = history_max_size - 1;
  763. }
  764. }
  765. button_hist_prev->set_disabled(history_pos == 0);
  766. button_hist_next->set_disabled(history_pos == history.size() - 1);
  767. }
  768. void FileSystemDock::_get_all_items_in_dir(EditorFileSystemDirectory *efsd, Vector<String> &files, Vector<String> &folders) const {
  769. if (efsd == NULL)
  770. return;
  771. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  772. folders.push_back(efsd->get_subdir(i)->get_path());
  773. _get_all_items_in_dir(efsd->get_subdir(i), files, folders);
  774. }
  775. for (int i = 0; i < efsd->get_file_count(); i++) {
  776. files.push_back(efsd->get_file_path(i));
  777. }
  778. }
  779. void FileSystemDock::_find_remaps(EditorFileSystemDirectory *efsd, const Map<String, String> &renames, Vector<String> &to_remaps) const {
  780. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  781. _find_remaps(efsd->get_subdir(i), renames, to_remaps);
  782. }
  783. for (int i = 0; i < efsd->get_file_count(); i++) {
  784. Vector<String> deps = efsd->get_file_deps(i);
  785. for (int j = 0; j < deps.size(); j++) {
  786. if (renames.has(deps[j])) {
  787. to_remaps.push_back(efsd->get_file_path(i));
  788. break;
  789. }
  790. }
  791. }
  792. }
  793. void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_new_path,
  794. Map<String, String> &p_file_renames, Map<String, String> &p_folder_renames) {
  795. //Ensure folder paths end with "/"
  796. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  797. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  798. if (new_path == old_path) {
  799. return;
  800. } else if (old_path == "res://") {
  801. EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
  802. return;
  803. } else if (!p_item.is_file && new_path.begins_with(old_path)) {
  804. //This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/"
  805. EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
  806. return;
  807. }
  808. //Build a list of files which will have new paths as a result of this operation
  809. Vector<String> file_changed_paths;
  810. Vector<String> folder_changed_paths;
  811. if (p_item.is_file) {
  812. file_changed_paths.push_back(old_path);
  813. } else {
  814. folder_changed_paths.push_back(old_path);
  815. _get_all_items_in_dir(EditorFileSystem::get_singleton()->get_filesystem_path(old_path), file_changed_paths, folder_changed_paths);
  816. }
  817. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  818. print_verbose("Moving " + old_path + " -> " + new_path);
  819. Error err = da->rename(old_path, new_path);
  820. if (err == OK) {
  821. //Move/Rename any corresponding import settings too
  822. if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
  823. err = da->rename(old_path + ".import", new_path + ".import");
  824. if (err != OK) {
  825. EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + ".import\n");
  826. }
  827. }
  828. // update scene if it is open
  829. for (int i = 0; i < file_changed_paths.size(); ++i) {
  830. String new_item_path = p_item.is_file ? new_path : file_changed_paths[i].replace_first(old_path, new_path);
  831. if (ResourceLoader::get_resource_type(new_item_path) == "PackedScene" && editor->is_scene_open(file_changed_paths[i])) {
  832. EditorData *ed = &editor->get_editor_data();
  833. for (int j = 0; j < ed->get_edited_scene_count(); j++) {
  834. if (ed->get_scene_path(j) == file_changed_paths[i]) {
  835. ed->get_edited_scene_root(j)->set_filename(new_item_path);
  836. editor->save_layout();
  837. break;
  838. }
  839. }
  840. }
  841. }
  842. //Only treat as a changed dependency if it was successfully moved
  843. for (int i = 0; i < file_changed_paths.size(); ++i) {
  844. p_file_renames[file_changed_paths[i]] = file_changed_paths[i].replace_first(old_path, new_path);
  845. print_verbose(" Remap: " + file_changed_paths[i] + " -> " + p_file_renames[file_changed_paths[i]]);
  846. emit_signal("files_moved", file_changed_paths[i], p_file_renames[file_changed_paths[i]]);
  847. }
  848. for (int i = 0; i < folder_changed_paths.size(); ++i) {
  849. p_folder_renames[folder_changed_paths[i]] = folder_changed_paths[i].replace_first(old_path, new_path);
  850. emit_signal("folder_moved", folder_changed_paths[i], p_folder_renames[folder_changed_paths[i]].substr(0, p_folder_renames[folder_changed_paths[i]].length() - 1));
  851. }
  852. } else {
  853. EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + "\n");
  854. }
  855. memdelete(da);
  856. }
  857. void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const {
  858. //Ensure folder paths end with "/"
  859. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  860. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  861. if (new_path == old_path) {
  862. return;
  863. } else if (old_path == "res://") {
  864. EditorNode::get_singleton()->add_io_error(TTR("Cannot move/rename resources root."));
  865. return;
  866. } else if (!p_item.is_file && new_path.begins_with(old_path)) {
  867. //This check doesn't erroneously catch renaming to a longer name as folder paths always end with "/"
  868. EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
  869. return;
  870. }
  871. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  872. print_verbose("Duplicating " + old_path + " -> " + new_path);
  873. Error err = p_item.is_file ? da->copy(old_path, new_path) : da->copy_dir(old_path, new_path);
  874. if (err == OK) {
  875. //Move/Rename any corresponding import settings too
  876. if (p_item.is_file && FileAccess::exists(old_path + ".import")) {
  877. err = da->copy(old_path + ".import", new_path + ".import");
  878. if (err != OK) {
  879. EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + ".import\n");
  880. }
  881. }
  882. } else {
  883. EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + "\n");
  884. }
  885. memdelete(da);
  886. }
  887. void FileSystemDock::_update_resource_paths_after_move(const Map<String, String> &p_renames) const {
  888. //Rename all resources loaded, be it subresources or actual resources
  889. List<Ref<Resource> > cached;
  890. ResourceCache::get_cached_resources(&cached);
  891. for (List<Ref<Resource> >::Element *E = cached.front(); E; E = E->next()) {
  892. Ref<Resource> r = E->get();
  893. String base_path = r->get_path();
  894. String extra_path;
  895. int sep_pos = r->get_path().find("::");
  896. if (sep_pos >= 0) {
  897. extra_path = base_path.substr(sep_pos, base_path.length());
  898. base_path = base_path.substr(0, sep_pos);
  899. }
  900. if (p_renames.has(base_path)) {
  901. base_path = p_renames[base_path];
  902. }
  903. r->set_path(base_path + extra_path);
  904. }
  905. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
  906. String path;
  907. if (i == EditorNode::get_editor_data().get_edited_scene()) {
  908. if (!get_tree()->get_edited_scene_root())
  909. continue;
  910. path = get_tree()->get_edited_scene_root()->get_filename();
  911. } else {
  912. path = EditorNode::get_editor_data().get_scene_path(i);
  913. }
  914. if (p_renames.has(path)) {
  915. path = p_renames[path];
  916. }
  917. if (i == EditorNode::get_editor_data().get_edited_scene()) {
  918. get_tree()->get_edited_scene_root()->set_filename(path);
  919. } else {
  920. EditorNode::get_editor_data().set_scene_path(i, path);
  921. }
  922. }
  923. }
  924. void FileSystemDock::_update_dependencies_after_move(const Map<String, String> &p_renames) const {
  925. //The following code assumes that the following holds:
  926. // 1) EditorFileSystem contains the old paths/folder structure from before the rename/move.
  927. // 2) ResourceLoader can use the new paths without needing to call rescan.
  928. Vector<String> remaps;
  929. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(), p_renames, remaps);
  930. for (int i = 0; i < remaps.size(); ++i) {
  931. //Because we haven't called a rescan yet the found remap might still be an old path itself.
  932. String file = p_renames.has(remaps[i]) ? p_renames[remaps[i]] : remaps[i];
  933. print_verbose("Remapping dependencies for: " + file);
  934. Error err = ResourceLoader::rename_dependencies(file, p_renames);
  935. if (err == OK) {
  936. if (ResourceLoader::get_resource_type(file) == "PackedScene")
  937. editor->reload_scene(file);
  938. } else {
  939. EditorNode::get_singleton()->add_io_error(TTR("Unable to update dependencies:") + "\n" + remaps[i] + "\n");
  940. }
  941. }
  942. }
  943. void FileSystemDock::_update_project_settings_after_move(const Map<String, String> &p_renames) const {
  944. // Find all project settings of type FILE and replace them if needed
  945. const Map<StringName, PropertyInfo> prop_info = ProjectSettings::get_singleton()->get_custom_property_info();
  946. for (const Map<StringName, PropertyInfo>::Element *E = prop_info.front(); E; E = E->next()) {
  947. if (E->get().hint == PROPERTY_HINT_FILE) {
  948. String old_path = GLOBAL_GET(E->key());
  949. if (p_renames.has(old_path)) {
  950. ProjectSettings::get_singleton()->set_setting(E->key(), p_renames[old_path]);
  951. }
  952. };
  953. }
  954. // Also search for the file in autoload, as they are stored differently from normal files.
  955. List<PropertyInfo> property_list;
  956. ProjectSettings::get_singleton()->get_property_list(&property_list);
  957. for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  958. if (E->get().name.begins_with("autoload/")) {
  959. // If the autoload resource paths has a leading "*", it indicates that it is a Singleton,
  960. // so we have to handle both cases when updating.
  961. String autoload = GLOBAL_GET(E->get().name);
  962. String autoload_singleton = autoload.substr(1, autoload.length());
  963. if (p_renames.has(autoload)) {
  964. ProjectSettings::get_singleton()->set_setting(E->get().name, p_renames[autoload]);
  965. } else if (autoload.begins_with("*") && p_renames.has(autoload_singleton)) {
  966. ProjectSettings::get_singleton()->set_setting(E->get().name, "*" + p_renames[autoload_singleton]);
  967. }
  968. }
  969. }
  970. ProjectSettings::get_singleton()->save();
  971. }
  972. void FileSystemDock::_update_favorites_list_after_move(const Map<String, String> &p_files_renames, const Map<String, String> &p_folders_renames) const {
  973. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  974. Vector<String> new_favorites;
  975. for (int i = 0; i < favorites.size(); i++) {
  976. String old_path = favorites[i];
  977. if (p_folders_renames.has(old_path)) {
  978. new_favorites.push_back(p_folders_renames[old_path]);
  979. } else if (p_files_renames.has(old_path)) {
  980. new_favorites.push_back(p_files_renames[old_path]);
  981. } else {
  982. new_favorites.push_back(old_path);
  983. }
  984. }
  985. EditorSettings::get_singleton()->set_favorites(new_favorites);
  986. }
  987. void FileSystemDock::_save_scenes_after_move(const Map<String, String> &p_renames) const {
  988. Vector<String> remaps;
  989. _find_remaps(EditorFileSystem::get_singleton()->get_filesystem(), p_renames, remaps);
  990. Vector<String> new_filenames;
  991. for (int i = 0; i < remaps.size(); ++i) {
  992. String file = p_renames.has(remaps[i]) ? p_renames[remaps[i]] : remaps[i];
  993. if (ResourceLoader::get_resource_type(file) == "PackedScene") {
  994. new_filenames.push_back(file);
  995. }
  996. }
  997. editor->save_scene_list(new_filenames);
  998. }
  999. void FileSystemDock::_make_dir_confirm() {
  1000. String dir_name = make_dir_dialog_text->get_text().strip_edges();
  1001. if (dir_name.length() == 0) {
  1002. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1003. return;
  1004. } else if (dir_name.find("/") != -1 || dir_name.find("\\") != -1 || dir_name.find(":") != -1 || dir_name.find("*") != -1 ||
  1005. dir_name.find("|") != -1 || dir_name.find(">") != -1 || dir_name.ends_with(".") || dir_name.ends_with(" ")) {
  1006. EditorNode::get_singleton()->show_warning(TTR("Provided name contains invalid characters"));
  1007. return;
  1008. }
  1009. String directory = path;
  1010. if (!directory.ends_with("/")) {
  1011. directory = directory.get_base_dir();
  1012. }
  1013. print_verbose("Making folder " + dir_name + " in " + directory);
  1014. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1015. Error err = da->change_dir(directory);
  1016. if (err == OK) {
  1017. err = da->make_dir(dir_name);
  1018. }
  1019. memdelete(da);
  1020. if (err == OK) {
  1021. print_verbose("FileSystem: calling rescan.");
  1022. _rescan();
  1023. } else {
  1024. EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
  1025. }
  1026. }
  1027. void FileSystemDock::_file_deleted(String p_file) {
  1028. emit_signal("file_deleted", p_file);
  1029. }
  1030. void FileSystemDock::_folder_deleted(String p_folder) {
  1031. emit_signal("folder_deleted", p_folder);
  1032. }
  1033. void FileSystemDock::_rename_operation_confirm() {
  1034. String new_name = rename_dialog_text->get_text().strip_edges();
  1035. if (new_name.length() == 0) {
  1036. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1037. return;
  1038. } else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
  1039. EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
  1040. return;
  1041. }
  1042. String old_path = to_rename.path.ends_with("/") ? to_rename.path.substr(0, to_rename.path.length() - 1) : to_rename.path;
  1043. String new_path = old_path.get_base_dir().plus_file(new_name);
  1044. if (old_path == new_path) {
  1045. return;
  1046. }
  1047. //Present a more user friendly warning for name conflict
  1048. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1049. #if defined(WINDOWS_ENABLED) || defined(UWP_ENABLED)
  1050. // Workaround case insensitivity on Windows
  1051. if ((da->file_exists(new_path) || da->dir_exists(new_path)) && new_path.to_lower() != old_path.to_lower()) {
  1052. #else
  1053. if (da->file_exists(new_path) || da->dir_exists(new_path)) {
  1054. #endif
  1055. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  1056. memdelete(da);
  1057. return;
  1058. }
  1059. memdelete(da);
  1060. Map<String, String> file_renames;
  1061. Map<String, String> folder_renames;
  1062. _try_move_item(to_rename, new_path, file_renames, folder_renames);
  1063. int current_tab = editor->get_current_tab();
  1064. _update_dependencies_after_move(file_renames);
  1065. _update_resource_paths_after_move(file_renames);
  1066. _update_project_settings_after_move(file_renames);
  1067. _update_favorites_list_after_move(file_renames, folder_renames);
  1068. editor->set_current_tab(current_tab);
  1069. print_verbose("FileSystem: calling rescan.");
  1070. _rescan();
  1071. print_verbose("FileSystem: saving moved scenes.");
  1072. _save_scenes_after_move(file_renames);
  1073. }
  1074. void FileSystemDock::_duplicate_operation_confirm() {
  1075. String new_name = duplicate_dialog_text->get_text().strip_edges();
  1076. if (new_name.length() == 0) {
  1077. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  1078. return;
  1079. } else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
  1080. EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
  1081. return;
  1082. }
  1083. String new_path;
  1084. String base_dir = to_duplicate.path.get_base_dir();
  1085. if (to_duplicate.is_file) {
  1086. new_path = base_dir.plus_file(new_name);
  1087. } else {
  1088. new_path = base_dir.substr(0, base_dir.find_last("/")) + "/" + new_name;
  1089. }
  1090. //Present a more user friendly warning for name conflict
  1091. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1092. if (da->file_exists(new_path) || da->dir_exists(new_path)) {
  1093. EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists."));
  1094. memdelete(da);
  1095. return;
  1096. }
  1097. memdelete(da);
  1098. _try_duplicate_item(to_duplicate, new_path);
  1099. //Rescan everything
  1100. print_verbose("FileSystem: calling rescan.");
  1101. _rescan();
  1102. }
  1103. void FileSystemDock::_move_with_overwrite() {
  1104. _move_operation_confirm(to_move_path, true);
  1105. }
  1106. bool FileSystemDock::_check_existing() {
  1107. String &p_to_path = to_move_path;
  1108. for (int i = 0; i < to_move.size(); i++) {
  1109. String ol_pth = to_move[i].path.ends_with("/") ? to_move[i].path.substr(0, to_move[i].path.length() - 1) : to_move[i].path;
  1110. String p_new_path = p_to_path.plus_file(ol_pth.get_file());
  1111. FileOrFolder p_item = to_move[i];
  1112. String old_path = (p_item.is_file || p_item.path.ends_with("/")) ? p_item.path : (p_item.path + "/");
  1113. String new_path = (p_item.is_file || p_new_path.ends_with("/")) ? p_new_path : (p_new_path + "/");
  1114. if (p_item.is_file && FileAccess::exists(new_path)) {
  1115. return false;
  1116. } else if (!p_item.is_file && DirAccess::exists(new_path)) {
  1117. return false;
  1118. }
  1119. }
  1120. return true;
  1121. }
  1122. void FileSystemDock::_move_operation_confirm(const String &p_to_path, bool overwrite) {
  1123. if (!overwrite) {
  1124. to_move_path = p_to_path;
  1125. bool can_move = _check_existing();
  1126. if (!can_move) {
  1127. //ask to do something
  1128. overwrite_dialog->popup_centered_minsize();
  1129. overwrite_dialog->grab_focus();
  1130. return;
  1131. }
  1132. }
  1133. Map<String, String> file_renames;
  1134. Map<String, String> folder_renames;
  1135. bool is_moved = false;
  1136. for (int i = 0; i < to_move.size(); i++) {
  1137. String old_path = to_move[i].path.ends_with("/") ? to_move[i].path.substr(0, to_move[i].path.length() - 1) : to_move[i].path;
  1138. String new_path = p_to_path.plus_file(old_path.get_file());
  1139. if (old_path != new_path) {
  1140. _try_move_item(to_move[i], new_path, file_renames, folder_renames);
  1141. is_moved = true;
  1142. }
  1143. }
  1144. if (is_moved) {
  1145. int current_tab = editor->get_current_tab();
  1146. _update_dependencies_after_move(file_renames);
  1147. _update_resource_paths_after_move(file_renames);
  1148. _update_project_settings_after_move(file_renames);
  1149. _update_favorites_list_after_move(file_renames, folder_renames);
  1150. editor->set_current_tab(current_tab);
  1151. print_verbose("FileSystem: calling rescan.");
  1152. _rescan();
  1153. print_verbose("FileSystem: saving moved scenes.");
  1154. _save_scenes_after_move(file_renames);
  1155. }
  1156. }
  1157. Vector<String> FileSystemDock::_tree_get_selected(bool remove_self_inclusion) {
  1158. // Build a list of selected items with the active one at the first position
  1159. Vector<String> selected_strings;
  1160. TreeItem *favorites_item = tree->get_root()->get_children();
  1161. TreeItem *active_selected = tree->get_selected();
  1162. if (active_selected && active_selected != favorites_item) {
  1163. selected_strings.push_back(active_selected->get_metadata(0));
  1164. }
  1165. TreeItem *selected = tree->get_root();
  1166. selected = tree->get_next_selected(selected);
  1167. while (selected) {
  1168. if (selected != active_selected && selected != favorites_item) {
  1169. selected_strings.push_back(selected->get_metadata(0));
  1170. }
  1171. selected = tree->get_next_selected(selected);
  1172. }
  1173. // Remove paths or files that are included into another
  1174. if (remove_self_inclusion && selected_strings.size() > 1) {
  1175. selected_strings.sort_custom<NaturalNoCaseComparator>();
  1176. String last_path = "";
  1177. for (int i = 0; i < selected_strings.size(); i++) {
  1178. if (last_path != "" && selected_strings[i].begins_with(last_path)) {
  1179. selected_strings.remove(i);
  1180. i--;
  1181. }
  1182. if (selected_strings[i].ends_with("/")) {
  1183. last_path = selected_strings[i];
  1184. }
  1185. }
  1186. }
  1187. return selected_strings;
  1188. }
  1189. void FileSystemDock::_tree_rmb_option(int p_option) {
  1190. Vector<String> selected_strings = _tree_get_selected();
  1191. // Execute the current option
  1192. switch (p_option) {
  1193. case FOLDER_EXPAND_ALL:
  1194. case FOLDER_COLLAPSE_ALL: {
  1195. // Expand or collapse the folder
  1196. if (selected_strings.size() == 1) {
  1197. bool is_collapsed = (p_option == FOLDER_COLLAPSE_ALL);
  1198. Vector<TreeItem *> needs_check;
  1199. needs_check.push_back(tree->get_selected());
  1200. while (needs_check.size()) {
  1201. needs_check[0]->set_collapsed(is_collapsed);
  1202. TreeItem *child = needs_check[0]->get_children();
  1203. while (child) {
  1204. needs_check.push_back(child);
  1205. child = child->get_next();
  1206. }
  1207. needs_check.remove(0);
  1208. }
  1209. }
  1210. } break;
  1211. default: {
  1212. _file_option(p_option, selected_strings);
  1213. } break;
  1214. }
  1215. }
  1216. void FileSystemDock::_file_list_rmb_option(int p_option) {
  1217. Vector<int> selected_id = files->get_selected_items();
  1218. Vector<String> selected;
  1219. for (int i = 0; i < selected_id.size(); i++) {
  1220. selected.push_back(files->get_item_metadata(selected_id[i]));
  1221. }
  1222. _file_option(p_option, selected);
  1223. }
  1224. void FileSystemDock::_file_option(int p_option, const Vector<String> p_selected) {
  1225. // The first one should be the active item
  1226. switch (p_option) {
  1227. case FILE_SHOW_IN_EXPLORER: {
  1228. // Show the file / folder in the OS explorer
  1229. String fpath = path;
  1230. if (path == "Favorites") {
  1231. fpath = p_selected[0];
  1232. }
  1233. if (!fpath.ends_with("/")) {
  1234. fpath = fpath.get_base_dir();
  1235. }
  1236. String dir = ProjectSettings::get_singleton()->globalize_path(fpath);
  1237. OS::get_singleton()->shell_open(String("file://") + dir);
  1238. } break;
  1239. case FILE_OPEN: {
  1240. // Open the file
  1241. for (int i = 0; i < p_selected.size(); i++) {
  1242. _select_file(p_selected[i]);
  1243. }
  1244. } break;
  1245. case FILE_INSTANCE: {
  1246. // Instance all selected scenes
  1247. Vector<String> paths;
  1248. for (int i = 0; i < p_selected.size(); i++) {
  1249. String fpath = p_selected[i];
  1250. if (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene") {
  1251. paths.push_back(fpath);
  1252. }
  1253. }
  1254. if (!paths.empty()) {
  1255. emit_signal("instance", paths);
  1256. }
  1257. } break;
  1258. case FILE_ADD_FAVORITE: {
  1259. // Add the files from favorites
  1260. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1261. for (int i = 0; i < p_selected.size(); i++) {
  1262. if (favorites.find(p_selected[i]) == -1) {
  1263. favorites.push_back(p_selected[i]);
  1264. }
  1265. }
  1266. EditorSettings::get_singleton()->set_favorites(favorites);
  1267. _update_tree(_compute_uncollapsed_paths());
  1268. } break;
  1269. case FILE_REMOVE_FAVORITE: {
  1270. // Remove the files from favorites
  1271. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1272. for (int i = 0; i < p_selected.size(); i++) {
  1273. favorites.erase(p_selected[i]);
  1274. }
  1275. EditorSettings::get_singleton()->set_favorites(favorites);
  1276. _update_tree(_compute_uncollapsed_paths());
  1277. if (path == "Favorites")
  1278. _update_file_list(true);
  1279. } break;
  1280. case FILE_DEPENDENCIES: {
  1281. // Checkout the file dependencies
  1282. if (!p_selected.empty()) {
  1283. String fpath = p_selected[0];
  1284. deps_editor->edit(fpath);
  1285. }
  1286. } break;
  1287. case FILE_OWNERS: {
  1288. // Checkout the file owners
  1289. if (!p_selected.empty()) {
  1290. String fpath = p_selected[0];
  1291. owners_editor->show(fpath);
  1292. }
  1293. } break;
  1294. case FILE_MOVE: {
  1295. // Move the files to a given location
  1296. to_move.clear();
  1297. for (int i = 0; i < p_selected.size(); i++) {
  1298. String fpath = p_selected[i];
  1299. if (fpath != "res://") {
  1300. to_move.push_back(FileOrFolder(fpath, !fpath.ends_with("/")));
  1301. }
  1302. }
  1303. if (to_move.size() > 0) {
  1304. move_dialog->popup_centered_ratio();
  1305. }
  1306. } break;
  1307. case FILE_RENAME: {
  1308. // Rename the active file
  1309. if (!p_selected.empty()) {
  1310. to_rename.path = p_selected[0];
  1311. if (to_rename.path != "res://") {
  1312. to_rename.is_file = !to_rename.path.ends_with("/");
  1313. if (to_rename.is_file) {
  1314. String name = to_rename.path.get_file();
  1315. rename_dialog->set_title(TTR("Renaming file:") + " " + name);
  1316. rename_dialog_text->set_text(name);
  1317. rename_dialog_text->select(0, name.find_last("."));
  1318. } else {
  1319. String name = to_rename.path.substr(0, to_rename.path.length() - 1).get_file();
  1320. rename_dialog->set_title(TTR("Renaming folder:") + " " + name);
  1321. rename_dialog_text->set_text(name);
  1322. rename_dialog_text->select(0, name.length());
  1323. }
  1324. rename_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  1325. rename_dialog_text->grab_focus();
  1326. }
  1327. }
  1328. } break;
  1329. case FILE_REMOVE: {
  1330. // Remove the selected files
  1331. Vector<String> remove_files;
  1332. Vector<String> remove_folders;
  1333. for (int i = 0; i < p_selected.size(); i++) {
  1334. String fpath = p_selected[i];
  1335. if (fpath != "res://") {
  1336. if (fpath.ends_with("/")) {
  1337. remove_folders.push_back(fpath);
  1338. } else {
  1339. remove_files.push_back(fpath);
  1340. }
  1341. }
  1342. }
  1343. if (remove_files.size() + remove_folders.size() > 0) {
  1344. remove_dialog->show(remove_folders, remove_files);
  1345. }
  1346. } break;
  1347. case FILE_DUPLICATE: {
  1348. // Duplicate the selected files
  1349. for (int i = 0; i < p_selected.size(); i++) {
  1350. to_duplicate.path = p_selected[i];
  1351. to_duplicate.is_file = !to_duplicate.path.ends_with("/");
  1352. if (to_duplicate.is_file) {
  1353. String name = to_duplicate.path.get_file();
  1354. duplicate_dialog->set_title(TTR("Duplicating file:") + " " + name);
  1355. duplicate_dialog_text->set_text(name);
  1356. duplicate_dialog_text->select(0, name.find_last("."));
  1357. } else {
  1358. String name = to_duplicate.path.substr(0, to_duplicate.path.length() - 1).get_file();
  1359. duplicate_dialog->set_title(TTR("Duplicating folder:") + " " + name);
  1360. duplicate_dialog_text->set_text(name);
  1361. duplicate_dialog_text->select(0, name.length());
  1362. }
  1363. duplicate_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  1364. duplicate_dialog_text->grab_focus();
  1365. }
  1366. } break;
  1367. case FILE_INFO: {
  1368. } break;
  1369. case FILE_REIMPORT: {
  1370. // Reimport all selected files
  1371. Vector<String> reimport;
  1372. for (int i = 0; i < p_selected.size(); i++) {
  1373. reimport.push_back(p_selected[i]);
  1374. }
  1375. ERR_FAIL_COND(reimport.size() == 0);
  1376. } break;
  1377. case FILE_NEW_FOLDER: {
  1378. // Create a new folder
  1379. make_dir_dialog_text->set_text("new folder");
  1380. make_dir_dialog_text->select_all();
  1381. make_dir_dialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  1382. make_dir_dialog_text->grab_focus();
  1383. } break;
  1384. case FILE_NEW_SCRIPT: {
  1385. // Create a new script
  1386. String fpath = path;
  1387. if (!fpath.ends_with("/")) {
  1388. fpath = fpath.get_base_dir();
  1389. }
  1390. make_script_dialog_text->config("Node", fpath.plus_file("new_script.gd"), false);
  1391. make_script_dialog_text->popup_centered(Size2(300, 300) * EDSCALE);
  1392. } break;
  1393. case FILE_COPY_PATH: {
  1394. // Copy the file path
  1395. if (!p_selected.empty()) {
  1396. String fpath = p_selected[0];
  1397. OS::get_singleton()->set_clipboard(fpath);
  1398. }
  1399. } break;
  1400. case FILE_NEW_RESOURCE: {
  1401. // Create a new resource
  1402. new_resource_dialog->popup_create(true);
  1403. } break;
  1404. }
  1405. }
  1406. void FileSystemDock::_resource_created() const {
  1407. Object *c = new_resource_dialog->instance_selected();
  1408. ERR_FAIL_COND(!c);
  1409. Resource *r = Object::cast_to<Resource>(c);
  1410. ERR_FAIL_COND(!r);
  1411. REF res(r);
  1412. editor->push_item(c);
  1413. RES current_res = RES(r);
  1414. String fpath = path;
  1415. if (!fpath.ends_with("/")) {
  1416. fpath = fpath.get_base_dir();
  1417. }
  1418. editor->save_resource_as(current_res, fpath);
  1419. }
  1420. void FileSystemDock::_search_changed(const String &p_text, const Control *p_from) {
  1421. if (searched_string.length() == 0 && p_text.length() > 0) {
  1422. // Register the uncollapsed paths before they change
  1423. uncollapsed_paths_before_search = _compute_uncollapsed_paths();
  1424. }
  1425. searched_string = p_text.to_lower();
  1426. if (p_from == tree_search_box)
  1427. file_list_search_box->set_text(searched_string);
  1428. else // file_list_search_box
  1429. tree_search_box->set_text(searched_string);
  1430. switch (display_mode) {
  1431. case DISPLAY_MODE_TREE_ONLY: {
  1432. _update_tree(searched_string.length() == 0 ? uncollapsed_paths_before_search : Vector<String>());
  1433. } break;
  1434. case DISPLAY_MODE_SPLIT: {
  1435. _update_file_list(false);
  1436. _update_tree(searched_string.length() == 0 ? uncollapsed_paths_before_search : Vector<String>());
  1437. } break;
  1438. }
  1439. }
  1440. void FileSystemDock::_rescan() {
  1441. _set_scanning_mode();
  1442. EditorFileSystem::get_singleton()->scan();
  1443. }
  1444. void FileSystemDock::_toggle_split_mode(bool p_active) {
  1445. set_display_mode(p_active ? DISPLAY_MODE_SPLIT : DISPLAY_MODE_TREE_ONLY);
  1446. emit_signal("display_mode_changed");
  1447. }
  1448. void FileSystemDock::fix_dependencies(const String &p_for_file) {
  1449. deps_editor->edit(p_for_file);
  1450. }
  1451. void FileSystemDock::focus_on_filter() {
  1452. file_list_search_box->grab_focus();
  1453. }
  1454. void FileSystemDock::set_file_list_display_mode(FileListDisplayMode p_mode) {
  1455. if (p_mode == file_list_display_mode)
  1456. return;
  1457. _toggle_file_display();
  1458. }
  1459. Variant FileSystemDock::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1460. bool all_favorites = true;
  1461. bool all_not_favorites = true;
  1462. Vector<String> paths;
  1463. if (p_from == tree) {
  1464. // Check if the first selected is in favorite
  1465. TreeItem *selected = tree->get_next_selected(tree->get_root());
  1466. while (selected) {
  1467. TreeItem *favorites_item = tree->get_root()->get_children();
  1468. if (selected == favorites_item) {
  1469. // The "Favorites" item is not draggable
  1470. return Variant();
  1471. }
  1472. bool is_favorite = selected->get_parent() != NULL && tree->get_root()->get_children() == selected->get_parent();
  1473. all_favorites &= is_favorite;
  1474. all_not_favorites &= !is_favorite;
  1475. selected = tree->get_next_selected(selected);
  1476. }
  1477. if (all_favorites) {
  1478. paths = _tree_get_selected(false);
  1479. } else {
  1480. paths = _tree_get_selected();
  1481. }
  1482. } else if (p_from == files) {
  1483. for (int i = 0; i < files->get_item_count(); i++) {
  1484. if (files->is_selected(i)) {
  1485. paths.push_back(files->get_item_metadata(i));
  1486. }
  1487. }
  1488. all_favorites = false;
  1489. all_not_favorites = true;
  1490. }
  1491. if (paths.empty())
  1492. return Variant();
  1493. if (!all_favorites && !all_not_favorites)
  1494. return Variant();
  1495. Dictionary drag_data = EditorNode::get_singleton()->drag_files_and_dirs(paths, p_from);
  1496. if (all_favorites) {
  1497. drag_data["type"] = "favorite";
  1498. }
  1499. return drag_data;
  1500. }
  1501. bool FileSystemDock::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1502. Dictionary drag_data = p_data;
  1503. if (drag_data.has("type") && String(drag_data["type"]) == "favorite") {
  1504. // Moving favorite around
  1505. TreeItem *ti = tree->get_item_at_position(p_point);
  1506. if (!ti)
  1507. return false;
  1508. int drop_section = tree->get_drop_section_at_position(p_point);
  1509. TreeItem *favorites_item = tree->get_root()->get_children();
  1510. TreeItem *resources_item = favorites_item->get_next();
  1511. if (ti == favorites_item) {
  1512. return (drop_section == 1); // The parent, first fav
  1513. }
  1514. if (ti->get_parent() && favorites_item == ti->get_parent()) {
  1515. return true; // A favorite
  1516. }
  1517. if (ti == resources_item) {
  1518. return (drop_section == -1); // The tree, last fav
  1519. }
  1520. return false;
  1521. }
  1522. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1523. // Move resources
  1524. String to_dir;
  1525. bool favorite;
  1526. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1527. return !to_dir.empty();
  1528. }
  1529. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1530. // Move files or dir
  1531. String to_dir;
  1532. bool favorite;
  1533. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1534. if (favorite)
  1535. return true;
  1536. if (to_dir.empty())
  1537. return false;
  1538. //Attempting to move a folder into itself will fail later
  1539. //Rather than bring up a message don't try to do it in the first place
  1540. to_dir = to_dir.ends_with("/") ? to_dir : (to_dir + "/");
  1541. Vector<String> fnames = drag_data["files"];
  1542. for (int i = 0; i < fnames.size(); ++i) {
  1543. if (fnames[i].ends_with("/") && to_dir.begins_with(fnames[i]))
  1544. return false;
  1545. }
  1546. return true;
  1547. }
  1548. return false;
  1549. }
  1550. void FileSystemDock::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1551. if (!can_drop_data_fw(p_point, p_data, p_from))
  1552. return;
  1553. Dictionary drag_data = p_data;
  1554. Vector<String> dirs = EditorSettings::get_singleton()->get_favorites();
  1555. if (drag_data.has("type") && String(drag_data["type"]) == "favorite") {
  1556. // Moving favorite around
  1557. TreeItem *ti = tree->get_item_at_position(p_point);
  1558. if (!ti)
  1559. return;
  1560. int drop_section = tree->get_drop_section_at_position(p_point);
  1561. int drop_position;
  1562. Vector<String> files = drag_data["files"];
  1563. TreeItem *favorites_item = tree->get_root()->get_children();
  1564. TreeItem *resources_item = favorites_item->get_next();
  1565. if (ti == favorites_item) {
  1566. // Drop on the favorite folder
  1567. drop_position = 0;
  1568. } else if (ti == resources_item) {
  1569. // Drop on the resource item
  1570. drop_position = dirs.size();
  1571. } else {
  1572. // Drop in the list
  1573. drop_position = dirs.find(ti->get_metadata(0));
  1574. if (drop_section == 1) {
  1575. drop_position++;
  1576. }
  1577. }
  1578. // Remove dragged favorites
  1579. Vector<int> to_remove;
  1580. int offset = 0;
  1581. for (int i = 0; i < files.size(); i++) {
  1582. int to_remove_pos = dirs.find(files[i]);
  1583. to_remove.push_back(to_remove_pos);
  1584. if (to_remove_pos < drop_position) {
  1585. offset++;
  1586. }
  1587. }
  1588. drop_position -= offset;
  1589. to_remove.sort();
  1590. for (int i = 0; i < to_remove.size(); i++) {
  1591. dirs.remove(to_remove[i] - i);
  1592. }
  1593. // Re-add them at the right position
  1594. for (int i = 0; i < files.size(); i++) {
  1595. dirs.insert(drop_position, files[i]);
  1596. drop_position++;
  1597. }
  1598. EditorSettings::get_singleton()->set_favorites(dirs);
  1599. _update_tree(_compute_uncollapsed_paths());
  1600. if (display_mode == DISPLAY_MODE_SPLIT && path == "Favorites")
  1601. _update_file_list(true);
  1602. return;
  1603. }
  1604. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  1605. // Moving resource
  1606. Ref<Resource> res = drag_data["resource"];
  1607. String to_dir;
  1608. bool favorite;
  1609. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1610. if (res.is_valid() && !to_dir.empty()) {
  1611. EditorNode::get_singleton()->push_item(res.ptr());
  1612. EditorNode::get_singleton()->save_resource_as(res, to_dir);
  1613. }
  1614. }
  1615. if (drag_data.has("type") && (String(drag_data["type"]) == "files" || String(drag_data["type"]) == "files_and_dirs")) {
  1616. // Move files or add to favorites
  1617. String to_dir;
  1618. bool favorite;
  1619. _get_drag_target_folder(to_dir, favorite, p_point, p_from);
  1620. if (!to_dir.empty()) {
  1621. Vector<String> fnames = drag_data["files"];
  1622. to_move.clear();
  1623. for (int i = 0; i < fnames.size(); i++) {
  1624. to_move.push_back(FileOrFolder(fnames[i], !fnames[i].ends_with("/")));
  1625. }
  1626. _move_operation_confirm(to_dir);
  1627. } else if (favorite) {
  1628. // Add the files from favorites
  1629. Vector<String> fnames = drag_data["files"];
  1630. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1631. for (int i = 0; i < fnames.size(); i++) {
  1632. if (favorites.find(fnames[i]) == -1) {
  1633. favorites.push_back(fnames[i]);
  1634. }
  1635. }
  1636. EditorSettings::get_singleton()->set_favorites(favorites);
  1637. _update_tree(_compute_uncollapsed_paths());
  1638. }
  1639. }
  1640. }
  1641. void FileSystemDock::_get_drag_target_folder(String &target, bool &target_favorites, const Point2 &p_point, Control *p_from) const {
  1642. target = String();
  1643. target_favorites = false;
  1644. // In the file list
  1645. if (p_from == files) {
  1646. int pos = files->get_item_at_position(p_point, true);
  1647. if (pos == -1) {
  1648. return;
  1649. }
  1650. String ltarget = files->get_item_metadata(pos);
  1651. target = ltarget.ends_with("/") ? ltarget : path.get_base_dir();
  1652. return;
  1653. }
  1654. // In the tree
  1655. if (p_from == tree) {
  1656. TreeItem *ti = tree->get_item_at_position(p_point);
  1657. int section = tree->get_drop_section_at_position(p_point);
  1658. if (ti) {
  1659. // Check the favorites first
  1660. if (ti == tree->get_root()->get_children() && section >= 0) {
  1661. target_favorites = true;
  1662. return;
  1663. } else if (ti->get_parent() == tree->get_root()->get_children()) {
  1664. target_favorites = true;
  1665. return;
  1666. } else {
  1667. String fpath = ti->get_metadata(0);
  1668. if (section == 0) {
  1669. if (fpath.ends_with("/")) {
  1670. // We drop on a folder
  1671. target = fpath;
  1672. return;
  1673. }
  1674. } else {
  1675. if (ti->get_parent() != tree->get_root()->get_children()) {
  1676. // Not in the favorite section
  1677. if (fpath != "res://") {
  1678. // We drop between two files
  1679. if (fpath.ends_with("/")) {
  1680. fpath = fpath.substr(0, fpath.length() - 1);
  1681. }
  1682. target = fpath.get_base_dir();
  1683. return;
  1684. }
  1685. }
  1686. }
  1687. }
  1688. }
  1689. }
  1690. return;
  1691. }
  1692. void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<String> p_paths, bool p_display_path_dependent_options) {
  1693. // Add options for files and folders
  1694. ERR_FAIL_COND(p_paths.empty())
  1695. Vector<String> filenames;
  1696. Vector<String> foldernames;
  1697. Vector<String> favorites = EditorSettings::get_singleton()->get_favorites();
  1698. bool all_files = true;
  1699. bool all_files_scenes = true;
  1700. bool all_folders = true;
  1701. bool all_favorites = true;
  1702. bool all_not_favorites = true;
  1703. for (int i = 0; i < p_paths.size(); i++) {
  1704. String fpath = p_paths[i];
  1705. if (fpath.ends_with("/")) {
  1706. foldernames.push_back(fpath);
  1707. all_files = false;
  1708. } else {
  1709. filenames.push_back(fpath);
  1710. all_folders = false;
  1711. all_files_scenes &= (EditorFileSystem::get_singleton()->get_file_type(fpath) == "PackedScene");
  1712. }
  1713. // Check if in favorites
  1714. bool found = false;
  1715. for (int j = 0; j < favorites.size(); j++) {
  1716. if (favorites[j] == fpath) {
  1717. found = true;
  1718. break;
  1719. }
  1720. }
  1721. if (found) {
  1722. all_not_favorites = false;
  1723. } else {
  1724. all_favorites = false;
  1725. }
  1726. }
  1727. if (all_files) {
  1728. if (all_files_scenes && filenames.size() >= 1) {
  1729. p_popup->add_item(TTR("Open Scene(s)"), FILE_OPEN);
  1730. p_popup->add_item(TTR("Instance"), FILE_INSTANCE);
  1731. p_popup->add_separator();
  1732. }
  1733. if (!all_files_scenes && filenames.size() == 1) {
  1734. p_popup->add_item(TTR("Open"), FILE_OPEN);
  1735. p_popup->add_separator();
  1736. }
  1737. }
  1738. if (p_paths.size() >= 1) {
  1739. if (!all_favorites) {
  1740. p_popup->add_item(TTR("Add to favorites"), FILE_ADD_FAVORITE);
  1741. }
  1742. if (!all_not_favorites) {
  1743. p_popup->add_item(TTR("Remove from favorites"), FILE_REMOVE_FAVORITE);
  1744. }
  1745. p_popup->add_separator();
  1746. }
  1747. if (all_files) {
  1748. if (filenames.size() == 1) {
  1749. p_popup->add_item(TTR("Edit Dependencies..."), FILE_DEPENDENCIES);
  1750. p_popup->add_item(TTR("View Owners..."), FILE_OWNERS);
  1751. p_popup->add_separator();
  1752. }
  1753. } else if (all_folders && foldernames.size() > 0) {
  1754. p_popup->add_item(TTR("Open"), FILE_OPEN);
  1755. p_popup->add_separator();
  1756. }
  1757. if (p_paths.size() == 1) {
  1758. p_popup->add_item(TTR("Copy Path"), FILE_COPY_PATH);
  1759. p_popup->add_item(TTR("Rename..."), FILE_RENAME);
  1760. p_popup->add_item(TTR("Duplicate..."), FILE_DUPLICATE);
  1761. }
  1762. p_popup->add_item(TTR("Move To..."), FILE_MOVE);
  1763. p_popup->add_item(TTR("Delete"), FILE_REMOVE);
  1764. if (p_paths.size() == 1) {
  1765. p_popup->add_separator();
  1766. if (p_display_path_dependent_options) {
  1767. p_popup->add_item(TTR("New Folder..."), FILE_NEW_FOLDER);
  1768. p_popup->add_item(TTR("New Script..."), FILE_NEW_SCRIPT);
  1769. p_popup->add_item(TTR("New Resource..."), FILE_NEW_RESOURCE);
  1770. }
  1771. String fpath = p_paths[0];
  1772. String item_text = fpath.ends_with("/") ? TTR("Open in File Manager") : TTR("Show in File Manager");
  1773. p_popup->add_item(item_text, FILE_SHOW_IN_EXPLORER);
  1774. }
  1775. }
  1776. void FileSystemDock::_tree_rmb_select(const Vector2 &p_pos) {
  1777. // Right click is pressed in the tree
  1778. Vector<String> paths = _tree_get_selected();
  1779. if (paths.size() == 1) {
  1780. if (paths[0].ends_with("/")) {
  1781. tree_popup->add_item(TTR("Expand All"), FOLDER_EXPAND_ALL);
  1782. tree_popup->add_item(TTR("Collapse All"), FOLDER_COLLAPSE_ALL);
  1783. tree_popup->add_separator();
  1784. }
  1785. }
  1786. // Popup
  1787. if (!paths.empty()) {
  1788. tree_popup->clear();
  1789. tree_popup->set_size(Size2(1, 1));
  1790. _file_and_folders_fill_popup(tree_popup, paths);
  1791. tree_popup->set_position(tree->get_global_position() + p_pos);
  1792. tree_popup->popup();
  1793. }
  1794. }
  1795. void FileSystemDock::_tree_empty_selected() {
  1796. tree->deselect_all();
  1797. }
  1798. void FileSystemDock::_file_list_rmb_select(int p_item, const Vector2 &p_pos) {
  1799. // Right click is pressed in the file list
  1800. Vector<String> paths;
  1801. for (int i = 0; i < files->get_item_count(); i++) {
  1802. if (!files->is_selected(i))
  1803. continue;
  1804. if (files->get_item_text(p_item) == "..") {
  1805. files->unselect(i);
  1806. continue;
  1807. }
  1808. paths.push_back(files->get_item_metadata(i));
  1809. }
  1810. // Popup
  1811. if (!paths.empty()) {
  1812. file_list_popup->clear();
  1813. file_list_popup->set_size(Size2(1, 1));
  1814. _file_and_folders_fill_popup(file_list_popup, paths, searched_string.length() == 0);
  1815. file_list_popup->set_position(files->get_global_position() + p_pos);
  1816. file_list_popup->popup();
  1817. }
  1818. }
  1819. void FileSystemDock::_file_list_rmb_pressed(const Vector2 &p_pos) {
  1820. // Right click on empty space for file list
  1821. if (searched_string.length() > 0)
  1822. return;
  1823. file_list_popup->clear();
  1824. file_list_popup->set_size(Size2(1, 1));
  1825. file_list_popup->add_item(TTR("New Folder..."), FILE_NEW_FOLDER);
  1826. file_list_popup->add_item(TTR("New Script..."), FILE_NEW_SCRIPT);
  1827. file_list_popup->add_item(TTR("New Resource..."), FILE_NEW_RESOURCE);
  1828. file_list_popup->add_item(TTR("Show in File Manager"), FILE_SHOW_IN_EXPLORER);
  1829. file_list_popup->set_position(files->get_global_position() + p_pos);
  1830. file_list_popup->popup();
  1831. }
  1832. void FileSystemDock::select_file(const String &p_file) {
  1833. _navigate_to_path(p_file);
  1834. }
  1835. void FileSystemDock::_file_multi_selected(int p_index, bool p_selected) {
  1836. // Set the path to the current focused item
  1837. int current = files->get_current();
  1838. if (current == p_index) {
  1839. String fpath = files->get_item_metadata(current);
  1840. if (!fpath.ends_with("/")) {
  1841. path = fpath;
  1842. if (display_mode == DISPLAY_MODE_SPLIT) {
  1843. _update_tree(_compute_uncollapsed_paths());
  1844. }
  1845. }
  1846. }
  1847. // Update the import dock
  1848. import_dock_needs_update = true;
  1849. call_deferred("_update_import_dock");
  1850. }
  1851. void FileSystemDock::_tree_gui_input(Ref<InputEvent> p_event) {
  1852. if (get_viewport()->get_modal_stack_top())
  1853. return; //ignore because of modal window
  1854. Ref<InputEventKey> key = p_event;
  1855. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1856. if (ED_IS_SHORTCUT("filesystem_dock/duplicate", p_event)) {
  1857. _tree_rmb_option(FILE_DUPLICATE);
  1858. } else if (ED_IS_SHORTCUT("filesystem_dock/copy_path", p_event)) {
  1859. _tree_rmb_option(FILE_COPY_PATH);
  1860. } else if (ED_IS_SHORTCUT("filesystem_dock/delete", p_event)) {
  1861. _tree_rmb_option(FILE_REMOVE);
  1862. } else if (ED_IS_SHORTCUT("filesystem_dock/rename", p_event)) {
  1863. _tree_rmb_option(FILE_RENAME);
  1864. }
  1865. }
  1866. }
  1867. void FileSystemDock::_file_list_gui_input(Ref<InputEvent> p_event) {
  1868. if (get_viewport()->get_modal_stack_top())
  1869. return; //ignore because of modal window
  1870. Ref<InputEventKey> key = p_event;
  1871. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1872. if (ED_IS_SHORTCUT("filesystem_dock/duplicate", p_event)) {
  1873. _file_list_rmb_option(FILE_DUPLICATE);
  1874. } else if (ED_IS_SHORTCUT("filesystem_dock/copy_path", p_event)) {
  1875. _file_list_rmb_option(FILE_COPY_PATH);
  1876. } else if (ED_IS_SHORTCUT("filesystem_dock/delete", p_event)) {
  1877. _file_list_rmb_option(FILE_REMOVE);
  1878. } else if (ED_IS_SHORTCUT("filesystem_dock/rename", p_event)) {
  1879. _file_list_rmb_option(FILE_RENAME);
  1880. }
  1881. }
  1882. }
  1883. void FileSystemDock::_update_import_dock() {
  1884. if (!import_dock_needs_update)
  1885. return;
  1886. // List selected
  1887. Vector<String> selected;
  1888. if (display_mode == DISPLAY_MODE_TREE_ONLY) {
  1889. // Use the tree
  1890. selected = _tree_get_selected();
  1891. } else {
  1892. // Use the file list
  1893. for (int i = 0; i < files->get_item_count(); i++) {
  1894. if (!files->is_selected(i))
  1895. continue;
  1896. selected.push_back(files->get_item_metadata(i));
  1897. }
  1898. }
  1899. // Check import
  1900. Vector<String> imports;
  1901. String import_type;
  1902. for (int i = 0; i < selected.size(); i++) {
  1903. String fpath = selected[i];
  1904. if (fpath.ends_with("/")) {
  1905. imports.clear();
  1906. break;
  1907. }
  1908. if (!FileAccess::exists(fpath + ".import")) {
  1909. imports.clear();
  1910. break;
  1911. }
  1912. Ref<ConfigFile> cf;
  1913. cf.instance();
  1914. Error err = cf->load(fpath + ".import");
  1915. if (err != OK) {
  1916. imports.clear();
  1917. break;
  1918. }
  1919. String type = cf->get_value("remap", "type");
  1920. if (import_type == "") {
  1921. import_type = type;
  1922. } else if (import_type != type) {
  1923. //all should be the same type
  1924. imports.clear();
  1925. break;
  1926. }
  1927. imports.push_back(fpath);
  1928. }
  1929. if (imports.size() == 0) {
  1930. EditorNode::get_singleton()->get_import_dock()->clear();
  1931. } else if (imports.size() == 1) {
  1932. EditorNode::get_singleton()->get_import_dock()->set_edit_path(imports[0]);
  1933. } else {
  1934. EditorNode::get_singleton()->get_import_dock()->set_edit_multiple_paths(imports);
  1935. }
  1936. import_dock_needs_update = false;
  1937. }
  1938. void FileSystemDock::_bind_methods() {
  1939. ClassDB::bind_method(D_METHOD("_file_list_gui_input"), &FileSystemDock::_file_list_gui_input);
  1940. ClassDB::bind_method(D_METHOD("_tree_gui_input"), &FileSystemDock::_tree_gui_input);
  1941. ClassDB::bind_method(D_METHOD("_update_tree"), &FileSystemDock::_update_tree);
  1942. ClassDB::bind_method(D_METHOD("_rescan"), &FileSystemDock::_rescan);
  1943. ClassDB::bind_method(D_METHOD("_toggle_split_mode"), &FileSystemDock::_toggle_split_mode);
  1944. ClassDB::bind_method(D_METHOD("_tree_rmb_option", "option"), &FileSystemDock::_tree_rmb_option);
  1945. ClassDB::bind_method(D_METHOD("_tree_rmb_select"), &FileSystemDock::_tree_rmb_select);
  1946. ClassDB::bind_method(D_METHOD("_tree_empty_selected"), &FileSystemDock::_tree_empty_selected);
  1947. ClassDB::bind_method(D_METHOD("_file_list_rmb_option", "option"), &FileSystemDock::_file_list_rmb_option);
  1948. ClassDB::bind_method(D_METHOD("_file_list_rmb_select"), &FileSystemDock::_file_list_rmb_select);
  1949. ClassDB::bind_method(D_METHOD("_file_list_rmb_pressed"), &FileSystemDock::_file_list_rmb_pressed);
  1950. ClassDB::bind_method(D_METHOD("_file_deleted"), &FileSystemDock::_file_deleted);
  1951. ClassDB::bind_method(D_METHOD("_folder_deleted"), &FileSystemDock::_folder_deleted);
  1952. ClassDB::bind_method(D_METHOD("_file_list_thumbnail_done"), &FileSystemDock::_file_list_thumbnail_done);
  1953. ClassDB::bind_method(D_METHOD("_tree_thumbnail_done"), &FileSystemDock::_tree_thumbnail_done);
  1954. ClassDB::bind_method(D_METHOD("_file_list_activate_file"), &FileSystemDock::_file_list_activate_file);
  1955. ClassDB::bind_method(D_METHOD("_tree_activate_file"), &FileSystemDock::_tree_activate_file);
  1956. ClassDB::bind_method(D_METHOD("_select_file"), &FileSystemDock::_select_file);
  1957. ClassDB::bind_method(D_METHOD("_navigate_to_path"), &FileSystemDock::_navigate_to_path, DEFVAL(false));
  1958. ClassDB::bind_method(D_METHOD("_toggle_file_display"), &FileSystemDock::_toggle_file_display);
  1959. ClassDB::bind_method(D_METHOD("_fw_history"), &FileSystemDock::_fw_history);
  1960. ClassDB::bind_method(D_METHOD("_bw_history"), &FileSystemDock::_bw_history);
  1961. ClassDB::bind_method(D_METHOD("_fs_changed"), &FileSystemDock::_fs_changed);
  1962. ClassDB::bind_method(D_METHOD("_tree_multi_selected"), &FileSystemDock::_tree_multi_selected);
  1963. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileSystemDock::_make_dir_confirm);
  1964. ClassDB::bind_method(D_METHOD("_resource_created"), &FileSystemDock::_resource_created);
  1965. ClassDB::bind_method(D_METHOD("_move_operation_confirm", "to_path", "overwrite"), &FileSystemDock::_move_operation_confirm, DEFVAL(false));
  1966. ClassDB::bind_method(D_METHOD("_move_with_overwrite"), &FileSystemDock::_move_with_overwrite);
  1967. ClassDB::bind_method(D_METHOD("_rename_operation_confirm"), &FileSystemDock::_rename_operation_confirm);
  1968. ClassDB::bind_method(D_METHOD("_duplicate_operation_confirm"), &FileSystemDock::_duplicate_operation_confirm);
  1969. ClassDB::bind_method(D_METHOD("_search_changed"), &FileSystemDock::_search_changed);
  1970. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &FileSystemDock::get_drag_data_fw);
  1971. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &FileSystemDock::can_drop_data_fw);
  1972. ClassDB::bind_method(D_METHOD("drop_data_fw"), &FileSystemDock::drop_data_fw);
  1973. ClassDB::bind_method(D_METHOD("navigate_to_path"), &FileSystemDock::navigate_to_path);
  1974. ClassDB::bind_method(D_METHOD("_preview_invalidated"), &FileSystemDock::_preview_invalidated);
  1975. ClassDB::bind_method(D_METHOD("_file_multi_selected"), &FileSystemDock::_file_multi_selected);
  1976. ClassDB::bind_method(D_METHOD("_update_import_dock"), &FileSystemDock::_update_import_dock);
  1977. ADD_SIGNAL(MethodInfo("instance", PropertyInfo(Variant::POOL_STRING_ARRAY, "files")));
  1978. ADD_SIGNAL(MethodInfo("open"));
  1979. ADD_SIGNAL(MethodInfo("file_removed", PropertyInfo(Variant::STRING, "file")));
  1980. ADD_SIGNAL(MethodInfo("folder_removed", PropertyInfo(Variant::STRING, "folder")));
  1981. ADD_SIGNAL(MethodInfo("files_moved", PropertyInfo(Variant::STRING, "old_file"), PropertyInfo(Variant::STRING, "new_file")));
  1982. ADD_SIGNAL(MethodInfo("folder_moved", PropertyInfo(Variant::STRING, "old_folder"), PropertyInfo(Variant::STRING, "new_file")));
  1983. ADD_SIGNAL(MethodInfo("display_mode_changed"));
  1984. }
  1985. FileSystemDock::FileSystemDock(EditorNode *p_editor) {
  1986. set_name("FileSystem");
  1987. editor = p_editor;
  1988. path = "res://";
  1989. ED_SHORTCUT("filesystem_dock/copy_path", TTR("Copy Path"), KEY_MASK_CMD | KEY_C);
  1990. ED_SHORTCUT("filesystem_dock/duplicate", TTR("Duplicate..."), KEY_MASK_CMD | KEY_D);
  1991. ED_SHORTCUT("filesystem_dock/delete", TTR("Delete"), KEY_DELETE);
  1992. ED_SHORTCUT("filesystem_dock/rename", TTR("Rename"));
  1993. VBoxContainer *top_vbc = memnew(VBoxContainer);
  1994. add_child(top_vbc);
  1995. HBoxContainer *toolbar_hbc = memnew(HBoxContainer);
  1996. toolbar_hbc->add_constant_override("separation", 0);
  1997. top_vbc->add_child(toolbar_hbc);
  1998. button_hist_prev = memnew(ToolButton);
  1999. button_hist_prev->set_disabled(true);
  2000. button_hist_prev->set_focus_mode(FOCUS_NONE);
  2001. button_hist_prev->set_tooltip(TTR("Previous Directory"));
  2002. toolbar_hbc->add_child(button_hist_prev);
  2003. button_hist_next = memnew(ToolButton);
  2004. button_hist_next->set_disabled(true);
  2005. button_hist_next->set_focus_mode(FOCUS_NONE);
  2006. button_hist_next->set_tooltip(TTR("Next Directory"));
  2007. toolbar_hbc->add_child(button_hist_next);
  2008. current_path = memnew(LineEdit);
  2009. current_path->set_h_size_flags(SIZE_EXPAND_FILL);
  2010. _set_current_path_text(path);
  2011. toolbar_hbc->add_child(current_path);
  2012. button_reload = memnew(Button);
  2013. button_reload->set_flat(true);
  2014. button_reload->connect("pressed", this, "_rescan");
  2015. button_reload->set_focus_mode(FOCUS_NONE);
  2016. button_reload->set_tooltip(TTR("Re-Scan Filesystem"));
  2017. button_reload->hide();
  2018. toolbar_hbc->add_child(button_reload);
  2019. button_toggle_display_mode = memnew(Button);
  2020. button_toggle_display_mode->set_flat(true);
  2021. button_toggle_display_mode->set_toggle_mode(true);
  2022. button_toggle_display_mode->connect("toggled", this, "_toggle_split_mode");
  2023. button_toggle_display_mode->set_focus_mode(FOCUS_NONE);
  2024. button_toggle_display_mode->set_tooltip(TTR("Toggle split mode"));
  2025. toolbar_hbc->add_child(button_toggle_display_mode);
  2026. HBoxContainer *toolbar2_hbc = memnew(HBoxContainer);
  2027. toolbar2_hbc->add_constant_override("separation", 0);
  2028. top_vbc->add_child(toolbar2_hbc);
  2029. tree_search_box = memnew(LineEdit);
  2030. tree_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2031. tree_search_box->set_placeholder(TTR("Search files"));
  2032. tree_search_box->connect("text_changed", this, "_search_changed", varray(tree_search_box));
  2033. toolbar2_hbc->add_child(tree_search_box);
  2034. file_list_popup = memnew(PopupMenu);
  2035. file_list_popup->set_hide_on_window_lose_focus(true);
  2036. add_child(file_list_popup);
  2037. tree_popup = memnew(PopupMenu);
  2038. tree_popup->set_hide_on_window_lose_focus(true);
  2039. add_child(tree_popup);
  2040. split_box = memnew(VSplitContainer);
  2041. split_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2042. add_child(split_box);
  2043. tree = memnew(Tree);
  2044. tree->set_hide_root(true);
  2045. tree->set_drag_forwarding(this);
  2046. tree->set_allow_rmb_select(true);
  2047. tree->set_select_mode(Tree::SELECT_MULTI);
  2048. tree->set_custom_minimum_size(Size2(0, 15 * EDSCALE));
  2049. split_box->add_child(tree);
  2050. tree->connect("item_edited", this, "_favorite_toggled");
  2051. tree->connect("item_activated", this, "_tree_activate_file");
  2052. tree->connect("multi_selected", this, "_tree_multi_selected");
  2053. tree->connect("item_rmb_selected", this, "_tree_rmb_select");
  2054. tree->connect("nothing_selected", this, "_tree_empty_selected");
  2055. tree->connect("gui_input", this, "_tree_gui_input");
  2056. file_list_vb = memnew(VBoxContainer);
  2057. file_list_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2058. split_box->add_child(file_list_vb);
  2059. path_hb = memnew(HBoxContainer);
  2060. file_list_vb->add_child(path_hb);
  2061. file_list_search_box = memnew(LineEdit);
  2062. file_list_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2063. file_list_search_box->set_placeholder(TTR("Search files"));
  2064. file_list_search_box->connect("text_changed", this, "_search_changed", varray(file_list_search_box));
  2065. path_hb->add_child(file_list_search_box);
  2066. button_file_list_display_mode = memnew(ToolButton);
  2067. path_hb->add_child(button_file_list_display_mode);
  2068. files = memnew(ItemList);
  2069. files->set_v_size_flags(SIZE_EXPAND_FILL);
  2070. files->set_select_mode(ItemList::SELECT_MULTI);
  2071. files->set_drag_forwarding(this);
  2072. files->connect("item_rmb_selected", this, "_file_list_rmb_select");
  2073. files->connect("gui_input", this, "_file_list_gui_input");
  2074. files->connect("multi_selected", this, "_file_multi_selected");
  2075. files->connect("rmb_clicked", this, "_file_list_rmb_pressed");
  2076. files->set_custom_minimum_size(Size2(0, 15 * EDSCALE));
  2077. files->set_allow_rmb_select(true);
  2078. file_list_vb->add_child(files);
  2079. scanning_vb = memnew(VBoxContainer);
  2080. scanning_vb->hide();
  2081. add_child(scanning_vb);
  2082. Label *slabel = memnew(Label);
  2083. slabel->set_text(TTR("Scanning Files,\nPlease Wait..."));
  2084. slabel->set_align(Label::ALIGN_CENTER);
  2085. scanning_vb->add_child(slabel);
  2086. scanning_progress = memnew(ProgressBar);
  2087. scanning_vb->add_child(scanning_progress);
  2088. deps_editor = memnew(DependencyEditor);
  2089. add_child(deps_editor);
  2090. owners_editor = memnew(DependencyEditorOwners(editor));
  2091. add_child(owners_editor);
  2092. remove_dialog = memnew(DependencyRemoveDialog);
  2093. remove_dialog->connect("file_removed", this, "_file_deleted");
  2094. remove_dialog->connect("folder_removed", this, "_folder_deleted");
  2095. add_child(remove_dialog);
  2096. move_dialog = memnew(EditorDirDialog);
  2097. move_dialog->get_ok()->set_text(TTR("Move"));
  2098. add_child(move_dialog);
  2099. move_dialog->connect("dir_selected", this, "_move_operation_confirm");
  2100. rename_dialog = memnew(ConfirmationDialog);
  2101. VBoxContainer *rename_dialog_vb = memnew(VBoxContainer);
  2102. rename_dialog->add_child(rename_dialog_vb);
  2103. rename_dialog_text = memnew(LineEdit);
  2104. rename_dialog_vb->add_margin_child(TTR("Name:"), rename_dialog_text);
  2105. rename_dialog->get_ok()->set_text(TTR("Rename"));
  2106. add_child(rename_dialog);
  2107. rename_dialog->register_text_enter(rename_dialog_text);
  2108. rename_dialog->connect("confirmed", this, "_rename_operation_confirm");
  2109. overwrite_dialog = memnew(ConfirmationDialog);
  2110. overwrite_dialog->set_text(TTR("There is already file or folder with the same name in this location."));
  2111. overwrite_dialog->get_ok()->set_text(TTR("Overwrite"));
  2112. add_child(overwrite_dialog);
  2113. overwrite_dialog->connect("confirmed", this, "_move_with_overwrite");
  2114. duplicate_dialog = memnew(ConfirmationDialog);
  2115. VBoxContainer *duplicate_dialog_vb = memnew(VBoxContainer);
  2116. duplicate_dialog->add_child(duplicate_dialog_vb);
  2117. duplicate_dialog_text = memnew(LineEdit);
  2118. duplicate_dialog_vb->add_margin_child(TTR("Name:"), duplicate_dialog_text);
  2119. duplicate_dialog->get_ok()->set_text(TTR("Duplicate"));
  2120. add_child(duplicate_dialog);
  2121. duplicate_dialog->register_text_enter(duplicate_dialog_text);
  2122. duplicate_dialog->connect("confirmed", this, "_duplicate_operation_confirm");
  2123. make_dir_dialog = memnew(ConfirmationDialog);
  2124. make_dir_dialog->set_title(TTR("Create Folder"));
  2125. VBoxContainer *make_folder_dialog_vb = memnew(VBoxContainer);
  2126. make_dir_dialog->add_child(make_folder_dialog_vb);
  2127. make_dir_dialog_text = memnew(LineEdit);
  2128. make_folder_dialog_vb->add_margin_child(TTR("Name:"), make_dir_dialog_text);
  2129. add_child(make_dir_dialog);
  2130. make_dir_dialog->register_text_enter(make_dir_dialog_text);
  2131. make_dir_dialog->connect("confirmed", this, "_make_dir_confirm");
  2132. make_script_dialog_text = memnew(ScriptCreateDialog);
  2133. make_script_dialog_text->set_title(TTR("Create Script"));
  2134. add_child(make_script_dialog_text);
  2135. new_resource_dialog = memnew(CreateDialog);
  2136. add_child(new_resource_dialog);
  2137. new_resource_dialog->set_base_type("Resource");
  2138. new_resource_dialog->connect("create", this, "_resource_created");
  2139. searched_string = String();
  2140. uncollapsed_paths_before_search = Vector<String>();
  2141. updating_tree = false;
  2142. tree_update_id = 0;
  2143. initialized = false;
  2144. import_dock_needs_update = false;
  2145. history_pos = 0;
  2146. history_max_size = 20;
  2147. history.push_back("res://");
  2148. display_mode = DISPLAY_MODE_TREE_ONLY;
  2149. old_display_mode = DISPLAY_MODE_TREE_ONLY;
  2150. file_list_display_mode = FILE_LIST_DISPLAY_THUMBNAILS;
  2151. always_show_folders = false;
  2152. }
  2153. FileSystemDock::~FileSystemDock() {
  2154. }