filesystem_dock.cpp 81 KB

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