]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/tab_settings.lua
Merge branch 'master' of https://github.com/minetest/minetest
[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 }
48
49 local dd_options = {
50         leaves = {
51                 table.concat(labels.leaves, ","),
52                 {"opaque", "simple", "fancy"}
53         },
54         node_highlighting = {
55                 table.concat(labels.node_highlighting, ","),
56                 {"box", "halo", "none"}
57         },
58         filters = {
59                 table.concat(labels.filters, ","),
60                 {"", "bilinear_filter", "trilinear_filter"}
61         },
62         mipmap = {
63                 table.concat(labels.mipmap, ","),
64                 {"", "mip_map", "anisotropic_filter"}
65         },
66         antialiasing = {
67                 table.concat(labels.antialiasing, ","),
68                 {"0", "2", "4", "8"}
69         }
70 }
71
72 local getSettingIndex = {
73         Leaves = function()
74                 local style = core.settings:get("leaves_style")
75                 for idx, name in pairs(dd_options.leaves[2]) do
76                         if style == name then return idx end
77                 end
78                 return 1
79         end,
80         NodeHighlighting = function()
81                 local style = core.settings:get("node_highlighting")
82                 for idx, name in pairs(dd_options.node_highlighting[2]) do
83                         if style == name then return idx end
84                 end
85                 return 1
86         end,
87         Filter = function()
88                 if core.settings:get(dd_options.filters[2][3]) == "true" then
89                         return 3
90                 elseif core.settings:get(dd_options.filters[2][3]) == "false" and
91                                 core.settings:get(dd_options.filters[2][2]) == "true" then
92                         return 2
93                 end
94                 return 1
95         end,
96         Mipmap = function()
97                 if core.settings:get(dd_options.mipmap[2][3]) == "true" then
98                         return 3
99                 elseif core.settings:get(dd_options.mipmap[2][3]) == "false" and
100                                 core.settings:get(dd_options.mipmap[2][2]) == "true" then
101                         return 2
102                 end
103                 return 1
104         end,
105         Antialiasing = function()
106                 local antialiasing_setting = core.settings:get("fsaa")
107                 for i = 1, #dd_options.antialiasing[2] do
108                         if antialiasing_setting == dd_options.antialiasing[2][i] then
109                                 return i
110                         end
111                 end
112                 return 1
113         end
114 }
115
116 local function antialiasing_fname_to_name(fname)
117         for i = 1, #labels.antialiasing do
118                 if fname == labels.antialiasing[i] then
119                         return dd_options.antialiasing[2][i]
120                 end
121         end
122         return 0
123 end
124
125 local function formspec(tabview, name, tabdata)
126         local tab_string =
127                 "box[0,0;3.75,4.5;#999999]" ..
128                 "checkbox[0.25,0;cb_smooth_lighting;" .. fgettext("Smooth Lighting") .. ";"
129                                 .. dump(core.settings:get_bool("smooth_lighting")) .. "]" ..
130                 "checkbox[0.25,0.5;cb_particles;" .. fgettext("Particles") .. ";"
131                                 .. dump(core.settings:get_bool("enable_particles")) .. "]" ..
132                 "checkbox[0.25,1;cb_3d_clouds;" .. fgettext("3D Clouds") .. ";"
133                                 .. dump(core.settings:get_bool("enable_3d_clouds")) .. "]" ..
134                 "checkbox[0.25,1.5;cb_opaque_water;" .. fgettext("Opaque Water") .. ";"
135                                 .. dump(core.settings:get_bool("opaque_water")) .. "]" ..
136                 "checkbox[0.25,2.0;cb_connected_glass;" .. fgettext("Connected Glass") .. ";"
137                                 .. dump(core.settings:get_bool("connected_glass")) .. "]" ..
138                 "dropdown[0.25,2.8;3.5;dd_node_highlighting;" .. dd_options.node_highlighting[1] .. ";"
139                                 .. getSettingIndex.NodeHighlighting() .. "]" ..
140                 "dropdown[0.25,3.6;3.5;dd_leaves_style;" .. dd_options.leaves[1] .. ";"
141                                 .. getSettingIndex.Leaves() .. "]" ..
142                 "box[4,0;3.75,4.5;#999999]" ..
143                 "label[4.25,0.1;" .. fgettext("Texturing:") .. "]" ..
144                 "dropdown[4.25,0.55;3.5;dd_filters;" .. dd_options.filters[1] .. ";"
145                                 .. getSettingIndex.Filter() .. "]" ..
146                 "dropdown[4.25,1.35;3.5;dd_mipmap;" .. dd_options.mipmap[1] .. ";"
147                                 .. getSettingIndex.Mipmap() .. "]" ..
148                 "label[4.25,2.15;" .. fgettext("Antialiasing:") .. "]" ..
149                 "dropdown[4.25,2.6;3.5;dd_antialiasing;" .. dd_options.antialiasing[1] .. ";"
150                                 .. getSettingIndex.Antialiasing() .. "]" ..
151                 "label[4.25,3.45;" .. fgettext("Screen:") .. "]" ..
152                 "checkbox[4.25,3.6;cb_autosave_screensize;" .. fgettext("Autosave Screen Size") .. ";"
153                                 .. dump(core.settings:get_bool("autosave_screensize")) .. "]" ..
154                 "box[8,0;3.75,4.5;#999999]"
155
156         local video_driver = core.settings:get("video_driver")
157         local shaders_enabled = core.settings:get_bool("enable_shaders")
158         if video_driver == "opengl" then
159                 tab_string = tab_string ..
160                         "checkbox[8.25,0;cb_shaders;" .. fgettext("Shaders") .. ";"
161                                         .. tostring(shaders_enabled) .. "]"
162         elseif video_driver == "ogles2" then
163                 tab_string = tab_string ..
164                         "checkbox[8.25,0;cb_shaders;" .. fgettext("Shaders (experimental)") .. ";"
165                                         .. tostring(shaders_enabled) .. "]"
166         else
167                 core.settings:set_bool("enable_shaders", false)
168                 shaders_enabled = false
169                 tab_string = tab_string ..
170                         "label[8.38,0.2;" .. core.colorize("#888888",
171                                         fgettext("Shaders (unavailable)")) .. "]"
172         end
173
174         tab_string = tab_string ..
175                 "button[8,4.75;3.95,1;btn_change_keys;"
176                 .. fgettext("Change Keys") .. "]"
177
178         tab_string = tab_string ..
179                 "button[0,4.75;3.95,1;btn_advanced_settings;"
180                 .. fgettext("All Settings") .. "]"
181
182
183         if core.settings:get("touchscreen_threshold") ~= nil then
184                 tab_string = tab_string ..
185                         "label[4.3,4.2;" .. fgettext("Touchthreshold: (px)") .. "]" ..
186                         "dropdown[4.25,4.65;3.5;dd_touchthreshold;0,10,20,30,40,50;" ..
187                         ((tonumber(core.settings:get("touchscreen_threshold")) / 10) + 1) ..
188                         "]box[4.0,4.5;3.75,1.0;#999999]"
189         end
190
191         if shaders_enabled then
192                 tab_string = tab_string ..
193                         "checkbox[8.25,0.5;cb_tonemapping;" .. fgettext("Tone Mapping") .. ";"
194                                         .. dump(core.settings:get_bool("tone_mapping")) .. "]" ..
195                         "checkbox[8.25,1;cb_waving_water;" .. fgettext("Waving Liquids") .. ";"
196                                         .. dump(core.settings:get_bool("enable_waving_water")) .. "]" ..
197                         "checkbox[8.25,1.5;cb_waving_leaves;" .. fgettext("Waving Leaves") .. ";"
198                                         .. dump(core.settings:get_bool("enable_waving_leaves")) .. "]" ..
199                         "checkbox[8.25,2;cb_waving_plants;" .. fgettext("Waving Plants") .. ";"
200                                         .. dump(core.settings:get_bool("enable_waving_plants")) .. "]"
201         else
202                 tab_string = tab_string ..
203                         "label[8.38,0.7;" .. core.colorize("#888888",
204                                         fgettext("Tone Mapping")) .. "]" ..
205                         "label[8.38,1.2;" .. core.colorize("#888888",
206                                         fgettext("Waving Liquids")) .. "]" ..
207                         "label[8.38,1.7;" .. core.colorize("#888888",
208                                         fgettext("Waving Leaves")) .. "]" ..
209                         "label[8.38,2.2;" .. core.colorize("#888888",
210                                         fgettext("Waving Plants")) .. "]"
211         end
212
213         return tab_string
214 end
215
216 --------------------------------------------------------------------------------
217 local function handle_settings_buttons(this, fields, tabname, tabdata)
218
219         if fields["btn_advanced_settings"] ~= nil then
220                 local adv_settings_dlg = create_adv_settings_dlg()
221                 adv_settings_dlg:set_parent(this)
222                 this:hide()
223                 adv_settings_dlg:show()
224                 --mm_texture.update("singleplayer", current_game())
225                 return true
226         end
227         if fields["cb_smooth_lighting"] then
228                 core.settings:set("smooth_lighting", fields["cb_smooth_lighting"])
229                 return true
230         end
231         if fields["cb_particles"] then
232                 core.settings:set("enable_particles", fields["cb_particles"])
233                 return true
234         end
235         if fields["cb_3d_clouds"] then
236                 core.settings:set("enable_3d_clouds", fields["cb_3d_clouds"])
237                 return true
238         end
239         if fields["cb_opaque_water"] then
240                 core.settings:set("opaque_water", fields["cb_opaque_water"])
241                 return true
242         end
243         if fields["cb_connected_glass"] then
244                 core.settings:set("connected_glass", fields["cb_connected_glass"])
245                 return true
246         end
247         if fields["cb_autosave_screensize"] then
248                 core.settings:set("autosave_screensize", fields["cb_autosave_screensize"])
249                 return true
250         end
251         if fields["cb_shaders"] then
252                 if (core.settings:get("video_driver") == "direct3d8" or
253                                 core.settings:get("video_driver") == "direct3d9") then
254                         core.settings:set("enable_shaders", "false")
255                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
256                 else
257                         core.settings:set("enable_shaders", fields["cb_shaders"])
258                 end
259                 return true
260         end
261         if fields["cb_tonemapping"] then
262                 core.settings:set("tone_mapping", fields["cb_tonemapping"])
263                 return true
264         end
265         if fields["cb_waving_water"] then
266                 core.settings:set("enable_waving_water", fields["cb_waving_water"])
267                 return true
268         end
269         if fields["cb_waving_leaves"] then
270                 core.settings:set("enable_waving_leaves", fields["cb_waving_leaves"])
271         end
272         if fields["cb_waving_plants"] then
273                 core.settings:set("enable_waving_plants", fields["cb_waving_plants"])
274                 return true
275         end
276         if fields["btn_change_keys"] then
277                 core.show_keys_menu()
278                 return true
279         end
280         if fields["cb_touchscreen_target"] then
281                 core.settings:set("touchtarget", fields["cb_touchscreen_target"])
282                 return true
283         end
284
285         --Note dropdowns have to be handled LAST!
286         local ddhandled = false
287
288         for i = 1, #labels.leaves do
289                 if fields["dd_leaves_style"] == labels.leaves[i] then
290                         core.settings:set("leaves_style", dd_options.leaves[2][i])
291                         ddhandled = true
292                 end
293         end
294         for i = 1, #labels.node_highlighting do
295                 if fields["dd_node_highlighting"] == labels.node_highlighting[i] then
296                         core.settings:set("node_highlighting", dd_options.node_highlighting[2][i])
297                         ddhandled = true
298                 end
299         end
300         if fields["dd_filters"] == labels.filters[1] then
301                 core.settings:set("bilinear_filter", "false")
302                 core.settings:set("trilinear_filter", "false")
303                 ddhandled = true
304         elseif fields["dd_filters"] == labels.filters[2] then
305                 core.settings:set("bilinear_filter", "true")
306                 core.settings:set("trilinear_filter", "false")
307                 ddhandled = true
308         elseif fields["dd_filters"] == labels.filters[3] then
309                 core.settings:set("bilinear_filter", "false")
310                 core.settings:set("trilinear_filter", "true")
311                 ddhandled = true
312         end
313         if fields["dd_mipmap"] == labels.mipmap[1] then
314                 core.settings:set("mip_map", "false")
315                 core.settings:set("anisotropic_filter", "false")
316                 ddhandled = true
317         elseif fields["dd_mipmap"] == labels.mipmap[2] then
318                 core.settings:set("mip_map", "true")
319                 core.settings:set("anisotropic_filter", "false")
320                 ddhandled = true
321         elseif fields["dd_mipmap"] == labels.mipmap[3] then
322                 core.settings:set("mip_map", "true")
323                 core.settings:set("anisotropic_filter", "true")
324                 ddhandled = true
325         end
326         if fields["dd_antialiasing"] then
327                 core.settings:set("fsaa",
328                         antialiasing_fname_to_name(fields["dd_antialiasing"]))
329                 ddhandled = true
330         end
331         if fields["dd_touchthreshold"] then
332                 core.settings:set("touchscreen_threshold", fields["dd_touchthreshold"])
333                 ddhandled = true
334         end
335
336         return ddhandled
337 end
338
339 return {
340         name = "settings",
341         caption = fgettext("Settings"),
342         cbf_formspec = formspec,
343         cbf_button_handler = handle_settings_buttons
344 }