filesystem_dock.cpp 84 KB

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