dlg_settings.lua 21 KB

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