58ef4c54070f8cdf4b574fa58df8c8f314cafc63
[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
113    return "0"
114 end
115
116 local function video_driver_fname_to_name(selected_driver)
117         local video_drivers = core.get_video_drivers()
118
119         for i=1, #video_drivers do
120                 if selected_driver == video_drivers[i].friendly_name then
121                         return video_drivers[i].name:lower()
122                 end
123         end
124
125         return ""
126 end
127
128 local function dlg_confirm_reset_formspec(data)
129         local retval =
130                 "size[8,3]" ..
131                 "label[1,1;".. fgettext("Are you sure to reset your singleplayer world?") .. "]"..
132                 "button[1,2;2.6,0.5;dlg_reset_singleplayer_confirm;"..
133                                 fgettext("Yes") .. "]" ..
134                 "button[4,2;2.8,0.5;dlg_reset_singleplayer_cancel;"..
135                                 fgettext("No!!!") .. "]"
136         return retval
137 end
138
139 local function dlg_confirm_reset_btnhandler(this, fields, dialogdata)
140
141         if fields["dlg_reset_singleplayer_confirm"] ~= nil then
142                 local worldlist = core.get_worlds()
143                 local found_singleplayerworld = false
144
145                 for i=1,#worldlist,1 do
146                         if worldlist[i].name == "singleplayerworld" then
147                                 found_singleplayerworld = true
148                                 gamedata.worldindex = i
149                         end
150                 end
151
152                 if found_singleplayerworld then
153                         core.delete_world(gamedata.worldindex)
154                 end
155
156                 core.create_world("singleplayerworld", 1)
157
158                 worldlist = core.get_worlds()
159
160                 found_singleplayerworld = false
161
162                 for i=1,#worldlist,1 do
163                         if worldlist[i].name == "singleplayerworld" then
164                                 found_singleplayerworld = true
165                                 gamedata.worldindex = i
166                         end
167                 end
168         end
169
170         this.parent:show()
171         this:hide()
172         this:delete()
173         return true
174 end
175
176 local function showconfirm_reset(tabview)
177         local new_dlg = dialog_create("reset_spworld",
178                 dlg_confirm_reset_formspec,
179                 dlg_confirm_reset_btnhandler,
180                 nil)
181         new_dlg:set_parent(tabview)
182         tabview:hide()
183         new_dlg:show()
184 end
185
186 local function gui_scale_to_scrollbar()
187         local current_value = tonumber(core.setting_get("gui_scaling"))
188
189         if (current_value == nil) or current_value < 0.25 then
190                 return 0
191         end
192         if current_value <= 1.25 then
193                 return ((current_value - 0.25)/ 1.0) * 700
194         end
195         if current_value <= 6 then
196                 return ((current_value -1.25) * 100) + 700
197         end
198
199         return 1000
200 end
201
202 local function scrollbar_to_gui_scale(value)
203         value = tonumber(value)
204
205         if (value <= 700) then
206                 return ((value / 700) * 1.0) + 0.25
207         end
208         if (value <=1000) then
209                 return ((value - 700) / 100) + 1.25
210         end
211
212         return 1
213 end
214
215 local function formspec(tabview, name, tabdata)
216         local video_drivers = core.get_video_drivers()
217         local current_video_driver = core.setting_get("video_driver"):lower()
218
219         local driver_formspec_string = ""
220         local driver_current_idx = 0
221
222         for i=2, #video_drivers do
223                 driver_formspec_string = driver_formspec_string .. video_drivers[i].friendly_name
224                 if i ~= #video_drivers then
225                         driver_formspec_string = driver_formspec_string .. ","
226                 end
227
228                 if current_video_driver == video_drivers[i].name:lower() then
229                         driver_current_idx = i - 1
230                 end
231         end
232
233         local tab_string =
234                 "box[0,0;3.5,4.0;#999999]" ..
235                 "checkbox[0.25,0;cb_smooth_lighting;".. fgettext("Smooth Lighting")
236                                 .. ";".. dump(core.setting_getbool("smooth_lighting")) .. "]"..
237                 "checkbox[0.25,0.5;cb_particles;".. fgettext("Enable Particles") .. ";"
238                                 .. dump(core.setting_getbool("enable_particles"))       .. "]"..
239                 "checkbox[0.25,1;cb_3d_clouds;".. fgettext("3D Clouds") .. ";"
240                                 .. dump(core.setting_getbool("enable_3d_clouds")) .. "]"..
241                 "checkbox[0.25,1.5;cb_opaque_water;".. fgettext("Opaque Water") .. ";"
242                                 .. dump(core.setting_getbool("opaque_water")) .. "]"..
243                 "checkbox[0.25,2.0;cb_connected_glass;".. fgettext("Connected Glass") .. ";"
244                                 .. dump(core.setting_getbool("connected_glass"))        .. "]"..
245                 "checkbox[0.25,2.5;cb_node_highlighting;".. fgettext("Node Highlighting") .. ";"
246                                 .. dump(core.setting_getbool("enable_node_highlighting")) .. "]"..
247                 "dropdown[0.25,3.2;3.3;dd_leaves_style;" .. leaves_style[1][1] .. ";"
248                                 .. getLeavesStyleSettingIndex() .. "]" ..
249                 "box[3.75,0;3.75,3.45;#999999]" ..
250                 "box[3.75,0;3.75,4.7;#999999]" ..
251                 "label[3.85,0.1;".. fgettext("Texturing:") .. "]"..
252                 "dropdown[3.85,0.55;3.85;dd_filters;" .. filters[1][1] .. ";"
253                                 .. getFilterSettingIndex() .. "]" ..
254                 "dropdown[3.85,1.35;3.85;dd_mipmap;" .. mipmap[1][1] .. ";"
255                                 .. getMipmapSettingIndex() .. "]" ..
256                 "label[3.85,2.15;".. fgettext("Antialiasing:") .. "]"..
257                 "dropdown[3.85,2.6;3.85;dd_antialiasing;" .. antialiasing[1][1] .. ";"
258                 .. getAntialiasingSettingIndex() .. "]" ..
259                 "label[3.85,3.4;".. fgettext("Rendering:") .. "]"..
260                 "dropdown[3.85,3.85;3.85;dd_video_driver;"
261                                 .. driver_formspec_string .. ";" .. driver_current_idx .. "]" ..
262                 "tooltip[dd_video_driver;" ..
263                                 fgettext("Restart minetest for driver change to take effect") .. "]" ..
264                 "box[7.75,0;4,4;#999999]" ..
265                 "checkbox[8,0;cb_shaders;".. fgettext("Shaders") .. ";"
266                                 .. dump(core.setting_getbool("enable_shaders")) .. "]"
267
268         if PLATFORM ~= "Android" then
269                 tab_string = tab_string ..
270                 "button[8,4.75;3.75,0.5;btn_change_keys;".. fgettext("Change keys") .. "]"
271         else
272                 tab_string = tab_string ..
273                 "button[8,4.75;3.75,0.5;btn_reset_singleplayer;".. fgettext("Reset singleplayer world") .. "]"
274         end
275         tab_string = tab_string ..
276                 "box[0,4.25;3.5,1.1;#999999]" ..
277                 "label[0.25,4.25;" .. fgettext("GUI scale factor") .. "]" ..
278                 "scrollbar[0.25,4.75;3,0.4;sb_gui_scaling;horizontal;" ..
279                  gui_scale_to_scrollbar() .. "]" ..
280                 "tooltip[sb_gui_scaling;" ..
281                         fgettext("Scaling factor applied to menu elements: ") ..
282                         dump(core.setting_get("gui_scaling")) .. "]"
283
284         if PLATFORM == "Android" then
285                 tab_string = tab_string ..
286                 "box[3.75,3.55;3.75,1.8;#999999]" ..
287                 "checkbox[3.9,3.45;cb_touchscreen_target;".. fgettext("Touch free target") .. ";"
288                                 .. dump(core.setting_getbool("touchtarget")) .. "]"
289         end
290
291         if core.setting_get("touchscreen_threshold") ~= nil then
292                 tab_string = tab_string ..
293                                 "label[4.3,4.1;" .. fgettext("Touchthreshold (px)") .. "]" ..
294                                 "dropdown[3.85,4.55;3.85;dd_touchthreshold;0,10,20,30,40,50;" ..
295                                 ((tonumber(core.setting_get("touchscreen_threshold"))/10)+1) .. "]"
296         end
297
298         if core.setting_getbool("enable_shaders") then
299                 tab_string = tab_string ..
300                                 "checkbox[8,0.5;cb_bumpmapping;".. fgettext("Bumpmapping") .. ";"
301                                                 .. dump(core.setting_getbool("enable_bumpmapping")) .. "]"..
302                                 "checkbox[8,1.0;cb_generate_normalmaps;".. fgettext("Generate Normalmaps") .. ";"
303                                                 .. dump(core.setting_getbool("generate_normalmaps")) .. "]"..
304                                 "checkbox[8,1.5;cb_parallax;".. fgettext("Parallax Occlusion") .. ";"
305                                                 .. dump(core.setting_getbool("enable_parallax_occlusion")) .. "]"..
306                                 "checkbox[8,2.0;cb_waving_water;".. fgettext("Waving Water") .. ";"
307                                                 .. dump(core.setting_getbool("enable_waving_water")) .. "]"..
308                                 "checkbox[8,2.5;cb_waving_leaves;".. fgettext("Waving Leaves") .. ";"
309                                                 .. dump(core.setting_getbool("enable_waving_leaves")) .. "]"..
310                                 "checkbox[8,3.0;cb_waving_plants;".. fgettext("Waving Plants") .. ";"
311                                                 .. dump(core.setting_getbool("enable_waving_plants")) .. "]"
312         else
313                 tab_string = tab_string ..
314                                 "textlist[8.33,0.7;4,1;;#888888" .. fgettext("Bumpmapping") .. ";0;true]" ..
315                                 "textlist[8.33,1.2;4,1;;#888888" .. fgettext("Generate Normalmaps") .. ";0;true]" ..
316                                 "textlist[8.33,1.7;4,1;;#888888" .. fgettext("Parallax Occlusion") .. ";0;true]" ..
317                                 "textlist[8.33,2.2;4,1;;#888888" .. fgettext("Waving Water") .. ";0;true]" ..
318                                 "textlist[8.33,2.7;4,1;;#888888" .. fgettext("Waving Leaves") .. ";0;true]" ..
319                                 "textlist[8.33,3.2;4,1;;#888888" .. fgettext("Waving Plants") .. ";0;true]"
320                 end
321         return tab_string
322 end
323
324 --------------------------------------------------------------------------------
325 local function handle_settings_buttons(this, fields, tabname, tabdata)
326         if fields["cb_fancy_trees"] then
327                 core.setting_set("new_style_leaves", fields["cb_fancy_trees"])
328                 return true
329         end
330         if fields["cb_smooth_lighting"] then
331                 core.setting_set("smooth_lighting", fields["cb_smooth_lighting"])
332                 return true
333         end
334         if fields["cb_3d_clouds"] then
335                 core.setting_set("enable_3d_clouds", fields["cb_3d_clouds"])
336                 return true
337         end
338         if fields["cb_opaque_water"] then
339                 core.setting_set("opaque_water", fields["cb_opaque_water"])
340                 return true
341         end
342         if fields["cb_shaders"] then
343                 if (core.setting_get("video_driver") == "direct3d8" or core.setting_get("video_driver") == "direct3d9") then
344                         core.setting_set("enable_shaders", "false")
345                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
346                 else
347                         core.setting_set("enable_shaders", fields["cb_shaders"])
348                 end
349                 return true
350         end
351         if fields["cb_connected_glass"] then
352                 core.setting_set("connected_glass", fields["cb_connected_glass"])
353                 return true
354         end
355         if fields["cb_node_highlighting"] then
356                 core.setting_set("enable_node_highlighting", fields["cb_node_highlighting"])
357                 return true
358         end
359         if fields["cb_particles"] then
360                 core.setting_set("enable_particles", fields["cb_particles"])
361                 return true
362         end
363         if fields["cb_bumpmapping"] then
364                 core.setting_set("enable_bumpmapping", fields["cb_bumpmapping"])
365         end
366         if fields["cb_generate_normalmaps"] then
367                 core.setting_set("generate_normalmaps", fields["cb_generate_normalmaps"])
368         end
369         if fields["cb_parallax"] then
370                 core.setting_set("enable_parallax_occlusion", fields["cb_parallax"])
371                 return true
372         end
373         if fields["cb_waving_water"] then
374                 core.setting_set("enable_waving_water", fields["cb_waving_water"])
375                 return true
376         end
377         if fields["cb_waving_leaves"] then
378                 core.setting_set("enable_waving_leaves", fields["cb_waving_leaves"])
379         end
380         if fields["cb_waving_plants"] then
381                 core.setting_set("enable_waving_plants", fields["cb_waving_plants"])
382                 return true
383         end
384         if fields["btn_change_keys"] ~= nil then
385                 core.show_keys_menu()
386                 return true
387         end
388
389         if fields["sb_gui_scaling"] then
390                 local event = core.explode_scrollbar_event(fields["sb_gui_scaling"])
391
392                 if event.type == "CHG" then
393                         local tosave = string.format("%.2f",scrollbar_to_gui_scale(event.value))
394                         core.setting_set("gui_scaling",tosave)
395                         return true
396                 end
397         end
398         if fields["cb_touchscreen_target"] then
399                 core.setting_set("touchtarget", fields["cb_touchscreen_target"])
400                 return true
401         end
402         if fields["btn_reset_singleplayer"] then
403                 showconfirm_reset(this)
404                 return true
405         end
406
407         --Note dropdowns have to be handled LAST!
408         local ddhandled = false
409
410         if fields["dd_touchthreshold"] then
411                 core.setting_set("touchscreen_threshold",fields["dd_touchthreshold"])
412                 ddhandled = true
413         end
414         if fields["dd_leaves_style"] == leaves_style_labels[3] then
415                 core.setting_set("leaves_style", leaves_style[2][3])
416                 ddhandled = true
417         end
418         if fields["dd_leaves_style"] == leaves_style_labels[2] then
419                 core.setting_set("leaves_style", leaves_style[2][2])
420                 ddhandled = true
421         end
422         if fields["dd_leaves_style"] == leaves_style_labels[1] then
423                 core.setting_set("leaves_style", leaves_style[2][1])
424                 ddhandled = true
425         end
426         if fields["dd_video_driver"] then
427                 core.setting_set("video_driver",
428                         video_driver_fname_to_name(fields["dd_video_driver"]))
429                 ddhandled = true
430         end
431         if fields["dd_filters"] == dd_filter_labels[1] then
432                 core.setting_set("bilinear_filter", "false")
433                 core.setting_set("trilinear_filter", "false")
434                 ddhandled = true
435         end
436         if fields["dd_filters"] == dd_filter_labels[2] then
437                 core.setting_set("bilinear_filter", "true")
438                 core.setting_set("trilinear_filter", "false")
439                 ddhandled = true
440         end
441         if fields["dd_filters"] == dd_filter_labels[3] then
442                 core.setting_set("bilinear_filter", "false")
443                 core.setting_set("trilinear_filter", "true")
444                 ddhandled = true
445         end
446         if fields["dd_mipmap"] == dd_mipmap_labels[1] then
447                 core.setting_set("mip_map", "false")
448                 core.setting_set("anisotropic_filter", "false")
449                 ddhandled = true
450         end
451         if fields["dd_mipmap"] == dd_mipmap_labels[2] then
452                 core.setting_set("mip_map", "true")
453                 core.setting_set("anisotropic_filter", "false")
454                 ddhandled = true
455         end
456         if fields["dd_mipmap"] == dd_mipmap_labels[3] then
457                 core.setting_set("mip_map", "true")
458                 core.setting_set("anisotropic_filter", "true")
459                 ddhandled = true
460         end
461         core.setting_set("fsaa",
462                 antialiasing_fname_to_name(fields["dd_antialiasing"]))
463
464         return ddhandled
465 end
466
467 tab_settings = {
468         name = "settings",
469         caption = fgettext("Settings"),
470         cbf_formspec = formspec,
471         cbf_button_handler = handle_settings_buttons
472 }