filesystem_dock.cpp 76 KB

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