editor_file_dialog.cpp 42 KB

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