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