dec28a96121f58890cabc68e2ba2daa22dadab4b
[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 function dlg_confirm_reset_formspec(data)
21         local retval =
22                 "size[8,3]" ..
23                 "label[1,1;".. fgettext("Are you sure to reset your singleplayer world?") .. "]"..
24                 "button[1,2;2.6,0.5;dlg_reset_singleplayer_confirm;"..
25                                 fgettext("Yes") .. "]" ..
26                 "button[4,2;2.8,0.5;dlg_reset_singleplayer_cancel;"..
27                                 fgettext("No!!!") .. "]"
28         return retval
29 end
30
31 local function dlg_confirm_reset_btnhandler(this, fields, dialogdata)
32
33         if fields["dlg_reset_singleplayer_confirm"] ~= nil then
34                 local worldlist = core.get_worlds()
35                 local found_singleplayerworld = false
36
37                 for i=1,#worldlist,1 do
38                         if worldlist[i].name == "singleplayerworld" then
39                                 found_singleplayerworld = true
40                                 gamedata.worldindex = i
41                         end
42                 end
43
44                 if found_singleplayerworld then
45                         core.delete_world(gamedata.worldindex)
46                 end
47
48                 core.create_world("singleplayerworld", 1)
49
50                 worldlist = core.get_worlds()
51
52                 found_singleplayerworld = false
53
54                 for i=1,#worldlist,1 do
55                         if worldlist[i].name == "singleplayerworld" then
56                                 found_singleplayerworld = true
57                                 gamedata.worldindex = i
58                         end
59                 end
60         end
61
62         this.parent:show()
63         this:hide()
64         this:delete()
65         return true
66 end
67
68 local function showconfirm_reset(tabview)
69         local new_dlg = dialog_create("reset_spworld",
70                 dlg_confirm_reset_formspec,
71                 dlg_confirm_reset_btnhandler,
72                 nil)
73         new_dlg:set_parent(tabview)
74         tabview:hide()
75         new_dlg:show()
76 end
77
78 local function gui_scale_index()
79
80         local current_value = tonumber(core.setting_get("gui_scaling"))
81
82         if (current_value == nil) then
83                 return 0
84         elseif current_value <= 0.5 then
85                 return 1
86         elseif current_value <= 0.625 then
87                 return 2
88         elseif current_value <= 0.75 then
89                 return 3
90         elseif current_value <= 0.875 then
91                 return 4
92         elseif current_value <= 1.0 then
93                 return 5
94         elseif current_value <= 1.25 then
95                 return 6
96         elseif current_value <= 1.5 then
97                 return 7
98         else
99                 return 8
100         end
101 end
102
103 local function formspec(tabview, name, tabdata)
104         local tab_string =
105                 "vertlabel[0,-0.25;" .. fgettext("SETTINGS") .. "]" ..
106                 "box[0.75,0;3.25,4;#999999]" ..
107                 "checkbox[1,0;cb_fancy_trees;".. fgettext("Fancy Trees") .. ";"
108                                 .. dump(core.setting_getbool("new_style_leaves")) .. "]"..
109                 "checkbox[1,0.5;cb_smooth_lighting;".. fgettext("Smooth Lighting")
110                                 .. ";".. dump(core.setting_getbool("smooth_lighting")) .. "]"..
111                 "checkbox[1,1;cb_3d_clouds;".. fgettext("3D Clouds") .. ";"
112                                 .. dump(core.setting_getbool("enable_3d_clouds")) .. "]"..
113                 "checkbox[1,1.5;cb_opaque_water;".. fgettext("Opaque Water") .. ";"
114                                 .. dump(core.setting_getbool("opaque_water")) .. "]"..
115                 "checkbox[1,2.0;cb_pre_ivis;".. fgettext("Preload item visuals") .. ";"
116                                 .. dump(core.setting_getbool("preload_item_visuals"))   .. "]"..
117                 "checkbox[1,2.5;cb_particles;".. fgettext("Enable Particles") .. ";"
118                                 .. dump(core.setting_getbool("enable_particles"))       .. "]"..
119                 "box[4.25,0;3.25,2.5;#999999]" ..
120                 "checkbox[4.5,0;cb_mipmapping;".. fgettext("Mip-Mapping") .. ";"
121                                 .. dump(core.setting_getbool("mip_map")) .. "]"..
122                 "checkbox[4.5,0.5;cb_anisotrophic;".. fgettext("Anisotropic Filtering") .. ";"
123                                 .. dump(core.setting_getbool("anisotropic_filter")) .. "]"..
124                 "checkbox[4.5,1.0;cb_bilinear;".. fgettext("Bi-Linear Filtering") .. ";"
125                                 .. dump(core.setting_getbool("bilinear_filter")) .. "]"..
126                 "checkbox[4.5,1.5;cb_trilinear;".. fgettext("Tri-Linear Filtering") .. ";"
127                                 .. dump(core.setting_getbool("trilinear_filter")) .. "]"..
128                 "box[7.75,0;4,4;#999999]" ..
129                 "checkbox[8,0;cb_shaders;".. fgettext("Shaders") .. ";"
130                                 .. dump(core.setting_getbool("enable_shaders")) .. "]"
131         if not ANDROID then
132                 tab_string = tab_string ..
133                 "button[8,4.75;3.75,0.5;btn_change_keys;".. fgettext("Change keys") .. "]"
134         else
135                 tab_string = tab_string ..
136                 "button[8,4.75;3.75,0.5;btn_reset_singleplayer;".. fgettext("Reset singleplayer world") .. "]"
137         end
138         tab_string = tab_string ..
139                 "box[0.75,4.25;3.25,1.25;#999999]" ..
140                 "label[1,4.25;" .. fgettext("GUI scale factor") .. "]" ..
141                 "dropdown[1,4.75;3.0;dd_gui_scaling;0.5,0.625,0.75,0.875,1.0,1.25,1.5,2.0;"
142                         .. gui_scale_index() .. "]"
143
144         if ANDROID then
145                 tab_string = tab_string ..
146                 "box[4.25,2.75;3.25,2.15;#999999]" ..
147                 "checkbox[4.5,2.75;cb_touchscreen_target;".. fgettext("Touch free target") .. ";"
148                                 .. dump(core.setting_getbool("touchtarget")) .. "]"
149         end
150
151         if core.setting_get("touchscreen_threshold") ~= nil then
152                 tab_string = tab_string ..
153                                 "label[4.5,3.5;" .. fgettext("Touchthreshold (px)") .. "]" ..
154                                 "dropdown[4.5,4;3;dd_touchthreshold;0,10,20,30,40,50;" ..
155                                 ((tonumber(core.setting_get("touchscreen_threshold"))/10)+1) .. "]"
156         end
157
158         if core.setting_getbool("enable_shaders") then
159                 tab_string = tab_string ..
160                                 "checkbox[8,0.5;cb_bumpmapping;".. fgettext("Bumpmapping") .. ";"
161                                                 .. dump(core.setting_getbool("enable_bumpmapping")) .. "]"..
162                                 "checkbox[8,1.0;cb_generate_normalmaps;".. fgettext("Generate Normalmaps") .. ";"
163                                                 .. dump(core.setting_getbool("generate_normalmaps")) .. "]"..
164                                 "checkbox[8,1.5;cb_parallax;".. fgettext("Parallax Occlusion") .. ";"
165                                                 .. dump(core.setting_getbool("enable_parallax_occlusion")) .. "]"..
166                                 "checkbox[8,2.0;cb_waving_water;".. fgettext("Waving Water") .. ";"
167                                                 .. dump(core.setting_getbool("enable_waving_water")) .. "]"..
168                                 "checkbox[8,2.5;cb_waving_leaves;".. fgettext("Waving Leaves") .. ";"
169                                                 .. dump(core.setting_getbool("enable_waving_leaves")) .. "]"..
170                                 "checkbox[8,3.0;cb_waving_plants;".. fgettext("Waving Plants") .. ";"
171                                                 .. dump(core.setting_getbool("enable_waving_plants")) .. "]"
172         else
173                 tab_string = tab_string ..
174                                 "textlist[8.33,0.7;4,1;;#888888" .. fgettext("Bumpmapping") .. ";0;true]" ..
175                                 "textlist[8.33,1.2;4,1;;#888888" .. fgettext("Generate Normalmaps") .. ";0;true]" ..
176                                 "textlist[8.33,1.7;4,1;;#888888" .. fgettext("Parallax Occlusion") .. ";0;true]" ..
177                                 "textlist[8.33,2.2;4,1;;#888888" .. fgettext("Waving Water") .. ";0;true]" ..
178                                 "textlist[8.33,2.7;4,1;;#888888" .. fgettext("Waving Leaves") .. ";0;true]" ..
179                                 "textlist[8.33,3.2;4,1;;#888888" .. fgettext("Waving Plants") .. ";0;true]"
180                 end
181         return tab_string
182 end
183
184 --------------------------------------------------------------------------------
185 local function handle_settings_buttons(this, fields, tabname, tabdata)
186         if fields["cb_fancy_trees"] then
187                 core.setting_set("new_style_leaves", fields["cb_fancy_trees"])
188                 return true
189         end
190         if fields["cb_smooth_lighting"] then
191                 core.setting_set("smooth_lighting", fields["cb_smooth_lighting"])
192                 return true
193         end
194         if fields["cb_3d_clouds"] then
195                 core.setting_set("enable_3d_clouds", fields["cb_3d_clouds"])
196                 return true
197         end
198         if fields["cb_opaque_water"] then
199                 core.setting_set("opaque_water", fields["cb_opaque_water"])
200                 return true
201         end
202         if fields["cb_mipmapping"] then
203                 core.setting_set("mip_map", fields["cb_mipmapping"])
204                 return true
205         end
206         if fields["cb_anisotrophic"] then
207                 core.setting_set("anisotropic_filter", fields["cb_anisotrophic"])
208                 return true
209         end
210         if fields["cb_bilinear"] then
211                 core.setting_set("bilinear_filter", fields["cb_bilinear"])
212                 return true
213         end
214         if fields["cb_trilinear"] then
215                 core.setting_set("trilinear_filter", fields["cb_trilinear"])
216                 return true
217         end
218         if fields["cb_shaders"] then
219                 if (core.setting_get("video_driver") == "direct3d8" or core.setting_get("video_driver") == "direct3d9") then
220                         core.setting_set("enable_shaders", "false")
221                         gamedata.errormessage = fgettext("To enable shaders the OpenGL driver needs to be used.")
222                 else
223                         core.setting_set("enable_shaders", fields["cb_shaders"])
224                 end
225                 return true
226         end
227         if fields["cb_pre_ivis"] then
228                 core.setting_set("preload_item_visuals", fields["cb_pre_ivis"])
229                 return true
230         end
231         if fields["cb_particles"] then
232                 core.setting_set("enable_particles", fields["cb_particles"])
233                 return true
234         end
235         if fields["cb_bumpmapping"] then
236                 core.setting_set("enable_bumpmapping", fields["cb_bumpmapping"])
237         end
238         if fields["cb_generate_normalmaps"] then
239                 core.setting_set("generate_normalmaps", fields["cb_generate_normalmaps"])
240         end
241         if fields["cb_parallax"] then
242                 core.setting_set("enable_parallax_occlusion", fields["cb_parallax"])
243                 return true
244         end
245         if fields["cb_waving_water"] then
246                 core.setting_set("enable_waving_water", fields["cb_waving_water"])
247                 return true
248         end
249         if fields["cb_waving_leaves"] then
250                 core.setting_set("enable_waving_leaves", fields["cb_waving_leaves"])
251         end
252         if fields["cb_waving_plants"] then
253                 core.setting_set("enable_waving_plants", fields["cb_waving_plants"])
254                 return true
255         end
256         if fields["btn_change_keys"] ~= nil then
257                 core.show_keys_menu()
258                 return true
259         end
260         if fields["cb_touchscreen_target"] then
261                 core.setting_set("touchtarget", fields["cb_touchscreen_target"])
262                 return true
263         end
264         if fields["btn_reset_singleplayer"] then
265                 print("sp reset")
266                 showconfirm_reset(this)
267                 return true
268         end
269         --Note dropdowns have to be handled LAST!
270         local ddhandled = false
271         if fields["dd_touchthreshold"] then
272                 core.setting_set("touchscreen_threshold",fields["dd_touchthreshold"])
273                 ddhandled = true
274         end
275         if fields["dd_gui_scaling"] then
276                 core.setting_set("gui_scaling",fields["dd_gui_scaling"])
277                 ddhandled = true
278         end
279         
280         return ddhandled
281 end
282
283 tab_settings = {
284         name = "settings",
285         caption = fgettext("Settings"),
286         cbf_formspec = formspec,
287         cbf_button_handler = handle_settings_buttons
288         }