filesystem_dock.cpp 79 KB

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