Main menu tweaks
[oweals/minetest.git] / builtin / mainmenu / dlg_settings_advanced.lua
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
18 local FILENAME = "settingtypes.txt"
19
20 local CHAR_CLASSES = {
21         SPACE = "[%s]",
22         VARIABLE = "[%w_%-%.]",
23         INTEGER = "[+-]?[%d]",
24         FLOAT = "[+-]?[%d%.]",
25         FLAGS = "[%w_%-%.,]",
26 }
27
28 -- returns error message, or nil
29 local function parse_setting_line(settings, line, read_all, base_level, allow_secure)
30         -- comment
31         local comment = line:match("^#" .. CHAR_CLASSES.SPACE .. "*(.*)$")
32         if comment then
33                 if settings.current_comment == "" then
34                         settings.current_comment = comment
35                 else
36                         settings.current_comment = settings.current_comment .. "\n" .. comment
37                 end
38                 return
39         end
40
41         -- clear current_comment so only comments directly above a setting are bound to it
42         -- but keep a local reference to it for variables in the current line
43         local current_comment = settings.current_comment
44         settings.current_comment = ""
45
46         -- empty lines
47         if line:match("^" .. CHAR_CLASSES.SPACE .. "*$") then
48                 return
49         end
50
51         -- category
52         local stars, category = line:match("^%[([%*]*)([^%]]+)%]$")
53         if category then
54                 table.insert(settings, {
55                         name = category,
56                         level = stars:len() + base_level,
57                         type = "category",
58                 })
59                 return
60         end
61
62         -- settings
63         local first_part, name, readable_name, setting_type = line:match("^"
64                         -- this first capture group matches the whole first part,
65                         --  so we can later strip it from the rest of the line
66                         .. "("
67                                 .. "([" .. CHAR_CLASSES.VARIABLE .. "+)" -- variable name
68                                 .. CHAR_CLASSES.SPACE .. "*"
69                                 .. "%(([^%)]*)%)"  -- readable name
70                                 .. CHAR_CLASSES.SPACE .. "*"
71                                 .. "(" .. CHAR_CLASSES.VARIABLE .. "+)" -- type
72                                 .. CHAR_CLASSES.SPACE .. "*"
73                         .. ")")
74
75         if not first_part then
76                 return "Invalid line"
77         end
78
79         if name:match("secure%.[.]*") and not allow_secure then
80                 return "Tried to add \"secure.\" setting"
81         end
82
83         if readable_name == "" then
84                 readable_name = nil
85         end
86         local remaining_line = line:sub(first_part:len() + 1)
87
88         if setting_type == "int" then
89                 local default, min, max = remaining_line:match("^"
90                                 -- first int is required, the last 2 are optional
91                                 .. "(" .. CHAR_CLASSES.INTEGER .. "+)" .. CHAR_CLASSES.SPACE .. "*"
92                                 .. "(" .. CHAR_CLASSES.INTEGER .. "*)" .. CHAR_CLASSES.SPACE .. "*"
93                                 .. "(" .. CHAR_CLASSES.INTEGER .. "*)"
94                                 .. "$")
95
96                 if not default or not tonumber(default) then
97                         return "Invalid integer setting"
98                 end
99
100                 min = tonumber(min)
101                 max = tonumber(max)
102                 table.insert(settings, {
103                         name = name,
104                         readable_name = readable_name,
105                         type = "int",
106                         default = default,
107                         min = min,
108                         max = max,
109                         comment = current_comment,
110                 })
111                 return
112         end
113
114         if setting_type == "string" or setting_type == "noise_params"
115                         or setting_type == "key" or setting_type == "v3f" then
116                 local default = remaining_line:match("^(.*)$")
117
118                 if not default then
119                         return "Invalid string setting"
120                 end
121                 if setting_type == "key" and not read_all then
122                         -- ignore key type if read_all is false
123                         return
124                 end
125
126                 table.insert(settings, {
127                         name = name,
128                         readable_name = readable_name,
129                         type = setting_type,
130                         default = default,
131                         comment = current_comment,
132                 })
133                 return
134         end
135
136         if setting_type == "bool" then
137                 if remaining_line ~= "false" and remaining_line ~= "true" then
138                         return "Invalid boolean setting"
139                 end
140
141                 table.insert(settings, {
142                         name = name,
143                         readable_name = readable_name,
144                         type = "bool",
145                         default = remaining_line,
146                         comment = current_comment,
147                 })
148                 return
149         end
150
151         if setting_type == "float" then
152                 local default, min, max = remaining_line:match("^"
153                                 -- first float is required, the last 2 are optional
154                                 .. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "*"
155                                 .. "(" .. CHAR_CLASSES.FLOAT .. "*)" .. CHAR_CLASSES.SPACE .. "*"
156                                 .. "(" .. CHAR_CLASSES.FLOAT .. "*)"
157                                 .."$")
158
159                 if not default or not tonumber(default) then
160                         return "Invalid float setting"
161                 end
162
163                 min = tonumber(min)
164                 max = tonumber(max)
165                 table.insert(settings, {
166                         name = name,
167                         readable_name = readable_name,
168                         type = "float",
169                         default = default,
170                         min = min,
171                         max = max,
172                         comment = current_comment,
173                 })
174                 return
175         end
176
177         if setting_type == "enum" then
178                 local default, values = remaining_line:match("^"
179                                 -- first value (default) may be empty (i.e. is optional)
180                                 .. "(" .. CHAR_CLASSES.VARIABLE .. "*)" .. CHAR_CLASSES.SPACE .. "*"
181                                 .. "(" .. CHAR_CLASSES.FLAGS .. "+)"
182                                 .. "$")
183
184                 if not default or values == "" then
185                         return "Invalid enum setting"
186                 end
187
188                 table.insert(settings, {
189                         name = name,
190                         readable_name = readable_name,
191                         type = "enum",
192                         default = default,
193                         values = values:split(",", true),
194                         comment = current_comment,
195                 })
196                 return
197         end
198
199         if setting_type == "path" then
200                 local default = remaining_line:match("^(.*)$")
201
202                 if not default then
203                         return "Invalid path setting"
204                 end
205
206                 table.insert(settings, {
207                         name = name,
208                         readable_name = readable_name,
209                         type = "path",
210                         default = default,
211                         comment = current_comment,
212                 })
213                 return
214         end
215
216         if setting_type == "flags" then
217                 local default, possible = remaining_line:match("^"
218                                 -- first value (default) may be empty (i.e. is optional)
219                                 -- this is implemented by making the last value optional, and
220                                 -- swapping them around if it turns out empty.
221                                 .. "(" .. CHAR_CLASSES.FLAGS .. "+)" .. CHAR_CLASSES.SPACE .. "*"
222                                 .. "(" .. CHAR_CLASSES.FLAGS .. "*)"
223                                 .. "$")
224
225                 if not default or not possible then
226                         return "Invalid flags setting"
227                 end
228
229                 if possible == "" then
230                         possible = default
231                         default = ""
232                 end
233
234                 table.insert(settings, {
235                         name = name,
236                         readable_name = readable_name,
237                         type = "flags",
238                         default = default,
239                         possible = possible,
240                         comment = current_comment,
241                 })
242                 return
243         end
244
245         return "Invalid setting type \"" .. setting_type .. "\""
246 end
247
248 local function parse_single_file(file, filepath, read_all, result, base_level, allow_secure)
249         -- store this helper variable in the table so it's easier to pass to parse_setting_line()
250         result.current_comment = ""
251
252         local line = file:read("*line")
253         while line do
254                 local error_msg = parse_setting_line(result, line, read_all, base_level, allow_secure)
255                 if error_msg then
256                         core.log("error", error_msg .. " in " .. filepath .. " \"" .. line .. "\"")
257                 end
258                 line = file:read("*line")
259         end
260
261         result.current_comment = nil
262 end
263
264 -- read_all: whether to ignore certain setting types for GUI or not
265 -- parse_mods: whether to parse settingtypes.txt in mods and games
266 local function parse_config_file(read_all, parse_mods)
267         local builtin_path = core.get_builtin_path() .. DIR_DELIM .. FILENAME
268         local file = io.open(builtin_path, "r")
269         local settings = {}
270         if not file then
271                 core.log("error", "Can't load " .. FILENAME)
272                 return settings
273         end
274
275         parse_single_file(file, builtin_path, read_all, settings, 0, true)
276
277         file:close()
278
279         if parse_mods then
280                 -- Parse games
281                 local games_category_initialized = false
282                 local index = 1
283                 local game = gamemgr.get_game(index)
284                 while game do
285                         local path = game.path .. DIR_DELIM .. FILENAME
286                         local file = io.open(path, "r")
287                         if file then
288                                 if not games_category_initialized then
289                                         local translation = fgettext_ne("Games"), -- not used, but needed for xgettext
290                                         table.insert(settings, {
291                                                 name = "Games",
292                                                 level = 0,
293                                                 type = "category",
294                                         })
295                                         games_category_initialized = true
296                                 end
297
298                                 table.insert(settings, {
299                                         name = game.name,
300                                         level = 1,
301                                         type = "category",
302                                 })
303
304                                 parse_single_file(file, path, read_all, settings, 2, false)
305
306                                 file:close()
307                         end
308
309                         index = index + 1
310                         game = gamemgr.get_game(index)
311                 end
312
313                 -- Parse mods
314                 local mods_category_initialized = false
315                 local mods = {}
316                 get_mods(core.get_modpath(), mods)
317                 for _, mod in ipairs(mods) do
318                         local path = mod.path .. DIR_DELIM .. FILENAME
319                         local file = io.open(path, "r")
320                         if file then
321                                 if not mods_category_initialized then
322                                         local translation = fgettext_ne("Mods"), -- not used, but needed for xgettext
323                                         table.insert(settings, {
324                                                 name = "Mods",
325                                                 level = 0,
326                                                 type = "category",
327                                         })
328                                         mods_category_initialized = true
329                                 end
330
331                                 table.insert(settings, {
332                                         name = mod.name,
333                                         level = 1,
334                                         type = "category",
335                                 })
336
337                                 parse_single_file(file, path, read_all, settings, 2, false)
338
339                                 file:close()
340                         end
341                 end
342         end
343
344         return settings
345 end
346
347 local settings = parse_config_file(false, true)
348 local selected_setting = 1
349
350 local function get_current_value(setting)
351         local value = core.setting_get(setting.name)
352         if value == nil then
353                 value = setting.default
354         end
355         return value
356 end
357
358 local function create_change_setting_formspec(dialogdata)
359         local setting = settings[selected_setting]
360         local formspec = "size[10,5.2,true]" ..
361                         "button[5,4.5;2,1;btn_done;" .. fgettext("Save") .. "]" ..
362                         "button[3,4.5;2,1;btn_cancel;" .. fgettext("Cancel") .. "]" ..
363                         "tablecolumns[color;text]" ..
364                         "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
365                         "table[0,0;10,3;info;"
366
367         if setting.readable_name then
368                 formspec = formspec .. "#FFFF00," .. fgettext(setting.readable_name)
369                                 .. " (" .. core.formspec_escape(setting.name) .. "),"
370         else
371                 formspec = formspec .. "#FFFF00," .. core.formspec_escape(setting.name) .. ","
372         end
373
374         formspec = formspec .. ",,"
375
376         local comment_text = ""
377
378         if setting.comment == "" then
379                 comment_text = fgettext_ne("(No description of setting given)")
380         else
381                 comment_text = fgettext_ne(setting.comment)
382         end
383         for _, comment_line in ipairs(comment_text:split("\n", true)) do
384                 formspec = formspec .. "," .. core.formspec_escape(comment_line) .. ","
385         end
386
387         if setting.type == "flags" then
388                 formspec = formspec .. ",,"
389                                 .. "," .. fgettext("Please enter a comma seperated list of flags.") .. ","
390                                 .. "," .. fgettext("Possible values are: ")
391                                 .. core.formspec_escape(setting.possible:gsub(",", ", ")) .. ","
392         elseif setting.type == "noise_params" then
393                 formspec = formspec .. ",,"
394                                 .. "," .. fgettext("Format: <offset>, <scale>, (<spreadX>, <spreadY>, <spreadZ>), <seed>, <octaves>, <persistence>") .. ","
395                                 .. "," .. fgettext("Optionally the lacunarity can be appended with a leading comma.") .. ","
396         elseif setting.type == "v3f" then
397                 formspec = formspec .. ",,"
398                                 .. "," .. fgettext_ne("Format is 3 numbers separated by commas and inside brackets.") .. ","
399         end
400
401         formspec = formspec:sub(1, -2) -- remove trailing comma
402
403         formspec = formspec .. ";1]"
404
405         if setting.type == "bool" then
406                 local selected_index
407                 if core.is_yes(get_current_value(setting)) then
408                         selected_index = 2
409                 else
410                         selected_index = 1
411                 end
412                 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
413                                 .. fgettext("Disabled") .. "," .. fgettext("Enabled") .. ";"
414                                 .. selected_index .. "]"
415
416         elseif setting.type == "enum" then
417                 local selected_index = 0
418                 formspec = formspec .. "dropdown[0.5,3.5;3,1;dd_setting_value;"
419                 for index, value in ipairs(setting.values) do
420                         -- translating value is not possible, since it's the value
421                         --  that we set the setting to
422                         formspec = formspec ..  core.formspec_escape(value) .. ","
423                         if get_current_value(setting) == value then
424                                 selected_index = index
425                         end
426                 end
427                 if #setting.values > 0 then
428                         formspec = formspec:sub(1, -2) -- remove trailing comma
429                 end
430                 formspec = formspec .. ";" .. selected_index .. "]"
431
432         elseif setting.type == "path" then
433                 local current_value = dialogdata.selected_path
434                 if not current_value then
435                         current_value = get_current_value(setting)
436                 end
437                 formspec = formspec .. "field[0.5,4;7.5,1;te_setting_value;;"
438                                 .. core.formspec_escape(current_value) .. "]"
439                                 .. "button[8,3.75;2,1;btn_browser_path;" .. fgettext("Browse") .. "]"
440
441         else
442                 -- TODO: fancy input for float, int, flags, noise_params, v3f
443                 local width = 10
444                 local text = get_current_value(setting)
445                 if dialogdata.error_message then
446                         formspec = formspec .. "tablecolumns[color;text]" ..
447                         "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
448                         "table[5,3.9;5,0.6;error_message;#FF0000,"
449                                         .. core.formspec_escape(dialogdata.error_message) .. ";0]"
450                         width = 5
451                         if dialogdata.entered_text then
452                                 text = dialogdata.entered_text
453                         end
454                 end
455                 formspec = formspec .. "field[0.5,4;" .. width .. ",1;te_setting_value;;"
456                                 .. core.formspec_escape(text) .. "]"
457         end
458         return formspec
459 end
460
461 local function handle_change_setting_buttons(this, fields)
462         if fields["btn_done"] or fields["key_enter"] then
463                 local setting = settings[selected_setting]
464                 if setting.type == "bool" then
465                         local new_value = fields["dd_setting_value"]
466                         -- Note: new_value is the actual (translated) value shown in the dropdown
467                         core.setting_setbool(setting.name, new_value == fgettext("Enabled"))
468
469                 elseif setting.type == "enum" then
470                         local new_value = fields["dd_setting_value"]
471                         core.setting_set(setting.name, new_value)
472
473                 elseif setting.type == "int" then
474                         local new_value = tonumber(fields["te_setting_value"])
475                         if not new_value or math.floor(new_value) ~= new_value then
476                                 this.data.error_message = fgettext_ne("Please enter a valid integer.")
477                                 this.data.entered_text = fields["te_setting_value"]
478                                 core.update_formspec(this:get_formspec())
479                                 return true
480                         end
481                         if setting.min and new_value < setting.min then
482                                 this.data.error_message = fgettext_ne("The value must be at least $1.", setting.min)
483                                 this.data.entered_text = fields["te_setting_value"]
484                                 core.update_formspec(this:get_formspec())
485                                 return true
486                         end
487                         if setting.max and new_value > setting.max then
488                                 this.data.error_message = fgettext_ne("The value must not be larger than $1.", setting.max)
489                                 this.data.entered_text = fields["te_setting_value"]
490                                 core.update_formspec(this:get_formspec())
491                                 return true
492                         end
493                         core.setting_set(setting.name, new_value)
494
495                 elseif setting.type == "float" then
496                         local new_value = tonumber(fields["te_setting_value"])
497                         if not new_value then
498                                 this.data.error_message = fgettext_ne("Please enter a valid number.")
499                                 this.data.entered_text = fields["te_setting_value"]
500                                 core.update_formspec(this:get_formspec())
501                                 return true
502                         end
503                         core.setting_set(setting.name, new_value)
504
505                 elseif setting.type == "flags" then
506                         local new_value = fields["te_setting_value"]
507                         for _,value in ipairs(new_value:split(",", true)) do
508                                 value = value:trim()
509                                 local possible = "," .. setting.possible .. ","
510                                 if not possible:find("," .. value .. ",", 0, true) then
511                                         this.data.error_message = fgettext_ne("\"$1\" is not a valid flag.", value)
512                                         this.data.entered_text = fields["te_setting_value"]
513                                         core.update_formspec(this:get_formspec())
514                                         return true
515                                 end
516                         end
517                         core.setting_set(setting.name, new_value)
518
519                 else
520                         local new_value = fields["te_setting_value"]
521                         core.setting_set(setting.name, new_value)
522                 end
523                 core.setting_save()
524                 this:delete()
525                 return true
526         end
527
528         if fields["btn_cancel"] then
529                 this:delete()
530                 return true
531         end
532
533         if fields["btn_browser_path"] then
534                 core.show_file_open_dialog("dlg_browse_path", fgettext_ne("Select path"))
535         end
536
537         if fields["dlg_browse_path_accepted"] then
538                 this.data.selected_path = fields["dlg_browse_path_accepted"]
539                 core.update_formspec(this:get_formspec())
540         end
541
542         return false
543 end
544
545 local function create_settings_formspec(tabview, name, tabdata)
546         local formspec = "size[12,6.5;true]" ..
547                         "tablecolumns[color;tree;text,width=32;text]" ..
548                         "tableoptions[background=#00000000;border=false]" ..
549                         "table[0,0;12,5.5;list_settings;"
550
551         local current_level = 0
552         for _, entry in ipairs(settings) do
553                 local name
554                 if not core.setting_getbool("main_menu_technical_settings") and entry.readable_name then
555                         name = fgettext_ne(entry.readable_name)
556                 else
557                         name = entry.name
558                 end
559
560                 if entry.type == "category" then
561                         current_level = entry.level
562                         formspec = formspec .. "#FFFF00," .. current_level .. "," .. fgettext(name) .. ",,"
563
564                 elseif entry.type == "bool" then
565                         local value = get_current_value(entry)
566                         if core.is_yes(value) then
567                                 value = fgettext("Enabled")
568                         else
569                                 value = fgettext("Disabled")
570                         end
571                         formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
572                                         .. value .. ","
573
574                 elseif entry.type == "key" then
575                         -- ignore key settings, since we have a special dialog for them
576
577                 else
578                         formspec = formspec .. "," .. (current_level + 1) .. "," .. core.formspec_escape(name) .. ","
579                                         .. core.formspec_escape(get_current_value(entry)) .. ","
580                 end
581         end
582
583         if #settings > 0 then
584                 formspec = formspec:sub(1, -2) -- remove trailing comma
585         end
586         formspec = formspec .. ";" .. selected_setting .. "]" ..
587                         "button[0,6;4,1;btn_back;".. fgettext("< Back to Settings page") .. "]" ..
588                         "button[10,6;2,1;btn_edit;" .. fgettext("Edit") .. "]" ..
589                         "button[7,6;3,1;btn_restore;" .. fgettext("Restore Default") .. "]" ..
590                         "checkbox[0,5.3;cb_tech_settings;" .. fgettext("Show technical names") .. ";"
591                                         .. dump(core.setting_getbool("main_menu_technical_settings")) .. "]"
592
593         return formspec
594 end
595
596 local function handle_settings_buttons(this, fields, tabname, tabdata)
597         local list_enter = false
598         if fields["list_settings"] then
599                 selected_setting = core.get_table_index("list_settings")
600                 if  core.explode_table_event(fields["list_settings"]).type == "DCL" then
601                         -- Directly toggle booleans
602                         local setting = settings[selected_setting]
603                         if setting.type == "bool" then
604                                 local current_value = get_current_value(setting)
605                                 core.setting_setbool(setting.name, not core.is_yes(current_value))
606                                 core.setting_save()
607                                 return true
608                         else
609                                 list_enter = true
610                         end
611                 else
612                         return true
613                 end
614         end
615
616         if fields["btn_edit"] or list_enter then
617                 local setting = settings[selected_setting]
618                 if setting.type ~= "category" then
619                         local edit_dialog = dialog_create("change_setting", create_change_setting_formspec,
620                                         handle_change_setting_buttons)
621                         edit_dialog:set_parent(this)
622                         this:hide()
623                         edit_dialog:show()
624                 end
625                 return true
626         end
627
628         if fields["btn_restore"] then
629                 local setting = settings[selected_setting]
630                 if setting.type ~= "category" then
631                         core.setting_set(setting.name, setting.default)
632                         core.setting_save()
633                         core.update_formspec(this:get_formspec())
634                 end
635                 return true
636         end
637
638         if fields["btn_back"] then
639                 this:delete()
640                 return true
641         end
642
643         if fields["cb_tech_settings"] then
644                 core.setting_set("main_menu_technical_settings", fields["cb_tech_settings"])
645                 core.setting_save()
646                 core.update_formspec(this:get_formspec())
647                 return true
648         end
649
650         return false
651 end
652
653 function create_adv_settings_dlg()
654         local dlg = dialog_create("settings_advanced",
655                                 create_settings_formspec,
656                                 handle_settings_buttons,
657                                 nil)
658
659                                 return dlg
660 end
661
662 -- Generate minetest.conf.example and settings_translation_file.cpp
663
664 -- *** Please note ***
665 -- There is text in minetest.conf.example that will not be generated from
666 -- settingtypes.txt but must be preserved:
667 -- The documentation of mapgen noise parameter formats (title plus 16 lines)
668 -- Noise parameter 'mgv5_np_ground' in group format (13 lines)
669
670 --assert(loadfile(core.get_builtin_path()..DIR_DELIM.."mainmenu"..DIR_DELIM.."generate_from_settingtypes.lua"))(parse_config_file(true, false))