tab_settings.lua 13 KB

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