editor_file_dialog.cpp 54 KB

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