Mainmenu: Refactor tab UI code
[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,3.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                 "box[7.75,0;4,4.4;#999999]" ..
203                 "checkbox[8,0;cb_shaders;" .. fgettext("Shaders") .. ";"
204                                 .. dump(core.setting_getbool("enable_shaders")) .. "]"
205
206         tab_string = tab_string ..
207                 "button[8,4.75;3.75,0.5;btn_change_keys;" .. fgettext("Change keys") .. "]" ..
208                 "button[0,4.75;3.75,0.5;btn_advanced_settings;" .. fgettext("Advanced Settings") .. "]"
209
210
211         if core.setting_get("touchscreen_threshold") ~= nil then
212                 tab_string = tab_string ..
213                         "label[4.3,4.1;" .. fgettext("Touchthreshold (px)") .. "]" ..
214                         "dropdown[3.85,4.55;3.85;dd_touchthreshold;0,10,20,30,40,50;" ..
215                         ((tonumber(core.setting_get("touchscreen_threshold")) / 10) + 1) .. "]"
216         end
217
218         if core.setting_getbool("enable_shaders") then
219                 tab_string = tab_string ..
220                         "checkbox[8,0.5;cb_bumpmapping;" .. fgettext("Bump Mapping") .. ";"
221                                         .. dump(core.setting_getbool("enable_bumpmapping")) .. "]" ..
222                         "checkbox[8,1;cb_tonemapping;" .. fgettext("Tone Mapping") .. ";"
223                                         .. dump(core.setting_getbool("tone_mapping")) .. "]" ..
224                         "checkbox[8,1.5;cb_generate_normalmaps;" .. fgettext("Normal Mapping") .. ";"
225                                         .. dump(core.setting_getbool("generate_normalmaps")) .. "]" ..
226                         "checkbox[8,2;cb_parallax;" .. fgettext("Parallax Occlusion") .. ";"
227                                         .. dump(core.setting_getbool("enable_parallax_occlusion")) .. "]" ..
228                         "checkbox[8,2.5;cb_waving_water;" .. fgettext("Waving Water") .. ";"
229                                         .. dump(core.setting_getbool("enable_waving_water")) .. "]" ..
230                         "checkbox[8,3;cb_waving_leaves;" .. fgettext("Waving Leaves") .. ";"
231                                         .. dump(core.setting_getbool("enable_waving_leaves")) .. "]" ..
232                         "checkbox[8,3.5;cb_waving_plants;" .. fgettext("Waving Plants") .. ";"
233                                         .. dump(core.setting_getbool("enable_waving_plants")) .. "]"
234         else
235                 tab_string = tab_string ..
236                         "tablecolumns[color;text]" ..
237                         "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
238                         "table[8.33,0.7;3.5,4;shaders;" ..
239                                 "#888888," .. fgettext("Bump Mapping") .. "," ..
240                                 "#888888," .. fgettext("Tone Mapping") .. "," ..
241                                 "#888888," .. fgettext("Normal Mapping") .. "," ..
242                                 "#888888," .. fgettext("Parallax Occlusion") .. "," ..
243                                 "#888888," .. fgettext("Waving Water") .. "," ..
244                                 "#888888," .. fgettext("Waving Leaves") .. "," ..
245                                 "#888888," .. fgettext("Waving Plants") .. "," ..
246                                 ";1]"
247         end
248
249         return tab_string
250 end
251
252 --------------------------------------------------------------------------------
253 local function handle_settings_buttons(this, fields, tabname, tabdata)
254
255         if fields["btn_advanced_settings"] ~= nil then
256                 local adv_settings_dlg = create_adv_settings_dlg()
257                 adv_settings_dlg:set_parent(this)
258                 this:hide()
259                 adv_settings_dlg:show()
260                 --mm_texture.update("singleplayer", current_game())
261                 return true
262         end
263         if fields["cb_smooth_lighting"] then
264                 core.setting_set("smooth_lighting", fields["cb_smooth_lighting"])
265                 return true
266         end
267         if fields["cb_particles"] then
268                 core.setting_set("enable_particles", fields["cb_particles"])
269                 return true
270         end
271         if fields["cb_3d_clouds"] then
272                 core.setting_set("enable_3d_clouds", fields["cb_3d_clouds"])
273                 return true
274         end
275         if fields["cb_opaque_water"] then
276                 core.setting_set("opaque_water", fields["cb_opaque_water"])
277                 return true
278         end
279         if fields["cb_connected_glass"] then
280                 core.setting_set("connected_glass", fields["cb_connected_glass"])
281                 return true
282         end
283         if fields["cb_shaders"] then
284                 if (core.setting_get("video_driver") == "direct3d8" or
285                                 core.setting_get("video_driver") == "direct3d9") then
286                         core.setting_set("enable_shaders", "false")
287                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
288                 else
289                         core.setting_set("enable_shaders", fields["cb_shaders"])
290                 end
291                 return true
292         end
293         if fields["cb_bumpmapping"] then
294                 core.setting_set("enable_bumpmapping", fields["cb_bumpmapping"])
295                 return true
296         end
297         if fields["cb_tonemapping"] then
298                 core.setting_set("tone_mapping", fields["cb_tonemapping"])
299                 return true
300         end
301         if fields["cb_generate_normalmaps"] then
302                 core.setting_set("generate_normalmaps", fields["cb_generate_normalmaps"])
303                 return true
304         end
305         if fields["cb_parallax"] then
306                 core.setting_set("enable_parallax_occlusion", fields["cb_parallax"])
307                 return true
308         end
309         if fields["cb_waving_water"] then
310                 core.setting_set("enable_waving_water", fields["cb_waving_water"])
311                 return true
312         end
313         if fields["cb_waving_leaves"] then
314                 core.setting_set("enable_waving_leaves", fields["cb_waving_leaves"])
315         end
316         if fields["cb_waving_plants"] then
317                 core.setting_set("enable_waving_plants", fields["cb_waving_plants"])
318                 return true
319         end
320         if fields["btn_change_keys"] then
321                 core.show_keys_menu()
322                 return true
323         end
324         if fields["cb_touchscreen_target"] then
325                 core.setting_set("touchtarget", fields["cb_touchscreen_target"])
326                 return true
327         end
328         if fields["btn_reset_singleplayer"] then
329                 showconfirm_reset(this)
330                 return true
331         end
332
333         --Note dropdowns have to be handled LAST!
334         local ddhandled = false
335
336         for i = 1, #labels.leaves do
337                 if fields["dd_leaves_style"] == labels.leaves[i] then
338                         core.setting_set("leaves_style", dd_options.leaves[2][i])
339                         ddhandled = true
340                 end
341         end
342         for i = 1, #labels.node_highlighting do
343                 if fields["dd_node_highlighting"] == labels.node_highlighting[i] then
344                         core.setting_set("node_highlighting", dd_options.node_highlighting[2][i])
345                         ddhandled = true
346                 end
347         end
348         if fields["dd_filters"] == labels.filters[1] then
349                 core.setting_set("bilinear_filter", "false")
350                 core.setting_set("trilinear_filter", "false")
351                 ddhandled = true
352         elseif fields["dd_filters"] == labels.filters[2] then
353                 core.setting_set("bilinear_filter", "true")
354                 core.setting_set("trilinear_filter", "false")
355                 ddhandled = true
356         elseif fields["dd_filters"] == labels.filters[3] then
357                 core.setting_set("bilinear_filter", "false")
358                 core.setting_set("trilinear_filter", "true")
359                 ddhandled = true
360         end
361         if fields["dd_mipmap"] == labels.mipmap[1] then
362                 core.setting_set("mip_map", "false")
363                 core.setting_set("anisotropic_filter", "false")
364                 ddhandled = true
365         elseif fields["dd_mipmap"] == labels.mipmap[2] then
366                 core.setting_set("mip_map", "true")
367                 core.setting_set("anisotropic_filter", "false")
368                 ddhandled = true
369         elseif fields["dd_mipmap"] == labels.mipmap[3] then
370                 core.setting_set("mip_map", "true")
371                 core.setting_set("anisotropic_filter", "true")
372                 ddhandled = true
373         end
374         if fields["dd_antialiasing"] then
375                 core.setting_set("fsaa",
376                         antialiasing_fname_to_name(fields["dd_antialiasing"]))
377                 ddhandled = true
378         end
379         if fields["dd_touchthreshold"] then
380                 core.setting_set("touchscreen_threshold", fields["dd_touchthreshold"])
381                 ddhandled = true
382         end
383
384         return ddhandled
385 end
386
387 return {
388         name = "settings",
389         caption = fgettext("Settings"),
390         cbf_formspec = formspec,
391         cbf_button_handler = handle_settings_buttons
392 }