editor_file_dialog.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /**************************************************************************/
  2. /* editor_file_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "editor_file_dialog.h"
  31. #include "core/os/file_access.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include "dependency_editor.h"
  36. #include "editor_file_system.h"
  37. #include "editor_resource_preview.h"
  38. #include "editor_scale.h"
  39. #include "editor_settings.h"
  40. #include "main/main.h"
  41. #include "scene/gui/center_container.h"
  42. #include "scene/gui/label.h"
  43. #include "scene/gui/margin_container.h"
  44. EditorFileDialog::GetIconFunc EditorFileDialog::get_icon_func = nullptr;
  45. EditorFileDialog::GetIconFunc EditorFileDialog::get_large_icon_func = nullptr;
  46. EditorFileDialog::RegisterFunc EditorFileDialog::register_func = nullptr;
  47. EditorFileDialog::RegisterFunc EditorFileDialog::unregister_func = nullptr;
  48. VBoxContainer *EditorFileDialog::get_vbox() {
  49. return vbox;
  50. }
  51. void EditorFileDialog::_notification(int p_what) {
  52. if (p_what == NOTIFICATION_ENTER_TREE) {
  53. // Update icons.
  54. mode_thumbnails->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  55. mode_list->set_icon(get_icon("FileList", "EditorIcons"));
  56. dir_prev->set_icon(get_icon("Back", "EditorIcons"));
  57. dir_next->set_icon(get_icon("Forward", "EditorIcons"));
  58. dir_up->set_icon(get_icon("ArrowUp", "EditorIcons"));
  59. refresh->set_icon(get_icon("Reload", "EditorIcons"));
  60. favorite->set_icon(get_icon("Favorites", "EditorIcons"));
  61. show_hidden->set_icon(get_icon("GuiVisibilityVisible", "EditorIcons"));
  62. fav_up->set_icon(get_icon("MoveUp", "EditorIcons"));
  63. fav_down->set_icon(get_icon("MoveDown", "EditorIcons"));
  64. } else if (p_what == NOTIFICATION_PROCESS) {
  65. if (preview_waiting) {
  66. preview_wheel_timeout -= get_process_delta_time();
  67. if (preview_wheel_timeout <= 0) {
  68. preview_wheel_index++;
  69. if (preview_wheel_index >= 8) {
  70. preview_wheel_index = 0;
  71. }
  72. Ref<Texture> frame = get_icon("Progress" + itos(preview_wheel_index + 1), "EditorIcons");
  73. preview->set_texture(frame);
  74. preview_wheel_timeout = 0.1;
  75. }
  76. }
  77. } else if (p_what == NOTIFICATION_POPUP_HIDE) {
  78. set_process_unhandled_input(false);
  79. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  80. bool is_showing_hidden = EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files");
  81. if (show_hidden_files != is_showing_hidden) {
  82. set_show_hidden_files(is_showing_hidden);
  83. }
  84. set_display_mode((DisplayMode)EditorSettings::get_singleton()->get("filesystem/file_dialog/display_mode").operator int());
  85. // Update icons.
  86. mode_thumbnails->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  87. mode_list->set_icon(get_icon("FileList", "EditorIcons"));
  88. dir_prev->set_icon(get_icon("Back", "EditorIcons"));
  89. dir_next->set_icon(get_icon("Forward", "EditorIcons"));
  90. dir_up->set_icon(get_icon("ArrowUp", "EditorIcons"));
  91. refresh->set_icon(get_icon("Reload", "EditorIcons"));
  92. favorite->set_icon(get_icon("Favorites", "EditorIcons"));
  93. fav_up->set_icon(get_icon("MoveUp", "EditorIcons"));
  94. fav_down->set_icon(get_icon("MoveDown", "EditorIcons"));
  95. // DO NOT CALL UPDATE FILE LIST HERE, ALL HUNDREDS OF HIDDEN DIALOGS WILL RESPOND, CALL INVALIDATE INSTEAD
  96. invalidate();
  97. } else if (p_what == NOTIFICATION_WM_FOCUS_IN) {
  98. // Check if the current directory was removed externally (much less likely to happen while editor window is focused).
  99. String previous_dir = get_current_dir();
  100. while (!dir_access->dir_exists(get_current_dir())) {
  101. _go_up();
  102. // In case we can't go further up, use some fallback and break.
  103. if (get_current_dir() == previous_dir) {
  104. _dir_entered(OS::get_singleton()->get_user_data_dir());
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. void EditorFileDialog::_unhandled_input(const Ref<InputEvent> &p_event) {
  111. Ref<InputEventKey> k = p_event;
  112. if (k.is_valid() && is_window_modal_on_top()) {
  113. if (k->is_pressed()) {
  114. bool handled = false;
  115. if (ED_IS_SHORTCUT("file_dialog/go_back", p_event)) {
  116. _go_back();
  117. handled = true;
  118. }
  119. if (ED_IS_SHORTCUT("file_dialog/go_forward", p_event)) {
  120. _go_forward();
  121. handled = true;
  122. }
  123. if (ED_IS_SHORTCUT("file_dialog/go_up", p_event)) {
  124. _go_up();
  125. handled = true;
  126. }
  127. if (ED_IS_SHORTCUT("file_dialog/refresh", p_event)) {
  128. invalidate();
  129. handled = true;
  130. }
  131. if (ED_IS_SHORTCUT("file_dialog/toggle_hidden_files", p_event)) {
  132. set_show_hidden_files(!show_hidden_files);
  133. handled = true;
  134. }
  135. if (ED_IS_SHORTCUT("file_dialog/toggle_favorite", p_event)) {
  136. _favorite_pressed();
  137. handled = true;
  138. }
  139. if (ED_IS_SHORTCUT("file_dialog/toggle_mode", p_event)) {
  140. if (mode_thumbnails->is_pressed()) {
  141. set_display_mode(DISPLAY_LIST);
  142. } else {
  143. set_display_mode(DISPLAY_THUMBNAILS);
  144. }
  145. handled = true;
  146. }
  147. if (ED_IS_SHORTCUT("file_dialog/create_folder", p_event)) {
  148. _make_dir();
  149. handled = true;
  150. }
  151. if (ED_IS_SHORTCUT("file_dialog/delete", p_event)) {
  152. _delete_items();
  153. handled = true;
  154. }
  155. if (ED_IS_SHORTCUT("file_dialog/focus_path", p_event)) {
  156. dir->grab_focus();
  157. handled = true;
  158. }
  159. if (ED_IS_SHORTCUT("file_dialog/move_favorite_up", p_event)) {
  160. _favorite_move_up();
  161. handled = true;
  162. }
  163. if (ED_IS_SHORTCUT("file_dialog/move_favorite_down", p_event)) {
  164. _favorite_move_down();
  165. handled = true;
  166. }
  167. if (handled) {
  168. accept_event();
  169. }
  170. }
  171. }
  172. }
  173. void EditorFileDialog::set_enable_multiple_selection(bool p_enable) {
  174. item_list->set_select_mode(p_enable ? ItemList::SELECT_MULTI : ItemList::SELECT_SINGLE);
  175. };
  176. Vector<String> EditorFileDialog::get_selected_files() const {
  177. Vector<String> list;
  178. for (int i = 0; i < item_list->get_item_count(); i++) {
  179. if (item_list->is_selected(i)) {
  180. list.push_back(item_list->get_item_text(i));
  181. }
  182. }
  183. return list;
  184. };
  185. void EditorFileDialog::update_dir() {
  186. if (drives->is_visible()) {
  187. if (dir_access->get_current_dir().is_network_share_path()) {
  188. _update_drives(false);
  189. drives->add_item(RTR("Network"));
  190. drives->set_item_disabled(drives->get_item_count() - 1, true);
  191. drives->select(drives->get_item_count() - 1);
  192. } else {
  193. drives->select(dir_access->get_current_drive());
  194. }
  195. }
  196. dir->set_text(dir_access->get_current_dir_without_drive());
  197. // Disable "Open" button only when selecting file(s) mode.
  198. get_ok()->set_disabled(_is_open_should_be_disabled());
  199. switch (mode) {
  200. case MODE_OPEN_FILE:
  201. case MODE_OPEN_FILES:
  202. get_ok()->set_text(TTR("Open"));
  203. break;
  204. case MODE_OPEN_DIR:
  205. get_ok()->set_text(TTR("Select Current Folder"));
  206. break;
  207. case MODE_OPEN_ANY:
  208. case MODE_SAVE_FILE:
  209. // FIXME: Implement, or refactor to avoid duplication with set_mode
  210. break;
  211. }
  212. }
  213. void EditorFileDialog::_dir_entered(String p_dir) {
  214. dir_access->change_dir(p_dir);
  215. invalidate();
  216. update_dir();
  217. _push_history();
  218. }
  219. void EditorFileDialog::_file_entered(const String &p_file) {
  220. _action_pressed();
  221. }
  222. void EditorFileDialog::_save_confirm_pressed() {
  223. String f = dir_access->get_current_dir().plus_file(file->get_text());
  224. _save_to_recent();
  225. hide();
  226. emit_signal("file_selected", f);
  227. }
  228. void EditorFileDialog::_post_popup() {
  229. ConfirmationDialog::_post_popup();
  230. // Check if the current path doesn't exist and correct it.
  231. String current = dir_access->get_current_dir();
  232. while (!dir_access->dir_exists(current)) {
  233. current = current.get_base_dir();
  234. }
  235. set_current_dir(current);
  236. if (invalidated) {
  237. update_file_list();
  238. invalidated = false;
  239. }
  240. if (mode == MODE_SAVE_FILE) {
  241. file->grab_focus();
  242. } else {
  243. item_list->grab_focus();
  244. }
  245. if (mode == MODE_OPEN_DIR) {
  246. file_box->set_visible(false);
  247. } else {
  248. file_box->set_visible(true);
  249. }
  250. if (is_visible_in_tree() && get_current_file() != "") {
  251. _request_single_thumbnail(get_current_dir().plus_file(get_current_file()));
  252. }
  253. if (is_visible_in_tree()) {
  254. Ref<Texture> folder = get_icon("folder", "FileDialog");
  255. const Color folder_color = get_color("folder_icon_modulate", "FileDialog");
  256. recent->clear();
  257. bool res = access == ACCESS_RESOURCES;
  258. Vector<String> recentd = EditorSettings::get_singleton()->get_recent_dirs();
  259. for (int i = 0; i < recentd.size(); i++) {
  260. bool cres = recentd[i].begins_with("res://");
  261. if (cres != res) {
  262. continue;
  263. }
  264. String name = recentd[i];
  265. if (res && name == "res://") {
  266. name = "/";
  267. } else {
  268. if (name.ends_with("/")) {
  269. name = name.substr(0, name.length() - 1);
  270. }
  271. name = name.get_file() + "/";
  272. }
  273. bool exists = dir_access->dir_exists(recentd[i]);
  274. if (!exists) {
  275. // Remove invalid directory from the list of Recent directories.
  276. recentd.remove(i--);
  277. } else {
  278. recent->add_item(name, folder);
  279. recent->set_item_metadata(recent->get_item_count() - 1, recentd[i]);
  280. recent->set_item_icon_modulate(recent->get_item_count() - 1, folder_color);
  281. }
  282. }
  283. EditorSettings::get_singleton()->set_recent_dirs(recentd);
  284. local_history.clear();
  285. local_history_pos = -1;
  286. _push_history();
  287. _update_favorites();
  288. }
  289. set_process_unhandled_input(true);
  290. }
  291. void EditorFileDialog::_thumbnail_result(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, const Variant &p_udata) {
  292. if (display_mode == DISPLAY_LIST || p_preview.is_null()) {
  293. return;
  294. }
  295. for (int i = 0; i < item_list->get_item_count(); i++) {
  296. Dictionary d = item_list->get_item_metadata(i);
  297. String pname = d["path"];
  298. if (pname == p_path) {
  299. item_list->set_item_icon(i, p_preview);
  300. item_list->set_item_tag_icon(i, Ref<Texture>());
  301. }
  302. }
  303. }
  304. void EditorFileDialog::_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, const Variant &p_udata) {
  305. set_process(false);
  306. preview_waiting = false;
  307. if (p_preview.is_valid() && get_current_path() == p_path) {
  308. preview->set_texture(p_preview);
  309. if (display_mode == DISPLAY_THUMBNAILS) {
  310. preview_vb->hide();
  311. } else {
  312. preview_vb->show();
  313. }
  314. } else {
  315. preview_vb->hide();
  316. preview->set_texture(Ref<Texture>());
  317. }
  318. }
  319. void EditorFileDialog::_request_single_thumbnail(const String &p_path) {
  320. if (!FileAccess::exists(p_path)) {
  321. return;
  322. }
  323. set_process(true);
  324. preview_waiting = true;
  325. preview_wheel_timeout = 0;
  326. EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, this, "_thumbnail_done", p_path);
  327. }
  328. void EditorFileDialog::_action_pressed() {
  329. if (mode == MODE_OPEN_FILES) {
  330. String fbase = dir_access->get_current_dir();
  331. PoolVector<String> files;
  332. for (int i = 0; i < item_list->get_item_count(); i++) {
  333. if (item_list->is_selected(i)) {
  334. files.push_back(fbase.plus_file(item_list->get_item_text(i)));
  335. }
  336. }
  337. if (files.size()) {
  338. _save_to_recent();
  339. hide();
  340. emit_signal("files_selected", files);
  341. }
  342. return;
  343. }
  344. String file_text = file->get_text();
  345. String f = file_text.is_abs_path() ? file_text : dir_access->get_current_dir().plus_file(file_text);
  346. if ((mode == MODE_OPEN_ANY || mode == MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  347. _save_to_recent();
  348. hide();
  349. emit_signal("file_selected", f);
  350. } else if (mode == MODE_OPEN_ANY || mode == MODE_OPEN_DIR) {
  351. String path = dir_access->get_current_dir();
  352. path = path.replace("\\", "/");
  353. for (int i = 0; i < item_list->get_item_count(); i++) {
  354. if (item_list->is_selected(i)) {
  355. Dictionary d = item_list->get_item_metadata(i);
  356. if (d["dir"]) {
  357. path = path.plus_file(d["name"]);
  358. break;
  359. }
  360. }
  361. }
  362. _save_to_recent();
  363. hide();
  364. emit_signal("dir_selected", path);
  365. }
  366. if (mode == MODE_SAVE_FILE) {
  367. bool valid = false;
  368. if (filter->get_selected() == filter->get_item_count() - 1) {
  369. valid = true; // match none
  370. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  371. // match all filters
  372. for (int i = 0; i < filters.size(); i++) {
  373. String flt = filters[i].get_slice(";", 0);
  374. for (int j = 0; j < flt.get_slice_count(","); j++) {
  375. String str = flt.get_slice(",", j).strip_edges();
  376. if (f.match(str)) {
  377. valid = true;
  378. break;
  379. }
  380. }
  381. if (valid) {
  382. break;
  383. }
  384. }
  385. } else {
  386. int idx = filter->get_selected();
  387. if (filters.size() > 1) {
  388. idx--;
  389. }
  390. if (idx >= 0 && idx < filters.size()) {
  391. String flt = filters[idx].get_slice(";", 0);
  392. int filterSliceCount = flt.get_slice_count(",");
  393. for (int j = 0; j < filterSliceCount; j++) {
  394. String str = (flt.get_slice(",", j).strip_edges());
  395. if (f.match(str)) {
  396. valid = true;
  397. break;
  398. }
  399. }
  400. if (!valid && filterSliceCount > 0) {
  401. String str = (flt.get_slice(",", 0).strip_edges());
  402. f += str.substr(1, str.length() - 1);
  403. _request_single_thumbnail(get_current_dir().plus_file(f.get_file()));
  404. file->set_text(f.get_file());
  405. valid = true;
  406. }
  407. } else {
  408. valid = true;
  409. }
  410. }
  411. // Add first extension of filter if no valid extension is found.
  412. if (!valid) {
  413. int idx = filter->get_selected();
  414. String flt = filters[idx].get_slice(";", 0);
  415. String ext = flt.get_slice(",", 0).strip_edges().get_extension();
  416. f += "." + ext;
  417. }
  418. if (dir_access->file_exists(f) && !disable_overwrite_warning) {
  419. confirm_save->set_text(TTR("File exists, overwrite?"));
  420. confirm_save->popup_centered(Size2(200, 80));
  421. } else {
  422. _save_to_recent();
  423. hide();
  424. emit_signal("file_selected", f);
  425. }
  426. }
  427. }
  428. void EditorFileDialog::_cancel_pressed() {
  429. file->set_text("");
  430. invalidate();
  431. hide();
  432. }
  433. void EditorFileDialog::_item_selected(int p_item) {
  434. int current = p_item;
  435. if (current < 0 || current >= item_list->get_item_count()) {
  436. return;
  437. }
  438. Dictionary d = item_list->get_item_metadata(current);
  439. if (!d["dir"]) {
  440. file->set_text(d["name"]);
  441. _request_single_thumbnail(get_current_dir().plus_file(get_current_file()));
  442. } else if (mode == MODE_OPEN_DIR) {
  443. get_ok()->set_text(TTR("Select This Folder"));
  444. }
  445. get_ok()->set_disabled(_is_open_should_be_disabled());
  446. }
  447. void EditorFileDialog::_multi_selected(int p_item, bool p_selected) {
  448. int current = p_item;
  449. if (current < 0 || current >= item_list->get_item_count()) {
  450. return;
  451. }
  452. Dictionary d = item_list->get_item_metadata(current);
  453. if (!d["dir"] && p_selected) {
  454. file->set_text(d["name"]);
  455. _request_single_thumbnail(get_current_dir().plus_file(get_current_file()));
  456. }
  457. get_ok()->set_disabled(_is_open_should_be_disabled());
  458. }
  459. void EditorFileDialog::_items_clear_selection() {
  460. item_list->unselect_all();
  461. // If nothing is selected, then block Open button.
  462. switch (mode) {
  463. case MODE_OPEN_FILE:
  464. case MODE_OPEN_FILES:
  465. get_ok()->set_text(TTR("Open"));
  466. get_ok()->set_disabled(!item_list->is_anything_selected());
  467. break;
  468. case MODE_OPEN_DIR:
  469. get_ok()->set_disabled(false);
  470. get_ok()->set_text(TTR("Select Current Folder"));
  471. break;
  472. case MODE_OPEN_ANY:
  473. case MODE_SAVE_FILE:
  474. // FIXME: Implement, or refactor to avoid duplication with set_mode
  475. break;
  476. }
  477. }
  478. void EditorFileDialog::_push_history() {
  479. local_history.resize(local_history_pos + 1);
  480. String new_path = dir_access->get_current_dir();
  481. if (local_history.size() == 0 || new_path != local_history[local_history_pos]) {
  482. local_history.push_back(new_path);
  483. local_history_pos++;
  484. dir_prev->set_disabled(local_history_pos == 0);
  485. dir_next->set_disabled(true);
  486. }
  487. }
  488. void EditorFileDialog::_item_dc_selected(int p_item) {
  489. int current = p_item;
  490. if (current < 0 || current >= item_list->get_item_count()) {
  491. return;
  492. }
  493. Dictionary d = item_list->get_item_metadata(current);
  494. if (d["dir"]) {
  495. dir_access->change_dir(d["name"]);
  496. call_deferred("_update_file_list");
  497. call_deferred("_update_dir");
  498. _push_history();
  499. } else {
  500. _action_pressed();
  501. }
  502. }
  503. void EditorFileDialog::_item_list_item_rmb_selected(int p_item, const Vector2 &p_pos) {
  504. // Right click on specific file(s) or folder(s).
  505. item_menu->clear();
  506. item_menu->set_size(Size2(1, 1));
  507. // Allow specific actions only on one item.
  508. bool single_item_selected = item_list->get_selected_items().size() == 1;
  509. // Disallow deleting the .import folder, Godot kills a cat if you do and it is possibly a senseless novice action.
  510. bool allow_delete = true;
  511. for (int i = 0; i < item_list->get_item_count(); i++) {
  512. if (!item_list->is_selected(i)) {
  513. continue;
  514. }
  515. Dictionary item_meta = item_list->get_item_metadata(i);
  516. if (String(item_meta["path"]).begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
  517. allow_delete = false;
  518. break;
  519. }
  520. }
  521. if (single_item_selected) {
  522. item_menu->add_icon_item(get_icon("ActionCopy", "EditorIcons"), TTR("Copy Path"), ITEM_MENU_COPY_PATH);
  523. }
  524. if (allow_delete) {
  525. item_menu->add_icon_item(get_icon("Remove", "EditorIcons"), TTR("Delete"), ITEM_MENU_DELETE, KEY_DELETE);
  526. }
  527. if (single_item_selected) {
  528. item_menu->add_separator();
  529. Dictionary item_meta = item_list->get_item_metadata(p_item);
  530. String item_text = item_meta["dir"] ? TTR("Open in File Manager") : TTR("Show in File Manager");
  531. item_menu->add_icon_item(get_icon("Filesystem", "EditorIcons"), item_text, ITEM_MENU_SHOW_IN_EXPLORER);
  532. }
  533. if (item_menu->get_item_count() > 0) {
  534. item_menu->set_position(item_list->get_global_position() + p_pos);
  535. item_menu->popup();
  536. }
  537. }
  538. void EditorFileDialog::_item_list_rmb_clicked(const Vector2 &p_pos) {
  539. // Right click on folder background. Deselect all files so that actions are applied on the current folder.
  540. for (int i = 0; i < item_list->get_item_count(); i++) {
  541. item_list->unselect(i);
  542. }
  543. item_menu->clear();
  544. item_menu->set_size(Size2(1, 1));
  545. if (can_create_dir) {
  546. item_menu->add_icon_item(get_icon("folder", "FileDialog"), TTR("New Folder..."), ITEM_MENU_NEW_FOLDER, KEY_MASK_CMD | KEY_N);
  547. }
  548. item_menu->add_icon_item(get_icon("Reload", "EditorIcons"), TTR("Refresh"), ITEM_MENU_REFRESH, KEY_F5);
  549. item_menu->add_separator();
  550. item_menu->add_icon_item(get_icon("Filesystem", "EditorIcons"), TTR("Open in File Manager"), ITEM_MENU_SHOW_IN_EXPLORER);
  551. item_menu->set_position(item_list->get_global_position() + p_pos);
  552. item_menu->popup();
  553. }
  554. void EditorFileDialog::_item_menu_id_pressed(int p_option) {
  555. switch (p_option) {
  556. case ITEM_MENU_COPY_PATH: {
  557. Dictionary item_meta = item_list->get_item_metadata(item_list->get_current());
  558. OS::get_singleton()->set_clipboard(item_meta["path"]);
  559. } break;
  560. case ITEM_MENU_DELETE: {
  561. _delete_items();
  562. } break;
  563. case ITEM_MENU_REFRESH: {
  564. invalidate();
  565. } break;
  566. case ITEM_MENU_NEW_FOLDER: {
  567. _make_dir();
  568. } break;
  569. case ITEM_MENU_SHOW_IN_EXPLORER: {
  570. String path;
  571. int idx = item_list->get_current();
  572. if (idx == -1 || item_list->get_selected_items().size() == 0) {
  573. // Folder background was clicked. Open this folder.
  574. path = ProjectSettings::get_singleton()->globalize_path(dir_access->get_current_dir());
  575. } else {
  576. // Specific item was clicked. Open folders directly, or the folder containing a selected file.
  577. Dictionary item_meta = item_list->get_item_metadata(idx);
  578. path = ProjectSettings::get_singleton()->globalize_path(item_meta["path"]);
  579. if (!item_meta["dir"]) {
  580. path = path.get_base_dir();
  581. }
  582. }
  583. OS::get_singleton()->shell_open(String("file://") + path);
  584. } break;
  585. }
  586. }
  587. bool EditorFileDialog::_is_open_should_be_disabled() {
  588. if (mode == MODE_OPEN_ANY || mode == MODE_SAVE_FILE) {
  589. return false;
  590. }
  591. Vector<int> items = item_list->get_selected_items();
  592. if (items.size() == 0) {
  593. return mode != MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  594. }
  595. for (int i = 0; i < items.size(); i++) {
  596. Dictionary d = item_list->get_item_metadata(items.get(i));
  597. if (((mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES) && d["dir"]) || (mode == MODE_OPEN_DIR && !d["dir"])) {
  598. return true;
  599. }
  600. }
  601. return false;
  602. }
  603. void EditorFileDialog::update_file_name() {
  604. int idx = filter->get_selected() - 1;
  605. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  606. if (idx == -1) {
  607. idx += 1;
  608. }
  609. String filter_str = filters[idx];
  610. String file_str = file->get_text();
  611. String base_name = file_str.get_basename();
  612. Vector<String> filter_substr = filter_str.split(";");
  613. if (filter_substr.size() >= 2) {
  614. file_str = base_name + "." + filter_substr[0].strip_edges().get_extension().to_lower();
  615. } else {
  616. file_str = base_name + "." + filter_str.strip_edges().get_extension().to_lower();
  617. }
  618. file->set_text(file_str);
  619. }
  620. }
  621. // DO NOT USE THIS FUNCTION UNLESS NEEDED, CALL INVALIDATE() INSTEAD.
  622. void EditorFileDialog::update_file_list() {
  623. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  624. thumbnail_size *= EDSCALE;
  625. Ref<Texture> folder_thumbnail;
  626. Ref<Texture> file_thumbnail;
  627. item_list->clear();
  628. // Scroll back to the top after opening a directory
  629. item_list->get_v_scroll()->set_value(0);
  630. if (display_mode == DISPLAY_THUMBNAILS) {
  631. item_list->set_max_columns(0);
  632. item_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  633. item_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  634. item_list->set_max_text_lines(2);
  635. item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  636. if (thumbnail_size < 64) {
  637. folder_thumbnail = get_icon("FolderMediumThumb", "EditorIcons");
  638. file_thumbnail = get_icon("FileMediumThumb", "EditorIcons");
  639. } else {
  640. folder_thumbnail = get_icon("FolderBigThumb", "EditorIcons");
  641. file_thumbnail = get_icon("FileBigThumb", "EditorIcons");
  642. }
  643. preview_vb->hide();
  644. } else {
  645. item_list->set_icon_mode(ItemList::ICON_MODE_LEFT);
  646. item_list->set_max_columns(1);
  647. item_list->set_max_text_lines(1);
  648. item_list->set_fixed_column_width(0);
  649. item_list->set_fixed_icon_size(Size2());
  650. if (preview->get_texture().is_valid()) {
  651. preview_vb->show();
  652. }
  653. }
  654. String cdir = dir_access->get_current_dir();
  655. dir_access->list_dir_begin();
  656. Ref<Texture> folder = get_icon("folder", "FileDialog");
  657. const Color folder_color = get_color("folder_icon_modulate", "FileDialog");
  658. List<String> files;
  659. List<String> dirs;
  660. String item;
  661. while ((item = dir_access->get_next()) != "") {
  662. if (item == "." || item == "..") {
  663. continue;
  664. }
  665. if (show_hidden_files) {
  666. if (!dir_access->current_is_dir()) {
  667. files.push_back(item);
  668. } else {
  669. dirs.push_back(item);
  670. }
  671. } else if (!dir_access->current_is_hidden()) {
  672. String full_path = cdir == "res://" ? item : dir_access->get_current_dir() + "/" + item;
  673. if (dir_access->current_is_dir() && (!EditorFileSystem::_should_skip_directory(full_path) || Main::is_project_manager())) {
  674. dirs.push_back(item);
  675. } else {
  676. files.push_back(item);
  677. }
  678. }
  679. }
  680. dirs.sort_custom<NaturalNoCaseComparator>();
  681. files.sort_custom<NaturalNoCaseComparator>();
  682. while (!dirs.empty()) {
  683. const String &dir_name = dirs.front()->get();
  684. item_list->add_item(dir_name);
  685. if (display_mode == DISPLAY_THUMBNAILS) {
  686. item_list->set_item_icon(item_list->get_item_count() - 1, folder_thumbnail);
  687. } else {
  688. item_list->set_item_icon(item_list->get_item_count() - 1, folder);
  689. }
  690. Dictionary d;
  691. d["name"] = dir_name;
  692. d["path"] = cdir.plus_file(dir_name);
  693. d["dir"] = true;
  694. item_list->set_item_metadata(item_list->get_item_count() - 1, d);
  695. item_list->set_item_icon_modulate(item_list->get_item_count() - 1, folder_color);
  696. dirs.pop_front();
  697. }
  698. List<String> patterns;
  699. // build filter
  700. if (filter->get_selected() == filter->get_item_count() - 1) {
  701. // match all
  702. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  703. // match all filters
  704. for (int i = 0; i < filters.size(); i++) {
  705. String f = filters[i].get_slice(";", 0);
  706. for (int j = 0; j < f.get_slice_count(","); j++) {
  707. patterns.push_back(f.get_slice(",", j).strip_edges());
  708. }
  709. }
  710. } else {
  711. int idx = filter->get_selected();
  712. if (filters.size() > 1) {
  713. idx--;
  714. }
  715. if (idx >= 0 && idx < filters.size()) {
  716. String f = filters[idx].get_slice(";", 0);
  717. for (int j = 0; j < f.get_slice_count(","); j++) {
  718. patterns.push_back(f.get_slice(",", j).strip_edges());
  719. }
  720. }
  721. }
  722. while (!files.empty()) {
  723. bool match = patterns.empty();
  724. for (List<String>::Element *E = patterns.front(); E; E = E->next()) {
  725. if (files.front()->get().matchn(E->get())) {
  726. match = true;
  727. break;
  728. }
  729. }
  730. if (match) {
  731. item_list->add_item(files.front()->get());
  732. if (get_icon_func) {
  733. Ref<Texture> icon = get_icon_func(cdir.plus_file(files.front()->get()));
  734. if (display_mode == DISPLAY_THUMBNAILS) {
  735. item_list->set_item_icon(item_list->get_item_count() - 1, file_thumbnail);
  736. item_list->set_item_tag_icon(item_list->get_item_count() - 1, icon);
  737. } else {
  738. item_list->set_item_icon(item_list->get_item_count() - 1, icon);
  739. }
  740. }
  741. Dictionary d;
  742. d["name"] = files.front()->get();
  743. d["dir"] = false;
  744. String fullpath = cdir.plus_file(files.front()->get());
  745. d["path"] = fullpath;
  746. item_list->set_item_metadata(item_list->get_item_count() - 1, d);
  747. if (display_mode == DISPLAY_THUMBNAILS) {
  748. EditorResourcePreview::get_singleton()->queue_resource_preview(fullpath, this, "_thumbnail_result", fullpath);
  749. }
  750. if (file->get_text() == files.front()->get()) {
  751. item_list->set_current(item_list->get_item_count() - 1);
  752. }
  753. }
  754. files.pop_front();
  755. }
  756. if (favorites->get_current() >= 0) {
  757. favorites->unselect(favorites->get_current());
  758. }
  759. favorite->set_pressed(false);
  760. fav_up->set_disabled(true);
  761. fav_down->set_disabled(true);
  762. get_ok()->set_disabled(_is_open_should_be_disabled());
  763. for (int i = 0; i < favorites->get_item_count(); i++) {
  764. if (favorites->get_item_metadata(i) == cdir || favorites->get_item_metadata(i) == cdir + "/") {
  765. favorites->select(i);
  766. favorite->set_pressed(true);
  767. if (i > 0) {
  768. fav_up->set_disabled(false);
  769. }
  770. if (i < favorites->get_item_count() - 1) {
  771. fav_down->set_disabled(false);
  772. }
  773. break;
  774. }
  775. }
  776. }
  777. void EditorFileDialog::_filter_selected(int) {
  778. update_file_name();
  779. update_file_list();
  780. }
  781. void EditorFileDialog::update_filters() {
  782. filter->clear();
  783. if (filters.size() > 1) {
  784. String all_filters;
  785. const int max_filters = 5;
  786. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  787. String flt = filters[i].get_slice(";", 0).strip_edges();
  788. if (i > 0) {
  789. all_filters += ", ";
  790. }
  791. all_filters += flt;
  792. }
  793. if (max_filters < filters.size()) {
  794. all_filters += ", ...";
  795. }
  796. filter->add_item(TTR("All Recognized") + " (" + all_filters + ")");
  797. }
  798. for (int i = 0; i < filters.size(); i++) {
  799. String flt = filters[i].get_slice(";", 0).strip_edges();
  800. String desc = filters[i].get_slice(";", 1).strip_edges();
  801. if (desc.length()) {
  802. filter->add_item(desc + " (" + flt + ")");
  803. } else {
  804. filter->add_item("(" + flt + ")");
  805. }
  806. }
  807. filter->add_item(TTR("All Files (*)"));
  808. }
  809. void EditorFileDialog::clear_filters() {
  810. filters.clear();
  811. update_filters();
  812. invalidate();
  813. }
  814. void EditorFileDialog::add_filter(const String &p_filter) {
  815. filters.push_back(p_filter);
  816. update_filters();
  817. invalidate();
  818. }
  819. String EditorFileDialog::get_current_dir() const {
  820. return dir_access->get_current_dir();
  821. }
  822. String EditorFileDialog::get_current_file() const {
  823. return file->get_text();
  824. }
  825. String EditorFileDialog::get_current_path() const {
  826. return dir_access->get_current_dir().plus_file(file->get_text());
  827. }
  828. void EditorFileDialog::set_current_dir(const String &p_dir) {
  829. if (p_dir.is_rel_path()) {
  830. dir_access->change_dir(OS::get_singleton()->get_resource_dir());
  831. }
  832. dir_access->change_dir(p_dir);
  833. update_dir();
  834. invalidate();
  835. }
  836. void EditorFileDialog::set_current_file(const String &p_file) {
  837. file->set_text(p_file);
  838. update_dir();
  839. invalidate();
  840. int lp = p_file.rfind(".");
  841. if (lp != -1) {
  842. file->select(0, lp);
  843. file->grab_focus();
  844. }
  845. if (is_visible_in_tree()) {
  846. _request_single_thumbnail(get_current_dir().plus_file(get_current_file()));
  847. }
  848. }
  849. void EditorFileDialog::set_current_path(const String &p_path) {
  850. if (!p_path.size()) {
  851. return;
  852. }
  853. int pos = MAX(p_path.rfind("/"), p_path.rfind("\\"));
  854. if (pos == -1) {
  855. set_current_file(p_path);
  856. } else {
  857. String dir = p_path.substr(0, pos);
  858. String file = p_path.substr(pos + 1, p_path.length());
  859. set_current_dir(dir);
  860. set_current_file(file);
  861. }
  862. }
  863. void EditorFileDialog::set_mode(Mode p_mode) {
  864. mode = p_mode;
  865. switch (mode) {
  866. case MODE_OPEN_FILE:
  867. get_ok()->set_text(TTR("Open"));
  868. set_title(TTR("Open a File"));
  869. can_create_dir = false;
  870. break;
  871. case MODE_OPEN_FILES:
  872. get_ok()->set_text(TTR("Open"));
  873. set_title(TTR("Open File(s)"));
  874. can_create_dir = false;
  875. break;
  876. case MODE_OPEN_DIR:
  877. get_ok()->set_text(TTR("Open"));
  878. set_title(TTR("Open a Directory"));
  879. can_create_dir = true;
  880. break;
  881. case MODE_OPEN_ANY:
  882. get_ok()->set_text(TTR("Open"));
  883. set_title(TTR("Open a File or Directory"));
  884. can_create_dir = true;
  885. break;
  886. case MODE_SAVE_FILE:
  887. get_ok()->set_text(TTR("Save"));
  888. set_title(TTR("Save a File"));
  889. can_create_dir = true;
  890. break;
  891. }
  892. if (mode == MODE_OPEN_FILES) {
  893. item_list->set_select_mode(ItemList::SELECT_MULTI);
  894. } else {
  895. item_list->set_select_mode(ItemList::SELECT_SINGLE);
  896. }
  897. if (can_create_dir) {
  898. makedir->show();
  899. } else {
  900. makedir->hide();
  901. }
  902. }
  903. EditorFileDialog::Mode EditorFileDialog::get_mode() const {
  904. return mode;
  905. }
  906. void EditorFileDialog::set_access(Access p_access) {
  907. ERR_FAIL_INDEX(p_access, 3);
  908. if (access == p_access) {
  909. return;
  910. }
  911. memdelete(dir_access);
  912. switch (p_access) {
  913. case ACCESS_FILESYSTEM: {
  914. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  915. } break;
  916. case ACCESS_RESOURCES: {
  917. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  918. } break;
  919. case ACCESS_USERDATA: {
  920. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  921. } break;
  922. }
  923. access = p_access;
  924. _update_drives();
  925. invalidate();
  926. update_filters();
  927. update_dir();
  928. }
  929. void EditorFileDialog::invalidate() {
  930. if (is_visible_in_tree()) {
  931. update_file_list();
  932. _update_favorites();
  933. invalidated = false;
  934. } else {
  935. invalidated = true;
  936. }
  937. }
  938. EditorFileDialog::Access EditorFileDialog::get_access() const {
  939. return access;
  940. }
  941. void EditorFileDialog::_make_dir_confirm() {
  942. String dir_name = makedirname->get_text().strip_edges();
  943. if (dir_name.begins_with(".")) {
  944. mkdirerr->set_text(TTR("Could not use a name with a leading dot."));
  945. mkdirerr->popup_centered_minsize(Size2(250, 50) * EDSCALE);
  946. return;
  947. }
  948. Error err = dir_access->make_dir(dir_name);
  949. if (err == OK) {
  950. dir_access->change_dir(dir_name);
  951. invalidate();
  952. update_filters();
  953. update_dir();
  954. _push_history();
  955. EditorFileSystem::get_singleton()->scan_changes(); //we created a dir, so rescan changes
  956. } else {
  957. mkdirerr->popup_centered_minsize(Size2(250, 50) * EDSCALE);
  958. }
  959. makedirname->set_text(""); // reset label
  960. }
  961. void EditorFileDialog::_make_dir() {
  962. makedialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  963. makedirname->grab_focus();
  964. }
  965. void EditorFileDialog::_delete_items() {
  966. // Collect the selected folders and files to delete and check them in the deletion dependency dialog.
  967. Vector<String> folders;
  968. Vector<String> files;
  969. for (int i = 0; i < item_list->get_item_count(); i++) {
  970. if (!item_list->is_selected(i)) {
  971. continue;
  972. }
  973. Dictionary item_meta = item_list->get_item_metadata(i);
  974. if (item_meta["dir"]) {
  975. folders.push_back(item_meta["path"]);
  976. } else {
  977. files.push_back(item_meta["path"]);
  978. }
  979. }
  980. if (folders.size() + files.size() > 0) {
  981. remove_dialog->set_size(Size2(1, 1));
  982. remove_dialog->show(folders, files);
  983. }
  984. }
  985. void EditorFileDialog::_select_drive(int p_idx) {
  986. String d = drives->get_item_text(p_idx);
  987. dir_access->change_dir(d);
  988. file->set_text("");
  989. invalidate();
  990. update_dir();
  991. _push_history();
  992. }
  993. void EditorFileDialog::_update_drives(bool p_select) {
  994. int dc = dir_access->get_drive_count();
  995. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  996. drives->hide();
  997. } else {
  998. drives->clear();
  999. Node *dp = drives->get_parent();
  1000. if (dp) {
  1001. dp->remove_child(drives);
  1002. }
  1003. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  1004. dp->add_child(drives);
  1005. drives->show();
  1006. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  1007. String d = dir_access->get_drive(i);
  1008. drives->add_item(dir_access->get_drive(i));
  1009. }
  1010. if (p_select) {
  1011. drives->select(dir_access->get_current_drive());
  1012. }
  1013. }
  1014. }
  1015. void EditorFileDialog::_favorite_selected(int p_idx) {
  1016. dir_access->change_dir(favorites->get_item_metadata(p_idx));
  1017. update_dir();
  1018. invalidate();
  1019. _push_history();
  1020. }
  1021. void EditorFileDialog::_favorite_move_up() {
  1022. int current = favorites->get_current();
  1023. if (current > 0 && current < favorites->get_item_count()) {
  1024. Vector<String> favorited = EditorSettings::get_singleton()->get_favorites();
  1025. int a_idx = favorited.find(String(favorites->get_item_metadata(current - 1)));
  1026. int b_idx = favorited.find(String(favorites->get_item_metadata(current)));
  1027. if (a_idx == -1 || b_idx == -1) {
  1028. return;
  1029. }
  1030. SWAP(favorited.write[a_idx], favorited.write[b_idx]);
  1031. EditorSettings::get_singleton()->set_favorites(favorited);
  1032. _update_favorites();
  1033. update_file_list();
  1034. }
  1035. }
  1036. void EditorFileDialog::_favorite_move_down() {
  1037. int current = favorites->get_current();
  1038. if (current >= 0 && current < favorites->get_item_count() - 1) {
  1039. Vector<String> favorited = EditorSettings::get_singleton()->get_favorites();
  1040. int a_idx = favorited.find(String(favorites->get_item_metadata(current + 1)));
  1041. int b_idx = favorited.find(String(favorites->get_item_metadata(current)));
  1042. if (a_idx == -1 || b_idx == -1) {
  1043. return;
  1044. }
  1045. SWAP(favorited.write[a_idx], favorited.write[b_idx]);
  1046. EditorSettings::get_singleton()->set_favorites(favorited);
  1047. _update_favorites();
  1048. update_file_list();
  1049. }
  1050. }
  1051. void EditorFileDialog::_update_favorites() {
  1052. bool res = access == ACCESS_RESOURCES;
  1053. String current = get_current_dir();
  1054. Ref<Texture> folder_icon = get_icon("Folder", "EditorIcons");
  1055. const Color folder_color = get_color("folder_icon_modulate", "FileDialog");
  1056. favorites->clear();
  1057. favorite->set_pressed(false);
  1058. Vector<String> favorited = EditorSettings::get_singleton()->get_favorites();
  1059. bool fav_changed = false;
  1060. for (int i = favorited.size() - 1; i >= 0; i--) {
  1061. if (!dir_access->dir_exists(favorited[i])) {
  1062. favorited.remove(i);
  1063. fav_changed = true;
  1064. }
  1065. }
  1066. if (fav_changed) {
  1067. EditorSettings::get_singleton()->set_favorites(favorited);
  1068. }
  1069. for (int i = 0; i < favorited.size(); i++) {
  1070. bool cres = favorited[i].begins_with("res://");
  1071. if (cres != res) {
  1072. continue;
  1073. }
  1074. String name = favorited[i];
  1075. bool setthis = false;
  1076. if (res && name == "res://") {
  1077. if (name == current) {
  1078. setthis = true;
  1079. }
  1080. name = "/";
  1081. favorites->add_item(name, folder_icon);
  1082. } else if (name.ends_with("/")) {
  1083. if (name == current || name == current + "/") {
  1084. setthis = true;
  1085. }
  1086. name = name.substr(0, name.length() - 1);
  1087. name = name.get_file();
  1088. favorites->add_item(name, folder_icon);
  1089. } else {
  1090. continue; // We don't handle favorite files here.
  1091. }
  1092. favorites->set_item_metadata(favorites->get_item_count() - 1, favorited[i]);
  1093. favorites->set_item_icon_modulate(favorites->get_item_count() - 1, folder_color);
  1094. if (setthis) {
  1095. favorite->set_pressed(true);
  1096. favorites->set_current(favorites->get_item_count() - 1);
  1097. recent->unselect_all();
  1098. }
  1099. }
  1100. }
  1101. void EditorFileDialog::_favorite_pressed() {
  1102. bool res = access == ACCESS_RESOURCES;
  1103. String cd = get_current_dir();
  1104. if (!cd.ends_with("/")) {
  1105. cd += "/";
  1106. }
  1107. Vector<String> favorited = EditorSettings::get_singleton()->get_favorites();
  1108. bool found = false;
  1109. for (int i = 0; i < favorited.size(); i++) {
  1110. bool cres = favorited[i].begins_with("res://");
  1111. if (cres != res) {
  1112. continue;
  1113. }
  1114. if (favorited[i] == cd) {
  1115. found = true;
  1116. break;
  1117. }
  1118. }
  1119. if (found) {
  1120. favorited.erase(cd);
  1121. } else {
  1122. favorited.push_back(cd);
  1123. }
  1124. EditorSettings::get_singleton()->set_favorites(favorited);
  1125. _update_favorites();
  1126. }
  1127. void EditorFileDialog::_recent_selected(int p_idx) {
  1128. Vector<String> recentd = EditorSettings::get_singleton()->get_recent_dirs();
  1129. ERR_FAIL_INDEX(p_idx, recentd.size());
  1130. dir_access->change_dir(recent->get_item_metadata(p_idx));
  1131. update_file_list();
  1132. update_dir();
  1133. _push_history();
  1134. }
  1135. void EditorFileDialog::_go_up() {
  1136. dir_access->change_dir(get_current_dir().get_base_dir());
  1137. update_file_list();
  1138. update_dir();
  1139. _push_history();
  1140. }
  1141. void EditorFileDialog::_go_back() {
  1142. if (local_history_pos <= 0) {
  1143. return;
  1144. }
  1145. local_history_pos--;
  1146. dir_access->change_dir(local_history[local_history_pos]);
  1147. update_file_list();
  1148. update_dir();
  1149. dir_prev->set_disabled(local_history_pos == 0);
  1150. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  1151. }
  1152. void EditorFileDialog::_go_forward() {
  1153. if (local_history_pos == local_history.size() - 1) {
  1154. return;
  1155. }
  1156. local_history_pos++;
  1157. dir_access->change_dir(local_history[local_history_pos]);
  1158. update_file_list();
  1159. update_dir();
  1160. dir_prev->set_disabled(local_history_pos == 0);
  1161. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  1162. }
  1163. bool EditorFileDialog::default_show_hidden_files = false;
  1164. EditorFileDialog::DisplayMode EditorFileDialog::default_display_mode = DISPLAY_THUMBNAILS;
  1165. void EditorFileDialog::set_display_mode(DisplayMode p_mode) {
  1166. if (display_mode == p_mode) {
  1167. return;
  1168. }
  1169. if (p_mode == DISPLAY_THUMBNAILS) {
  1170. mode_list->set_pressed(false);
  1171. mode_thumbnails->set_pressed(true);
  1172. } else {
  1173. mode_thumbnails->set_pressed(false);
  1174. mode_list->set_pressed(true);
  1175. }
  1176. display_mode = p_mode;
  1177. invalidate();
  1178. }
  1179. EditorFileDialog::DisplayMode EditorFileDialog::get_display_mode() const {
  1180. return display_mode;
  1181. }
  1182. void EditorFileDialog::_bind_methods() {
  1183. ClassDB::bind_method(D_METHOD("_unhandled_input"), &EditorFileDialog::_unhandled_input);
  1184. ClassDB::bind_method(D_METHOD("_item_selected"), &EditorFileDialog::_item_selected);
  1185. ClassDB::bind_method(D_METHOD("_multi_selected"), &EditorFileDialog::_multi_selected);
  1186. ClassDB::bind_method(D_METHOD("_items_clear_selection"), &EditorFileDialog::_items_clear_selection);
  1187. ClassDB::bind_method(D_METHOD("_item_list_item_rmb_selected"), &EditorFileDialog::_item_list_item_rmb_selected);
  1188. ClassDB::bind_method(D_METHOD("_item_list_rmb_clicked"), &EditorFileDialog::_item_list_rmb_clicked);
  1189. ClassDB::bind_method(D_METHOD("_item_menu_id_pressed"), &EditorFileDialog::_item_menu_id_pressed);
  1190. ClassDB::bind_method(D_METHOD("_item_db_selected"), &EditorFileDialog::_item_dc_selected);
  1191. ClassDB::bind_method(D_METHOD("_dir_entered"), &EditorFileDialog::_dir_entered);
  1192. ClassDB::bind_method(D_METHOD("_file_entered"), &EditorFileDialog::_file_entered);
  1193. ClassDB::bind_method(D_METHOD("_action_pressed"), &EditorFileDialog::_action_pressed);
  1194. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &EditorFileDialog::_cancel_pressed);
  1195. ClassDB::bind_method(D_METHOD("_filter_selected"), &EditorFileDialog::_filter_selected);
  1196. ClassDB::bind_method(D_METHOD("_save_confirm_pressed"), &EditorFileDialog::_save_confirm_pressed);
  1197. ClassDB::bind_method(D_METHOD("clear_filters"), &EditorFileDialog::clear_filters);
  1198. ClassDB::bind_method(D_METHOD("add_filter", "filter"), &EditorFileDialog::add_filter);
  1199. ClassDB::bind_method(D_METHOD("get_current_dir"), &EditorFileDialog::get_current_dir);
  1200. ClassDB::bind_method(D_METHOD("get_current_file"), &EditorFileDialog::get_current_file);
  1201. ClassDB::bind_method(D_METHOD("get_current_path"), &EditorFileDialog::get_current_path);
  1202. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &EditorFileDialog::set_current_dir);
  1203. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &EditorFileDialog::set_current_file);
  1204. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &EditorFileDialog::set_current_path);
  1205. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &EditorFileDialog::set_mode);
  1206. ClassDB::bind_method(D_METHOD("get_mode"), &EditorFileDialog::get_mode);
  1207. ClassDB::bind_method(D_METHOD("get_vbox"), &EditorFileDialog::get_vbox);
  1208. ClassDB::bind_method(D_METHOD("set_access", "access"), &EditorFileDialog::set_access);
  1209. ClassDB::bind_method(D_METHOD("get_access"), &EditorFileDialog::get_access);
  1210. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &EditorFileDialog::set_show_hidden_files);
  1211. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &EditorFileDialog::is_showing_hidden_files);
  1212. ClassDB::bind_method(D_METHOD("_select_drive"), &EditorFileDialog::_select_drive);
  1213. ClassDB::bind_method(D_METHOD("_make_dir"), &EditorFileDialog::_make_dir);
  1214. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &EditorFileDialog::_make_dir_confirm);
  1215. ClassDB::bind_method(D_METHOD("_update_file_name"), &EditorFileDialog::update_file_name);
  1216. ClassDB::bind_method(D_METHOD("_update_file_list"), &EditorFileDialog::update_file_list);
  1217. ClassDB::bind_method(D_METHOD("_update_dir"), &EditorFileDialog::update_dir);
  1218. ClassDB::bind_method(D_METHOD("_thumbnail_done"), &EditorFileDialog::_thumbnail_done);
  1219. ClassDB::bind_method(D_METHOD("set_display_mode", "mode"), &EditorFileDialog::set_display_mode);
  1220. ClassDB::bind_method(D_METHOD("get_display_mode"), &EditorFileDialog::get_display_mode);
  1221. ClassDB::bind_method(D_METHOD("_thumbnail_result"), &EditorFileDialog::_thumbnail_result);
  1222. ClassDB::bind_method(D_METHOD("set_disable_overwrite_warning", "disable"), &EditorFileDialog::set_disable_overwrite_warning);
  1223. ClassDB::bind_method(D_METHOD("is_overwrite_warning_disabled"), &EditorFileDialog::is_overwrite_warning_disabled);
  1224. ClassDB::bind_method(D_METHOD("_recent_selected"), &EditorFileDialog::_recent_selected);
  1225. ClassDB::bind_method(D_METHOD("_go_back"), &EditorFileDialog::_go_back);
  1226. ClassDB::bind_method(D_METHOD("_go_forward"), &EditorFileDialog::_go_forward);
  1227. ClassDB::bind_method(D_METHOD("_go_up"), &EditorFileDialog::_go_up);
  1228. ClassDB::bind_method(D_METHOD("_favorite_pressed"), &EditorFileDialog::_favorite_pressed);
  1229. ClassDB::bind_method(D_METHOD("_favorite_selected"), &EditorFileDialog::_favorite_selected);
  1230. ClassDB::bind_method(D_METHOD("_favorite_move_up"), &EditorFileDialog::_favorite_move_up);
  1231. ClassDB::bind_method(D_METHOD("_favorite_move_down"), &EditorFileDialog::_favorite_move_down);
  1232. ClassDB::bind_method(D_METHOD("invalidate"), &EditorFileDialog::invalidate);
  1233. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  1234. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::POOL_STRING_ARRAY, "paths")));
  1235. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  1236. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access");
  1237. ADD_PROPERTY(PropertyInfo(Variant::INT, "display_mode", PROPERTY_HINT_ENUM, "Thumbnails,List"), "set_display_mode", "get_display_mode");
  1238. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Open one,Open many,Open folder,Open any,Save"), "set_mode", "get_mode");
  1239. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir", PROPERTY_HINT_DIR, "", 0), "set_current_dir", "get_current_dir");
  1240. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file", PROPERTY_HINT_FILE, "*", 0), "set_current_file", "get_current_file");
  1241. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path", PROPERTY_HINT_NONE, "", 0), "set_current_path", "get_current_path");
  1242. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  1243. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_overwrite_warning"), "set_disable_overwrite_warning", "is_overwrite_warning_disabled");
  1244. BIND_ENUM_CONSTANT(MODE_OPEN_FILE);
  1245. BIND_ENUM_CONSTANT(MODE_OPEN_FILES);
  1246. BIND_ENUM_CONSTANT(MODE_OPEN_DIR);
  1247. BIND_ENUM_CONSTANT(MODE_OPEN_ANY);
  1248. BIND_ENUM_CONSTANT(MODE_SAVE_FILE);
  1249. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  1250. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  1251. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  1252. BIND_ENUM_CONSTANT(DISPLAY_THUMBNAILS);
  1253. BIND_ENUM_CONSTANT(DISPLAY_LIST);
  1254. }
  1255. void EditorFileDialog::set_show_hidden_files(bool p_show) {
  1256. if (p_show == show_hidden_files) {
  1257. return;
  1258. }
  1259. EditorSettings::get_singleton()->set("filesystem/file_dialog/show_hidden_files", p_show);
  1260. show_hidden_files = p_show;
  1261. show_hidden->set_pressed(p_show);
  1262. invalidate();
  1263. }
  1264. bool EditorFileDialog::is_showing_hidden_files() const {
  1265. return show_hidden_files;
  1266. }
  1267. void EditorFileDialog::set_default_show_hidden_files(bool p_show) {
  1268. default_show_hidden_files = p_show;
  1269. }
  1270. void EditorFileDialog::set_default_display_mode(DisplayMode p_mode) {
  1271. default_display_mode = p_mode;
  1272. }
  1273. void EditorFileDialog::_save_to_recent() {
  1274. String dir = get_current_dir();
  1275. Vector<String> recent = EditorSettings::get_singleton()->get_recent_dirs();
  1276. const int max = 20;
  1277. int count = 0;
  1278. bool res = dir.begins_with("res://");
  1279. for (int i = 0; i < recent.size(); i++) {
  1280. bool cres = recent[i].begins_with("res://");
  1281. if (recent[i] == dir || (res == cres && count > max)) {
  1282. recent.remove(i);
  1283. i--;
  1284. } else {
  1285. count++;
  1286. }
  1287. }
  1288. recent.insert(0, dir);
  1289. EditorSettings::get_singleton()->set_recent_dirs(recent);
  1290. }
  1291. void EditorFileDialog::set_disable_overwrite_warning(bool p_disable) {
  1292. disable_overwrite_warning = p_disable;
  1293. }
  1294. bool EditorFileDialog::is_overwrite_warning_disabled() const {
  1295. return disable_overwrite_warning;
  1296. }
  1297. EditorFileDialog::EditorFileDialog() {
  1298. set_resizable(true);
  1299. show_hidden_files = default_show_hidden_files;
  1300. display_mode = default_display_mode;
  1301. local_history_pos = 0;
  1302. disable_overwrite_warning = false;
  1303. VBoxContainer *vbc = memnew(VBoxContainer);
  1304. add_child(vbc);
  1305. mode = MODE_SAVE_FILE;
  1306. set_title(TTR("Save a File"));
  1307. ED_SHORTCUT("file_dialog/go_back", TTR("Go Back"), KEY_MASK_ALT | KEY_LEFT);
  1308. ED_SHORTCUT("file_dialog/go_forward", TTR("Go Forward"), KEY_MASK_ALT | KEY_RIGHT);
  1309. ED_SHORTCUT("file_dialog/go_up", TTR("Go Up"), KEY_MASK_ALT | KEY_UP);
  1310. ED_SHORTCUT("file_dialog/refresh", TTR("Refresh"), KEY_F5);
  1311. ED_SHORTCUT("file_dialog/toggle_hidden_files", TTR("Toggle Hidden Files"), KEY_MASK_CMD | KEY_H);
  1312. ED_SHORTCUT("file_dialog/toggle_favorite", TTR("Toggle Favorite"), KEY_MASK_ALT | KEY_F);
  1313. ED_SHORTCUT("file_dialog/toggle_mode", TTR("Toggle Mode"), KEY_MASK_ALT | KEY_V);
  1314. ED_SHORTCUT("file_dialog/create_folder", TTR("Create Folder"), KEY_MASK_CMD | KEY_N);
  1315. ED_SHORTCUT("file_dialog/delete", TTR("Delete"), KEY_DELETE);
  1316. ED_SHORTCUT("file_dialog/focus_path", TTR("Focus Path"), KEY_MASK_CMD | KEY_D);
  1317. ED_SHORTCUT("file_dialog/move_favorite_up", TTR("Move Favorite Up"), KEY_MASK_CMD | KEY_UP);
  1318. ED_SHORTCUT("file_dialog/move_favorite_down", TTR("Move Favorite Down"), KEY_MASK_CMD | KEY_DOWN);
  1319. HBoxContainer *pathhb = memnew(HBoxContainer);
  1320. dir_prev = memnew(ToolButton);
  1321. dir_prev->set_tooltip(TTR("Go to previous folder."));
  1322. dir_next = memnew(ToolButton);
  1323. dir_next->set_tooltip(TTR("Go to next folder."));
  1324. dir_up = memnew(ToolButton);
  1325. dir_up->set_tooltip(TTR("Go to parent folder."));
  1326. pathhb->add_child(dir_prev);
  1327. pathhb->add_child(dir_next);
  1328. pathhb->add_child(dir_up);
  1329. dir_prev->connect("pressed", this, "_go_back");
  1330. dir_next->connect("pressed", this, "_go_forward");
  1331. dir_up->connect("pressed", this, "_go_up");
  1332. pathhb->add_child(memnew(Label(TTR("Path:"))));
  1333. drives_container = memnew(HBoxContainer);
  1334. pathhb->add_child(drives_container);
  1335. drives = memnew(OptionButton);
  1336. drives->connect("item_selected", this, "_select_drive");
  1337. pathhb->add_child(drives);
  1338. dir = memnew(LineEdit);
  1339. pathhb->add_child(dir);
  1340. dir->set_h_size_flags(SIZE_EXPAND_FILL);
  1341. refresh = memnew(ToolButton);
  1342. refresh->set_tooltip(TTR("Refresh files."));
  1343. refresh->connect("pressed", this, "_update_file_list");
  1344. pathhb->add_child(refresh);
  1345. favorite = memnew(ToolButton);
  1346. favorite->set_toggle_mode(true);
  1347. favorite->set_tooltip(TTR("(Un)favorite current folder."));
  1348. favorite->connect("pressed", this, "_favorite_pressed");
  1349. pathhb->add_child(favorite);
  1350. show_hidden = memnew(ToolButton);
  1351. show_hidden->set_toggle_mode(true);
  1352. show_hidden->set_pressed(is_showing_hidden_files());
  1353. show_hidden->set_tooltip(TTR("Toggle the visibility of hidden files."));
  1354. show_hidden->connect("toggled", this, "set_show_hidden_files");
  1355. pathhb->add_child(show_hidden);
  1356. pathhb->add_child(memnew(VSeparator));
  1357. Ref<ButtonGroup> view_mode_group;
  1358. view_mode_group.instance();
  1359. mode_thumbnails = memnew(ToolButton);
  1360. mode_thumbnails->connect("pressed", this, "set_display_mode", varray(DISPLAY_THUMBNAILS));
  1361. mode_thumbnails->set_toggle_mode(true);
  1362. mode_thumbnails->set_pressed(display_mode == DISPLAY_THUMBNAILS);
  1363. mode_thumbnails->set_button_group(view_mode_group);
  1364. mode_thumbnails->set_tooltip(TTR("View items as a grid of thumbnails."));
  1365. pathhb->add_child(mode_thumbnails);
  1366. mode_list = memnew(ToolButton);
  1367. mode_list->connect("pressed", this, "set_display_mode", varray(DISPLAY_LIST));
  1368. mode_list->set_toggle_mode(true);
  1369. mode_list->set_pressed(display_mode == DISPLAY_LIST);
  1370. mode_list->set_button_group(view_mode_group);
  1371. mode_list->set_tooltip(TTR("View items as a list."));
  1372. pathhb->add_child(mode_list);
  1373. shortcuts_container = memnew(HBoxContainer);
  1374. pathhb->add_child(shortcuts_container);
  1375. makedir = memnew(Button);
  1376. makedir->set_text(TTR("Create Folder"));
  1377. makedir->connect("pressed", this, "_make_dir");
  1378. pathhb->add_child(makedir);
  1379. list_hb = memnew(HSplitContainer);
  1380. vbc->add_child(pathhb);
  1381. vbc->add_child(list_hb);
  1382. list_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1383. VSplitContainer *vsc = memnew(VSplitContainer);
  1384. list_hb->add_child(vsc);
  1385. VBoxContainer *fav_vb = memnew(VBoxContainer);
  1386. vsc->add_child(fav_vb);
  1387. fav_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
  1388. fav_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1389. HBoxContainer *fav_hb = memnew(HBoxContainer);
  1390. fav_vb->add_child(fav_hb);
  1391. fav_hb->add_child(memnew(Label(TTR("Favorites:"))));
  1392. fav_hb->add_spacer();
  1393. fav_up = memnew(ToolButton);
  1394. fav_hb->add_child(fav_up);
  1395. fav_up->connect("pressed", this, "_favorite_move_up");
  1396. fav_down = memnew(ToolButton);
  1397. fav_hb->add_child(fav_down);
  1398. fav_down->connect("pressed", this, "_favorite_move_down");
  1399. favorites = memnew(ItemList);
  1400. fav_vb->add_child(favorites);
  1401. favorites->set_v_size_flags(SIZE_EXPAND_FILL);
  1402. favorites->connect("item_selected", this, "_favorite_selected");
  1403. VBoxContainer *rec_vb = memnew(VBoxContainer);
  1404. vsc->add_child(rec_vb);
  1405. rec_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
  1406. rec_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1407. recent = memnew(ItemList);
  1408. recent->set_allow_reselect(true);
  1409. rec_vb->add_margin_child(TTR("Recent:"), recent, true);
  1410. recent->connect("item_selected", this, "_recent_selected");
  1411. VBoxContainer *item_vb = memnew(VBoxContainer);
  1412. list_hb->add_child(item_vb);
  1413. item_vb->set_custom_minimum_size(Size2(320, 0) * EDSCALE);
  1414. HBoxContainer *preview_hb = memnew(HBoxContainer);
  1415. preview_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1416. item_vb->add_child(preview_hb);
  1417. VBoxContainer *list_vb = memnew(VBoxContainer);
  1418. list_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1419. list_vb->add_child(memnew(Label(TTR("Directories & Files:"))));
  1420. preview_hb->add_child(list_vb);
  1421. // Item (files and folders) list with context menu.
  1422. item_list = memnew(ItemList);
  1423. item_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1424. item_list->connect("item_rmb_selected", this, "_item_list_item_rmb_selected");
  1425. item_list->connect("rmb_clicked", this, "_item_list_rmb_clicked");
  1426. item_list->set_allow_rmb_select(true);
  1427. list_vb->add_child(item_list);
  1428. item_menu = memnew(PopupMenu);
  1429. item_menu->connect("id_pressed", this, "_item_menu_id_pressed");
  1430. add_child(item_menu);
  1431. // Other stuff.
  1432. preview_vb = memnew(VBoxContainer);
  1433. preview_hb->add_child(preview_vb);
  1434. CenterContainer *prev_cc = memnew(CenterContainer);
  1435. preview_vb->add_margin_child(TTR("Preview:"), prev_cc);
  1436. preview = memnew(TextureRect);
  1437. prev_cc->add_child(preview);
  1438. preview_vb->hide();
  1439. file_box = memnew(HBoxContainer);
  1440. file_box->add_child(memnew(Label(TTR("File:"))));
  1441. file = memnew(LineEdit);
  1442. file->set_stretch_ratio(4);
  1443. file->set_h_size_flags(SIZE_EXPAND_FILL);
  1444. file->set_deselect_on_focus_loss_enabled(false);
  1445. file_box->add_child(file);
  1446. filter = memnew(OptionButton);
  1447. filter->set_stretch_ratio(3);
  1448. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1449. filter->set_clip_text(true); // Too many extensions overflow it.
  1450. file_box->add_child(filter);
  1451. file_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1452. item_vb->add_child(file_box);
  1453. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1454. access = ACCESS_RESOURCES;
  1455. _update_drives();
  1456. connect("confirmed", this, "_action_pressed");
  1457. item_list->connect("item_selected", this, "_item_selected", varray(), CONNECT_DEFERRED);
  1458. item_list->connect("multi_selected", this, "_multi_selected", varray(), CONNECT_DEFERRED);
  1459. item_list->connect("item_activated", this, "_item_db_selected", varray());
  1460. item_list->connect("nothing_selected", this, "_items_clear_selection");
  1461. dir->connect("text_entered", this, "_dir_entered");
  1462. file->connect("text_entered", this, "_file_entered");
  1463. filter->connect("item_selected", this, "_filter_selected");
  1464. confirm_save = memnew(ConfirmationDialog);
  1465. confirm_save->set_as_toplevel(true);
  1466. add_child(confirm_save);
  1467. confirm_save->connect("confirmed", this, "_save_confirm_pressed");
  1468. remove_dialog = memnew(DependencyRemoveDialog);
  1469. add_child(remove_dialog);
  1470. makedialog = memnew(ConfirmationDialog);
  1471. makedialog->set_title(TTR("Create Folder"));
  1472. VBoxContainer *makevb = memnew(VBoxContainer);
  1473. makedialog->add_child(makevb);
  1474. makedirname = memnew(LineEdit);
  1475. makevb->add_margin_child(TTR("Name:"), makedirname);
  1476. add_child(makedialog);
  1477. makedialog->register_text_enter(makedirname);
  1478. makedialog->connect("confirmed", this, "_make_dir_confirm");
  1479. mkdirerr = memnew(AcceptDialog);
  1480. mkdirerr->set_text(TTR("Could not create folder."));
  1481. add_child(mkdirerr);
  1482. update_filters();
  1483. update_dir();
  1484. set_hide_on_ok(false);
  1485. vbox = vbc;
  1486. invalidated = true;
  1487. if (register_func) {
  1488. register_func(this);
  1489. }
  1490. preview_wheel_timeout = 0;
  1491. preview_wheel_index = 0;
  1492. preview_waiting = false;
  1493. }
  1494. EditorFileDialog::~EditorFileDialog() {
  1495. if (unregister_func) {
  1496. unregister_func(this);
  1497. }
  1498. memdelete(dir_access);
  1499. }
  1500. void EditorLineEditFileChooser::_notification(int p_what) {
  1501. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1502. button->set_icon(get_icon("Folder", "EditorIcons"));
  1503. }
  1504. }
  1505. void EditorLineEditFileChooser::_bind_methods() {
  1506. ClassDB::bind_method(D_METHOD("_browse"), &EditorLineEditFileChooser::_browse);
  1507. ClassDB::bind_method(D_METHOD("_chosen"), &EditorLineEditFileChooser::_chosen);
  1508. ClassDB::bind_method(D_METHOD("get_button"), &EditorLineEditFileChooser::get_button);
  1509. ClassDB::bind_method(D_METHOD("get_line_edit"), &EditorLineEditFileChooser::get_line_edit);
  1510. ClassDB::bind_method(D_METHOD("get_file_dialog"), &EditorLineEditFileChooser::get_file_dialog);
  1511. }
  1512. void EditorLineEditFileChooser::_chosen(const String &p_text) {
  1513. line_edit->set_text(p_text);
  1514. line_edit->emit_signal("text_entered", p_text);
  1515. }
  1516. void EditorLineEditFileChooser::_browse() {
  1517. dialog->popup_centered_ratio();
  1518. }
  1519. EditorLineEditFileChooser::EditorLineEditFileChooser() {
  1520. line_edit = memnew(LineEdit);
  1521. add_child(line_edit);
  1522. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1523. button = memnew(Button);
  1524. add_child(button);
  1525. button->connect("pressed", this, "_browse");
  1526. dialog = memnew(EditorFileDialog);
  1527. add_child(dialog);
  1528. dialog->connect("file_selected", this, "_chosen");
  1529. dialog->connect("dir_selected", this, "_chosen");
  1530. dialog->connect("files_selected", this, "_chosen");
  1531. }