editor_file_dialog.cpp 51 KB

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