Add configurable key bindings for hotbar scrolling, and for changing volume.
[oweals/minetest.git] / builtin / mainmenu / tab_settings.lua
1 --Minetest
2 --Copyright (C) 2013 sapier
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 --------------------------------------------------------------------------------
19
20 local labels = {
21         leaves = {
22                 fgettext("Opaque Leaves"),
23                 fgettext("Simple Leaves"),
24                 fgettext("Fancy Leaves")
25         },
26         node_highlighting = {
27                 fgettext("Node Outlining"),
28                 fgettext("Node Highlighting")
29         },
30         filters = {
31                 fgettext("No Filter"),
32                 fgettext("Bilinear Filter"),
33                 fgettext("Trilinear Filter")
34         },
35         mipmap = {
36                 fgettext("No Mipmap"),
37                 fgettext("Mipmap"),
38                 fgettext("Mipmap + Aniso. Filter")
39         },
40         antialiasing = {
41                 fgettext("None"),
42                 fgettext("2x"),
43                 fgettext("4x"),
44                 fgettext("8x")
45         }
46 }
47
48 local dd_options = {
49         leaves = {
50                 table.concat(labels.leaves, ","),
51                 {"opaque", "simple", "fancy"}
52         },
53         node_highlighting = {
54                 table.concat(labels.node_highlighting, ","),
55                 {"box", "halo"}
56         },
57         filters = {
58                 table.concat(labels.filters, ","),
59                 {"", "bilinear_filter", "trilinear_filter"}
60         },
61         mipmap = {
62                 table.concat(labels.mipmap, ","),
63                 {"", "mip_map", "anisotropic_filter"}
64         },
65         antialiasing = {
66                 table.concat(labels.antialiasing, ","),
67                 {"0", "2", "4", "8"}
68         }
69 }
70
71 local getSettingIndex = {
72         Leaves = function()
73                 local style = core.setting_get("leaves_style")
74                 for idx, name in pairs(dd_options.leaves[2]) do
75                         if style == name then return idx end
76                 end
77                 return 1
78         end,
79         NodeHighlighting = function()
80                 local style = core.setting_get("node_highlighting")
81                 for idx, name in pairs(dd_options.node_highlighting[2]) do
82                         if style == name then return idx end
83                 end
84                 return 1
85         end,
86         Filter = function()
87                 if core.setting_get(dd_options.filters[2][3]) == "true" then
88                         return 3
89                 elseif core.setting_get(dd_options.filters[2][3]) == "false" and
90                                 core.setting_get(dd_options.filters[2][2]) == "true" then
91                         return 2
92                 end
93                 return 1
94         end,
95         Mipmap = function()
96                 if core.setting_get(dd_options.mipmap[2][3]) == "true" then
97                         return 3
98                 elseif core.setting_get(dd_options.mipmap[2][3]) == "false" and
99                                 core.setting_get(dd_options.mipmap[2][2]) == "true" then
100                         return 2
101                 end
102                 return 1
103         end,
104         Antialiasing = function()
105                 local antialiasing_setting = core.setting_get("fsaa")
106                 for i = 1, #dd_options.antialiasing[2] do
107                         if antialiasing_setting == dd_options.antialiasing[2][i] then
108                                 return i
109                         end
110                 end
111                 return 1
112         end
113 }
114
115 local function antialiasing_fname_to_name(fname)
116         for i = 1, #labels.antialiasing do
117                 if fname == labels.antialiasing[i] then
118                         return dd_options.antialiasing[2][i]
119                 end
120         end
121         return 0
122 end
123
124 local function dlg_confirm_reset_formspec(data)
125         return  "size[8,3]" ..
126                 "label[1,1;" .. fgettext("Are you sure to reset your singleplayer world?") .. "]" ..
127                 "button[1,2;2.6,0.5;dlg_reset_singleplayer_confirm;" .. fgettext("Yes") .. "]" ..
128                 "button[4,2;2.8,0.5;dlg_reset_singleplayer_cancel;" .. fgettext("No") .. "]"
129 end
130
131 local function dlg_confirm_reset_btnhandler(this, fields, dialogdata)
132
133         if fields["dlg_reset_singleplayer_confirm"] ~= nil then
134                 local worldlist = core.get_worlds()
135                 local found_singleplayerworld = false
136
137                 for i = 1, #worldlist do
138                         if worldlist[i].name == "singleplayerworld" then
139                                 found_singleplayerworld = true
140                                 gamedata.worldindex = i
141                         end
142                 end
143
144                 if found_singleplayerworld then
145                         core.delete_world(gamedata.worldindex)
146                 end
147
148                 core.create_world("singleplayerworld", 1)
149                 worldlist = core.get_worlds()
150                 found_singleplayerworld = false
151
152                 for i = 1, #worldlist do
153                         if worldlist[i].name == "singleplayerworld" then
154                                 found_singleplayerworld = true
155                                 gamedata.worldindex = i
156                         end
157                 end
158         end
159
160         this.parent:show()
161         this:hide()
162         this:delete()
163         return true
164 end
165
166 local function showconfirm_reset(tabview)
167         local new_dlg = dialog_create("reset_spworld",
168                 dlg_confirm_reset_formspec,
169                 dlg_confirm_reset_btnhandler,
170                 nil)
171         new_dlg:set_parent(tabview)
172         tabview:hide()
173         new_dlg:show()
174 end
175
176 local function formspec(tabview, name, tabdata)
177         local tab_string =
178                 "box[0,0;3.5,4.5;#999999]" ..
179                 "checkbox[0.25,0;cb_smooth_lighting;" .. fgettext("Smooth Lighting") .. ";"
180                                 .. dump(core.setting_getbool("smooth_lighting")) .. "]" ..
181                 "checkbox[0.25,0.5;cb_particles;" .. fgettext("Particles") .. ";"
182                                 .. dump(core.setting_getbool("enable_particles")) .. "]" ..
183                 "checkbox[0.25,1;cb_3d_clouds;" .. fgettext("3D Clouds") .. ";"
184                                 .. dump(core.setting_getbool("enable_3d_clouds")) .. "]" ..
185                 "checkbox[0.25,1.5;cb_opaque_water;" .. fgettext("Opaque Water") .. ";"
186                                 .. dump(core.setting_getbool("opaque_water")) .. "]" ..
187                 "checkbox[0.25,2.0;cb_connected_glass;" .. fgettext("Connected Glass") .. ";"
188                                 .. dump(core.setting_getbool("connected_glass")) .. "]" ..
189                 "dropdown[0.25,2.8;3.3;dd_node_highlighting;" .. dd_options.node_highlighting[1] .. ";"
190                                 .. getSettingIndex.NodeHighlighting() .. "]" ..
191                 "dropdown[0.25,3.6;3.3;dd_leaves_style;" .. dd_options.leaves[1] .. ";"
192                                 .. getSettingIndex.Leaves() .. "]" ..
193                 "box[3.75,0;3.75,4.45;#999999]" ..
194                 "label[3.85,0.1;" .. fgettext("Texturing:") .. "]" ..
195                 "dropdown[3.85,0.55;3.85;dd_filters;" .. dd_options.filters[1] .. ";"
196                                 .. getSettingIndex.Filter() .. "]" ..
197                 "dropdown[3.85,1.35;3.85;dd_mipmap;" .. dd_options.mipmap[1] .. ";"
198                                 .. getSettingIndex.Mipmap() .. "]" ..
199                 "label[3.85,2.15;" .. fgettext("Antialiasing:") .. "]" ..
200                 "dropdown[3.85,2.6;3.85;dd_antialiasing;" .. dd_options.antialiasing[1] .. ";"
201                                 .. getSettingIndex.Antialiasing() .. "]" ..
202                 "label[3.85,3.45;" .. fgettext("Screen:") .. "]" ..
203                 "checkbox[3.85,3.6;cb_autosave_screensize;" .. fgettext("Autosave screen size") .. ";"
204                                 .. dump(core.setting_getbool("autosave_screensize")) .. "]" ..
205                 "box[7.75,0;4,4.4;#999999]" ..
206                 "checkbox[8,0;cb_shaders;" .. fgettext("Shaders") .. ";"
207                                 .. dump(core.setting_getbool("enable_shaders")) .. "]"
208
209         if PLATFORM == "Android" then
210                 tab_string = tab_string ..
211                         "button[8,4.75;3.75,0.5;btn_reset_singleplayer;"
212                         .. fgettext("Reset singleplayer world") .. "]"
213         else
214                 tab_string = tab_string ..
215                         "button[8,4.85;3.75,0.5;btn_change_keys;"
216                         .. fgettext("Change keys") .. "]"
217         end
218
219         tab_string = tab_string ..
220                 "button[0,4.85;3.75,0.5;btn_advanced_settings;"
221                 .. fgettext("Advanced Settings") .. "]"
222
223
224         if core.setting_get("touchscreen_threshold") ~= nil then
225                 tab_string = tab_string ..
226                         "label[4.3,4.1;" .. fgettext("Touchthreshold (px)") .. "]" ..
227                         "dropdown[3.85,4.55;3.85;dd_touchthreshold;0,10,20,30,40,50;" ..
228                         ((tonumber(core.setting_get("touchscreen_threshold")) / 10) + 1) .. "]"
229         end
230
231         if core.setting_getbool("enable_shaders") then
232                 tab_string = tab_string ..
233                         "checkbox[8,0.5;cb_bumpmapping;" .. fgettext("Bump Mapping") .. ";"
234                                         .. dump(core.setting_getbool("enable_bumpmapping")) .. "]" ..
235                         "checkbox[8,1;cb_tonemapping;" .. fgettext("Tone Mapping") .. ";"
236                                         .. dump(core.setting_getbool("tone_mapping")) .. "]" ..
237                         "checkbox[8,1.5;cb_generate_normalmaps;" .. fgettext("Normal Mapping") .. ";"
238                                         .. dump(core.setting_getbool("generate_normalmaps")) .. "]" ..
239                         "checkbox[8,2;cb_parallax;" .. fgettext("Parallax Occlusion") .. ";"
240                                         .. dump(core.setting_getbool("enable_parallax_occlusion")) .. "]" ..
241                         "checkbox[8,2.5;cb_waving_water;" .. fgettext("Waving Water") .. ";"
242                                         .. dump(core.setting_getbool("enable_waving_water")) .. "]" ..
243                         "checkbox[8,3;cb_waving_leaves;" .. fgettext("Waving Leaves") .. ";"
244                                         .. dump(core.setting_getbool("enable_waving_leaves")) .. "]" ..
245                         "checkbox[8,3.5;cb_waving_plants;" .. fgettext("Waving Plants") .. ";"
246                                         .. dump(core.setting_getbool("enable_waving_plants")) .. "]"
247         else
248                 tab_string = tab_string ..
249                         "tablecolumns[color;text]" ..
250                         "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
251                         "table[8.33,0.7;3.5,4;shaders;" ..
252                                 "#888888," .. fgettext("Bump Mapping") .. "," ..
253                                 "#888888," .. fgettext("Tone Mapping") .. "," ..
254                                 "#888888," .. fgettext("Normal Mapping") .. "," ..
255                                 "#888888," .. fgettext("Parallax Occlusion") .. "," ..
256                                 "#888888," .. fgettext("Waving Water") .. "," ..
257                                 "#888888," .. fgettext("Waving Leaves") .. "," ..
258                                 "#888888," .. fgettext("Waving Plants") .. "," ..
259                                 ";1]"
260         end
261
262         return tab_string
263 end
264
265 --------------------------------------------------------------------------------
266 local function handle_settings_buttons(this, fields, tabname, tabdata)
267
268         if fields["btn_advanced_settings"] ~= nil then
269                 local adv_settings_dlg = create_adv_settings_dlg()
270                 adv_settings_dlg:set_parent(this)
271                 this:hide()
272                 adv_settings_dlg:show()
273                 --mm_texture.update("singleplayer", current_game())
274                 return true
275         end
276         if fields["cb_smooth_lighting"] then
277                 core.setting_set("smooth_lighting", fields["cb_smooth_lighting"])
278                 return true
279         end
280         if fields["cb_particles"] then
281                 core.setting_set("enable_particles", fields["cb_particles"])
282                 return true
283         end
284         if fields["cb_3d_clouds"] then
285                 core.setting_set("enable_3d_clouds", fields["cb_3d_clouds"])
286                 return true
287         end
288         if fields["cb_opaque_water"] then
289                 core.setting_set("opaque_water", fields["cb_opaque_water"])
290                 return true
291         end
292         if fields["cb_connected_glass"] then
293                 core.setting_set("connected_glass", fields["cb_connected_glass"])
294                 return true
295         end
296         if fields["cb_autosave_screensize"] then
297                 core.setting_set("autosave_screensize", fields["cb_autosave_screensize"])
298                 return true
299         end
300         if fields["cb_shaders"] then
301                 if (core.setting_get("video_driver") == "direct3d8" or
302                                 core.setting_get("video_driver") == "direct3d9") then
303                         core.setting_set("enable_shaders", "false")
304                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
305                 else
306                         core.setting_set("enable_shaders", fields["cb_shaders"])
307                 end
308                 return true
309         end
310         if fields["cb_bumpmapping"] then
311                 core.setting_set("enable_bumpmapping", fields["cb_bumpmapping"])
312                 return true
313         end
314         if fields["cb_tonemapping"] then
315                 core.setting_set("tone_mapping", fields["cb_tonemapping"])
316                 return true
317         end
318         if fields["cb_generate_normalmaps"] then
319                 core.setting_set("generate_normalmaps", fields["cb_generate_normalmaps"])
320                 return true
321         end
322         if fields["cb_parallax"] then
323                 core.setting_set("enable_parallax_occlusion", fields["cb_parallax"])
324                 return true
325         end
326         if fields["cb_waving_water"] then
327                 core.setting_set("enable_waving_water", fields["cb_waving_water"])
328                 return true
329         end
330         if fields["cb_waving_leaves"] then
331                 core.setting_set("enable_waving_leaves", fields["cb_waving_leaves"])
332         end
333         if fields["cb_waving_plants"] then
334                 core.setting_set("enable_waving_plants", fields["cb_waving_plants"])
335                 return true
336         end
337         if fields["btn_change_keys"] then
338                 core.show_keys_menu()
339                 return true
340         end
341         if fields["cb_touchscreen_target"] then
342                 core.setting_set("touchtarget", fields["cb_touchscreen_target"])
343                 return true
344         end
345         if fields["btn_reset_singleplayer"] then
346                 showconfirm_reset(this)
347                 return true
348         end
349
350         --Note dropdowns have to be handled LAST!
351         local ddhandled = false
352
353         for i = 1, #labels.leaves do
354                 if fields["dd_leaves_style"] == labels.leaves[i] then
355                         core.setting_set("leaves_style", dd_options.leaves[2][i])
356                         ddhandled = true
357                 end
358         end
359         for i = 1, #labels.node_highlighting do
360                 if fields["dd_node_highlighting"] == labels.node_highlighting[i] then
361                         core.setting_set("node_highlighting", dd_options.node_highlighting[2][i])
362                         ddhandled = true
363                 end
364         end
365         if fields["dd_filters"] == labels.filters[1] then
366                 core.setting_set("bilinear_filter", "false")
367                 core.setting_set("trilinear_filter", "false")
368                 ddhandled = true
369         elseif fields["dd_filters"] == labels.filters[2] then
370                 core.setting_set("bilinear_filter", "true")
371                 core.setting_set("trilinear_filter", "false")
372                 ddhandled = true
373         elseif fields["dd_filters"] == labels.filters[3] then
374                 core.setting_set("bilinear_filter", "false")
375                 core.setting_set("trilinear_filter", "true")
376                 ddhandled = true
377         end
378         if fields["dd_mipmap"] == labels.mipmap[1] then
379                 core.setting_set("mip_map", "false")
380                 core.setting_set("anisotropic_filter", "false")
381                 ddhandled = true
382         elseif fields["dd_mipmap"] == labels.mipmap[2] then
383                 core.setting_set("mip_map", "true")
384                 core.setting_set("anisotropic_filter", "false")
385                 ddhandled = true
386         elseif fields["dd_mipmap"] == labels.mipmap[3] then
387                 core.setting_set("mip_map", "true")
388                 core.setting_set("anisotropic_filter", "true")
389                 ddhandled = true
390         end
391         if fields["dd_antialiasing"] then
392                 core.setting_set("fsaa",
393                         antialiasing_fname_to_name(fields["dd_antialiasing"]))
394                 ddhandled = true
395         end
396         if fields["dd_touchthreshold"] then
397                 core.setting_set("touchscreen_threshold", fields["dd_touchthreshold"])
398                 ddhandled = true
399         end
400
401         return ddhandled
402 end
403
404 return {
405         name = "settings",
406         caption = fgettext("Settings"),
407         cbf_formspec = formspec,
408         cbf_button_handler = handle_settings_buttons
409 }