filesystem_dock.cpp 87 KB

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