Remove vertlabels from main menu and relayout a bit
[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 function dlg_confirm_reset_formspec(data)
21         local retval =
22                 "size[8,3]" ..
23                 "label[1,1;".. fgettext("Are you sure to reset your singleplayer world?") .. "]"..
24                 "button[1,2;2.6,0.5;dlg_reset_singleplayer_confirm;"..
25                                 fgettext("Yes") .. "]" ..
26                 "button[4,2;2.8,0.5;dlg_reset_singleplayer_cancel;"..
27                                 fgettext("No!!!") .. "]"
28         return retval
29 end
30
31 local function dlg_confirm_reset_btnhandler(this, fields, dialogdata)
32
33         if fields["dlg_reset_singleplayer_confirm"] ~= nil then
34                 local worldlist = core.get_worlds()
35                 local found_singleplayerworld = false
36
37                 for i=1,#worldlist,1 do
38                         if worldlist[i].name == "singleplayerworld" then
39                                 found_singleplayerworld = true
40                                 gamedata.worldindex = i
41                         end
42                 end
43
44                 if found_singleplayerworld then
45                         core.delete_world(gamedata.worldindex)
46                 end
47
48                 core.create_world("singleplayerworld", 1)
49
50                 worldlist = core.get_worlds()
51
52                 found_singleplayerworld = false
53
54                 for i=1,#worldlist,1 do
55                         if worldlist[i].name == "singleplayerworld" then
56                                 found_singleplayerworld = true
57                                 gamedata.worldindex = i
58                         end
59                 end
60         end
61
62         this.parent:show()
63         this:hide()
64         this:delete()
65         return true
66 end
67
68 local function showconfirm_reset(tabview)
69         local new_dlg = dialog_create("reset_spworld",
70                 dlg_confirm_reset_formspec,
71                 dlg_confirm_reset_btnhandler,
72                 nil)
73         new_dlg:set_parent(tabview)
74         tabview:hide()
75         new_dlg:show()
76 end
77
78 local function gui_scale_to_scrollbar()
79
80         local current_value = tonumber(core.setting_get("gui_scaling"))
81
82         if (current_value == nil) or current_value < 0.25 then
83                 return 0
84         end
85
86         if current_value <= 1.25 then
87                 return ((current_value - 0.25)/ 1.0) * 700
88         end
89
90         if current_value <= 6 then
91                 return ((current_value -1.25) * 100) + 700
92         end
93
94         return 1000
95 end
96
97 local function scrollbar_to_gui_scale(value)
98
99         value = tonumber(value)
100
101         if (value <= 700) then
102                 return ((value / 700) * 1.0) + 0.25
103         end
104
105         if (value <=1000) then
106                 return ((value - 700) / 100) + 1.25
107         end
108
109         return 1
110 end
111
112 local function formspec(tabview, name, tabdata)
113         local video_drivers = core.get_video_drivers()
114         
115         local video_driver_string = ""
116         local current_video_driver_idx = 0
117         local current_video_driver = core.setting_get("video_driver")
118         for i=1, #video_drivers, 1 do
119         
120                 if i ~= 1 then
121                         video_driver_string = video_driver_string .. ","
122                 end
123                 video_driver_string = video_driver_string .. video_drivers[i]
124                 
125                 local video_driver = string.gsub(video_drivers[i], " ", "")
126                 if current_video_driver:lower() == video_driver:lower() then
127                         current_video_driver_idx = i
128                 end
129         end
130         
131         
132         local tab_string =
133                 "box[0,0;3.5,4;#999999]" ..
134                 "checkbox[0.25,0;cb_smooth_lighting;".. fgettext("Smooth Lighting")
135                                 .. ";".. dump(core.setting_getbool("smooth_lighting")) .. "]"..
136                 "checkbox[0.25,0.5;cb_particles;".. fgettext("Enable Particles") .. ";"
137                                 .. dump(core.setting_getbool("enable_particles"))       .. "]"..
138                 "checkbox[0.25,1;cb_3d_clouds;".. fgettext("3D Clouds") .. ";"
139                                 .. dump(core.setting_getbool("enable_3d_clouds")) .. "]"..
140                 "checkbox[0.25,1.5;cb_fancy_trees;".. fgettext("Fancy Trees") .. ";"
141                                 .. dump(core.setting_getbool("new_style_leaves")) .. "]"..
142                 "checkbox[0.25,2.0;cb_opaque_water;".. fgettext("Opaque Water") .. ";"
143                                 .. dump(core.setting_getbool("opaque_water")) .. "]"..
144                 "checkbox[0.25,2.5;cb_connected_glass;".. fgettext("Connected Glass") .. ";"
145                                 .. dump(core.setting_getbool("connected_glass"))        .. "]"..
146                 "dropdown[0.25,3.25;3.25;dd_video_driver;"
147                         .. video_driver_string .. ";" .. current_video_driver_idx .. "]" ..
148                 "tooltip[dd_video_driver;" ..
149                         fgettext("Restart minetest for driver change to take effect") .. "]" ..
150                 "box[3.75,0;3.75,2.5;#999999]" ..
151                 "checkbox[4,0;cb_mipmapping;".. fgettext("Mip-Mapping") .. ";"
152                                 .. dump(core.setting_getbool("mip_map")) .. "]"..
153                 "checkbox[4,0.5;cb_anisotrophic;".. fgettext("Anisotropic Filtering") .. ";"
154                                 .. dump(core.setting_getbool("anisotropic_filter")) .. "]"..
155                 "checkbox[4,1.0;cb_bilinear;".. fgettext("Bi-Linear Filtering") .. ";"
156                                 .. dump(core.setting_getbool("bilinear_filter")) .. "]"..
157                 "checkbox[4,1.5;cb_trilinear;".. fgettext("Tri-Linear Filtering") .. ";"
158                                 .. dump(core.setting_getbool("trilinear_filter")) .. "]"..
159                 "box[7.75,0;4,4;#999999]" ..
160                 "checkbox[8,0;cb_shaders;".. fgettext("Shaders") .. ";"
161                                 .. dump(core.setting_getbool("enable_shaders")) .. "]"
162         if PLATFORM ~= "Android" then
163                 tab_string = tab_string ..
164                 "button[8,4.75;3.75,0.5;btn_change_keys;".. fgettext("Change keys") .. "]"
165         else
166                 tab_string = tab_string ..
167                 "button[8,4.75;3.75,0.5;btn_reset_singleplayer;".. fgettext("Reset singleplayer world") .. "]"
168         end
169         tab_string = tab_string ..
170                 "box[0,4.25;3.5,1.25;#999999]" ..
171                 "label[0.25,4.25;" .. fgettext("GUI scale factor") .. "]" ..
172                 "scrollbar[0.25,4.75;3,0.4;sb_gui_scaling;horizontal;" ..
173                  gui_scale_to_scrollbar() .. "]" ..
174                 "tooltip[sb_gui_scaling;" ..
175                         fgettext("Scaling factor applied to menu elements: ") ..
176                         dump(core.setting_get("gui_scaling")) .. "]"
177
178         if PLATFORM == "Android" then
179                 tab_string = tab_string ..
180                 "box[4.25,2.75;3.25,2.15;#999999]" ..
181                 "checkbox[4.5,2.75;cb_touchscreen_target;".. fgettext("Touch free target") .. ";"
182                                 .. dump(core.setting_getbool("touchtarget")) .. "]"
183         end
184
185         if core.setting_get("touchscreen_threshold") ~= nil then
186                 tab_string = tab_string ..
187                                 "label[4.5,3.5;" .. fgettext("Touchthreshold (px)") .. "]" ..
188                                 "dropdown[4.5,4;3;dd_touchthreshold;0,10,20,30,40,50;" ..
189                                 ((tonumber(core.setting_get("touchscreen_threshold"))/10)+1) .. "]"
190         end
191
192         if core.setting_getbool("enable_shaders") then
193                 tab_string = tab_string ..
194                                 "checkbox[8,0.5;cb_bumpmapping;".. fgettext("Bumpmapping") .. ";"
195                                                 .. dump(core.setting_getbool("enable_bumpmapping")) .. "]"..
196                                 "checkbox[8,1.0;cb_generate_normalmaps;".. fgettext("Generate Normalmaps") .. ";"
197                                                 .. dump(core.setting_getbool("generate_normalmaps")) .. "]"..
198                                 "checkbox[8,1.5;cb_parallax;".. fgettext("Parallax Occlusion") .. ";"
199                                                 .. dump(core.setting_getbool("enable_parallax_occlusion")) .. "]"..
200                                 "checkbox[8,2.0;cb_waving_water;".. fgettext("Waving Water") .. ";"
201                                                 .. dump(core.setting_getbool("enable_waving_water")) .. "]"..
202                                 "checkbox[8,2.5;cb_waving_leaves;".. fgettext("Waving Leaves") .. ";"
203                                                 .. dump(core.setting_getbool("enable_waving_leaves")) .. "]"..
204                                 "checkbox[8,3.0;cb_waving_plants;".. fgettext("Waving Plants") .. ";"
205                                                 .. dump(core.setting_getbool("enable_waving_plants")) .. "]"
206         else
207                 tab_string = tab_string ..
208                                 "textlist[8.33,0.7;4,1;;#888888" .. fgettext("Bumpmapping") .. ";0;true]" ..
209                                 "textlist[8.33,1.2;4,1;;#888888" .. fgettext("Generate Normalmaps") .. ";0;true]" ..
210                                 "textlist[8.33,1.7;4,1;;#888888" .. fgettext("Parallax Occlusion") .. ";0;true]" ..
211                                 "textlist[8.33,2.2;4,1;;#888888" .. fgettext("Waving Water") .. ";0;true]" ..
212                                 "textlist[8.33,2.7;4,1;;#888888" .. fgettext("Waving Leaves") .. ";0;true]" ..
213                                 "textlist[8.33,3.2;4,1;;#888888" .. fgettext("Waving Plants") .. ";0;true]"
214                 end
215         return tab_string
216 end
217
218 --------------------------------------------------------------------------------
219 local function handle_settings_buttons(this, fields, tabname, tabdata)
220         if fields["cb_fancy_trees"] then
221                 core.setting_set("new_style_leaves", fields["cb_fancy_trees"])
222                 return true
223         end
224         if fields["cb_smooth_lighting"] then
225                 core.setting_set("smooth_lighting", fields["cb_smooth_lighting"])
226                 return true
227         end
228         if fields["cb_3d_clouds"] then
229                 core.setting_set("enable_3d_clouds", fields["cb_3d_clouds"])
230                 return true
231         end
232         if fields["cb_opaque_water"] then
233                 core.setting_set("opaque_water", fields["cb_opaque_water"])
234                 return true
235         end
236         if fields["cb_mipmapping"] then
237                 core.setting_set("mip_map", fields["cb_mipmapping"])
238                 return true
239         end
240         if fields["cb_anisotrophic"] then
241                 core.setting_set("anisotropic_filter", fields["cb_anisotrophic"])
242                 return true
243         end
244         if fields["cb_bilinear"] then
245                 core.setting_set("bilinear_filter", fields["cb_bilinear"])
246                 return true
247         end
248         if fields["cb_trilinear"] then
249                 core.setting_set("trilinear_filter", fields["cb_trilinear"])
250                 return true
251         end
252         if fields["cb_shaders"] then
253                 if (core.setting_get("video_driver") == "direct3d8" or core.setting_get("video_driver") == "direct3d9") then
254                         core.setting_set("enable_shaders", "false")
255                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
256                 else
257                         core.setting_set("enable_shaders", fields["cb_shaders"])
258                 end
259                 return true
260         end
261         if fields["cb_connected_glass"] then
262                 core.setting_set("connected_glass", fields["cb_connected_glass"])
263                 return true
264         end
265         if fields["cb_particles"] then
266                 core.setting_set("enable_particles", fields["cb_particles"])
267                 return true
268         end
269         if fields["cb_bumpmapping"] then
270                 core.setting_set("enable_bumpmapping", fields["cb_bumpmapping"])
271         end
272         if fields["cb_generate_normalmaps"] then
273                 core.setting_set("generate_normalmaps", fields["cb_generate_normalmaps"])
274         end
275         if fields["cb_parallax"] then
276                 core.setting_set("enable_parallax_occlusion", fields["cb_parallax"])
277                 return true
278         end
279         if fields["cb_waving_water"] then
280                 core.setting_set("enable_waving_water", fields["cb_waving_water"])
281                 return true
282         end
283         if fields["cb_waving_leaves"] then
284                 core.setting_set("enable_waving_leaves", fields["cb_waving_leaves"])
285         end
286         if fields["cb_waving_plants"] then
287                 core.setting_set("enable_waving_plants", fields["cb_waving_plants"])
288                 return true
289         end
290         if fields["btn_change_keys"] ~= nil then
291                 core.show_keys_menu()
292                 return true
293         end
294
295         if fields["sb_gui_scaling"] then
296                 local event = core.explode_scrollbar_event(fields["sb_gui_scaling"])
297
298                 if event.type == "CHG" then
299                         local tosave = string.format("%.2f",scrollbar_to_gui_scale(event.value))
300                         core.setting_set("gui_scaling",tosave)
301                         return true
302                 end
303         end
304         if fields["cb_touchscreen_target"] then
305                 core.setting_set("touchtarget", fields["cb_touchscreen_target"])
306                 return true
307         end
308         if fields["btn_reset_singleplayer"] then
309                 showconfirm_reset(this)
310                 return true
311         end
312
313         --Note dropdowns have to be handled LAST!
314         local ddhandled = false
315         if fields["dd_touchthreshold"] then
316                 core.setting_set("touchscreen_threshold",fields["dd_touchthreshold"])
317                 ddhandled = true
318         end
319         if fields["dd_video_driver"] then
320                 local video_driver = string.gsub(fields["dd_video_driver"], " ", "")
321                 core.setting_set("video_driver",string.lower(video_driver))
322                 ddhandled = true
323         end
324         
325         return ddhandled
326 end
327
328 tab_settings = {
329         name = "settings",
330         caption = fgettext("Settings"),
331         cbf_formspec = formspec,
332         cbf_button_handler = handle_settings_buttons
333         }