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