tab_settings.lua 13 KB

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