dlg_settings.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. --Luanti
  2. --Copyright (C) 2022 rubenwardy
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. local component_funcs = dofile(core.get_mainmenu_path() .. DIR_DELIM ..
  18. "settings" .. DIR_DELIM .. "components.lua")
  19. local shadows_component = dofile(core.get_mainmenu_path() .. DIR_DELIM ..
  20. "settings" .. DIR_DELIM .. "shadows_component.lua")
  21. local loaded = false
  22. local full_settings
  23. local info_icon_path = core.formspec_escape(defaulttexturedir .. "settings_info.png")
  24. local reset_icon_path = core.formspec_escape(defaulttexturedir .. "settings_reset.png")
  25. local all_pages = {}
  26. local page_by_id = {}
  27. local filtered_pages = all_pages
  28. local filtered_page_by_id = page_by_id
  29. local function get_setting_info(name)
  30. for _, entry in ipairs(full_settings) do
  31. if entry.type ~= "category" and entry.name == name then
  32. return entry
  33. end
  34. end
  35. return nil
  36. end
  37. local function add_page(page)
  38. assert(type(page.id) == "string")
  39. assert(type(page.title) == "string")
  40. assert(page.section == nil or type(page.section) == "string")
  41. assert(type(page.content) == "table")
  42. assert(not page_by_id[page.id], "Page " .. page.id .. " already registered")
  43. all_pages[#all_pages + 1] = page
  44. page_by_id[page.id] = page
  45. return page
  46. end
  47. local function load_settingtypes()
  48. local page = nil
  49. local section = nil
  50. local function ensure_page_started()
  51. if not page then
  52. page = add_page({
  53. id = (section or "general"):lower():gsub(" ", "_"),
  54. title = section or fgettext_ne("General"),
  55. section = section,
  56. content = {},
  57. })
  58. end
  59. end
  60. for _, entry in ipairs(full_settings) do
  61. if entry.type == "category" then
  62. if entry.level == 0 then
  63. section = entry.name
  64. page = nil
  65. elseif entry.level == 1 then
  66. page = {
  67. id = ((section and section .. "_" or "") .. entry.name):lower():gsub(" ", "_"),
  68. title = entry.readable_name or entry.name,
  69. section = section,
  70. content = {},
  71. }
  72. page = add_page(page)
  73. elseif entry.level == 2 then
  74. ensure_page_started()
  75. page.content[#page.content + 1] = {
  76. heading = fgettext_ne(entry.readable_name or entry.name),
  77. }
  78. end
  79. else
  80. ensure_page_started()
  81. page.content[#page.content + 1] = entry.name
  82. end
  83. end
  84. end
  85. local function load()
  86. if loaded then
  87. return
  88. end
  89. loaded = true
  90. full_settings = settingtypes.parse_config_file(false, true)
  91. local change_keys = {
  92. query_text = "Controls",
  93. requires = {
  94. touch_controls = false,
  95. },
  96. get_formspec = function(self, avail_w)
  97. local btn_w = math.min(avail_w, 3)
  98. return ("button[0,0;%f,0.8;btn_change_keys;%s]"):format(btn_w, fgettext("Controls")), 0.8
  99. end,
  100. on_submit = function(self, fields)
  101. if fields.btn_change_keys then
  102. core.show_keys_menu()
  103. end
  104. end,
  105. }
  106. add_page({
  107. id = "accessibility",
  108. title = fgettext_ne("Accessibility"),
  109. content = {
  110. "language",
  111. { heading = fgettext_ne("General") },
  112. "font_size",
  113. "chat_font_size",
  114. "gui_scaling",
  115. "hud_scaling",
  116. "show_nametag_backgrounds",
  117. { heading = fgettext_ne("Chat") },
  118. "console_height",
  119. "console_alpha",
  120. "console_color",
  121. { heading = fgettext_ne("Controls") },
  122. "autojump",
  123. "safe_dig_and_place",
  124. { heading = fgettext_ne("Movement") },
  125. "arm_inertia",
  126. "view_bobbing_amount",
  127. "fall_bobbing_amount",
  128. },
  129. })
  130. load_settingtypes()
  131. table.insert(page_by_id.controls_keyboard_and_mouse.content, 1, change_keys)
  132. do
  133. local content = page_by_id.graphics_and_audio_effects.content
  134. local idx = table.indexof(content, "enable_dynamic_shadows")
  135. table.insert(content, idx, shadows_component)
  136. idx = table.indexof(content, "enable_auto_exposure") + 1
  137. local note = component_funcs.note(fgettext_ne("(The game will need to enable automatic exposure as well)"))
  138. note.requires = get_setting_info("enable_auto_exposure").requires
  139. table.insert(content, idx, note)
  140. idx = table.indexof(content, "enable_bloom") + 1
  141. note = component_funcs.note(fgettext_ne("(The game will need to enable bloom as well)"))
  142. note.requires = get_setting_info("enable_bloom").requires
  143. table.insert(content, idx, note)
  144. idx = table.indexof(content, "enable_volumetric_lighting") + 1
  145. note = component_funcs.note(fgettext_ne("(The game will need to enable volumetric lighting as well)"))
  146. note.requires = get_setting_info("enable_volumetric_lighting").requires
  147. table.insert(content, idx, note)
  148. end
  149. -- These must not be translated, as they need to show in the local
  150. -- language no matter the user's current language.
  151. -- This list must be kept in sync with src/unsupported_language_list.txt.
  152. get_setting_info("language").option_labels = {
  153. [""] = fgettext_ne("(Use system language)"),
  154. --ar = " [ar]", blacklisted
  155. be = "Беларуская [be]",
  156. bg = "Български [bg]",
  157. ca = "Català [ca]",
  158. cs = "Česky [cs]",
  159. cy = "Cymraeg [cy]",
  160. da = "Dansk [da]",
  161. de = "Deutsch [de]",
  162. --dv = " [dv]", blacklisted
  163. el = "Ελληνικά [el]",
  164. en = "English [en]",
  165. eo = "Esperanto [eo]",
  166. es = "Español [es]",
  167. et = "Eesti [et]",
  168. eu = "Euskara [eu]",
  169. fi = "Suomi [fi]",
  170. fil = "Wikang Filipino [fil]",
  171. fr = "Français [fr]",
  172. gd = "Gàidhlig [gd]",
  173. gl = "Galego [gl]",
  174. --he = " [he]", blacklisted
  175. --hi = " [hi]", blacklisted
  176. hu = "Magyar [hu]",
  177. id = "Bahasa Indonesia [id]",
  178. it = "Italiano [it]",
  179. ja = "日本語 [ja]",
  180. jbo = "Lojban [jbo]",
  181. kk = "Қазақша [kk]",
  182. --kn = " [kn]", blacklisted
  183. ko = "한국어 [ko]",
  184. ky = "Kırgızca / Кыргызча [ky]",
  185. lt = "Lietuvių [lt]",
  186. lv = "Latviešu [lv]",
  187. mn = "Монгол [mn]",
  188. mr = "मराठी [mr]",
  189. ms = "Bahasa Melayu [ms]",
  190. --ms_Arab = " [ms_Arab]", blacklisted
  191. nb = "Norsk Bokmål [nb]",
  192. nl = "Nederlands [nl]",
  193. nn = "Norsk Nynorsk [nn]",
  194. oc = "Occitan [oc]",
  195. pl = "Polski [pl]",
  196. pt = "Português [pt]",
  197. pt_BR = "Português do Brasil [pt_BR]",
  198. ro = "Română [ro]",
  199. ru = "Русский [ru]",
  200. sk = "Slovenčina [sk]",
  201. sl = "Slovenščina [sl]",
  202. sr_Cyrl = "Српски [sr_Cyrl]",
  203. sr_Latn = "Srpski (Latinica) [sr_Latn]",
  204. sv = "Svenska [sv]",
  205. sw = "Kiswahili [sw]",
  206. --th = " [th]", blacklisted
  207. tr = "Türkçe [tr]",
  208. tt = "Tatarça [tt]",
  209. uk = "Українська [uk]",
  210. vi = "Tiếng Việt [vi]",
  211. zh_CN = "中文 (简体) [zh_CN]",
  212. zh_TW = "正體中文 (繁體) [zh_TW]",
  213. }
  214. get_setting_info("touch_controls").option_labels = {
  215. ["auto"] = fgettext_ne("Auto"),
  216. ["true"] = fgettext_ne("Enabled"),
  217. ["false"] = fgettext_ne("Disabled"),
  218. }
  219. end
  220. -- See if setting matches keywords
  221. local function get_setting_match_weight(entry, query_keywords)
  222. local setting_score = 0
  223. for _, keyword in ipairs(query_keywords) do
  224. if string.find(entry.name:lower(), keyword, 1, true) then
  225. setting_score = setting_score + 1
  226. end
  227. if entry.readable_name and
  228. string.find(fgettext(entry.readable_name):lower(), keyword, 1, true) then
  229. setting_score = setting_score + 1
  230. end
  231. if entry.comment and
  232. string.find(fgettext_ne(entry.comment):lower(), keyword, 1, true) then
  233. setting_score = setting_score + 1
  234. end
  235. end
  236. return setting_score
  237. end
  238. local function filter_page_content(page, query_keywords)
  239. if #query_keywords == 0 then
  240. return page.content, 0
  241. end
  242. local retval = {}
  243. local i = 1
  244. local max_weight = 0
  245. for _, content in ipairs(page.content) do
  246. if type(content) == "string" then
  247. local setting = get_setting_info(content)
  248. assert(setting, "Unknown setting: " .. content)
  249. local weight = get_setting_match_weight(setting, query_keywords)
  250. if weight > 0 then
  251. max_weight = math.max(max_weight, weight)
  252. retval[i] = content
  253. i = i + 1
  254. end
  255. elseif type(content) == "table" and content.query_text then
  256. for _, keyword in ipairs(query_keywords) do
  257. if string.find(fgettext(content.query_text), keyword, 1, true) then
  258. max_weight = math.max(max_weight, 1)
  259. retval[i] = content
  260. i = i + 1
  261. break
  262. end
  263. end
  264. end
  265. end
  266. return retval, max_weight
  267. end
  268. local function update_filtered_pages(query)
  269. filtered_pages = {}
  270. filtered_page_by_id = {}
  271. local query_keywords = {}
  272. for word in query:lower():gmatch("%S+") do
  273. table.insert(query_keywords, word)
  274. end
  275. local best_page = nil
  276. local best_page_weight = -1
  277. for _, page in ipairs(all_pages) do
  278. local content, page_weight = filter_page_content(page, query_keywords)
  279. if page_has_contents(page, content) then
  280. local new_page = table.copy(page)
  281. new_page.content = content
  282. filtered_pages[#filtered_pages + 1] = new_page
  283. filtered_page_by_id[new_page.id] = new_page
  284. if page_weight > best_page_weight then
  285. best_page = new_page
  286. best_page_weight = page_weight
  287. end
  288. end
  289. end
  290. return best_page and best_page.id or nil
  291. end
  292. local function check_requirements(name, requires)
  293. if requires == nil then
  294. return true
  295. end
  296. local video_driver = core.get_active_driver()
  297. local touch_support = core.irrlicht_device_supports_touch()
  298. local touch_controls = core.settings:get("touch_controls")
  299. local special = {
  300. android = PLATFORM == "Android",
  301. desktop = PLATFORM ~= "Android",
  302. touch_support = touch_support,
  303. -- When touch_controls is "auto", we don't know which input method will
  304. -- be used, so we show settings for both.
  305. touchscreen = touch_support and (touch_controls == "auto" or core.is_yes(touch_controls)),
  306. keyboard_mouse = not touch_support or (touch_controls == "auto" or not core.is_yes(touch_controls)),
  307. opengl = (video_driver == "opengl" or video_driver == "opengl3"),
  308. gles = video_driver:sub(1, 5) == "ogles",
  309. }
  310. for req_key, req_value in pairs(requires) do
  311. if special[req_key] == nil then
  312. local required_setting = get_setting_info(req_key)
  313. if required_setting == nil then
  314. core.log("warning", "Unknown setting " .. req_key .. " required by " .. (name or "???"))
  315. end
  316. local actual_value = core.settings:get_bool(req_key,
  317. required_setting and core.is_yes(required_setting.default))
  318. if actual_value ~= req_value then
  319. return false
  320. end
  321. elseif special[req_key] ~= req_value then
  322. return false
  323. end
  324. end
  325. return true
  326. end
  327. function page_has_contents(page, actual_content)
  328. local is_advanced =
  329. page.id:sub(1, #"client_and_server") == "client_and_server" or
  330. page.id:sub(1, #"mapgen") == "mapgen" or
  331. page.id:sub(1, #"advanced") == "advanced"
  332. local show_advanced = core.settings:get_bool("show_advanced")
  333. if is_advanced and not show_advanced then
  334. return false
  335. end
  336. for _, item in ipairs(actual_content) do
  337. if item == false or item.heading then --luacheck: ignore
  338. -- skip
  339. elseif type(item) == "string" then
  340. local setting = get_setting_info(item)
  341. assert(setting, "Unknown setting: " .. item)
  342. if check_requirements(setting.name, setting.requires) then
  343. return true
  344. end
  345. elseif item.get_formspec then
  346. if check_requirements(item.id, item.requires) then
  347. return true
  348. end
  349. else
  350. error("Unknown content in page: " .. dump(item))
  351. end
  352. end
  353. return false
  354. end
  355. local function build_page_components(page)
  356. -- Filter settings based on requirements
  357. local content = {}
  358. local last_heading
  359. for _, item in ipairs(page.content) do
  360. if item == false then --luacheck: ignore
  361. -- skip
  362. elseif item.heading then
  363. last_heading = item
  364. else
  365. local name, requires
  366. if type(item) == "string" then
  367. local setting = get_setting_info(item)
  368. assert(setting, "Unknown setting: " .. item)
  369. name = setting.name
  370. requires = setting.requires
  371. elseif item.get_formspec then
  372. name = item.id
  373. requires = item.requires
  374. else
  375. error("Unknown content in page: " .. dump(item))
  376. end
  377. if check_requirements(name, requires) then
  378. if last_heading then
  379. content[#content + 1] = last_heading
  380. last_heading = nil
  381. end
  382. content[#content + 1] = item
  383. end
  384. end
  385. end
  386. -- Create components
  387. local retval = {}
  388. for i, item in ipairs(content) do
  389. if type(item) == "string" then
  390. local setting = get_setting_info(item)
  391. local component_func = component_funcs[setting.type]
  392. assert(component_func, "Unknown setting type: " .. setting.type)
  393. retval[i] = component_func(setting)
  394. elseif item.get_formspec then
  395. retval[i] = item
  396. elseif item.heading then
  397. retval[i] = component_funcs.heading(item.heading)
  398. end
  399. end
  400. return retval
  401. end
  402. local formspec_show_hack = false
  403. local function get_formspec(dialogdata)
  404. local page_id = dialogdata.page_id or "accessibility"
  405. local page = filtered_page_by_id[page_id]
  406. local extra_h = 1 -- not included in tabsize.height
  407. local tabsize = {
  408. width = core.settings:get_bool("touch_gui") and 16.5 or 15.5,
  409. height = core.settings:get_bool("touch_gui") and (10 - extra_h) or 12,
  410. }
  411. local scrollbar_w = core.settings:get_bool("touch_gui") and 0.6 or 0.4
  412. local left_pane_width = core.settings:get_bool("touch_gui") and 4.5 or 4.25
  413. local left_pane_padding = 0.25
  414. local search_width = left_pane_width + scrollbar_w - (0.75 * 2)
  415. local back_w = 3
  416. local checkbox_w = (tabsize.width - back_w - 2*0.2) / 2
  417. local show_technical_names = core.settings:get_bool("show_technical_names")
  418. local show_advanced = core.settings:get_bool("show_advanced")
  419. formspec_show_hack = not formspec_show_hack
  420. local fs = {
  421. "formspec_version[6]",
  422. "size[", tostring(tabsize.width), ",", tostring(tabsize.height + extra_h), "]",
  423. core.settings:get_bool("touch_gui") and "padding[0.01,0.01]" or "",
  424. "bgcolor[#0000]",
  425. -- HACK: this is needed to allow resubmitting the same formspec
  426. formspec_show_hack and " " or "",
  427. "box[0,0;", tostring(tabsize.width), ",", tostring(tabsize.height), ";#0000008C]",
  428. ("button[0,%f;%f,0.8;back;%s]"):format(
  429. tabsize.height + 0.2, back_w, fgettext("Back")),
  430. ("box[%f,%f;%f,0.8;#0000008C]"):format(
  431. back_w + 0.2, tabsize.height + 0.2, checkbox_w),
  432. ("checkbox[%f,%f;show_technical_names;%s;%s]"):format(
  433. back_w + 2*0.2, tabsize.height + 0.6,
  434. fgettext("Show technical names"), tostring(show_technical_names)),
  435. ("box[%f,%f;%f,0.8;#0000008C]"):format(
  436. back_w + 2*0.2 + checkbox_w, tabsize.height + 0.2, checkbox_w),
  437. ("checkbox[%f,%f;show_advanced;%s;%s]"):format(
  438. back_w + 3*0.2 + checkbox_w, tabsize.height + 0.6,
  439. fgettext("Show advanced settings"), tostring(show_advanced)),
  440. "field[0.25,0.25;", tostring(search_width), ",0.75;search_query;;",
  441. core.formspec_escape(dialogdata.query or ""), "]",
  442. "field_enter_after_edit[search_query;true]",
  443. "container[", tostring(search_width + 0.25), ", 0.25]",
  444. "image_button[0,0;0.75,0.75;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
  445. "image_button[0.75,0;0.75,0.75;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";search_clear;]",
  446. "tooltip[search;", fgettext("Search"), "]",
  447. "tooltip[search_clear;", fgettext("Clear"), "]",
  448. "container_end[]",
  449. ("scroll_container[0.25,1.25;%f,%f;leftscroll;vertical;0.1;0]"):format(
  450. left_pane_width, tabsize.height - 1.5),
  451. "style_type[button;border=false;bgcolor=#3333]",
  452. "style_type[button:hover;border=false;bgcolor=#6663]",
  453. }
  454. local y = 0
  455. local last_section = nil
  456. for _, other_page in ipairs(filtered_pages) do
  457. if other_page.section ~= last_section then
  458. fs[#fs + 1] = ("label[0.1,%f;%s]"):format(
  459. y + 0.41, core.colorize("#ff0", fgettext(other_page.section)))
  460. last_section = other_page.section
  461. y = y + 0.82
  462. end
  463. fs[#fs + 1] = ("box[0,%f;%f,0.8;%s]"):format(
  464. y, left_pane_width-left_pane_padding, other_page.id == page_id and "#467832FF" or "#3339")
  465. fs[#fs + 1] = ("button[0,%f;%f,0.8;page_%s;%s]")
  466. :format(y, left_pane_width-left_pane_padding, other_page.id, fgettext(other_page.title))
  467. y = y + 0.82
  468. end
  469. if #filtered_pages == 0 then
  470. fs[#fs + 1] = "label[0.1,0.41;"
  471. fs[#fs + 1] = fgettext("No results")
  472. fs[#fs + 1] = "]"
  473. end
  474. fs[#fs + 1] = "scroll_container_end[]"
  475. if y >= tabsize.height - 1.25 then
  476. fs[#fs + 1] = ("scrollbar[%f,1.25;%f,%f;vertical;leftscroll;%f]"):format(
  477. left_pane_width + 0.25, scrollbar_w, tabsize.height - 1.5, dialogdata.leftscroll or 0)
  478. end
  479. fs[#fs + 1] = "style_type[button;border=;bgcolor=]"
  480. if not dialogdata.components then
  481. dialogdata.components = page and build_page_components(page) or {}
  482. end
  483. local right_pane_width = tabsize.width - left_pane_width - 0.375 - 2*scrollbar_w - 0.25
  484. fs[#fs + 1] = ("scroll_container[%f,0;%f,%f;rightscroll;vertical;0.1;0.25]"):format(
  485. tabsize.width - right_pane_width - scrollbar_w, right_pane_width, tabsize.height)
  486. y = 0.25
  487. for i, comp in ipairs(dialogdata.components) do
  488. fs[#fs + 1] = ("container[0,%f]"):format(y)
  489. local avail_w = right_pane_width - 0.25
  490. if not comp.full_width then
  491. avail_w = avail_w - 1.4
  492. end
  493. if comp.max_w then
  494. avail_w = math.min(avail_w, comp.max_w)
  495. end
  496. local comp_fs, used_h = comp:get_formspec(avail_w)
  497. fs[#fs + 1] = comp_fs
  498. fs[#fs + 1] = "style_type[image_button;border=false;padding=]"
  499. local show_reset = comp.resettable and comp.setting
  500. local show_info = comp.info_text and comp.info_text ~= ""
  501. if show_reset or show_info then
  502. -- ensure there's enough space for reset/info
  503. used_h = math.max(used_h, 0.5)
  504. end
  505. local info_reset_y = used_h / 2 - 0.25
  506. if show_reset then
  507. local default = comp.setting.default
  508. local reset_tooltip = default and
  509. fgettext("Reset setting to default ($1)", tostring(default)) or
  510. fgettext("Reset setting to default")
  511. fs[#fs + 1] = ("image_button[%f,%f;0.5,0.5;%s;%s;]"):format(
  512. right_pane_width - 1.4, info_reset_y, reset_icon_path, "reset_" .. i)
  513. fs[#fs + 1] = ("tooltip[%s;%s]"):format("reset_" .. i, reset_tooltip)
  514. end
  515. if show_info then
  516. local info_x = right_pane_width - 0.75
  517. fs[#fs + 1] = ("image[%f,%f;0.5,0.5;%s]"):format(info_x, info_reset_y, info_icon_path)
  518. fs[#fs + 1] = ("tooltip[%f,%f;0.5,0.5;%s]"):format(info_x, info_reset_y, fgettext(comp.info_text))
  519. end
  520. fs[#fs + 1] = "style_type[image_button;border=;padding=]"
  521. fs[#fs + 1] = "container_end[]"
  522. if used_h > 0 then
  523. y = y + used_h + 0.25
  524. end
  525. end
  526. fs[#fs + 1] = "scroll_container_end[]"
  527. if y >= tabsize.height then
  528. fs[#fs + 1] = ("scrollbar[%f,0;%f,%f;vertical;rightscroll;%f]"):format(
  529. tabsize.width - scrollbar_w, scrollbar_w, tabsize.height, dialogdata.rightscroll or 0)
  530. end
  531. return table.concat(fs, "")
  532. end
  533. -- On Android, closing the app via the "Recents screen" won't result in a clean
  534. -- exit, discarding any setting changes made by the user.
  535. -- To avoid that, we write the settings file in more cases on Android.
  536. function write_settings_early()
  537. if PLATFORM == "Android" then
  538. core.settings:write()
  539. end
  540. end
  541. local function regenerate_page_list(dialogdata)
  542. local suggested_page_id = update_filtered_pages(dialogdata.query)
  543. dialogdata.components = nil
  544. if not filtered_page_by_id[dialogdata.page_id] then
  545. dialogdata.leftscroll = 0
  546. dialogdata.rightscroll = 0
  547. dialogdata.page_id = suggested_page_id
  548. end
  549. end
  550. local function buttonhandler(this, fields)
  551. local dialogdata = this.data
  552. dialogdata.leftscroll = core.explode_scrollbar_event(fields.leftscroll).value or dialogdata.leftscroll
  553. dialogdata.rightscroll = core.explode_scrollbar_event(fields.rightscroll).value or dialogdata.rightscroll
  554. dialogdata.query = fields.search_query
  555. if fields.back then
  556. this:delete()
  557. return true
  558. end
  559. if fields.show_technical_names ~= nil then
  560. local value = core.is_yes(fields.show_technical_names)
  561. core.settings:set_bool("show_technical_names", value)
  562. write_settings_early()
  563. return true
  564. end
  565. if fields.show_advanced ~= nil then
  566. local value = core.is_yes(fields.show_advanced)
  567. core.settings:set_bool("show_advanced", value)
  568. write_settings_early()
  569. regenerate_page_list(dialogdata)
  570. return true
  571. end
  572. if fields.search or fields.key_enter_field == "search_query" then
  573. dialogdata.components = nil
  574. dialogdata.leftscroll = 0
  575. dialogdata.rightscroll = 0
  576. dialogdata.page_id = update_filtered_pages(dialogdata.query)
  577. return true
  578. end
  579. if fields.search_clear then
  580. dialogdata.query = ""
  581. dialogdata.components = nil
  582. dialogdata.leftscroll = 0
  583. dialogdata.rightscroll = 0
  584. dialogdata.page_id = update_filtered_pages("")
  585. return true
  586. end
  587. for _, page in ipairs(all_pages) do
  588. if fields["page_" .. page.id] then
  589. dialogdata.page_id = page.id
  590. dialogdata.components = nil
  591. dialogdata.rightscroll = 0
  592. return true
  593. end
  594. end
  595. local function after_setting_change(comp)
  596. write_settings_early()
  597. if comp.setting and comp.setting.name == "touch_controls" then
  598. -- Changing the "touch_controls" setting may result in a different
  599. -- page list.
  600. regenerate_page_list(dialogdata)
  601. else
  602. -- Clear components so they regenerate
  603. dialogdata.components = nil
  604. end
  605. end
  606. for i, comp in ipairs(dialogdata.components) do
  607. if comp.on_submit and comp:on_submit(fields, this) then
  608. after_setting_change(comp)
  609. return true
  610. end
  611. if comp.setting and fields["reset_" .. i] then
  612. core.settings:remove(comp.setting.name)
  613. after_setting_change(comp)
  614. return true
  615. end
  616. end
  617. return false
  618. end
  619. local function eventhandler(event)
  620. if event == "DialogShow" then
  621. -- Don't show the header image behind the dialog.
  622. mm_game_theme.set_engine(true)
  623. return true
  624. end
  625. if event == "FullscreenChange" then
  626. -- Refresh the formspec to keep the fullscreen checkbox up to date.
  627. ui.update()
  628. return true
  629. end
  630. return false
  631. end
  632. function create_settings_dlg()
  633. load()
  634. local dlg = dialog_create("dlg_settings", get_formspec, buttonhandler, eventhandler)
  635. dlg.data.page_id = update_filtered_pages("")
  636. return dlg
  637. end