tab_settings.lua 14 KB

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