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