dlg_change_mapgen_flags.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. --Minetest
  2. --Copyright (C) 2015 PilzAdam
  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. local checkboxes = {}
  18. local function flags_to_table(flags)
  19. return flags:gsub("%s+", ""):split(",", true) -- Remove all spaces and split
  20. end
  21. local function get_current_np_group(setting)
  22. local value = core.settings:get_np_group(setting.name)
  23. if value == nil then
  24. return setting.values
  25. end
  26. local p = "%g"
  27. return {
  28. p:format(value.offset),
  29. p:format(value.scale),
  30. p:format(value.spread.x),
  31. p:format(value.spread.y),
  32. p:format(value.spread.z),
  33. p:format(value.seed),
  34. p:format(value.octaves),
  35. p:format(value.persistence),
  36. p:format(value.lacunarity),
  37. value.flags
  38. }
  39. end
  40. local function get_formspec(dialogdata)
  41. local setting = dialogdata.setting
  42. -- Final formspec will be created at the end of this function
  43. -- Default values below, may be changed depending on setting type
  44. local width = 10
  45. local height = 2
  46. local description_height = 1.5
  47. local t = get_current_np_group(setting)
  48. local dimension = 3
  49. if setting.type == "noise_params_2d" then
  50. dimension = 2
  51. end
  52. local fields = {}
  53. local function add_field(x, name, label, value)
  54. fields[#fields + 1] = ("field[%f,%f;3.3,1;%s;%s;%s]"):format(
  55. x, height, name, label, core.formspec_escape(value or "")
  56. )
  57. end
  58. -- First row
  59. height = height + 0.3
  60. add_field(0.3, "te_offset", fgettext("Offset"), t[1])
  61. add_field(3.6, "te_scale", fgettext("Scale"), t[2])
  62. add_field(6.9, "te_seed", fgettext("Seed"), t[6])
  63. height = height + 1.1
  64. -- Second row
  65. add_field(0.3, "te_spreadx", fgettext("X spread"), t[3])
  66. if dimension == 3 then
  67. add_field(3.6, "te_spready", fgettext("Y spread"), t[4])
  68. else
  69. fields[#fields + 1] = "label[4," .. height - 0.2 .. ";" ..
  70. fgettext("2D Noise") .. "]"
  71. end
  72. add_field(6.9, "te_spreadz", fgettext("Z spread"), t[5])
  73. height = height + 1.1
  74. -- Third row
  75. add_field(0.3, "te_octaves", fgettext("Octaves"), t[7])
  76. add_field(3.6, "te_persist", fgettext("Persistence"), t[8])
  77. add_field(6.9, "te_lacun", fgettext("Lacunarity"), t[9])
  78. height = height + 1.1
  79. local enabled_flags = flags_to_table(t[10])
  80. local flags = {}
  81. for _, name in ipairs(enabled_flags) do
  82. -- Index by name, to avoid iterating over all enabled_flags for every possible flag.
  83. flags[name] = true
  84. end
  85. for _, name in ipairs(setting.flags) do
  86. local checkbox_name = "cb_" .. name
  87. local is_enabled = flags[name] == true -- to get false if nil
  88. checkboxes[checkbox_name] = is_enabled
  89. end
  90. local formspec = table.concat(fields)
  91. .. "checkbox[0.5," .. height - 0.6 .. ";cb_defaults;"
  92. --[[~ "defaults" is a noise parameter flag.
  93. It describes the default processing options
  94. for noise settings in the settings menu. ]]
  95. .. fgettext("defaults") .. ";" -- defaults
  96. .. tostring(flags["defaults"] == true) .. "]" -- to get false if nil
  97. .. "checkbox[5," .. height - 0.6 .. ";cb_eased;"
  98. --[[~ "eased" is a noise parameter flag.
  99. It is used to make the map smoother and
  100. can be enabled in noise settings in
  101. the settings menu. ]]
  102. .. fgettext("eased") .. ";" -- eased
  103. .. tostring(flags["eased"] == true) .. "]"
  104. .. "checkbox[5," .. height - 0.15 .. ";cb_absvalue;"
  105. --[[~ "absvalue" is a noise parameter flag.
  106. It is short for "absolute value".
  107. It can be enabled in noise settings in
  108. the settings menu. ]]
  109. .. fgettext("absvalue") .. ";" -- absvalue
  110. .. tostring(flags["absvalue"] == true) .. "]"
  111. height = height + 1
  112. -- Box good, textarea bad. Calculate textarea size from box.
  113. local function create_textfield(size, label, text, bg_color)
  114. local textarea = {
  115. x = size.x + 0.3,
  116. y = size.y,
  117. w = size.w + 0.25,
  118. h = size.h * 1.16 + 0.12
  119. }
  120. return ("box[%f,%f;%f,%f;%s]textarea[%f,%f;%f,%f;;%s;%s]"):format(
  121. size.x, size.y, size.w, size.h, bg_color or "#000",
  122. textarea.x, textarea.y, textarea.w, textarea.h,
  123. core.formspec_escape(label), core.formspec_escape(text)
  124. )
  125. end
  126. -- When there's an error: Shrink description textarea and add error below
  127. if dialogdata.error_message then
  128. local error_box = {
  129. x = 0,
  130. y = description_height - 0.4,
  131. w = width - 0.25,
  132. h = 0.5
  133. }
  134. formspec = formspec ..
  135. create_textfield(error_box, "", dialogdata.error_message, "#600")
  136. description_height = description_height - 0.75
  137. end
  138. -- Get description field
  139. local description_box = {
  140. x = 0,
  141. y = 0.2,
  142. w = width - 0.25,
  143. h = description_height
  144. }
  145. local setting_name = setting.name
  146. if setting.readable_name then
  147. setting_name = fgettext_ne(setting.readable_name) ..
  148. " (" .. setting.name .. ")"
  149. end
  150. local comment_text
  151. if setting.comment == "" then
  152. comment_text = fgettext_ne("(No description of setting given)")
  153. else
  154. comment_text = fgettext_ne(setting.comment)
  155. end
  156. return (
  157. "size[" .. width .. "," .. height + 0.25 .. ",true]" ..
  158. create_textfield(description_box, setting_name, comment_text) ..
  159. formspec ..
  160. "button[" .. width / 2 - 2.5 .. "," .. height - 0.4 .. ";2.5,1;btn_done;" ..
  161. fgettext("Save") .. "]" ..
  162. "button[" .. width / 2 .. "," .. height - 0.4 .. ";2.5,1;btn_cancel;" ..
  163. fgettext("Cancel") .. "]"
  164. )
  165. end
  166. local function buttonhandler(this, fields)
  167. local setting = this.data.setting
  168. if fields["btn_done"] or fields["key_enter"] then
  169. local np_flags = {}
  170. for _, name in ipairs(setting.flags) do
  171. if checkboxes["cb_" .. name] then
  172. table.insert(np_flags, name)
  173. end
  174. end
  175. checkboxes = {}
  176. if setting.type == "noise_params_2d" then
  177. fields["te_spready"] = fields["te_spreadz"]
  178. end
  179. local new_value = {
  180. offset = fields["te_offset"],
  181. scale = fields["te_scale"],
  182. spread = {
  183. x = fields["te_spreadx"],
  184. y = fields["te_spready"],
  185. z = fields["te_spreadz"]
  186. },
  187. seed = fields["te_seed"],
  188. octaves = fields["te_octaves"],
  189. persistence = fields["te_persist"],
  190. lacunarity = fields["te_lacun"],
  191. flags = table.concat(np_flags, ", ")
  192. }
  193. core.settings:set_np_group(setting.name, new_value)
  194. core.settings:write()
  195. this:delete()
  196. return true
  197. end
  198. if fields["btn_cancel"] then
  199. this:delete()
  200. return true
  201. end
  202. for name, value in pairs(fields) do
  203. if name:sub(1, 3) == "cb_" then
  204. checkboxes[name] = core.is_yes(value)
  205. return false -- Don't update the formspec!
  206. end
  207. end
  208. return false
  209. end
  210. function create_change_mapgen_flags_dlg(setting)
  211. assert(type(setting) == "table")
  212. local retval = dialog_create("dlg_change_mapgen_flags",
  213. get_formspec,
  214. buttonhandler,
  215. nil)
  216. retval.data.setting = setting
  217. return retval
  218. end