fsbuilder.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. local version = {major = 0, minor = 0, revison = 1}
  2. local fsbuilder = {version = version}
  3. local modname = minetest.get_current_modname()
  4. local fs_esc = minetest.formspec_escape
  5. local theme = {
  6. bgcolor = "#000000BB",
  7. boxcolor = "#87CEFA17",
  8. lists = {
  9. normal = "#172d3a75",
  10. hover = "#1E90FF15",
  11. border = "#1E90FF40",
  12. tt_background = "#2F2F2F",
  13. tt_font = "#c1c1c1"
  14. },
  15. buttons = {
  16. normal = "#7C8500",
  17. hover = "green",
  18. pressed = "#DAA520",
  19. alpha = "true"
  20. },
  21. color = "#c1c1c1"
  22. }
  23. fsbuilder.esc = fs_esc
  24. fsbuilder.concat = function(t)
  25. if t == nil then
  26. return ""
  27. else
  28. if type(t) == "string" then
  29. return t
  30. end
  31. end
  32. return table.concat(t, "")
  33. end
  34. function fsbuilder:new(def)
  35. def = def or {}
  36. local o = {
  37. valid = true,
  38. form = {},
  39. id = 0,
  40. height = def.height or 0,
  41. width = def.width or 0,
  42. spacing = 0.25,
  43. padding = 0.125,
  44. background = def.background or modname .. "_background",
  45. theme = theme
  46. }
  47. setmetatable(o, self)
  48. self.__index = self
  49. return o
  50. end
  51. function fsbuilder:set_size(height, width)
  52. self:set_height(height)
  53. self:set_width(width)
  54. end
  55. function fsbuilder:set_height(height)
  56. self.height = height
  57. end
  58. function fsbuilder:set_width(width)
  59. self.width = width
  60. end
  61. function fsbuilder:set_background(background)
  62. self.background = background
  63. end
  64. function fsbuilder:set_theme(key, value, force)
  65. value = value == nil and "" or value
  66. if not key then
  67. return false
  68. end
  69. if not force then
  70. if not self.theme[key] then
  71. self.theme[key] = value
  72. return true
  73. end
  74. else
  75. self.theme[key] = value
  76. return true
  77. end
  78. return false
  79. end
  80. function fsbuilder:get_theme(key)
  81. return self.theme[key]
  82. end
  83. function fsbuilder:new_id()
  84. self.id = self.id + 1
  85. return self.id
  86. end
  87. function fsbuilder:add_form_element(data)
  88. self.form[self:new_id()] = self.concat(data)
  89. return self.id
  90. end
  91. function fsbuilder:get_form_element(key)
  92. if self.form[key] ~= nil then
  93. return self.form[key]
  94. end
  95. return false
  96. end
  97. function fsbuilder:is_valid()
  98. return self.valid and true or false
  99. end
  100. function fsbuilder:set_invalid()
  101. self.valid = false
  102. end
  103. function fsbuilder:set_valid()
  104. self.valid = true
  105. end
  106. function fsbuilder:del_form_element(key)
  107. if self.form[key] ~= nil then
  108. self.form[key] = nil
  109. end
  110. end
  111. function fsbuilder:get_form_string(add_header)
  112. add_header = add_header or true
  113. return (add_header and self:add_header() or "") .. self.concat(self.form)
  114. end
  115. function fsbuilder:add_header()
  116. return string.format(
  117. [[
  118. formspec_version[4]
  119. size[%f,%f]
  120. background9[0,0;0,0;%s.png;true;4]
  121. bgcolor[%s;true]
  122. listcolors[%s;%s;%s;%s;%s]
  123. theme_type[image_button,button,item_image_button;bgcolor=%s;bgcolor_hovered=%s;bgcolor_pressed=%s;alpha=%s]
  124. ]],
  125. self.width,
  126. self.height,
  127. self.background,
  128. self.theme.bgcolor,
  129. self.theme.lists.normal,
  130. self.theme.lists.hover,
  131. self.theme.lists.border,
  132. self.theme.lists.tt_background,
  133. self.theme.lists.tt_font,
  134. self.theme.buttons.normal,
  135. self.theme.buttons.hover,
  136. self.theme.buttons.pressed,
  137. self.theme.buttons.alpha
  138. )
  139. end
  140. -- listring[<inventory location>;<list name>]
  141. function fsbuilder:listring(location, listname)
  142. self:add_form_element("listring[" .. location .. ";" .. listname .. "]")
  143. end
  144. function fsbuilder:add_footer()
  145. end
  146. -- helper functions
  147. function fsbuilder:slot(slot)
  148. return slot + (self.spacing * (slot + 1))
  149. end
  150. -- Minetest formspec elements
  151. --### `list[<inventory location>;<list name>;<X>,<Y>;<W>,<H>;<starting item index>]`
  152. function fsbuilder:list(loc, inv_name, x, y, w, h, start, x_offset, y_offset)
  153. y_offset = y_offset or 0 --hotbar padding
  154. x_offset = x_offset or 0
  155. start = start or ""
  156. self:add_form_element(
  157. {
  158. string.format(
  159. [[
  160. list[%s;%s;%f,%f;%f,%f;%s]
  161. ]],
  162. fs_esc(loc),
  163. fs_esc(inv_name),
  164. x + x_offset,
  165. y + y_offset,
  166. w,
  167. h,
  168. start
  169. )
  170. }
  171. )
  172. end
  173. function fsbuilder:player_list(x, y)
  174. self:list("current_player", "main", self:slot(x), self:slot(y), 8, 1)
  175. self:list("current_player", "main", self:slot(x), self:slot(y), 8, 3, 8, 0, 1.5)
  176. end
  177. --### `label[<X>,<Y>;<label>]`
  178. function fsbuilder:label(x, y, str)
  179. self:add_form_element(
  180. {
  181. string.format("label[%f,%f;%s]", x, y, fs_esc(str))
  182. }
  183. )
  184. end
  185. --### `tooltip[<X>,<Y>;<W>,<H>;<tooltip_text>;<bgcolor>;<fontcolor>]`
  186. function fsbuilder:tooltip(x, y, w, h, tooltip_text, bgcolor, fontcolor)
  187. self:add_form_element(
  188. {
  189. string.format(
  190. [[ tooltip[%f,%f;%f,%f;%s;%s;%s] ]],
  191. x,
  192. y,
  193. w,
  194. h,
  195. fs_esc(tooltip_text),
  196. fs_esc(bgcolor and bgcolor or self:get_theme("lists").tt_background),
  197. fs_esc(fontcolor and fontcolor or self:get_theme("lists").tt_font)
  198. )
  199. }
  200. )
  201. end
  202. --### `tooltip[<gui_element_name>;<tooltip_text>;<bgcolor>;<fontcolor>]`
  203. function fsbuilder:tooltip_element(gui_element_name, tooltip_text, bgcolor, fontcolor)
  204. self:add_form_element(
  205. {
  206. string.format(
  207. [[ tooltip[%s,%s,%s,%s] ]],
  208. fs_esc(gui_element_name),
  209. fs_esc(tooltip_text),
  210. fs_esc(bgcolor and bgcolor or self:get_theme("bgcolor")),
  211. fs_esc(fontcolor and fontcolor or self:get_theme("color"))
  212. )
  213. }
  214. )
  215. end
  216. -- ### `button[<X>,<Y>;<W>,<H>;<name>;<label>]`
  217. function fsbuilder:button(x, y, w, h, name, label)
  218. self:add_form_element(
  219. {
  220. string.format("button[%f,%f;%f,%f;%s;%s]", x, y, w, h, fs_esc(name), fs_esc(label))
  221. }
  222. )
  223. end
  224. -- ### `image[<X>,<Y>;<W>,<H>;<texture name>]`
  225. function fsbuilder:image(x, y, w, h, image)
  226. self:add_form_element(
  227. {
  228. string.format("image[%f,%f;%f,%f;%s]", x, y, w, h, fs_esc(image))
  229. }
  230. )
  231. end
  232. return fsbuilder