]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/tab_settings.lua
880978800b24e4d4468607f94b93f6985978a2fb
[dragonfireclient.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 labels = {
21         leaves = {
22                 fgettext("Opaque Leaves"),
23                 fgettext("Simple Leaves"),
24                 fgettext("Fancy Leaves")
25         },
26         node_highlighting = {
27                 fgettext("Node Outlining"),
28                 fgettext("Node Highlighting"),
29                 fgettext("None")
30         },
31         filters = {
32                 fgettext("No Filter"),
33                 fgettext("Bilinear Filter"),
34                 fgettext("Trilinear Filter")
35         },
36         mipmap = {
37                 fgettext("No Mipmap"),
38                 fgettext("Mipmap"),
39                 fgettext("Mipmap + Aniso. Filter")
40         },
41         antialiasing = {
42                 fgettext("None"),
43                 fgettext("2x"),
44                 fgettext("4x"),
45                 fgettext("8x")
46         },
47         shadow_levels = {
48                 fgettext("Disabled"),
49                 fgettext("Very Low"),
50                 fgettext("Low"),
51                 fgettext("Medium"),
52                 fgettext("High"),
53                 fgettext("Very High")
54         }
55 }
56
57 local dd_options = {
58         leaves = {
59                 table.concat(labels.leaves, ","),
60                 {"opaque", "simple", "fancy"}
61         },
62         node_highlighting = {
63                 table.concat(labels.node_highlighting, ","),
64                 {"box", "halo", "none"}
65         },
66         filters = {
67                 table.concat(labels.filters, ","),
68                 {"", "bilinear_filter", "trilinear_filter"}
69         },
70         mipmap = {
71                 table.concat(labels.mipmap, ","),
72                 {"", "mip_map", "anisotropic_filter"}
73         },
74         antialiasing = {
75                 table.concat(labels.antialiasing, ","),
76                 {"0", "2", "4", "8"}
77         },
78         shadow_levels = {
79                 table.concat(labels.shadow_levels, ","),
80                 { "0", "1", "2", "3", "4", "5" }
81         }
82 }
83
84 local getSettingIndex = {
85         Leaves = function()
86                 local style = core.settings:get("leaves_style")
87                 for idx, name in pairs(dd_options.leaves[2]) do
88                         if style == name then return idx end
89                 end
90                 return 1
91         end,
92         NodeHighlighting = function()
93                 local style = core.settings:get("node_highlighting")
94                 for idx, name in pairs(dd_options.node_highlighting[2]) do
95                         if style == name then return idx end
96                 end
97                 return 1
98         end,
99         Filter = function()
100                 if core.settings:get(dd_options.filters[2][3]) == "true" then
101                         return 3
102                 elseif core.settings:get(dd_options.filters[2][3]) == "false" and
103                                 core.settings:get(dd_options.filters[2][2]) == "true" then
104                         return 2
105                 end
106                 return 1
107         end,
108         Mipmap = function()
109                 if core.settings:get(dd_options.mipmap[2][3]) == "true" then
110                         return 3
111                 elseif core.settings:get(dd_options.mipmap[2][3]) == "false" and
112                                 core.settings:get(dd_options.mipmap[2][2]) == "true" then
113                         return 2
114                 end
115                 return 1
116         end,
117         Antialiasing = function()
118                 local antialiasing_setting = core.settings:get("fsaa")
119                 for i = 1, #dd_options.antialiasing[2] do
120                         if antialiasing_setting == dd_options.antialiasing[2][i] then
121                                 return i
122                         end
123                 end
124                 return 1
125         end,
126         ShadowMapping = function()
127                 local shadow_setting = core.settings:get("shadow_levels")
128                 for i = 1, #dd_options.shadow_levels[2] do
129                         if shadow_setting == dd_options.shadow_levels[2][i] then
130                                 return i
131                         end
132                 end
133                 return 1
134         end
135 }
136
137 local function antialiasing_fname_to_name(fname)
138         for i = 1, #labels.antialiasing do
139                 if fname == labels.antialiasing[i] then
140                         return dd_options.antialiasing[2][i]
141                 end
142         end
143         return 0
144 end
145
146 local function formspec(tabview, name, tabdata)
147         local tab_string =
148                 "box[0,0;3.75,4.5;#999999]" ..
149                 "checkbox[0.25,0;cb_smooth_lighting;" .. fgettext("Smooth Lighting") .. ";"
150                                 .. dump(core.settings:get_bool("smooth_lighting")) .. "]" ..
151                 "checkbox[0.25,0.5;cb_particles;" .. fgettext("Particles") .. ";"
152                                 .. dump(core.settings:get_bool("enable_particles")) .. "]" ..
153                 "checkbox[0.25,1;cb_3d_clouds;" .. fgettext("3D Clouds") .. ";"
154                                 .. dump(core.settings:get_bool("enable_3d_clouds")) .. "]" ..
155                 "checkbox[0.25,1.5;cb_opaque_water;" .. fgettext("Opaque Water") .. ";"
156                                 .. dump(core.settings:get_bool("opaque_water")) .. "]" ..
157                 "checkbox[0.25,2.0;cb_connected_glass;" .. fgettext("Connected Glass") .. ";"
158                                 .. dump(core.settings:get_bool("connected_glass")) .. "]" ..
159                 "dropdown[0.25,2.8;3.5;dd_node_highlighting;" .. dd_options.node_highlighting[1] .. ";"
160                                 .. getSettingIndex.NodeHighlighting() .. "]" ..
161                 "dropdown[0.25,3.6;3.5;dd_leaves_style;" .. dd_options.leaves[1] .. ";"
162                                 .. getSettingIndex.Leaves() .. "]" ..
163                 "box[4,0;3.75,4.5;#999999]" ..
164                 "label[4.25,0.1;" .. fgettext("Texturing:") .. "]" ..
165                 "dropdown[4.25,0.55;3.5;dd_filters;" .. dd_options.filters[1] .. ";"
166                                 .. getSettingIndex.Filter() .. "]" ..
167                 "dropdown[4.25,1.35;3.5;dd_mipmap;" .. dd_options.mipmap[1] .. ";"
168                                 .. getSettingIndex.Mipmap() .. "]" ..
169                 "label[4.25,2.15;" .. fgettext("Antialiasing:") .. "]" ..
170                 "dropdown[4.25,2.6;3.5;dd_antialiasing;" .. dd_options.antialiasing[1] .. ";"
171                                 .. getSettingIndex.Antialiasing() .. "]" ..
172                 "label[4.25,3.45;" .. fgettext("Screen:") .. "]" ..
173                 "checkbox[4.25,3.6;cb_autosave_screensize;" .. fgettext("Autosave Screen Size") .. ";"
174                                 .. dump(core.settings:get_bool("autosave_screensize")) .. "]" ..
175                 "box[8,0;3.75,4.5;#999999]"
176
177         local video_driver = core.settings:get("video_driver")
178         local shaders_enabled = core.settings:get_bool("enable_shaders")
179         if video_driver == "opengl" then
180                 tab_string = tab_string ..
181                         "checkbox[8.25,0;cb_shaders;" .. fgettext("Shaders") .. ";"
182                                         .. tostring(shaders_enabled) .. "]"
183         elseif video_driver == "ogles2" then
184                 tab_string = tab_string ..
185                         "checkbox[8.25,0;cb_shaders;" .. fgettext("Shaders (experimental)") .. ";"
186                                         .. tostring(shaders_enabled) .. "]"
187         else
188                 core.settings:set_bool("enable_shaders", false)
189                 shaders_enabled = false
190                 tab_string = tab_string ..
191                         "label[8.38,0.2;" .. core.colorize("#888888",
192                                         fgettext("Shaders (unavailable)")) .. "]"
193         end
194
195         tab_string = tab_string ..
196                 "button[8,4.75;3.95,1;btn_change_keys;"
197                 .. fgettext("Change Keys") .. "]"
198
199         tab_string = tab_string ..
200                 "button[0,4.75;3.95,1;btn_advanced_settings;"
201                 .. fgettext("All Settings") .. "]"
202
203
204         if core.settings:get("touchscreen_threshold") ~= nil then
205                 tab_string = tab_string ..
206                         "label[4.3,4.2;" .. fgettext("Touchthreshold: (px)") .. "]" ..
207                         "dropdown[4.25,4.65;3.5;dd_touchthreshold;0,10,20,30,40,50;" ..
208                         ((tonumber(core.settings:get("touchscreen_threshold")) / 10) + 1) ..
209                         "]box[4.0,4.5;3.75,1.0;#999999]"
210         end
211
212         if shaders_enabled then
213                 tab_string = tab_string ..
214                         "checkbox[8.25,0.5;cb_tonemapping;" .. fgettext("Tone Mapping") .. ";"
215                                         .. dump(core.settings:get_bool("tone_mapping")) .. "]" ..
216                         "checkbox[8.25,1;cb_waving_water;" .. fgettext("Waving Liquids") .. ";"
217                                         .. dump(core.settings:get_bool("enable_waving_water")) .. "]" ..
218                         "checkbox[8.25,1.5;cb_waving_leaves;" .. fgettext("Waving Leaves") .. ";"
219                                         .. dump(core.settings:get_bool("enable_waving_leaves")) .. "]" ..
220                         "checkbox[8.25,2;cb_waving_plants;" .. fgettext("Waving Plants") .. ";"
221                                         .. dump(core.settings:get_bool("enable_waving_plants")) .. "]"..
222                         "label[8.25,2.8;" .. fgettext("Dynamic shadows:") .. "]" ..
223                         "label[8.25,3.2;" .. fgettext("(game support required)") .. "]" ..
224                                         "dropdown[8.25,3.7;3.5;dd_shadows;" .. dd_options.shadow_levels[1] .. ";"
225                                         .. getSettingIndex.ShadowMapping() .. "]"
226         else
227                 tab_string = tab_string ..
228                         "label[8.38,0.7;" .. core.colorize("#888888",
229                                         fgettext("Tone Mapping")) .. "]" ..
230                         "label[8.38,1.2;" .. core.colorize("#888888",
231                                         fgettext("Waving Liquids")) .. "]" ..
232                         "label[8.38,1.7;" .. core.colorize("#888888",
233                                         fgettext("Waving Leaves")) .. "]" ..
234                         "label[8.38,2.2;" .. core.colorize("#888888",
235                                         fgettext("Waving Plants")) .. "]"..
236                         "label[8.38,2.7;" .. core.colorize("#888888",
237                                         fgettext("Dynamic shadows")) .. "]"
238         end
239
240         return tab_string
241 end
242
243 --------------------------------------------------------------------------------
244 local function handle_settings_buttons(this, fields, tabname, tabdata)
245
246         if fields["btn_advanced_settings"] ~= nil then
247                 local adv_settings_dlg = create_adv_settings_dlg()
248                 adv_settings_dlg:set_parent(this)
249                 this:hide()
250                 adv_settings_dlg:show()
251                 --mm_game_theme.update("singleplayer", current_game())
252                 return true
253         end
254         if fields["cb_smooth_lighting"] then
255                 core.settings:set("smooth_lighting", fields["cb_smooth_lighting"])
256                 return true
257         end
258         if fields["cb_particles"] then
259                 core.settings:set("enable_particles", fields["cb_particles"])
260                 return true
261         end
262         if fields["cb_3d_clouds"] then
263                 core.settings:set("enable_3d_clouds", fields["cb_3d_clouds"])
264                 return true
265         end
266         if fields["cb_opaque_water"] then
267                 core.settings:set("opaque_water", fields["cb_opaque_water"])
268                 return true
269         end
270         if fields["cb_connected_glass"] then
271                 core.settings:set("connected_glass", fields["cb_connected_glass"])
272                 return true
273         end
274         if fields["cb_autosave_screensize"] then
275                 core.settings:set("autosave_screensize", fields["cb_autosave_screensize"])
276                 return true
277         end
278         if fields["cb_shaders"] then
279                 core.settings:set("enable_shaders", fields["cb_shaders"])
280                 return true
281         end
282         if fields["cb_tonemapping"] then
283                 core.settings:set("tone_mapping", fields["cb_tonemapping"])
284                 return true
285         end
286         if fields["cb_waving_water"] then
287                 core.settings:set("enable_waving_water", fields["cb_waving_water"])
288                 return true
289         end
290         if fields["cb_waving_leaves"] then
291                 core.settings:set("enable_waving_leaves", fields["cb_waving_leaves"])
292         end
293         if fields["cb_waving_plants"] then
294                 core.settings:set("enable_waving_plants", fields["cb_waving_plants"])
295                 return true
296         end
297         if fields["btn_change_keys"] then
298                 core.show_keys_menu()
299                 return true
300         end
301         if fields["cb_touchscreen_target"] then
302                 core.settings:set("touchtarget", fields["cb_touchscreen_target"])
303                 return true
304         end
305
306         --Note dropdowns have to be handled LAST!
307         local ddhandled = false
308
309         for i = 1, #labels.leaves do
310                 if fields["dd_leaves_style"] == labels.leaves[i] then
311                         core.settings:set("leaves_style", dd_options.leaves[2][i])
312                         ddhandled = true
313                 end
314         end
315         for i = 1, #labels.node_highlighting do
316                 if fields["dd_node_highlighting"] == labels.node_highlighting[i] then
317                         core.settings:set("node_highlighting", dd_options.node_highlighting[2][i])
318                         ddhandled = true
319                 end
320         end
321         if fields["dd_filters"] == labels.filters[1] then
322                 core.settings:set("bilinear_filter", "false")
323                 core.settings:set("trilinear_filter", "false")
324                 ddhandled = true
325         elseif fields["dd_filters"] == labels.filters[2] then
326                 core.settings:set("bilinear_filter", "true")
327                 core.settings:set("trilinear_filter", "false")
328                 ddhandled = true
329         elseif fields["dd_filters"] == labels.filters[3] then
330                 core.settings:set("bilinear_filter", "false")
331                 core.settings:set("trilinear_filter", "true")
332                 ddhandled = true
333         end
334         if fields["dd_mipmap"] == labels.mipmap[1] then
335                 core.settings:set("mip_map", "false")
336                 core.settings:set("anisotropic_filter", "false")
337                 ddhandled = true
338         elseif fields["dd_mipmap"] == labels.mipmap[2] then
339                 core.settings:set("mip_map", "true")
340                 core.settings:set("anisotropic_filter", "false")
341                 ddhandled = true
342         elseif fields["dd_mipmap"] == labels.mipmap[3] then
343                 core.settings:set("mip_map", "true")
344                 core.settings:set("anisotropic_filter", "true")
345                 ddhandled = true
346         end
347         if fields["dd_antialiasing"] then
348                 core.settings:set("fsaa",
349                         antialiasing_fname_to_name(fields["dd_antialiasing"]))
350                 ddhandled = true
351         end
352         if fields["dd_touchthreshold"] then
353                 core.settings:set("touchscreen_threshold", fields["dd_touchthreshold"])
354                 ddhandled = true
355         end
356
357         for i = 1, #labels.shadow_levels do
358                 if fields["dd_shadows"] == labels.shadow_levels[i] then
359                         core.settings:set("shadow_levels", dd_options.shadow_levels[2][i])
360                         ddhandled = true
361                 end
362         end
363
364         if fields["dd_shadows"] == labels.shadow_levels[1] then
365                 core.settings:set("enable_dynamic_shadows", "false")
366         else
367                 local shadow_presets = {
368                         [2] = { 62,  512,  "true", 0, "false" },
369                         [3] = { 93,  1024, "true", 0, "false" },
370                         [4] = { 140, 2048, "true", 1, "false" },
371                         [5] = { 210, 4096, "true", 2,  "true" },
372                         [6] = { 300, 8192, "true", 2,  "true" },
373                 }
374                 local s = shadow_presets[table.indexof(labels.shadow_levels, fields["dd_shadows"])]
375                 if s then
376                         core.settings:set("enable_dynamic_shadows", "true")
377                         core.settings:set("shadow_map_max_distance", s[1])
378                         core.settings:set("shadow_map_texture_size", s[2])
379                         core.settings:set("shadow_map_texture_32bit", s[3])
380                         core.settings:set("shadow_filters", s[4])
381                         core.settings:set("shadow_map_color", s[5])
382                 end
383         end
384
385         return ddhandled
386 end
387
388 return {
389         name = "settings",
390         caption = fgettext("Settings"),
391         cbf_formspec = formspec,
392         cbf_button_handler = handle_settings_buttons
393 }