editor_file_dialog.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_resource_preview.h"
  32. #include "editor_scale.h"
  33. #include "editor_settings.h"
  34. #include "os/file_access.h"
  35. #include "os/keyboard.h"
  36. #include "print_string.h"
  37. #include "scene/gui/center_container.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/gui/margin_container.h"
  40. EditorFileDialog::GetIconFunc EditorFileDialog::get_icon_func = NULL;
  41. EditorFileDialog::GetIconFunc EditorFileDialog::get_large_icon_func = NULL;
  42. EditorFileDialog::RegisterFunc EditorFileDialog::register_func = NULL;
  43. EditorFileDialog::RegisterFunc EditorFileDialog::unregister_func = NULL;
  44. VBoxContainer *EditorFileDialog::get_vbox() {
  45. return vbox;
  46. }
  47. void EditorFileDialog::_notification(int p_what) {
  48. if (p_what == NOTIFICATION_ENTER_TREE) {
  49. //_update_icons
  50. mode_thumbnails->set_icon(get_icon("FileThumbnail", "EditorIcons"));
  51. mode_list->set_icon(get_icon("FileList", "EditorIcons"));
  52. dir_prev->set_icon(get_icon("Back", "EditorIcons"));
  53. dir_next->set_icon(get_icon("Forward", "EditorIcons"));
  54. dir_up->set_icon(get_icon("ArrowUp", "EditorIcons"));
  55. refresh->set_icon(get_icon("Reload", "EditorIcons"));
  56. favorite->set_icon(get_icon("Favorites", "EditorIcons"));
  57. fav_up->set_icon(get_icon("MoveUp", "EditorIcons"));
  58. fav_down->set_icon(get_icon("MoveDown", "EditorIcons"));
  59. fav_rm->set_icon(get_icon("Remove", "EditorIcons"));
  60. } else if (p_what == NOTIFICATION_PROCESS) {
  61. if (preview_waiting) {
  62. preview_wheel_timeout -= get_process_delta_time();
  63. if (preview_wheel_timeout <= 0) {
  64. preview_wheel_index++;
  65. if (preview_wheel_index >= 8)
  66. preview_wheel_index = 0;
  67. Ref<Texture> frame = get_icon("WaitPreview" + itos(preview_wheel_index + 1), "EditorIcons");
  68. preview->set_texture(frame);
  69. preview_wheel_timeout = 0.1;
  70. }
  71. }
  72. } else if (p_what == NOTIFICATION_DRAW) {
  73. //RID ci = get_canvas_item();
  74. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  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 show_hidden = EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files");
  79. if (show_hidden_files != show_hidden)
  80. set_show_hidden_files(show_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. fav_rm->set_icon(get_icon("Remove", "EditorIcons"));
  93. update_file_list();
  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_toggled(favorite->is_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/focus_path", p_event)) {
  140. dir->grab_focus();
  141. handled = true;
  142. }
  143. if (ED_IS_SHORTCUT("file_dialog/move_favorite_up", p_event)) {
  144. _favorite_move_up();
  145. handled = true;
  146. }
  147. if (ED_IS_SHORTCUT("file_dialog/move_favorite_down", p_event)) {
  148. _favorite_move_down();
  149. handled = true;
  150. }
  151. if (handled) {
  152. accept_event();
  153. }
  154. }
  155. }
  156. }
  157. void EditorFileDialog::set_enable_multiple_selection(bool p_enable) {
  158. item_list->set_select_mode(p_enable ? ItemList::SELECT_MULTI : ItemList::SELECT_SINGLE);
  159. };
  160. Vector<String> EditorFileDialog::get_selected_files() const {
  161. Vector<String> list;
  162. for (int i = 0; i < item_list->get_item_count(); i++) {
  163. if (item_list->is_selected(i))
  164. list.push_back(item_list->get_item_text(i));
  165. }
  166. return list;
  167. };
  168. void EditorFileDialog::update_dir() {
  169. dir->set_text(dir_access->get_current_dir());
  170. }
  171. void EditorFileDialog::_dir_entered(String p_dir) {
  172. dir_access->change_dir(p_dir);
  173. file->set_text("");
  174. invalidate();
  175. update_dir();
  176. _push_history();
  177. }
  178. void EditorFileDialog::_file_entered(const String &p_file) {
  179. _action_pressed();
  180. }
  181. void EditorFileDialog::_save_confirm_pressed() {
  182. String f = dir_access->get_current_dir().plus_file(file->get_text());
  183. _save_to_recent();
  184. emit_signal("file_selected", f);
  185. hide();
  186. }
  187. void EditorFileDialog::_post_popup() {
  188. ConfirmationDialog::_post_popup();
  189. if (invalidated) {
  190. update_file_list();
  191. invalidated = false;
  192. }
  193. if (mode == MODE_SAVE_FILE)
  194. file->grab_focus();
  195. else
  196. item_list->grab_focus();
  197. if (is_visible_in_tree() && get_current_file() != "")
  198. _request_single_thumbnail(get_current_dir().plus_file(get_current_file()));
  199. if (is_visible_in_tree()) {
  200. Ref<Texture> folder = get_icon("folder", "FileDialog");
  201. recent->clear();
  202. bool res = access == ACCESS_RESOURCES;
  203. Vector<String> recentd = EditorSettings::get_singleton()->get_recent_dirs();
  204. for (int i = 0; i < recentd.size(); i++) {
  205. bool cres = recentd[i].begins_with("res://");
  206. if (cres != res)
  207. continue;
  208. String name = recentd[i];
  209. if (res && name == "res://") {
  210. name = "/";
  211. } else {
  212. name = name.get_file() + "/";
  213. }
  214. //print_line("file: "+name);
  215. recent->add_item(name, folder);
  216. recent->set_item_metadata(recent->get_item_count() - 1, recentd[i]);
  217. }
  218. local_history.clear();
  219. local_history_pos = -1;
  220. _push_history();
  221. _update_favorites();
  222. }
  223. set_process_unhandled_input(true);
  224. }
  225. void EditorFileDialog::_thumbnail_result(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata) {
  226. if (display_mode == DISPLAY_LIST || p_preview.is_null())
  227. return;
  228. for (int i = 0; i < item_list->get_item_count(); i++) {
  229. Dictionary d = item_list->get_item_metadata(i);
  230. String pname = d["path"];
  231. if (pname == p_path) {
  232. item_list->set_item_icon(i, p_preview);
  233. item_list->set_item_tag_icon(i, Ref<Texture>());
  234. }
  235. }
  236. }
  237. void EditorFileDialog::_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata) {
  238. set_process(false);
  239. preview_waiting = false;
  240. if (p_preview.is_valid() && get_current_path() == p_path) {
  241. preview->set_texture(p_preview);
  242. if (display_mode == DISPLAY_THUMBNAILS) {
  243. preview_vb->hide();
  244. } else {
  245. preview_vb->show();
  246. }
  247. } else {
  248. preview_vb->hide();
  249. preview->set_texture(Ref<Texture>());
  250. }
  251. }
  252. void EditorFileDialog::_request_single_thumbnail(const String &p_path) {
  253. if (!FileAccess::exists(p_path))
  254. return;
  255. EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, this, "_thumbnail_done", p_path);
  256. //print_line("want file "+p_path);
  257. set_process(true);
  258. preview_waiting = true;
  259. preview_wheel_timeout = 0;
  260. }
  261. void EditorFileDialog::_action_pressed() {
  262. if (mode == MODE_OPEN_FILES) {
  263. String fbase = dir_access->get_current_dir();
  264. PoolVector<String> files;
  265. for (int i = 0; i < item_list->get_item_count(); i++) {
  266. if (item_list->is_selected(i))
  267. files.push_back(fbase.plus_file(item_list->get_item_text(i)));
  268. }
  269. if (files.size()) {
  270. _save_to_recent();
  271. emit_signal("files_selected", files);
  272. hide();
  273. }
  274. return;
  275. }
  276. String f = dir_access->get_current_dir().plus_file(file->get_text());
  277. if ((mode == MODE_OPEN_ANY || mode == MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  278. _save_to_recent();
  279. emit_signal("file_selected", f);
  280. hide();
  281. } else if (mode == MODE_OPEN_ANY || mode == MODE_OPEN_DIR) {
  282. String path = dir_access->get_current_dir();
  283. path = path.replace("\\", "/");
  284. for (int i = 0; i < item_list->get_item_count(); i++) {
  285. if (item_list->is_selected(i)) {
  286. Dictionary d = item_list->get_item_metadata(i);
  287. if (d["dir"]) {
  288. path = path.plus_file(d["name"]);
  289. break;
  290. }
  291. }
  292. }
  293. _save_to_recent();
  294. emit_signal("dir_selected", path);
  295. hide();
  296. }
  297. if (mode == MODE_SAVE_FILE) {
  298. bool valid = false;
  299. if (filter->get_selected() == filter->get_item_count() - 1) {
  300. valid = true; //match none
  301. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  302. // match all filters
  303. for (int i = 0; i < filters.size(); i++) {
  304. String flt = filters[i].get_slice(";", 0);
  305. for (int j = 0; j < flt.get_slice_count(","); j++) {
  306. String str = flt.get_slice(",", j).strip_edges();
  307. if (f.match(str)) {
  308. valid = true;
  309. break;
  310. }
  311. }
  312. if (valid)
  313. break;
  314. }
  315. } else {
  316. int idx = filter->get_selected();
  317. if (filters.size() > 1)
  318. idx--;
  319. if (idx >= 0 && idx < filters.size()) {
  320. String flt = filters[idx].get_slice(";", 0);
  321. int filterSliceCount = flt.get_slice_count(",");
  322. for (int j = 0; j < filterSliceCount; j++) {
  323. String str = (flt.get_slice(",", j).strip_edges());
  324. if (f.match(str)) {
  325. valid = true;
  326. break;
  327. }
  328. }
  329. if (!valid && filterSliceCount > 0) {
  330. String str = (flt.get_slice(",", 0).strip_edges());
  331. f += str.substr(1, str.length() - 1);
  332. _request_single_thumbnail(get_current_dir().plus_file(f.get_file()));
  333. file->set_text(f.get_file());
  334. valid = true;
  335. }
  336. } else {
  337. valid = true;
  338. }
  339. }
  340. if (!valid) {
  341. exterr->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  342. return;
  343. }
  344. if (dir_access->file_exists(f) && !disable_overwrite_warning) {
  345. confirm_save->set_text(TTR("File Exists, Overwrite?"));
  346. confirm_save->popup_centered(Size2(200, 80));
  347. } else {
  348. _save_to_recent();
  349. emit_signal("file_selected", f);
  350. hide();
  351. }
  352. }
  353. }
  354. void EditorFileDialog::_cancel_pressed() {
  355. file->set_text("");
  356. invalidate();
  357. hide();
  358. }
  359. void EditorFileDialog::_item_selected(int p_item) {
  360. int current = p_item;
  361. if (current < 0 || current >= item_list->get_item_count())
  362. return;
  363. Dictionary d = item_list->get_item_metadata(current);
  364. if (!d["dir"]) {
  365. file->set_text(d["name"]);
  366. _request_single_thumbnail(get_current_dir().plus_file(get_current_file()));
  367. }
  368. }
  369. void EditorFileDialog::_push_history() {
  370. local_history.resize(local_history_pos + 1);
  371. String new_path = dir_access->get_current_dir();
  372. if (local_history.size() == 0 || new_path != local_history[local_history_pos]) {
  373. local_history.push_back(new_path);
  374. local_history_pos++;
  375. dir_prev->set_disabled(local_history_pos == 0);
  376. dir_next->set_disabled(true);
  377. }
  378. }
  379. void EditorFileDialog::_item_dc_selected(int p_item) {
  380. int current = p_item;
  381. if (current < 0 || current >= item_list->get_item_count())
  382. return;
  383. Dictionary d = item_list->get_item_metadata(current);
  384. if (d["dir"]) {
  385. dir_access->change_dir(d["name"]);
  386. call_deferred("_update_file_list");
  387. call_deferred("_update_dir");
  388. _push_history();
  389. } else {
  390. _action_pressed();
  391. }
  392. }
  393. void EditorFileDialog::update_file_list() {
  394. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  395. thumbnail_size *= EDSCALE;
  396. Ref<Texture> folder_thumbnail;
  397. Ref<Texture> file_thumbnail;
  398. item_list->clear();
  399. if (display_mode == DISPLAY_THUMBNAILS) {
  400. item_list->set_max_columns(0);
  401. item_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  402. item_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  403. item_list->set_max_text_lines(2);
  404. item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  405. if (thumbnail_size < 64) {
  406. folder_thumbnail = get_icon("FolderMediumThumb", "EditorIcons");
  407. file_thumbnail = get_icon("FileMediumThumb", "EditorIcons");
  408. } else {
  409. folder_thumbnail = get_icon("FolderBigThumb", "EditorIcons");
  410. file_thumbnail = get_icon("FileBigThumb", "EditorIcons");
  411. }
  412. preview_vb->hide();
  413. } else {
  414. item_list->set_icon_mode(ItemList::ICON_MODE_LEFT);
  415. item_list->set_max_columns(1);
  416. item_list->set_max_text_lines(1);
  417. item_list->set_fixed_column_width(0);
  418. item_list->set_fixed_icon_size(Size2());
  419. if (preview->get_texture().is_valid())
  420. preview_vb->show();
  421. }
  422. String cdir = dir_access->get_current_dir();
  423. bool skip_pp = access == ACCESS_RESOURCES && cdir == "res://";
  424. dir_access->list_dir_begin();
  425. Ref<Texture> folder = get_icon("folder", "FileDialog");
  426. List<String> files;
  427. List<String> dirs;
  428. bool isdir;
  429. bool ishidden;
  430. bool show_hidden = show_hidden_files;
  431. String item;
  432. while ((item = dir_access->get_next(&isdir)) != "") {
  433. ishidden = dir_access->current_is_hidden();
  434. if (show_hidden || !ishidden) {
  435. if (!isdir)
  436. files.push_back(item);
  437. else if (item != ".." || !skip_pp)
  438. dirs.push_back(item);
  439. }
  440. }
  441. if (dirs.find("..") == NULL) {
  442. //may happen if lacking permissions
  443. dirs.push_back("..");
  444. }
  445. dirs.sort_custom<NaturalNoCaseComparator>();
  446. files.sort_custom<NaturalNoCaseComparator>();
  447. while (!dirs.empty()) {
  448. const String &dir_name = dirs.front()->get();
  449. item_list->add_item(dir_name + "/");
  450. if (display_mode == DISPLAY_THUMBNAILS) {
  451. item_list->set_item_icon(item_list->get_item_count() - 1, folder_thumbnail);
  452. } else {
  453. item_list->set_item_icon(item_list->get_item_count() - 1, folder);
  454. }
  455. Dictionary d;
  456. d["name"] = dir_name;
  457. d["path"] = String();
  458. d["dir"] = true;
  459. item_list->set_item_metadata(item_list->get_item_count() - 1, d);
  460. dirs.pop_front();
  461. }
  462. dirs.clear();
  463. List<String> patterns;
  464. // build filter
  465. if (filter->get_selected() == filter->get_item_count() - 1) {
  466. // match all
  467. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  468. // match all filters
  469. for (int i = 0; i < filters.size(); i++) {
  470. String f = filters[i].get_slice(";", 0);
  471. for (int j = 0; j < f.get_slice_count(","); j++) {
  472. patterns.push_back(f.get_slice(",", j).strip_edges());
  473. }
  474. }
  475. } else {
  476. int idx = filter->get_selected();
  477. if (filters.size() > 1)
  478. idx--;
  479. if (idx >= 0 && idx < filters.size()) {
  480. String f = filters[idx].get_slice(";", 0);
  481. for (int j = 0; j < f.get_slice_count(","); j++) {
  482. patterns.push_back(f.get_slice(",", j).strip_edges());
  483. }
  484. }
  485. }
  486. String base_dir = dir_access->get_current_dir();
  487. while (!files.empty()) {
  488. bool match = patterns.empty();
  489. for (List<String>::Element *E = patterns.front(); E; E = E->next()) {
  490. if (files.front()->get().matchn(E->get())) {
  491. match = true;
  492. break;
  493. }
  494. }
  495. if (match) {
  496. //TreeItem *ti=tree->create_item(root);
  497. //ti->set_text(0,files.front()->get());
  498. item_list->add_item(files.front()->get());
  499. if (get_icon_func) {
  500. Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  501. //ti->set_icon(0,icon);
  502. if (display_mode == DISPLAY_THUMBNAILS) {
  503. item_list->set_item_icon(item_list->get_item_count() - 1, file_thumbnail);
  504. item_list->set_item_tag_icon(item_list->get_item_count() - 1, icon);
  505. } else {
  506. item_list->set_item_icon(item_list->get_item_count() - 1, icon);
  507. }
  508. }
  509. if (mode == MODE_OPEN_DIR) {
  510. //disabled mode?
  511. //ti->set_custom_color(0,get_color("files_disabled"));
  512. //ti->set_selectable(0,false);
  513. }
  514. Dictionary d;
  515. d["name"] = files.front()->get();
  516. d["dir"] = false;
  517. String fullpath = base_dir.plus_file(files.front()->get());
  518. if (display_mode == DISPLAY_THUMBNAILS) {
  519. EditorResourcePreview::get_singleton()->queue_resource_preview(fullpath, this, "_thumbnail_result", fullpath);
  520. }
  521. d["path"] = base_dir.plus_file(files.front()->get());
  522. //ti->set_metadata(0,d);
  523. item_list->set_item_metadata(item_list->get_item_count() - 1, d);
  524. if (file->get_text() == files.front()->get())
  525. item_list->set_current(item_list->get_item_count() - 1);
  526. }
  527. files.pop_front();
  528. }
  529. if (favorites->get_current() >= 0) {
  530. favorites->unselect(favorites->get_current());
  531. }
  532. favorite->set_pressed(false);
  533. fav_up->set_disabled(true);
  534. fav_down->set_disabled(true);
  535. for (int i = 0; i < favorites->get_item_count(); i++) {
  536. if (favorites->get_item_metadata(i) == base_dir) {
  537. favorites->select(i);
  538. favorite->set_pressed(true);
  539. if (i > 0) {
  540. fav_up->set_disabled(false);
  541. }
  542. if (i < favorites->get_item_count() - 1) {
  543. fav_down->set_disabled(false);
  544. }
  545. break;
  546. }
  547. }
  548. // ??
  549. /*
  550. if (tree->get_root() && tree->get_root()->get_children())
  551. tree->get_root()->get_children()->select(0);
  552. */
  553. files.clear();
  554. }
  555. void EditorFileDialog::_filter_selected(int) {
  556. update_file_list();
  557. }
  558. void EditorFileDialog::update_filters() {
  559. filter->clear();
  560. if (filters.size() > 1) {
  561. String all_filters;
  562. const int max_filters = 5;
  563. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  564. String flt = filters[i].get_slice(";", 0);
  565. if (i > 0)
  566. all_filters += ",";
  567. all_filters += flt;
  568. }
  569. if (max_filters < filters.size())
  570. all_filters += ", ...";
  571. filter->add_item(TTR("All Recognized") + " ( " + all_filters + " )");
  572. }
  573. for (int i = 0; i < filters.size(); i++) {
  574. String flt = filters[i].get_slice(";", 0).strip_edges();
  575. String desc = filters[i].get_slice(";", 1).strip_edges();
  576. if (desc.length())
  577. filter->add_item(desc + " ( " + flt + " )");
  578. else
  579. filter->add_item("( " + flt + " )");
  580. }
  581. filter->add_item(TTR("All Files (*)"));
  582. }
  583. void EditorFileDialog::clear_filters() {
  584. filters.clear();
  585. update_filters();
  586. invalidate();
  587. }
  588. void EditorFileDialog::add_filter(const String &p_filter) {
  589. filters.push_back(p_filter);
  590. update_filters();
  591. invalidate();
  592. }
  593. String EditorFileDialog::get_current_dir() const {
  594. return dir->get_text();
  595. }
  596. String EditorFileDialog::get_current_file() const {
  597. return file->get_text();
  598. }
  599. String EditorFileDialog::get_current_path() const {
  600. return dir->get_text().plus_file(file->get_text());
  601. }
  602. void EditorFileDialog::set_current_dir(const String &p_dir) {
  603. dir_access->change_dir(p_dir);
  604. update_dir();
  605. invalidate();
  606. //_push_history();
  607. }
  608. void EditorFileDialog::set_current_file(const String &p_file) {
  609. file->set_text(p_file);
  610. update_dir();
  611. invalidate();
  612. int lp = p_file.find_last(".");
  613. if (lp != -1) {
  614. file->select(0, lp);
  615. file->grab_focus();
  616. }
  617. if (is_visible_in_tree())
  618. _request_single_thumbnail(get_current_dir().plus_file(get_current_file()));
  619. }
  620. void EditorFileDialog::set_current_path(const String &p_path) {
  621. if (!p_path.size())
  622. return;
  623. int pos = MAX(p_path.find_last("/"), p_path.find_last("\\"));
  624. if (pos == -1) {
  625. set_current_file(p_path);
  626. } else {
  627. String dir = p_path.substr(0, pos);
  628. String file = p_path.substr(pos + 1, p_path.length());
  629. set_current_dir(dir);
  630. set_current_file(file);
  631. }
  632. }
  633. void EditorFileDialog::set_mode(Mode p_mode) {
  634. mode = p_mode;
  635. switch (mode) {
  636. case MODE_OPEN_FILE:
  637. get_ok()->set_text(TTR("Open"));
  638. set_title(TTR("Open a File"));
  639. makedir->hide();
  640. break;
  641. case MODE_OPEN_FILES:
  642. get_ok()->set_text(TTR("Open"));
  643. set_title(TTR("Open File(s)"));
  644. makedir->hide();
  645. break;
  646. case MODE_OPEN_DIR:
  647. get_ok()->set_text(TTR("Open"));
  648. set_title(TTR("Open a Directory"));
  649. makedir->show();
  650. break;
  651. case MODE_OPEN_ANY:
  652. get_ok()->set_text(TTR("Open"));
  653. set_title(TTR("Open a File or Directory"));
  654. makedir->show();
  655. break;
  656. case MODE_SAVE_FILE:
  657. get_ok()->set_text(TTR("Save"));
  658. set_title(TTR("Save a File"));
  659. makedir->show();
  660. break;
  661. }
  662. if (mode == MODE_OPEN_FILES) {
  663. item_list->set_select_mode(ItemList::SELECT_MULTI);
  664. } else {
  665. item_list->set_select_mode(ItemList::SELECT_SINGLE);
  666. }
  667. }
  668. EditorFileDialog::Mode EditorFileDialog::get_mode() const {
  669. return mode;
  670. }
  671. void EditorFileDialog::set_access(Access p_access) {
  672. ERR_FAIL_INDEX(p_access, 3);
  673. if (access == p_access)
  674. return;
  675. memdelete(dir_access);
  676. switch (p_access) {
  677. case ACCESS_FILESYSTEM: {
  678. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  679. } break;
  680. case ACCESS_RESOURCES: {
  681. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  682. } break;
  683. case ACCESS_USERDATA: {
  684. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  685. } break;
  686. }
  687. access = p_access;
  688. _update_drives();
  689. invalidate();
  690. update_filters();
  691. update_dir();
  692. }
  693. void EditorFileDialog::invalidate() {
  694. if (is_visible_in_tree()) {
  695. update_file_list();
  696. invalidated = false;
  697. } else {
  698. invalidated = true;
  699. }
  700. }
  701. EditorFileDialog::Access EditorFileDialog::get_access() const {
  702. return access;
  703. }
  704. void EditorFileDialog::_make_dir_confirm() {
  705. Error err = dir_access->make_dir(makedirname->get_text());
  706. if (err == OK) {
  707. dir_access->change_dir(makedirname->get_text());
  708. invalidate();
  709. update_filters();
  710. update_dir();
  711. _push_history();
  712. } else {
  713. mkdirerr->popup_centered_minsize(Size2(250, 50) * EDSCALE);
  714. }
  715. makedirname->set_text(""); // reset label
  716. }
  717. void EditorFileDialog::_make_dir() {
  718. makedialog->popup_centered_minsize(Size2(250, 80) * EDSCALE);
  719. makedirname->grab_focus();
  720. }
  721. void EditorFileDialog::_select_drive(int p_idx) {
  722. String d = drives->get_item_text(p_idx);
  723. dir_access->change_dir(d);
  724. file->set_text("");
  725. invalidate();
  726. update_dir();
  727. _push_history();
  728. }
  729. void EditorFileDialog::_update_drives() {
  730. int dc = dir_access->get_drive_count();
  731. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  732. drives->hide();
  733. } else {
  734. drives->clear();
  735. drives->show();
  736. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  737. String d = dir_access->get_drive(i);
  738. drives->add_item(dir_access->get_drive(i));
  739. }
  740. drives->select(dir_access->get_current_drive());
  741. }
  742. }
  743. void EditorFileDialog::_favorite_selected(int p_idx) {
  744. Vector<String> favorited = EditorSettings::get_singleton()->get_favorite_dirs();
  745. ERR_FAIL_INDEX(p_idx, favorited.size());
  746. dir_access->change_dir(favorited[p_idx]);
  747. file->set_text("");
  748. invalidate();
  749. update_dir();
  750. _push_history();
  751. }
  752. void EditorFileDialog::_favorite_move_up() {
  753. int current = favorites->get_current();
  754. if (current > 0 && current < favorites->get_item_count()) {
  755. Vector<String> favorited = EditorSettings::get_singleton()->get_favorite_dirs();
  756. int a_idx = favorited.find(String(favorites->get_item_metadata(current - 1)));
  757. int b_idx = favorited.find(String(favorites->get_item_metadata(current)));
  758. if (a_idx == -1 || b_idx == -1)
  759. return;
  760. SWAP(favorited[a_idx], favorited[b_idx]);
  761. EditorSettings::get_singleton()->set_favorite_dirs(favorited);
  762. _update_favorites();
  763. update_file_list();
  764. }
  765. }
  766. void EditorFileDialog::_favorite_move_down() {
  767. int current = favorites->get_current();
  768. if (current >= 0 && current < favorites->get_item_count() - 1) {
  769. Vector<String> favorited = EditorSettings::get_singleton()->get_favorite_dirs();
  770. int a_idx = favorited.find(String(favorites->get_item_metadata(current + 1)));
  771. int b_idx = favorited.find(String(favorites->get_item_metadata(current)));
  772. if (a_idx == -1 || b_idx == -1)
  773. return;
  774. SWAP(favorited[a_idx], favorited[b_idx]);
  775. EditorSettings::get_singleton()->set_favorite_dirs(favorited);
  776. _update_favorites();
  777. update_file_list();
  778. }
  779. }
  780. void EditorFileDialog::_update_favorites() {
  781. bool res = access == ACCESS_RESOURCES;
  782. String current = get_current_dir();
  783. Ref<Texture> star = get_icon("Favorites", "EditorIcons");
  784. favorites->clear();
  785. favorite->set_pressed(false);
  786. Vector<String> favorited = EditorSettings::get_singleton()->get_favorite_dirs();
  787. for (int i = 0; i < favorited.size(); i++) {
  788. bool cres = favorited[i].begins_with("res://");
  789. if (cres != res)
  790. continue;
  791. String name = favorited[i];
  792. bool setthis = name == current;
  793. if (res && name == "res://") {
  794. name = "/";
  795. } else {
  796. name = name.get_file() + "/";
  797. }
  798. //print_line("file: "+name);
  799. favorites->add_item(name, star);
  800. favorites->set_item_metadata(favorites->get_item_count() - 1, favorited[i]);
  801. if (setthis) {
  802. favorite->set_pressed(true);
  803. favorites->set_current(favorites->get_item_count() - 1);
  804. }
  805. }
  806. }
  807. void EditorFileDialog::_favorite_toggled(bool p_toggle) {
  808. bool res = access == ACCESS_RESOURCES;
  809. String cd = get_current_dir();
  810. Vector<String> favorited = EditorSettings::get_singleton()->get_favorite_dirs();
  811. bool found = false;
  812. for (int i = 0; i < favorited.size(); i++) {
  813. bool cres = favorited[i].begins_with("res://");
  814. if (cres != res)
  815. continue;
  816. if (favorited[i] == cd) {
  817. found = true;
  818. break;
  819. }
  820. }
  821. if (found) {
  822. favorited.erase(cd);
  823. favorite->set_pressed(false);
  824. } else {
  825. favorited.push_back(cd);
  826. favorite->set_pressed(true);
  827. }
  828. EditorSettings::get_singleton()->set_favorite_dirs(favorited);
  829. _update_favorites();
  830. }
  831. void EditorFileDialog::_recent_selected(int p_idx) {
  832. Vector<String> recentd = EditorSettings::get_singleton()->get_recent_dirs();
  833. ERR_FAIL_INDEX(p_idx, recentd.size());
  834. dir_access->change_dir(recent->get_item_metadata(p_idx));
  835. update_file_list();
  836. update_dir();
  837. _push_history();
  838. }
  839. void EditorFileDialog::_go_up() {
  840. dir_access->change_dir("..");
  841. update_file_list();
  842. update_dir();
  843. _push_history();
  844. }
  845. void EditorFileDialog::_go_back() {
  846. if (local_history_pos <= 0) {
  847. return;
  848. }
  849. local_history_pos--;
  850. dir_access->change_dir(local_history[local_history_pos]);
  851. update_file_list();
  852. update_dir();
  853. dir_prev->set_disabled(local_history_pos == 0);
  854. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  855. }
  856. void EditorFileDialog::_go_forward() {
  857. if (local_history_pos == local_history.size() - 1) {
  858. return;
  859. }
  860. local_history_pos++;
  861. dir_access->change_dir(local_history[local_history_pos]);
  862. update_file_list();
  863. update_dir();
  864. dir_prev->set_disabled(local_history_pos == 0);
  865. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  866. }
  867. bool EditorFileDialog::default_show_hidden_files = false;
  868. EditorFileDialog::DisplayMode EditorFileDialog::default_display_mode = DISPLAY_THUMBNAILS;
  869. void EditorFileDialog::set_display_mode(DisplayMode p_mode) {
  870. if (display_mode == p_mode)
  871. return;
  872. if (p_mode == DISPLAY_THUMBNAILS) {
  873. mode_list->set_pressed(false);
  874. mode_thumbnails->set_pressed(true);
  875. } else {
  876. mode_thumbnails->set_pressed(false);
  877. mode_list->set_pressed(true);
  878. }
  879. display_mode = p_mode;
  880. invalidate();
  881. }
  882. EditorFileDialog::DisplayMode EditorFileDialog::get_display_mode() const {
  883. return display_mode;
  884. }
  885. void EditorFileDialog::_bind_methods() {
  886. ClassDB::bind_method(D_METHOD("_unhandled_input"), &EditorFileDialog::_unhandled_input);
  887. ClassDB::bind_method(D_METHOD("_item_selected"), &EditorFileDialog::_item_selected);
  888. ClassDB::bind_method(D_METHOD("_item_db_selected"), &EditorFileDialog::_item_dc_selected);
  889. ClassDB::bind_method(D_METHOD("_dir_entered"), &EditorFileDialog::_dir_entered);
  890. ClassDB::bind_method(D_METHOD("_file_entered"), &EditorFileDialog::_file_entered);
  891. ClassDB::bind_method(D_METHOD("_action_pressed"), &EditorFileDialog::_action_pressed);
  892. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &EditorFileDialog::_cancel_pressed);
  893. ClassDB::bind_method(D_METHOD("_filter_selected"), &EditorFileDialog::_filter_selected);
  894. ClassDB::bind_method(D_METHOD("_save_confirm_pressed"), &EditorFileDialog::_save_confirm_pressed);
  895. ClassDB::bind_method(D_METHOD("clear_filters"), &EditorFileDialog::clear_filters);
  896. ClassDB::bind_method(D_METHOD("add_filter", "filter"), &EditorFileDialog::add_filter);
  897. ClassDB::bind_method(D_METHOD("get_current_dir"), &EditorFileDialog::get_current_dir);
  898. ClassDB::bind_method(D_METHOD("get_current_file"), &EditorFileDialog::get_current_file);
  899. ClassDB::bind_method(D_METHOD("get_current_path"), &EditorFileDialog::get_current_path);
  900. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &EditorFileDialog::set_current_dir);
  901. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &EditorFileDialog::set_current_file);
  902. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &EditorFileDialog::set_current_path);
  903. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &EditorFileDialog::set_mode);
  904. ClassDB::bind_method(D_METHOD("get_mode"), &EditorFileDialog::get_mode);
  905. ClassDB::bind_method(D_METHOD("get_vbox"), &EditorFileDialog::get_vbox);
  906. ClassDB::bind_method(D_METHOD("set_access", "access"), &EditorFileDialog::set_access);
  907. ClassDB::bind_method(D_METHOD("get_access"), &EditorFileDialog::get_access);
  908. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &EditorFileDialog::set_show_hidden_files);
  909. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &EditorFileDialog::is_showing_hidden_files);
  910. ClassDB::bind_method(D_METHOD("_select_drive"), &EditorFileDialog::_select_drive);
  911. ClassDB::bind_method(D_METHOD("_make_dir"), &EditorFileDialog::_make_dir);
  912. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &EditorFileDialog::_make_dir_confirm);
  913. ClassDB::bind_method(D_METHOD("_update_file_list"), &EditorFileDialog::update_file_list);
  914. ClassDB::bind_method(D_METHOD("_update_dir"), &EditorFileDialog::update_dir);
  915. ClassDB::bind_method(D_METHOD("_thumbnail_done"), &EditorFileDialog::_thumbnail_done);
  916. ClassDB::bind_method(D_METHOD("set_display_mode", "mode"), &EditorFileDialog::set_display_mode);
  917. ClassDB::bind_method(D_METHOD("get_display_mode"), &EditorFileDialog::get_display_mode);
  918. ClassDB::bind_method(D_METHOD("_thumbnail_result"), &EditorFileDialog::_thumbnail_result);
  919. ClassDB::bind_method(D_METHOD("set_disable_overwrite_warning", "disable"), &EditorFileDialog::set_disable_overwrite_warning);
  920. ClassDB::bind_method(D_METHOD("is_overwrite_warning_disabled"), &EditorFileDialog::is_overwrite_warning_disabled);
  921. ClassDB::bind_method(D_METHOD("_recent_selected"), &EditorFileDialog::_recent_selected);
  922. ClassDB::bind_method(D_METHOD("_go_back"), &EditorFileDialog::_go_back);
  923. ClassDB::bind_method(D_METHOD("_go_forward"), &EditorFileDialog::_go_forward);
  924. ClassDB::bind_method(D_METHOD("_go_up"), &EditorFileDialog::_go_up);
  925. ClassDB::bind_method(D_METHOD("_favorite_toggled"), &EditorFileDialog::_favorite_toggled);
  926. ClassDB::bind_method(D_METHOD("_favorite_selected"), &EditorFileDialog::_favorite_selected);
  927. ClassDB::bind_method(D_METHOD("_favorite_move_up"), &EditorFileDialog::_favorite_move_up);
  928. ClassDB::bind_method(D_METHOD("_favorite_move_down"), &EditorFileDialog::_favorite_move_down);
  929. ClassDB::bind_method(D_METHOD("invalidate"), &EditorFileDialog::invalidate);
  930. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  931. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::POOL_STRING_ARRAY, "paths")));
  932. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  933. BIND_ENUM_CONSTANT(MODE_OPEN_FILE);
  934. BIND_ENUM_CONSTANT(MODE_OPEN_FILES);
  935. BIND_ENUM_CONSTANT(MODE_OPEN_DIR);
  936. BIND_ENUM_CONSTANT(MODE_OPEN_ANY);
  937. BIND_ENUM_CONSTANT(MODE_SAVE_FILE);
  938. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  939. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  940. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  941. BIND_ENUM_CONSTANT(DISPLAY_THUMBNAILS);
  942. BIND_ENUM_CONSTANT(DISPLAY_LIST);
  943. }
  944. void EditorFileDialog::set_show_hidden_files(bool p_show) {
  945. show_hidden_files = p_show;
  946. invalidate();
  947. }
  948. bool EditorFileDialog::is_showing_hidden_files() const {
  949. return show_hidden_files;
  950. }
  951. void EditorFileDialog::set_default_show_hidden_files(bool p_show) {
  952. default_show_hidden_files = p_show;
  953. }
  954. void EditorFileDialog::set_default_display_mode(DisplayMode p_mode) {
  955. default_display_mode = p_mode;
  956. }
  957. void EditorFileDialog::_save_to_recent() {
  958. String dir = get_current_dir();
  959. Vector<String> recent = EditorSettings::get_singleton()->get_recent_dirs();
  960. const int max = 20;
  961. int count = 0;
  962. bool res = dir.begins_with("res://");
  963. for (int i = 0; i < recent.size(); i++) {
  964. bool cres = recent[i].begins_with("res://");
  965. if (recent[i] == dir || (res == cres && count > max)) {
  966. recent.remove(i);
  967. i--;
  968. } else {
  969. count++;
  970. }
  971. }
  972. recent.insert(0, dir);
  973. EditorSettings::get_singleton()->set_recent_dirs(recent);
  974. }
  975. void EditorFileDialog::set_disable_overwrite_warning(bool p_disable) {
  976. disable_overwrite_warning = p_disable;
  977. }
  978. bool EditorFileDialog::is_overwrite_warning_disabled() const {
  979. return disable_overwrite_warning;
  980. }
  981. EditorFileDialog::EditorFileDialog() {
  982. show_hidden_files = default_show_hidden_files;
  983. display_mode = default_display_mode;
  984. local_history_pos = 0;
  985. disable_overwrite_warning = false;
  986. VBoxContainer *vbc = memnew(VBoxContainer);
  987. add_child(vbc);
  988. mode = MODE_SAVE_FILE;
  989. set_title(TTR("Save a File"));
  990. ED_SHORTCUT("file_dialog/go_back", TTR("Go Back"), KEY_MASK_ALT | KEY_LEFT);
  991. ED_SHORTCUT("file_dialog/go_forward", TTR("Go Forward"), KEY_MASK_ALT | KEY_RIGHT);
  992. ED_SHORTCUT("file_dialog/go_up", TTR("Go Up"), KEY_MASK_ALT | KEY_UP);
  993. ED_SHORTCUT("file_dialog/refresh", TTR("Refresh"), KEY_F5);
  994. ED_SHORTCUT("file_dialog/toggle_hidden_files", TTR("Toggle Hidden Files"), KEY_MASK_CMD | KEY_H);
  995. ED_SHORTCUT("file_dialog/toggle_favorite", TTR("Toggle Favorite"), KEY_MASK_ALT | KEY_F);
  996. ED_SHORTCUT("file_dialog/toggle_mode", TTR("Toggle Mode"), KEY_MASK_ALT | KEY_V);
  997. ED_SHORTCUT("file_dialog/create_folder", TTR("Create Folder"), KEY_MASK_CMD | KEY_N);
  998. ED_SHORTCUT("file_dialog/focus_path", TTR("Focus Path"), KEY_MASK_CMD | KEY_D);
  999. ED_SHORTCUT("file_dialog/move_favorite_up", TTR("Move Favorite Up"), KEY_MASK_CMD | KEY_UP);
  1000. ED_SHORTCUT("file_dialog/move_favorite_down", TTR("Move Favorite Down"), KEY_MASK_CMD | KEY_DOWN);
  1001. HBoxContainer *pathhb = memnew(HBoxContainer);
  1002. dir_prev = memnew(ToolButton);
  1003. dir_next = memnew(ToolButton);
  1004. dir_up = memnew(ToolButton);
  1005. dir_up->set_tooltip(TTR("Go to parent folder"));
  1006. pathhb->add_child(dir_prev);
  1007. pathhb->add_child(dir_next);
  1008. pathhb->add_child(dir_up);
  1009. dir_prev->connect("pressed", this, "_go_back");
  1010. dir_next->connect("pressed", this, "_go_forward");
  1011. dir_up->connect("pressed", this, "_go_up");
  1012. pathhb->add_child(memnew(Label(TTR("Path:"))));
  1013. dir = memnew(LineEdit);
  1014. pathhb->add_child(dir);
  1015. dir->set_h_size_flags(SIZE_EXPAND_FILL);
  1016. refresh = memnew(ToolButton);
  1017. refresh->connect("pressed", this, "_update_file_list");
  1018. pathhb->add_child(refresh);
  1019. favorite = memnew(ToolButton);
  1020. favorite->set_flat(true);
  1021. favorite->set_toggle_mode(true);
  1022. favorite->connect("toggled", this, "_favorite_toggled");
  1023. pathhb->add_child(favorite);
  1024. Ref<ButtonGroup> view_mode_group;
  1025. view_mode_group.instance();
  1026. mode_thumbnails = memnew(ToolButton);
  1027. mode_thumbnails->connect("pressed", this, "set_display_mode", varray(DISPLAY_THUMBNAILS));
  1028. mode_thumbnails->set_toggle_mode(true);
  1029. mode_thumbnails->set_pressed(display_mode == DISPLAY_THUMBNAILS);
  1030. mode_thumbnails->set_button_group(view_mode_group);
  1031. pathhb->add_child(mode_thumbnails);
  1032. mode_list = memnew(ToolButton);
  1033. mode_list->connect("pressed", this, "set_display_mode", varray(DISPLAY_LIST));
  1034. mode_list->set_toggle_mode(true);
  1035. mode_list->set_pressed(display_mode == DISPLAY_LIST);
  1036. mode_list->set_button_group(view_mode_group);
  1037. pathhb->add_child(mode_list);
  1038. drives = memnew(OptionButton);
  1039. pathhb->add_child(drives);
  1040. drives->connect("item_selected", this, "_select_drive");
  1041. makedir = memnew(Button);
  1042. makedir->set_text(TTR("Create Folder"));
  1043. makedir->connect("pressed", this, "_make_dir");
  1044. pathhb->add_child(makedir);
  1045. list_hb = memnew(HSplitContainer);
  1046. vbc->add_child(pathhb);
  1047. vbc->add_child(list_hb);
  1048. list_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1049. VBoxContainer *fav_vb = memnew(VBoxContainer);
  1050. list_hb->add_child(fav_vb);
  1051. HBoxContainer *fav_hb = memnew(HBoxContainer);
  1052. fav_vb->add_child(fav_hb);
  1053. fav_hb->add_child(memnew(Label(TTR("Favorites:"))));
  1054. fav_hb->add_spacer();
  1055. fav_up = memnew(ToolButton);
  1056. fav_hb->add_child(fav_up);
  1057. fav_up->connect("pressed", this, "_favorite_move_up");
  1058. fav_down = memnew(ToolButton);
  1059. fav_hb->add_child(fav_down);
  1060. fav_down->connect("pressed", this, "_favorite_move_down");
  1061. fav_rm = memnew(ToolButton);
  1062. fav_hb->add_child(fav_rm);
  1063. fav_rm->hide(); // redundant
  1064. MarginContainer *fav_mv = memnew(MarginContainer);
  1065. fav_vb->add_child(fav_mv);
  1066. fav_mv->set_v_size_flags(SIZE_EXPAND_FILL);
  1067. favorites = memnew(ItemList);
  1068. fav_mv->add_child(favorites);
  1069. favorites->connect("item_selected", this, "_favorite_selected");
  1070. recent = memnew(ItemList);
  1071. fav_vb->add_margin_child(TTR("Recent:"), recent, true);
  1072. recent->connect("item_selected", this, "_recent_selected");
  1073. VBoxContainer *item_vb = memnew(VBoxContainer);
  1074. list_hb->add_child(item_vb);
  1075. HBoxContainer *preview_hb = memnew(HBoxContainer);
  1076. preview_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1077. item_vb->add_child(preview_hb);
  1078. VBoxContainer *list_vb = memnew(VBoxContainer);
  1079. list_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1080. list_vb->add_child(memnew(Label(TTR("Directories & Files:"))));
  1081. preview_hb->add_child(list_vb);
  1082. item_list = memnew(ItemList);
  1083. item_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1084. list_vb->add_child(item_list);
  1085. preview_vb = memnew(VBoxContainer);
  1086. preview_hb->add_child(preview_vb);
  1087. CenterContainer *prev_cc = memnew(CenterContainer);
  1088. preview_vb->add_margin_child(TTR("Preview:"), prev_cc);
  1089. preview = memnew(TextureRect);
  1090. prev_cc->add_child(preview);
  1091. preview_vb->hide();
  1092. HBoxContainer *filename_hbc = memnew(HBoxContainer);
  1093. filename_hbc->add_child(memnew(Label(TTR("File:"))));
  1094. file = memnew(LineEdit);
  1095. file->set_stretch_ratio(4);
  1096. file->set_h_size_flags(SIZE_EXPAND_FILL);
  1097. filename_hbc->add_child(file);
  1098. filter = memnew(OptionButton);
  1099. filter->set_stretch_ratio(3);
  1100. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1101. filter->set_clip_text(true); //too many extensions overflow it
  1102. filename_hbc->add_child(filter);
  1103. filename_hbc->set_h_size_flags(SIZE_EXPAND_FILL);
  1104. item_vb->add_child(filename_hbc);
  1105. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1106. access = ACCESS_RESOURCES;
  1107. _update_drives();
  1108. connect("confirmed", this, "_action_pressed");
  1109. //cancel->connect("pressed", this,"_cancel_pressed");
  1110. item_list->connect("item_selected", this, "_item_selected", varray(), CONNECT_DEFERRED);
  1111. item_list->connect("item_activated", this, "_item_db_selected", varray());
  1112. dir->connect("text_entered", this, "_dir_entered");
  1113. file->connect("text_entered", this, "_file_entered");
  1114. filter->connect("item_selected", this, "_filter_selected");
  1115. confirm_save = memnew(ConfirmationDialog);
  1116. confirm_save->set_as_toplevel(true);
  1117. add_child(confirm_save);
  1118. confirm_save->connect("confirmed", this, "_save_confirm_pressed");
  1119. makedialog = memnew(ConfirmationDialog);
  1120. makedialog->set_title(TTR("Create Folder"));
  1121. VBoxContainer *makevb = memnew(VBoxContainer);
  1122. makedialog->add_child(makevb);
  1123. makedirname = memnew(LineEdit);
  1124. makevb->add_margin_child(TTR("Name:"), makedirname);
  1125. add_child(makedialog);
  1126. makedialog->register_text_enter(makedirname);
  1127. makedialog->connect("confirmed", this, "_make_dir_confirm");
  1128. mkdirerr = memnew(AcceptDialog);
  1129. mkdirerr->set_text(TTR("Could not create folder."));
  1130. add_child(mkdirerr);
  1131. exterr = memnew(AcceptDialog);
  1132. exterr->set_text(TTR("Must use a valid extension."));
  1133. add_child(exterr);
  1134. //update_file_list();
  1135. update_filters();
  1136. update_dir();
  1137. set_hide_on_ok(false);
  1138. vbox = vbc;
  1139. invalidated = true;
  1140. if (register_func)
  1141. register_func(this);
  1142. preview_wheel_timeout = 0;
  1143. preview_wheel_index = 0;
  1144. preview_waiting = false;
  1145. }
  1146. EditorFileDialog::~EditorFileDialog() {
  1147. if (unregister_func)
  1148. unregister_func(this);
  1149. memdelete(dir_access);
  1150. }
  1151. void EditorLineEditFileChooser::_bind_methods() {
  1152. ClassDB::bind_method(D_METHOD("_browse"), &EditorLineEditFileChooser::_browse);
  1153. ClassDB::bind_method(D_METHOD("_chosen"), &EditorLineEditFileChooser::_chosen);
  1154. ClassDB::bind_method(D_METHOD("get_button"), &EditorLineEditFileChooser::get_button);
  1155. ClassDB::bind_method(D_METHOD("get_line_edit"), &EditorLineEditFileChooser::get_line_edit);
  1156. ClassDB::bind_method(D_METHOD("get_file_dialog"), &EditorLineEditFileChooser::get_file_dialog);
  1157. }
  1158. void EditorLineEditFileChooser::_chosen(const String &p_text) {
  1159. line_edit->set_text(p_text);
  1160. line_edit->emit_signal("text_entered", p_text);
  1161. }
  1162. void EditorLineEditFileChooser::_browse() {
  1163. dialog->popup_centered_ratio();
  1164. }
  1165. EditorLineEditFileChooser::EditorLineEditFileChooser() {
  1166. line_edit = memnew(LineEdit);
  1167. add_child(line_edit);
  1168. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1169. button = memnew(Button);
  1170. button->set_text(" .. ");
  1171. add_child(button);
  1172. button->connect("pressed", this, "_browse");
  1173. dialog = memnew(EditorFileDialog);
  1174. add_child(dialog);
  1175. dialog->connect("file_selected", this, "_chosen");
  1176. dialog->connect("dir_selected", this, "_chosen");
  1177. dialog->connect("files_selected", this, "_chosen");
  1178. }