filesystem_dock.cpp 99 KB

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