fsbuilder.lua 6.6 KB

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