Fix FSAA dropdown option reset after changing another dropdown option
[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 leaves_style_labels = {
21         fgettext("Opaque Leaves"),
22         fgettext("Simple Leaves"),
23         fgettext("Fancy Leaves")
24 }
25
26 local leaves_style = {
27         {leaves_style_labels[1]..","..leaves_style_labels[2]..","..leaves_style_labels[3]},
28         {"opaque", "simple", "fancy"},
29 }
30
31 local dd_filter_labels = {
32         fgettext("No Filter"),
33         fgettext("Bilinear Filter"),
34         fgettext("Trilinear Filter")
35 }
36
37 local filters = {
38         {dd_filter_labels[1]..","..dd_filter_labels[2]..","..dd_filter_labels[3]},
39         {"", "bilinear_filter", "trilinear_filter"},
40 }
41
42 local dd_mipmap_labels = {
43         fgettext("No Mipmap"),
44         fgettext("Mipmap"),
45         fgettext("Mipmap + Aniso. Filter")
46 }
47
48 local mipmap = {
49         {dd_mipmap_labels[1]..","..dd_mipmap_labels[2]..","..dd_mipmap_labels[3]},
50         {"", "mip_map", "anisotropic_filter"},
51 }
52
53 local function getLeavesStyleSettingIndex()
54         local style = core.setting_get("leaves_style")
55         if (style == leaves_style[2][3]) then
56                 return 3
57         elseif (style == leaves_style[2][2]) then
58                 return 2
59         end
60         return 1
61 end
62
63 local dd_antialiasing_labels = {
64         fgettext("None"),
65         fgettext("2x"),
66         fgettext("4x"),
67         fgettext("8x"),
68 }
69
70 local antialiasing = {
71         {dd_antialiasing_labels[1]..","..dd_antialiasing_labels[2]..","..
72                 dd_antialiasing_labels[3]..","..dd_antialiasing_labels[4]},
73         {"0", "2", "4", "8"}
74 }
75
76 local function getFilterSettingIndex()
77         if (core.setting_get(filters[2][3]) == "true") then
78                 return 3
79         end
80         if (core.setting_get(filters[2][3]) == "false" and core.setting_get(filters[2][2]) == "true") then
81                 return 2
82         end
83         return 1
84 end
85
86 local function getMipmapSettingIndex()
87         if (core.setting_get(mipmap[2][3]) == "true") then
88                 return 3
89         end
90         if (core.setting_get(mipmap[2][3]) == "false" and core.setting_get(mipmap[2][2]) == "true") then
91                 return 2
92         end
93         return 1
94 end
95
96 local function getAntialiasingSettingIndex()
97         local antialiasing_setting = core.setting_get("fsaa")
98         for i = 1, #(antialiasing[2]) do
99                 if antialiasing_setting == antialiasing[2][i] then
100                         return i
101                 end
102         end
103         return 1
104 end
105
106 local function antialiasing_fname_to_name(fname)
107         for i = 1, #(dd_antialiasing_labels) do
108                 if fname == dd_antialiasing_labels[i] then
109                         return antialiasing[2][i]
110                 end
111         end
112         return 0
113 end
114
115 local function dlg_confirm_reset_formspec(data)
116         local retval =
117                 "size[8,3]" ..
118                 "label[1,1;".. fgettext("Are you sure to reset your singleplayer world?") .. "]"..
119                 "button[1,2;2.6,0.5;dlg_reset_singleplayer_confirm;"..
120                                 fgettext("Yes") .. "]" ..
121                 "button[4,2;2.8,0.5;dlg_reset_singleplayer_cancel;"..
122                                 fgettext("No!!!") .. "]"
123         return retval
124 end
125
126 local function dlg_confirm_reset_btnhandler(this, fields, dialogdata)
127
128         if fields["dlg_reset_singleplayer_confirm"] ~= nil then
129                 local worldlist = core.get_worlds()
130                 local found_singleplayerworld = false
131
132                 for i=1,#worldlist,1 do
133                         if worldlist[i].name == "singleplayerworld" then
134                                 found_singleplayerworld = true
135                                 gamedata.worldindex = i
136                         end
137                 end
138
139                 if found_singleplayerworld then
140                         core.delete_world(gamedata.worldindex)
141                 end
142
143                 core.create_world("singleplayerworld", 1)
144
145                 worldlist = core.get_worlds()
146
147                 found_singleplayerworld = false
148
149                 for i=1,#worldlist,1 do
150                         if worldlist[i].name == "singleplayerworld" then
151                                 found_singleplayerworld = true
152                                 gamedata.worldindex = i
153                         end
154                 end
155         end
156
157         this.parent:show()
158         this:hide()
159         this:delete()
160         return true
161 end
162
163 local function showconfirm_reset(tabview)
164         local new_dlg = dialog_create("reset_spworld",
165                 dlg_confirm_reset_formspec,
166                 dlg_confirm_reset_btnhandler,
167                 nil)
168         new_dlg:set_parent(tabview)
169         tabview:hide()
170         new_dlg:show()
171 end
172
173 local function gui_scale_to_scrollbar()
174         local current_value = tonumber(core.setting_get("gui_scaling"))
175
176         if (current_value == nil) or current_value < 0.25 then
177                 return 0
178         end
179         if current_value <= 1.25 then
180                 return ((current_value - 0.25)/ 1.0) * 700
181         end
182         if current_value <= 6 then
183                 return ((current_value -1.25) * 100) + 700
184         end
185
186         return 1000
187 end
188
189 local function scrollbar_to_gui_scale(value)
190         value = tonumber(value)
191
192         if (value <= 700) then
193                 return ((value / 700) * 1.0) + 0.25
194         end
195         if (value <=1000) then
196                 return ((value - 700) / 100) + 1.25
197         end
198
199         return 1
200 end
201
202 local function formspec(tabview, name, tabdata)
203         local tab_string =
204                 "box[0,0;3.5,4.0;#999999]" ..
205                 "checkbox[0.25,0;cb_smooth_lighting;".. fgettext("Smooth Lighting")
206                                 .. ";".. dump(core.setting_getbool("smooth_lighting")) .. "]"..
207                 "checkbox[0.25,0.5;cb_particles;".. fgettext("Enable Particles") .. ";"
208                                 .. dump(core.setting_getbool("enable_particles"))       .. "]"..
209                 "checkbox[0.25,1;cb_3d_clouds;".. fgettext("3D Clouds") .. ";"
210                                 .. dump(core.setting_getbool("enable_3d_clouds")) .. "]"..
211                 "checkbox[0.25,1.5;cb_opaque_water;".. fgettext("Opaque Water") .. ";"
212                                 .. dump(core.setting_getbool("opaque_water")) .. "]"..
213                 "checkbox[0.25,2.0;cb_connected_glass;".. fgettext("Connected Glass") .. ";"
214                                 .. dump(core.setting_getbool("connected_glass"))        .. "]"..
215                 "checkbox[0.25,2.5;cb_node_highlighting;".. fgettext("Node Highlighting") .. ";"
216                                 .. dump(core.setting_getbool("enable_node_highlighting")) .. "]"..
217                 "dropdown[0.25,3.2;3.3;dd_leaves_style;" .. leaves_style[1][1] .. ";"
218                                 .. getLeavesStyleSettingIndex() .. "]" ..
219                 "box[3.75,0;3.75,3.45;#999999]" ..
220                 "label[3.85,0.1;".. fgettext("Texturing:") .. "]"..
221                 "dropdown[3.85,0.55;3.85;dd_filters;" .. filters[1][1] .. ";"
222                                 .. getFilterSettingIndex() .. "]" ..
223                 "dropdown[3.85,1.35;3.85;dd_mipmap;" .. mipmap[1][1] .. ";"
224                                 .. getMipmapSettingIndex() .. "]" ..
225                 "label[3.85,2.15;".. fgettext("Antialiasing:") .. "]"..
226                 "dropdown[3.85,2.6;3.85;dd_antialiasing;" .. antialiasing[1][1] .. ";"
227                 .. getAntialiasingSettingIndex() .. "]" ..
228                 "box[7.75,0;4,4;#999999]" ..
229                 "checkbox[8,0;cb_shaders;".. fgettext("Shaders") .. ";"
230                                 .. dump(core.setting_getbool("enable_shaders")) .. "]"
231
232         if PLATFORM ~= "Android" then
233                 tab_string = tab_string ..
234                 "button[8,4.75;3.75,0.5;btn_change_keys;".. fgettext("Change keys") .. "]"
235         else
236                 tab_string = tab_string ..
237                 "button[8,4.75;3.75,0.5;btn_reset_singleplayer;".. fgettext("Reset singleplayer world") .. "]"
238         end
239         tab_string = tab_string ..
240                 "box[0,4.25;3.5,1.1;#999999]" ..
241                 "label[0.25,4.25;" .. fgettext("GUI scale factor") .. "]" ..
242                 "scrollbar[0.25,4.75;3,0.4;sb_gui_scaling;horizontal;" ..
243                  gui_scale_to_scrollbar() .. "]" ..
244                 "tooltip[sb_gui_scaling;" ..
245                         fgettext("Scaling factor applied to menu elements: ") ..
246                         dump(core.setting_get("gui_scaling")) .. "]"
247
248         if PLATFORM == "Android" then
249                 tab_string = tab_string ..
250                 "box[3.75,3.55;3.75,1.8;#999999]" ..
251                 "checkbox[3.9,3.45;cb_touchscreen_target;".. fgettext("Touch free target") .. ";"
252                                 .. dump(core.setting_getbool("touchtarget")) .. "]"
253         end
254
255         if core.setting_get("touchscreen_threshold") ~= nil then
256                 tab_string = tab_string ..
257                                 "label[4.3,4.1;" .. fgettext("Touchthreshold (px)") .. "]" ..
258                                 "dropdown[3.85,4.55;3.85;dd_touchthreshold;0,10,20,30,40,50;" ..
259                                 ((tonumber(core.setting_get("touchscreen_threshold"))/10)+1) .. "]"
260         end
261
262         if core.setting_getbool("enable_shaders") then
263                 tab_string = tab_string ..
264                                 "checkbox[8,0.5;cb_bumpmapping;".. fgettext("Bumpmapping") .. ";"
265                                                 .. dump(core.setting_getbool("enable_bumpmapping")) .. "]"..
266                                 "checkbox[8,1.0;cb_generate_normalmaps;".. fgettext("Generate Normalmaps") .. ";"
267                                                 .. dump(core.setting_getbool("generate_normalmaps")) .. "]"..
268                                 "checkbox[8,1.5;cb_parallax;".. fgettext("Parallax Occlusion") .. ";"
269                                                 .. dump(core.setting_getbool("enable_parallax_occlusion")) .. "]"..
270                                 "checkbox[8,2.0;cb_waving_water;".. fgettext("Waving Water") .. ";"
271                                                 .. dump(core.setting_getbool("enable_waving_water")) .. "]"..
272                                 "checkbox[8,2.5;cb_waving_leaves;".. fgettext("Waving Leaves") .. ";"
273                                                 .. dump(core.setting_getbool("enable_waving_leaves")) .. "]"..
274                                 "checkbox[8,3.0;cb_waving_plants;".. fgettext("Waving Plants") .. ";"
275                                                 .. dump(core.setting_getbool("enable_waving_plants")) .. "]"
276         else
277                 tab_string = tab_string ..
278                                 "textlist[8.33,0.7;4,1;;#888888" .. fgettext("Bumpmapping") .. ";0;true]" ..
279                                 "textlist[8.33,1.2;4,1;;#888888" .. fgettext("Generate Normalmaps") .. ";0;true]" ..
280                                 "textlist[8.33,1.7;4,1;;#888888" .. fgettext("Parallax Occlusion") .. ";0;true]" ..
281                                 "textlist[8.33,2.2;4,1;;#888888" .. fgettext("Waving Water") .. ";0;true]" ..
282                                 "textlist[8.33,2.7;4,1;;#888888" .. fgettext("Waving Leaves") .. ";0;true]" ..
283                                 "textlist[8.33,3.2;4,1;;#888888" .. fgettext("Waving Plants") .. ";0;true]"
284                 end
285         return tab_string
286 end
287
288 --------------------------------------------------------------------------------
289 local function handle_settings_buttons(this, fields, tabname, tabdata)
290         if fields["cb_fancy_trees"] then
291                 core.setting_set("new_style_leaves", fields["cb_fancy_trees"])
292                 return true
293         end
294         if fields["cb_smooth_lighting"] then
295                 core.setting_set("smooth_lighting", fields["cb_smooth_lighting"])
296                 return true
297         end
298         if fields["cb_3d_clouds"] then
299                 core.setting_set("enable_3d_clouds", fields["cb_3d_clouds"])
300                 return true
301         end
302         if fields["cb_opaque_water"] then
303                 core.setting_set("opaque_water", fields["cb_opaque_water"])
304                 return true
305         end
306         if fields["cb_shaders"] then
307                 if (core.setting_get("video_driver") == "direct3d8" or core.setting_get("video_driver") == "direct3d9") then
308                         core.setting_set("enable_shaders", "false")
309                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
310                 else
311                         core.setting_set("enable_shaders", fields["cb_shaders"])
312                 end
313                 return true
314         end
315         if fields["cb_connected_glass"] then
316                 core.setting_set("connected_glass", fields["cb_connected_glass"])
317                 return true
318         end
319         if fields["cb_node_highlighting"] then
320                 core.setting_set("enable_node_highlighting", fields["cb_node_highlighting"])
321                 return true
322         end
323         if fields["cb_particles"] then
324                 core.setting_set("enable_particles", fields["cb_particles"])
325                 return true
326         end
327         if fields["cb_bumpmapping"] then
328                 core.setting_set("enable_bumpmapping", fields["cb_bumpmapping"])
329         end
330         if fields["cb_generate_normalmaps"] then
331                 core.setting_set("generate_normalmaps", fields["cb_generate_normalmaps"])
332         end
333         if fields["cb_parallax"] then
334                 core.setting_set("enable_parallax_occlusion", fields["cb_parallax"])
335                 return true
336         end
337         if fields["cb_waving_water"] then
338                 core.setting_set("enable_waving_water", fields["cb_waving_water"])
339                 return true
340         end
341         if fields["cb_waving_leaves"] then
342                 core.setting_set("enable_waving_leaves", fields["cb_waving_leaves"])
343         end
344         if fields["cb_waving_plants"] then
345                 core.setting_set("enable_waving_plants", fields["cb_waving_plants"])
346                 return true
347         end
348         if fields["btn_change_keys"] ~= nil then
349                 core.show_keys_menu()
350                 return true
351         end
352
353         if fields["sb_gui_scaling"] then
354                 local event = core.explode_scrollbar_event(fields["sb_gui_scaling"])
355
356                 if event.type == "CHG" then
357                         local tosave = string.format("%.2f",scrollbar_to_gui_scale(event.value))
358                         core.setting_set("gui_scaling",tosave)
359                         return true
360                 end
361         end
362         if fields["cb_touchscreen_target"] then
363                 core.setting_set("touchtarget", fields["cb_touchscreen_target"])
364                 return true
365         end
366         if fields["btn_reset_singleplayer"] then
367                 showconfirm_reset(this)
368                 return true
369         end
370
371         --Note dropdowns have to be handled LAST!
372         local ddhandled = false
373
374         if fields["dd_touchthreshold"] then
375                 core.setting_set("touchscreen_threshold",fields["dd_touchthreshold"])
376                 ddhandled = true
377         end
378         if fields["dd_leaves_style"] == leaves_style_labels[3] then
379                 core.setting_set("leaves_style", leaves_style[2][3])
380                 ddhandled = true
381         end
382         if fields["dd_leaves_style"] == leaves_style_labels[2] then
383                 core.setting_set("leaves_style", leaves_style[2][2])
384                 ddhandled = true
385         end
386         if fields["dd_leaves_style"] == leaves_style_labels[1] then
387                 core.setting_set("leaves_style", leaves_style[2][1])
388                 ddhandled = true
389         end
390         if fields["dd_filters"] == dd_filter_labels[1] then
391                 core.setting_set("bilinear_filter", "false")
392                 core.setting_set("trilinear_filter", "false")
393                 ddhandled = true
394         end
395         if fields["dd_filters"] == dd_filter_labels[2] then
396                 core.setting_set("bilinear_filter", "true")
397                 core.setting_set("trilinear_filter", "false")
398                 ddhandled = true
399         end
400         if fields["dd_filters"] == dd_filter_labels[3] then
401                 core.setting_set("bilinear_filter", "false")
402                 core.setting_set("trilinear_filter", "true")
403                 ddhandled = true
404         end
405         if fields["dd_mipmap"] == dd_mipmap_labels[1] then
406                 core.setting_set("mip_map", "false")
407                 core.setting_set("anisotropic_filter", "false")
408                 ddhandled = true
409         end
410         if fields["dd_mipmap"] == dd_mipmap_labels[2] then
411                 core.setting_set("mip_map", "true")
412                 core.setting_set("anisotropic_filter", "false")
413                 ddhandled = true
414         end
415         if fields["dd_mipmap"] == dd_mipmap_labels[3] then
416                 core.setting_set("mip_map", "true")
417                 core.setting_set("anisotropic_filter", "true")
418                 ddhandled = true
419         end
420         if fields["dd_antialiasing"] then
421                 core.setting_set("fsaa",
422                         antialiasing_fname_to_name(fields["dd_antialiasing"]))
423                 ddhandled = true
424         end
425
426         return ddhandled
427 end
428
429 tab_settings = {
430         name = "settings",
431         caption = fgettext("Settings"),
432         cbf_formspec = formspec,
433         cbf_button_handler = handle_settings_buttons
434 }