dlg_settings_advanced.lua 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. --Minetest
  2. --Copyright (C) 2015 PilzAdam
  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 FILENAME = "settingtypes.txt"
  18. local CHAR_CLASSES = {
  19. SPACE = "[%s]",
  20. VARIABLE = "[%w_%-%.]",
  21. INTEGER = "[+-]?[%d]",
  22. FLOAT = "[+-]?[%d%.]",
  23. FLAGS = "[%w_%-%.,]",
  24. }
  25. local function flags_to_table(flags)
  26. return flags:gsub("%s+", ""):split(",", true) -- Remove all spaces and split
  27. end
  28. -- returns error message, or nil
  29. local function parse_setting_line(settings, line, read_all, base_level, allow_secure)
  30. -- strip carriage returns (CR, /r)
  31. line = line:gsub("\r", "")
  32. -- comment
  33. local comment = line:match("^#" .. CHAR_CLASSES.SPACE .. "*(.*)$")
  34. if comment then
  35. if settings.current_comment == "" then
  36. settings.current_comment = comment
  37. else
  38. settings.current_comment = settings.current_comment .. "\n" .. comment
  39. end
  40. return
  41. end
  42. -- clear current_comment so only comments directly above a setting are bound to it
  43. -- but keep a local reference to it for variables in the current line
  44. local current_comment = settings.current_comment
  45. settings.current_comment = ""
  46. -- empty lines
  47. if line:match("^" .. CHAR_CLASSES.SPACE .. "*$") then
  48. return
  49. end
  50. -- category
  51. local stars, category = line:match("^%[([%*]*)([^%]]+)%]$")
  52. if category then
  53. table.insert(settings, {
  54. name = category,
  55. level = stars:len() + base_level,
  56. type = "category",
  57. })
  58. return
  59. end
  60. -- settings
  61. local first_part, name, readable_name, setting_type = line:match("^"
  62. -- this first capture group matches the whole first part,
  63. -- so we can later strip it from the rest of the line
  64. .. "("
  65. .. "([" .. CHAR_CLASSES.VARIABLE .. "+)" -- variable name
  66. .. CHAR_CLASSES.SPACE .. "*"
  67. .. "%(([^%)]*)%)" -- readable name
  68. .. CHAR_CLASSES.SPACE .. "*"
  69. .. "(" .. CHAR_CLASSES.VARIABLE .. "+)" -- type
  70. .. CHAR_CLASSES.SPACE .. "*"
  71. .. ")")
  72. if not first_part then
  73. return "Invalid line"
  74. end
  75. if name:match("secure%.[.]*") and not allow_secure then
  76. return "Tried to add \"secure.\" setting"
  77. end
  78. if readable_name == "" then
  79. readable_name = nil
  80. end
  81. local remaining_line = line:sub(first_part:len() + 1)
  82. if setting_type == "int" then
  83. local default, min, max = remaining_line:match("^"
  84. -- first int is required, the last 2 are optional
  85. .. "(" .. CHAR_CLASSES.INTEGER .. "+)" .. CHAR_CLASSES.SPACE .. "*"
  86. .. "(" .. CHAR_CLASSES.INTEGER .. "*)" .. CHAR_CLASSES.SPACE .. "*"
  87. .. "(" .. CHAR_CLASSES.INTEGER .. "*)"
  88. .. "$")
  89. if not default or not tonumber(default) then
  90. return "Invalid integer setting"
  91. end
  92. min = tonumber(min)
  93. max = tonumber(max)
  94. table.insert(settings, {
  95. name = name,
  96. readable_name = readable_name,
  97. type = "int",
  98. default = default,
  99. min = min,
  100. max = max,
  101. comment = current_comment,
  102. })
  103. return
  104. end
  105. if setting_type == "string"
  106. or setting_type == "key" or setting_type == "v3f" then
  107. local default = remaining_line:match("^(.*)$")
  108. if not default then
  109. return "Invalid string setting"
  110. end
  111. if setting_type == "key" and not read_all then
  112. -- ignore key type if read_all is false
  113. return
  114. end
  115. table.insert(settings, {
  116. name = name,
  117. readable_name = readable_name,
  118. type = setting_type,
  119. default = default,
  120. comment = current_comment,
  121. })
  122. return
  123. end
  124. if setting_type == "noise_params_2d"
  125. or setting_type == "noise_params_3d" then
  126. local default = remaining_line:match("^(.*)$")
  127. if not default then
  128. return "Invalid string setting"
  129. end
  130. local values = {}
  131. local ti = 1
  132. local index = 1
  133. for match in default:gmatch("[+-]?[%d.-e]+") do -- All numeric characters
  134. index = default:find("[+-]?[%d.-e]+", index) + match:len()
  135. table.insert(values, match)
  136. ti = ti + 1
  137. if ti > 9 then
  138. break
  139. end
  140. end
  141. index = default:find("[^, ]", index)
  142. local flags = ""
  143. if index then
  144. flags = default:sub(index)
  145. default = default:sub(1, index - 3) -- Make sure no flags in single-line format
  146. end
  147. table.insert(values, flags)
  148. table.insert(settings, {
  149. name = name,
  150. readable_name = readable_name,
  151. type = setting_type,
  152. default = default,
  153. default_table = {
  154. offset = values[1],
  155. scale = values[2],
  156. spread = {
  157. x = values[3],
  158. y = values[4],
  159. z = values[5]
  160. },
  161. seed = values[6],
  162. octaves = values[7],
  163. persistence = values[8],
  164. lacunarity = values[9],
  165. flags = values[10]
  166. },
  167. values = values,
  168. comment = current_comment,
  169. noise_params = true,
  170. flags = flags_to_table("defaults,eased,absvalue")
  171. })
  172. return
  173. end
  174. if setting_type == "bool" then
  175. if remaining_line ~= "false" and remaining_line ~= "true" then
  176. return "Invalid boolean setting"
  177. end
  178. table.insert(settings, {
  179. name = name,
  180. readable_name = readable_name,
  181. type = "bool",
  182. default = remaining_line,
  183. comment = current_comment,
  184. })
  185. return
  186. end
  187. if setting_type == "float" then
  188. local default, min, max = remaining_line:match("^"
  189. -- first float is required, the last 2 are optional
  190. .. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "*"
  191. .. "(" .. CHAR_CLASSES.FLOAT .. "*)" .. CHAR_CLASSES.SPACE .. "*"
  192. .. "(" .. CHAR_CLASSES.FLOAT .. "*)"
  193. .."$")
  194. if not default or not tonumber(default) then
  195. return "Invalid float setting"
  196. end
  197. min = tonumber(min)
  198. max = tonumber(max)
  199. table.insert(settings, {
  200. name = name,
  201. readable_name = readable_name,
  202. type = "float",
  203. default = default,
  204. min = min,
  205. max = max,
  206. comment = current_comment,
  207. })
  208. return
  209. end
  210. if setting_type == "enum" then
  211. local default, values = remaining_line:match("^"
  212. -- first value (default) may be empty (i.e. is optional)
  213. .. "(" .. CHAR_CLASSES.VARIABLE .. "*)" .. CHAR_CLASSES.SPACE .. "*"
  214. .. "(" .. CHAR_CLASSES.FLAGS .. "+)"
  215. .. "$")
  216. if not default or values == "" then
  217. return "Invalid enum setting"
  218. end
  219. table.insert(settings, {
  220. name = name,
  221. readable_name = readable_name,
  222. type = "enum",
  223. default = default,
  224. values = values:split(",", true),
  225. comment = current_comment,
  226. })
  227. return
  228. end
  229. if setting_type == "path" or setting_type == "filepath" then
  230. local default = remaining_line:match("^(.*)$")
  231. if not default then
  232. return "Invalid path setting"
  233. end
  234. table.insert(settings, {
  235. name = name,
  236. readable_name = readable_name,
  237. type = setting_type,
  238. default = default,
  239. comment = current_comment,
  240. })
  241. return
  242. end
  243. if setting_type == "flags" then
  244. local default, possible = remaining_line:match("^"
  245. -- first value (default) may be empty (i.e. is optional)
  246. -- this is implemented by making the last value optional, and
  247. -- swapping them around if it turns out empty.
  248. .. "(" .. CHAR_CLASSES.FLAGS .. "+)" .. CHAR_CLASSES.SPACE .. "*"
  249. .. "(" .. CHAR_CLASSES.FLAGS .. "*)"
  250. .. "$")
  251. if not default or not possible then
  252. return "Invalid flags setting"
  253. end
  254. if possible == "" then
  255. possible = default
  256. default = ""
  257. end
  258. table.insert(settings, {
  259. name = name,
  260. readable_name = readable_name,
  261. type = "flags",
  262. default = default,
  263. possible = flags_to_table(possible),
  264. comment = current_comment,
  265. })
  266. return
  267. end
  268. return "Invalid setting type \"" .. setting_type .. "\""
  269. end
  270. local function parse_single_file(file, filepath, read_all, result, base_level, allow_secure)
  271. -- store this helper variable in the table so it's easier to pass to parse_setting_line()
  272. result.current_comment = ""
  273. local line = file:read("*line")
  274. while line do
  275. local error_msg = parse_setting_line(result, line, read_all, base_level, allow_secure)
  276. if error_msg then
  277. core.log("error", error_msg .. " in " .. filepath .. " \"" .. line .. "\"")
  278. end
  279. line = file:read("*line")
  280. end
  281. result.current_comment = nil
  282. end
  283. -- read_all: whether to ignore certain setting types for GUI or not
  284. -- parse_mods: whether to parse settingtypes.txt in mods and games
  285. local function parse_config_file(read_all, parse_mods)
  286. local settings = {}
  287. do
  288. local builtin_path = core.get_builtin_path() .. FILENAME
  289. local file = io.open(builtin_path, "r")
  290. if not file then
  291. core.log("error", "Can't load " .. FILENAME)
  292. return settings
  293. end
  294. parse_single_file(file, builtin_path, read_all, settings, 0, true)
  295. file:close()
  296. end
  297. if parse_mods then
  298. -- Parse games
  299. local games_category_initialized = false
  300. local index = 1
  301. local game = pkgmgr.get_game(index)
  302. while game do
  303. local path = game.path .. DIR_DELIM .. FILENAME
  304. local file = io.open(path, "r")
  305. if file then
  306. if not games_category_initialized then
  307. fgettext_ne("Content: Games") -- not used, but needed for xgettext
  308. table.insert(settings, {
  309. name = "Content: Games",
  310. level = 0,
  311. type = "category",
  312. })
  313. games_category_initialized = true
  314. end
  315. table.insert(settings, {
  316. name = game.name,
  317. level = 1,
  318. type = "category",
  319. })
  320. parse_single_file(file, path, read_all, settings, 2, false)
  321. file:close()
  322. end
  323. index = index + 1
  324. game = pkgmgr.get_game(index)
  325. end
  326. -- Parse mods
  327. local mods_category_initialized = false
  328. local mods = {}
  329. get_mods(core.get_modpath(), "mods", mods)
  330. for _, mod in ipairs(mods) do
  331. local path = mod.path .. DIR_DELIM .. FILENAME
  332. local file = io.open(path, "r")
  333. if file then
  334. if not mods_category_initialized then
  335. fgettext_ne("Content: Mods") -- not used, but needed for xgettext
  336. table.insert(settings, {
  337. name = "Content: Mods",
  338. level = 0,
  339. type = "category",
  340. })
  341. mods_category_initialized = true
  342. end
  343. table.insert(settings, {
  344. name = mod.name,
  345. readable_name = mod.title,
  346. level = 1,
  347. type = "category",
  348. })
  349. parse_single_file(file, path, read_all, settings, 2, false)
  350. file:close()
  351. end
  352. end
  353. end
  354. return settings
  355. end
  356. local function filter_settings(settings, searchstring)
  357. if not searchstring or searchstring == "" then
  358. return settings, -1
  359. end
  360. -- Setup the keyword list
  361. local keywords = {}
  362. for word in searchstring:lower():gmatch("%S+") do
  363. table.insert(keywords, word)
  364. end
  365. local result = {}
  366. local category_stack = {}
  367. local current_level = 0
  368. local best_setting = nil
  369. for _, entry in pairs(settings) do
  370. if entry.type == "category" then
  371. -- Remove all settingless categories
  372. while #category_stack > 0 and entry.level <= current_level do
  373. table.remove(category_stack, #category_stack)
  374. if #category_stack > 0 then
  375. current_level = category_stack[#category_stack].level
  376. else
  377. current_level = 0
  378. end
  379. end
  380. -- Push category onto stack
  381. category_stack[#category_stack + 1] = entry
  382. current_level = entry.level
  383. else
  384. -- See if setting matches keywords
  385. local setting_score = 0
  386. for k = 1, #keywords do
  387. local keyword = keywords[k]
  388. if string.find(entry.name:lower(), keyword, 1, true) then
  389. setting_score = setting_score + 1
  390. end
  391. if entry.readable_name and
  392. string.find(fgettext(entry.readable_name):lower(), keyword, 1, true) then
  393. setting_score = setting_score + 1
  394. end
  395. if entry.comment and
  396. string.find(fgettext_ne(entry.comment):lower(), keyword, 1, true) then
  397. setting_score = setting_score + 1
  398. end
  399. end
  400. -- Add setting to results if match
  401. if setting_score > 0 then
  402. -- Add parent categories
  403. for _, category in pairs(category_stack) do
  404. result[#result + 1] = category
  405. end
  406. category_stack = {}
  407. -- Add setting
  408. result[#result + 1] = entry
  409. entry.score = setting_score
  410. if not best_setting or
  411. setting_score > result[best_setting].score then
  412. best_setting = #result
  413. end
  414. end
  415. end
  416. end
  417. return result, best_setting or -1
  418. end
  419. local full_settings = parse_config_file(false, true)
  420. local search_string = ""
  421. local settings = full_settings
  422. local selected_setting = 1
  423. local function get_current_value(setting)
  424. local value = core.settings:get(setting.name)
  425. if value == nil then
  426. value = setting.default
  427. end
  428. return value
  429. end
  430. local function get_current_np_group(setting)
  431. local value = core.settings:get_np_group(setting.name)
  432. if value == nil then
  433. return setting.values
  434. end
  435. local p = "%g"
  436. return {
  437. p:format(value.offset),
  438. p:format(value.scale),
  439. p:format(value.spread.x),
  440. p:format(value.spread.y),
  441. p:format(value.spread.z),
  442. p:format(value.seed),
  443. p:format(value.octaves),
  444. p:format(value.persistence),
  445. p:format(value.lacunarity),
  446. value.flags
  447. }
  448. end
  449. local function get_current_np_group_as_string(setting)
  450. local value = core.settings:get_np_group(setting.name)
  451. if value == nil then
  452. return setting.default
  453. end
  454. return ("%g, %g, (%g, %g, %g), %g, %g, %g, %g"):format(
  455. value.offset,
  456. value.scale,
  457. value.spread.x,
  458. value.spread.y,
  459. value.spread.z,
  460. value.seed,
  461. value.octaves,
  462. value.persistence,
  463. value.lacunarity
  464. ) .. (value.flags ~= "" and (", " .. value.flags) or "")
  465. end
  466. local checkboxes = {} -- handle checkboxes events
  467. local function create_change_setting_formspec(dialogdata)
  468. local setting = settings[selected_setting]
  469. -- Final formspec will be created at the end of this function
  470. -- Default values below, may be changed depending on setting type
  471. local width = 10
  472. local height = 3.5
  473. local description_height = 3
  474. local formspec = ""
  475. -- Setting-specific formspec elements
  476. if setting.type == "bool" then
  477. local selected_index = 1
  478. if core.is_yes(get_current_value(setting)) then
  479. selected_index = 2
  480. end
  481. formspec = "dropdown[3," .. height .. ";4,1;dd_setting_value;"
  482. .. fgettext("Disabled") .. "," .. fgettext("Enabled") .. ";"
  483. .. selected_index .. "]"
  484. height = height + 1.25
  485. elseif setting.type == "enum" then
  486. local selected_index = 0
  487. formspec = "dropdown[3," .. height .. ";4,1;dd_setting_value;"
  488. for index, value in ipairs(setting.values) do
  489. -- translating value is not possible, since it's the value
  490. -- that we set the setting to
  491. formspec = formspec .. core.formspec_escape(value) .. ","
  492. if get_current_value(setting) == value then
  493. selected_index = index
  494. end
  495. end
  496. if #setting.values > 0 then
  497. formspec = formspec:sub(1, -2) -- remove trailing comma
  498. end
  499. formspec = formspec .. ";" .. selected_index .. "]"
  500. height = height + 1.25
  501. elseif setting.type == "path" or setting.type == "filepath" then
  502. local current_value = dialogdata.selected_path
  503. if not current_value then
  504. current_value = get_current_value(setting)
  505. end
  506. formspec = "field[0.28," .. height + 0.15 .. ";8,1;te_setting_value;;"
  507. .. core.formspec_escape(current_value) .. "]"
  508. .. "button[8," .. height - 0.15 .. ";2,1;btn_browser_"
  509. .. setting.type .. ";" .. fgettext("Browse") .. "]"
  510. height = height + 1.15
  511. elseif setting.type == "noise_params_2d" or setting.type == "noise_params_3d" then
  512. local t = get_current_np_group(setting)
  513. local dimension = 3
  514. if setting.type == "noise_params_2d" then
  515. dimension = 2
  516. end
  517. -- More space for 3x3 fields
  518. description_height = description_height - 1.5
  519. height = height - 1.5
  520. local fields = {}
  521. local function add_field(x, name, label, value)
  522. fields[#fields + 1] = ("field[%f,%f;3.3,1;%s;%s;%s]"):format(
  523. x, height, name, label, core.formspec_escape(value or "")
  524. )
  525. end
  526. -- First row
  527. height = height + 0.3
  528. add_field(0.3, "te_offset", fgettext("Offset"), t[1])
  529. add_field(3.6, "te_scale", fgettext("Scale"), t[2])
  530. add_field(6.9, "te_seed", fgettext("Seed"), t[6])
  531. height = height + 1.1
  532. -- Second row
  533. add_field(0.3, "te_spreadx", fgettext("X spread"), t[3])
  534. if dimension == 3 then
  535. add_field(3.6, "te_spready", fgettext("Y spread"), t[4])
  536. else
  537. fields[#fields + 1] = "label[4," .. height - 0.2 .. ";" ..
  538. fgettext("2D Noise") .. "]"
  539. end
  540. add_field(6.9, "te_spreadz", fgettext("Z spread"), t[5])
  541. height = height + 1.1
  542. -- Third row
  543. add_field(0.3, "te_octaves", fgettext("Octaves"), t[7])
  544. add_field(3.6, "te_persist", fgettext("Persistence"), t[8])
  545. add_field(6.9, "te_lacun", fgettext("Lacunarity"), t[9])
  546. height = height + 1.1
  547. local enabled_flags = flags_to_table(t[10])
  548. local flags = {}
  549. for _, name in ipairs(enabled_flags) do
  550. -- Index by name, to avoid iterating over all enabled_flags for every possible flag.
  551. flags[name] = true
  552. end
  553. for _, name in ipairs(setting.flags) do
  554. local checkbox_name = "cb_" .. name
  555. local is_enabled = flags[name] == true -- to get false if nil
  556. checkboxes[checkbox_name] = is_enabled
  557. end
  558. -- Flags
  559. formspec = table.concat(fields)
  560. .. "checkbox[0.5," .. height - 0.6 .. ";cb_defaults;"
  561. --[[~ "defaults" is a noise parameter flag.
  562. It describes the default processing options
  563. for noise settings in main menu -> "All Settings". ]]
  564. .. fgettext("defaults") .. ";" -- defaults
  565. .. tostring(flags["defaults"] == true) .. "]" -- to get false if nil
  566. .. "checkbox[5," .. height - 0.6 .. ";cb_eased;"
  567. --[[~ "eased" is a noise parameter flag.
  568. It is used to make the map smoother and
  569. can be enabled in noise settings in
  570. main menu -> "All Settings". ]]
  571. .. fgettext("eased") .. ";" -- eased
  572. .. tostring(flags["eased"] == true) .. "]"
  573. .. "checkbox[5," .. height - 0.15 .. ";cb_absvalue;"
  574. --[[~ "absvalue" is a noise parameter flag.
  575. It is short for "absolute value".
  576. It can be enabled in noise settings in
  577. main menu -> "All Settings". ]]
  578. .. fgettext("absvalue") .. ";" -- absvalue
  579. .. tostring(flags["absvalue"] == true) .. "]"
  580. height = height + 1
  581. elseif setting.type == "v3f" then
  582. local val = get_current_value(setting)
  583. local v3f = {}
  584. for line in val:gmatch("[+-]?[%d.+-eE]+") do -- All numeric characters
  585. table.insert(v3f, line)
  586. end
  587. height = height + 0.3
  588. formspec = formspec
  589. .. "field[0.3," .. height .. ";3.3,1;te_x;"
  590. .. fgettext("X") .. ";" -- X
  591. .. core.formspec_escape(v3f[1] or "") .. "]"
  592. .. "field[3.6," .. height .. ";3.3,1;te_y;"
  593. .. fgettext("Y") .. ";" -- Y
  594. .. core.formspec_escape(v3f[2] or "") .. "]"
  595. .. "field[6.9," .. height .. ";3.3,1;te_z;"
  596. .. fgettext("Z") .. ";" -- Z
  597. .. core.formspec_escape(v3f[3] or "") .. "]"
  598. height = height + 1.1
  599. elseif setting.type == "flags" then
  600. local current_flags = flags_to_table(get_current_value(setting))
  601. local flags = {}
  602. for _, name in ipairs(current_flags) do
  603. -- Index by name, to avoid iterating over all enabled_flags for every possible flag.
  604. if name:sub(1, 2) == "no" then
  605. flags[name:sub(3)] = false
  606. else
  607. flags[name] = true
  608. end
  609. end
  610. local flags_count = #setting.possible / 2
  611. local max_height = math.ceil(flags_count / 2) / 2
  612. -- More space for flags
  613. description_height = description_height - 1
  614. height = height - 1
  615. local fields = {} -- To build formspec
  616. local j = 1
  617. for _, name in ipairs(setting.possible) do
  618. if name:sub(1, 2) ~= "no" then
  619. local x = 0.5
  620. local y = height + j / 2 - 0.75
  621. if j - 1 >= flags_count / 2 then -- 2nd column
  622. x = 5
  623. y = y - max_height
  624. end
  625. j = j + 1;
  626. local checkbox_name = "cb_" .. name
  627. local is_enabled = flags[name] == true -- to get false if nil
  628. checkboxes[checkbox_name] = is_enabled
  629. fields[#fields + 1] = ("checkbox[%f,%f;%s;%s;%s]"):format(
  630. x, y, checkbox_name, name, tostring(is_enabled)
  631. )
  632. end
  633. end
  634. formspec = table.concat(fields)
  635. height = height + max_height + 0.25
  636. else
  637. -- TODO: fancy input for float, int
  638. local text = get_current_value(setting)
  639. if dialogdata.error_message and dialogdata.entered_text then
  640. text = dialogdata.entered_text
  641. end
  642. formspec = "field[0.28," .. height + 0.15 .. ";" .. width .. ",1;te_setting_value;;"
  643. .. core.formspec_escape(text) .. "]"
  644. height = height + 1.15
  645. end
  646. -- Box good, textarea bad. Calculate textarea size from box.
  647. local function create_textfield(size, label, text, bg_color)
  648. local textarea = {
  649. x = size.x + 0.3,
  650. y = size.y,
  651. w = size.w + 0.25,
  652. h = size.h * 1.16 + 0.12
  653. }
  654. return ("box[%f,%f;%f,%f;%s]textarea[%f,%f;%f,%f;;%s;%s]"):format(
  655. size.x, size.y, size.w, size.h, bg_color or "#000",
  656. textarea.x, textarea.y, textarea.w, textarea.h,
  657. core.formspec_escape(label), core.formspec_escape(text)
  658. )
  659. end
  660. -- When there's an error: Shrink description textarea and add error below
  661. if dialogdata.error_message then
  662. local error_box = {
  663. x = 0,
  664. y = description_height - 0.4,
  665. w = width - 0.25,
  666. h = 0.5
  667. }
  668. formspec = formspec ..
  669. create_textfield(error_box, "", dialogdata.error_message, "#600")
  670. description_height = description_height - 0.75
  671. end
  672. -- Get description field
  673. local description_box = {
  674. x = 0,
  675. y = 0.2,
  676. w = width - 0.25,
  677. h = description_height
  678. }
  679. local setting_name = setting.name
  680. if setting.readable_name then
  681. setting_name = fgettext_ne(setting.readable_name) ..
  682. " (" .. setting.name .. ")"
  683. end
  684. local comment_text
  685. if setting.comment == "" then
  686. comment_text = fgettext_ne("(No description of setting given)")
  687. else
  688. comment_text = fgettext_ne(setting.comment)
  689. end
  690. return (
  691. "size[" .. width .. "," .. height + 0.25 .. ",true]" ..
  692. create_textfield(description_box, setting_name, comment_text) ..
  693. formspec ..
  694. "button[" .. width / 2 - 2.5 .. "," .. height - 0.4 .. ";2.5,1;btn_done;" ..
  695. fgettext("Save") .. "]" ..
  696. "button[" .. width / 2 .. "," .. height - 0.4 .. ";2.5,1;btn_cancel;" ..
  697. fgettext("Cancel") .. "]"
  698. )
  699. end
  700. local function handle_change_setting_buttons(this, fields)
  701. local setting = settings[selected_setting]
  702. if fields["btn_done"] or fields["key_enter"] then
  703. if setting.type == "bool" then
  704. local new_value = fields["dd_setting_value"]
  705. -- Note: new_value is the actual (translated) value shown in the dropdown
  706. core.settings:set_bool(setting.name, new_value == fgettext("Enabled"))
  707. elseif setting.type == "enum" then
  708. local new_value = fields["dd_setting_value"]
  709. core.settings:set(setting.name, new_value)
  710. elseif setting.type == "int" then
  711. local new_value = tonumber(fields["te_setting_value"])
  712. if not new_value or math.floor(new_value) ~= new_value then
  713. this.data.error_message = fgettext_ne("Please enter a valid integer.")
  714. this.data.entered_text = fields["te_setting_value"]
  715. core.update_formspec(this:get_formspec())
  716. return true
  717. end
  718. if setting.min and new_value < setting.min then
  719. this.data.error_message = fgettext_ne("The value must be at least $1.", setting.min)
  720. this.data.entered_text = fields["te_setting_value"]
  721. core.update_formspec(this:get_formspec())
  722. return true
  723. end
  724. if setting.max and new_value > setting.max then
  725. this.data.error_message = fgettext_ne("The value must not be larger than $1.", setting.max)
  726. this.data.entered_text = fields["te_setting_value"]
  727. core.update_formspec(this:get_formspec())
  728. return true
  729. end
  730. core.settings:set(setting.name, new_value)
  731. elseif setting.type == "float" then
  732. local new_value = tonumber(fields["te_setting_value"])
  733. if not new_value then
  734. this.data.error_message = fgettext_ne("Please enter a valid number.")
  735. this.data.entered_text = fields["te_setting_value"]
  736. core.update_formspec(this:get_formspec())
  737. return true
  738. end
  739. if setting.min and new_value < setting.min then
  740. this.data.error_message = fgettext_ne("The value must be at least $1.", setting.min)
  741. this.data.entered_text = fields["te_setting_value"]
  742. core.update_formspec(this:get_formspec())
  743. return true
  744. end
  745. if setting.max and new_value > setting.max then
  746. this.data.error_message = fgettext_ne("The value must not be larger than $1.", setting.max)
  747. this.data.entered_text = fields["te_setting_value"]
  748. core.update_formspec(this:get_formspec())
  749. return true
  750. end
  751. core.settings:set(setting.name, new_value)
  752. elseif setting.type == "flags" then
  753. local values = {}
  754. for _, name in ipairs(setting.possible) do
  755. if name:sub(1, 2) ~= "no" then
  756. if checkboxes["cb_" .. name] then
  757. table.insert(values, name)
  758. else
  759. table.insert(values, "no" .. name)
  760. end
  761. end
  762. end
  763. checkboxes = {}
  764. local new_value = table.concat(values, ", ")
  765. core.settings:set(setting.name, new_value)
  766. elseif setting.type == "noise_params_2d" or setting.type == "noise_params_3d" then
  767. local np_flags = {}
  768. for _, name in ipairs(setting.flags) do
  769. if checkboxes["cb_" .. name] then
  770. table.insert(np_flags, name)
  771. end
  772. end
  773. checkboxes = {}
  774. if setting.type == "noise_params_2d" then
  775. fields["te_spready"] = fields["te_spreadz"]
  776. end
  777. local new_value = {
  778. offset = fields["te_offset"],
  779. scale = fields["te_scale"],
  780. spread = {
  781. x = fields["te_spreadx"],
  782. y = fields["te_spready"],
  783. z = fields["te_spreadz"]
  784. },
  785. seed = fields["te_seed"],
  786. octaves = fields["te_octaves"],
  787. persistence = fields["te_persist"],
  788. lacunarity = fields["te_lacun"],
  789. flags = table.concat(np_flags, ", ")
  790. }
  791. core.settings:set_np_group(setting.name, new_value)
  792. elseif setting.type == "v3f" then
  793. local new_value = "("
  794. .. fields["te_x"] .. ", "
  795. .. fields["te_y"] .. ", "
  796. .. fields["te_z"] .. ")"
  797. core.settings:set(setting.name, new_value)
  798. else
  799. local new_value = fields["te_setting_value"]
  800. core.settings:set(setting.name, new_value)
  801. end
  802. core.settings:write()
  803. this:delete()
  804. return true
  805. end
  806. if fields["btn_cancel"] then
  807. this:delete()
  808. return true
  809. end
  810. if fields["btn_browser_path"] then
  811. core.show_path_select_dialog("dlg_browse_path",
  812. fgettext_ne("Select directory"), false)
  813. end
  814. if fields["btn_browser_filepath"] then
  815. core.show_path_select_dialog("dlg_browse_path",
  816. fgettext_ne("Select file"), true)
  817. end
  818. if fields["dlg_browse_path_accepted"] then
  819. this.data.selected_path = fields["dlg_browse_path_accepted"]
  820. core.update_formspec(this:get_formspec())
  821. end
  822. if setting.type == "flags"
  823. or setting.type == "noise_params_2d"
  824. or setting.type == "noise_params_3d" then
  825. for name, value in pairs(fields) do
  826. if name:sub(1, 3) == "cb_" then
  827. checkboxes[name] = value == "true"
  828. end
  829. end
  830. end
  831. return false
  832. end
  833. local function create_settings_formspec(tabview, _, tabdata)
  834. local formspec = "size[12,5.4;true]" ..
  835. "tablecolumns[color;tree;text,width=28;text]" ..
  836. "tableoptions[background=#00000000;border=false]" ..
  837. "field[0.3,0.1;10.2,1;search_string;;" .. core.formspec_escape(search_string) .. "]" ..
  838. "field_close_on_enter[search_string;false]" ..
  839. "button[10.2,-0.2;2,1;search;" .. fgettext("Search") .. "]" ..
  840. "table[0,0.8;12,3.5;list_settings;"
  841. local current_level = 0
  842. for _, entry in ipairs(settings) do
  843. local name
  844. if not core.settings:get_bool("show_technical_names") and entry.readable_name then
  845. name = fgettext_ne(entry.readable_name)
  846. else
  847. name = entry.name
  848. end
  849. if entry.type == "category" then
  850. current_level = entry.level
  851. formspec = formspec .. "#FFFF00," .. current_level .. "," .. fgettext(name) .. ",,"
  852. elseif entry.type == "bool" then
  853. local value = get_current_value(entry)
  854. if core.is_yes(value) then
  855. value = fgettext("Enabled")
  856. else
  857. value = fgettext("Disabled")
  858. end
  859. formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
  860. .. value .. ","
  861. elseif entry.type == "key" then --luacheck: ignore
  862. -- ignore key settings, since we have a special dialog for them
  863. elseif entry.type == "noise_params_2d" or entry.type == "noise_params_3d" then
  864. formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
  865. .. core.formspec_escape(get_current_np_group_as_string(entry)) .. ","
  866. else
  867. formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
  868. .. core.formspec_escape(get_current_value(entry)) .. ","
  869. end
  870. end
  871. if #settings > 0 then
  872. formspec = formspec:sub(1, -2) -- remove trailing comma
  873. end
  874. formspec = formspec .. ";" .. selected_setting .. "]" ..
  875. "button[0,4.9;4,1;btn_back;".. fgettext("< Back to Settings page") .. "]" ..
  876. "button[10,4.9;2,1;btn_edit;" .. fgettext("Edit") .. "]" ..
  877. "button[7,4.9;3,1;btn_restore;" .. fgettext("Restore Default") .. "]" ..
  878. "checkbox[0,4.3;cb_tech_settings;" .. fgettext("Show technical names") .. ";"
  879. .. dump(core.settings:get_bool("show_technical_names")) .. "]"
  880. return formspec
  881. end
  882. local function handle_settings_buttons(this, fields, tabname, tabdata)
  883. local list_enter = false
  884. if fields["list_settings"] then
  885. selected_setting = core.get_table_index("list_settings")
  886. if core.explode_table_event(fields["list_settings"]).type == "DCL" then
  887. -- Directly toggle booleans
  888. local setting = settings[selected_setting]
  889. if setting and setting.type == "bool" then
  890. local current_value = get_current_value(setting)
  891. core.settings:set_bool(setting.name, not core.is_yes(current_value))
  892. core.settings:write()
  893. return true
  894. else
  895. list_enter = true
  896. end
  897. else
  898. return true
  899. end
  900. end
  901. if fields.search or fields.key_enter_field == "search_string" then
  902. if search_string == fields.search_string then
  903. if selected_setting > 0 then
  904. -- Go to next result on enter press
  905. local i = selected_setting + 1
  906. local looped = false
  907. while i > #settings or settings[i].type == "category" do
  908. i = i + 1
  909. if i > #settings then
  910. -- Stop infinte looping
  911. if looped then
  912. return false
  913. end
  914. i = 1
  915. looped = true
  916. end
  917. end
  918. selected_setting = i
  919. core.update_formspec(this:get_formspec())
  920. return true
  921. end
  922. else
  923. -- Search for setting
  924. search_string = fields.search_string
  925. settings, selected_setting = filter_settings(full_settings, search_string)
  926. core.update_formspec(this:get_formspec())
  927. end
  928. return true
  929. end
  930. if fields["btn_edit"] or list_enter then
  931. local setting = settings[selected_setting]
  932. if setting and setting.type ~= "category" then
  933. local edit_dialog = dialog_create("change_setting",
  934. create_change_setting_formspec, handle_change_setting_buttons)
  935. edit_dialog:set_parent(this)
  936. this:hide()
  937. edit_dialog:show()
  938. end
  939. return true
  940. end
  941. if fields["btn_restore"] then
  942. local setting = settings[selected_setting]
  943. if setting and setting.type ~= "category" then
  944. core.settings:remove(setting.name)
  945. core.settings:write()
  946. core.update_formspec(this:get_formspec())
  947. end
  948. return true
  949. end
  950. if fields["btn_back"] then
  951. this:delete()
  952. return true
  953. end
  954. if fields["cb_tech_settings"] then
  955. core.settings:set("show_technical_names", fields["cb_tech_settings"])
  956. core.settings:write()
  957. core.update_formspec(this:get_formspec())
  958. return true
  959. end
  960. return false
  961. end
  962. function create_adv_settings_dlg()
  963. local dlg = dialog_create("settings_advanced",
  964. create_settings_formspec,
  965. handle_settings_buttons,
  966. nil)
  967. return dlg
  968. end
  969. -- Uncomment to generate 'minetest.conf.example' and 'settings_translation_file.cpp'.
  970. -- For RUN_IN_PLACE the generated files may appear in the 'bin' folder.
  971. -- See comment and alternative line at the end of 'generate_from_settingtypes.lua'.
  972. --assert(loadfile(core.get_builtin_path().."mainmenu"..DIR_DELIM..
  973. -- "generate_from_settingtypes.lua"))(parse_config_file(true, false))