A minetest mod for mapmaking
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
11KB

  1. -- Gui API (Internal)
  2. --
  3. -- Contains functions for building formspec-based uis
  4. local gui = {
  5. };
  6. -- Create a formspec
  7. --
  8. -- w: The width of the window
  9. -- h: The height of the window
  10. -- (Optional) version: The formspec version. Defaults to 3.
  11. -- (Optional) bg: A 9-slice background skin object
  12. -- Additional arguments are added as additional formspec elements
  13. --
  14. -- Returns a table. Calling table.concat on the result will produce
  15. -- a usable formspec string.
  16. function gui.formspec(args)
  17. local data = string.format("formspec_version[%d] size[%f,%f] no_prepend[] bgcolor[#00000000;false]",
  18. args.version or 3,
  19. args.w, args.h);
  20. if args.bg then
  21. data = data .. gui.bg9 {
  22. skin = args.bg,
  23. fullsize = true,
  24. };
  25. end
  26. for _,element in ipairs(args) do
  27. data = data .. element;
  28. end
  29. return data;
  30. end
  31. -- Create an animated image formspec element
  32. --
  33. -- animation: An animation skin object
  34. -- (Optional) x: The x position of the element. Defaults to 0.
  35. -- (Optional) y: The y position of the element. Defaults to 0.
  36. -- (Optional) w: The width of the element. Defaults to 1.
  37. -- (Optional) h: The height of the element. Defaults to 1.
  38. -- (Optional) size: Multiplies the width and height. Defaults to 1.
  39. -- (Optional) id: The element id
  40. --
  41. -- Returns a formspec string
  42. function gui.animated_image(args)
  43. local x = args.x or 0;
  44. local y = args.y or 0;
  45. local w = args.w or 1 * (args.size or 1);
  46. local h = args.h or 1 * (args.size or 1);
  47. return string.format("animated_image[%f,%f;%f,%f;%s;%s;%d;%d]",
  48. x, y,
  49. w, h,
  50. args.id or "",
  51. args.animation.texture .. ".png",
  52. args.animation.frame_count,
  53. args.animation.frame_duration);
  54. end
  55. -- Create a 9-slice background formspec element
  56. --
  57. -- skin: A 9-slice background skin object
  58. -- (Optional) x: The x position of the element. Defaults to 0.
  59. -- (Optional) y: The y position of the element. Defaults to 0.
  60. -- (Optional) w: The width of the element. Defaults to 1.
  61. -- (Optional) h: The height of the element. Defaults to 1.
  62. -- (Optional) size: Multiplies the width and height. Defaults to 1.
  63. -- (Optional) fullsize: Whether or not to fill the parent formspec. Defaults to false.
  64. --
  65. -- Returns a formspec string
  66. function gui.bg9(args)
  67. local x = args.x or 0;
  68. local y = args.y or 0;
  69. local w = args.w or 1 * (args.size or 1);
  70. local h = args.h or 1 * (args.size or 1);
  71. return string.format("background9[%f,%f;%f,%f;%s;%s;%s]",
  72. x, y,
  73. w, h,
  74. args.skin.texture .. ".png",
  75. args.fullsize or false,
  76. tostring(args.skin.radius));
  77. end
  78. -- Create a button formspec element
  79. --
  80. -- (Optional) text: The text to display on the button
  81. -- (Optional) x: The x position of the element. Defaults to 0.
  82. -- (Optional) y: The y position of the element. Defaults to 0.
  83. -- (Optional) w: The width of the element. Defaults to 1.
  84. -- (Optional) h: The height of the element. Defaults to 1.
  85. -- (Optional) size: Multiplies the width and height. Defaults to 1.
  86. -- (Optional) id: The element id
  87. -- (Optional) tooltip: The tooltip to display when hovering this element.
  88. -- (Optional) disabled: Replaces the id with "disabled_button", allowing it to
  89. -- receive a specific style
  90. --
  91. -- Returns a formspec string
  92. function gui.button(args)
  93. local x = args.x or 0;
  94. local y = args.y or 0;
  95. local w = args.w or 1 * (args.size or 1);
  96. local h = args.h or 1 * (args.size or 1);
  97. if args.disabled then
  98. return string.format("button[%f,%f;%f,%f;disabled_button;%s]", x, y, w, h, args.text or "");
  99. end
  100. local data = string.format("button[%f,%f;%f,%f;%s;%s]", x, y, w, h, args.id or "", args.text or "");
  101. if args.tooltip then
  102. if args.id and not args.disabled then
  103. data = data .. gui.tooltip {
  104. id = args.id,
  105. text = args.tooltip
  106. };
  107. else
  108. data = data .. gui.tooltip {
  109. x = x,
  110. y = y,
  111. w = w,
  112. h = h,
  113. text = args.tooltip
  114. };
  115. end
  116. end
  117. return data;
  118. end
  119. -- Create a formspec container
  120. --
  121. -- (Optional) x: The x offset of the container. Defaults to 0.
  122. -- (Optional) y: The y offset of the container. Defaults to 0.
  123. -- (Optional) w: The width of the container (for drawing a background). Defaults to 1.
  124. -- (Optional) h: The height of the container (for drawing a background). Defaults to 1.
  125. -- (Optional) size: Multiplies the width and height. Defaults to 1.
  126. -- (Optional) bg: A 9-slice background skin object
  127. --
  128. -- Additional arguments are added as the container's child elements
  129. --
  130. -- Returns a formspec string
  131. function gui.container(args)
  132. local x = args.x or 0;
  133. local y = args.y or 0;
  134. local w = args.w or 1 * (args.size or 1);
  135. local h = args.h or 1 * (args.size or 1);
  136. local data = string.format("container[%f,%f]", x, y);
  137. if args.bg then
  138. data = data .. gui.bg9 {
  139. x = 0,
  140. y = 0,
  141. w = w,
  142. h = h,
  143. skin = args.bg,
  144. };
  145. end
  146. for _,element in ipairs(args) do
  147. data = data .. element;
  148. end
  149. return data .. "container_end[]";
  150. end
  151. -- Create an image formspec element
  152. --
  153. -- image: The image to display.
  154. -- (Optional) x: The x offset of the container. Defaults to 0.
  155. -- (Optional) y: The y offset of the container. Defaults to 0.
  156. -- (Optional) w: The width of the container (for drawing a background). Defaults to 1.
  157. -- (Optional) h: The height of the container (for drawing a background). Defaults to 1.
  158. -- (Optional) size: Multiplies the width and height. Defaults to 1.
  159. --
  160. -- Returns a formspec string
  161. function gui.image(args)
  162. local x = args.x or 0;
  163. local y = args.y or 0;
  164. local w = args.w or 1 * (args.size or 1);
  165. local h = args.h or 1 * (args.size or 1);
  166. return string.format("image[%f,%f;%f,%f;%s]", x, y, w, h, args.image);
  167. end
  168. -- Create an image button formspec element
  169. --
  170. -- image: The image to display on the button
  171. -- (Optional) text: The text to display on the button
  172. -- (Optional) x: The x position of the element. Defaults to 0.
  173. -- (Optional) y: The y position of the element. Defaults to 0.
  174. -- (Optional) w: The width of the element. Defaults to 1.
  175. -- (Optional) h: The height of the element. Defaults to 1.
  176. -- (Optional) size: Multiplies the width and height. Defaults to 1.
  177. -- (Optional) id: The element id
  178. -- (Optional) tooltip: The tooltip to display when hovering this element.
  179. -- (Optional) disabled: Replaces the id with "disabled_button", allowing it to
  180. -- receive a specific style
  181. --
  182. -- Returns a formspec string
  183. function gui.image_button(args)
  184. local x = args.x or 0;
  185. local y = args.y or 0;
  186. local w = args.w or 1 * (args.size or 1);
  187. local h = args.h or 1 * (args.size or 1);
  188. if args.disabled then
  189. return string.format("image_button[%f,%f;%f,%f;%s;disabled_button;%s]",
  190. x, y,
  191. w, h,
  192. args.image,
  193. args.text or "");
  194. end
  195. local data = string.format("image_button[%f,%f;%f,%f;%s;%s;%s]",
  196. x, y,
  197. w, h,
  198. args.image,
  199. args.id or "",
  200. args.text or "");
  201. if args.tooltip then
  202. if args.id and not args.disabled then
  203. data = data .. gui.tooltip {
  204. id = args.id,
  205. text = args.tooltip
  206. };
  207. else
  208. data = data .. gui.tooltip {
  209. x = x,
  210. y = y,
  211. w = w,
  212. h = h,
  213. text = args.tooltip
  214. };
  215. end
  216. end
  217. return data;
  218. end
  219. -- Create an inventory list formspec element
  220. --
  221. -- location: The location of the inventory
  222. -- id: The id of the inventory list
  223. -- w: The number of columns in the inventory list
  224. -- h: The number of rows in the inventory list
  225. -- (Optional) x: The x position of the element. Defaults to 0.
  226. -- (Optional) y: The y position of the element. Defaults to 0.
  227. -- (Optional) bg: A 9-slice background skin object (To display under each slot)
  228. -- (Optional) tooltip: The tooltip to display when hovering this element.
  229. --
  230. -- Returns a formspec string
  231. function gui.inventory(args)
  232. local data = "";
  233. if args.bg then
  234. for i = 0,args.w - 1 do
  235. for j = 0,args.h - 1 do
  236. data = data .. gui.bg9 {
  237. x = args.x + (i * 1.25),
  238. y = args.y + (j * 1.25),
  239. skin = args.bg,
  240. };
  241. end
  242. end
  243. end
  244. data = data .. string.format("listcolors[#00000000;#00000022] list[%s;%s;%f,%f;%f,%f;]",
  245. args.location, args.id,
  246. args.x or 0, args.y or 0,
  247. args.w, args.h);
  248. if args.tooltip then
  249. data = data .. gui.tooltip {
  250. x = args.x,
  251. y = args.y,
  252. w = args.w,
  253. h = args.h,
  254. text = args.tooltip,
  255. };
  256. end
  257. return data;
  258. end
  259. -- Create a label formspec element
  260. --
  261. -- text: The text of the label
  262. -- (Optional) textcolor: The color of the label
  263. -- (Optional) x: The x position of the element. Defaults to 0.
  264. -- (Optional) y: The y position of the element. Defaults to 0.
  265. --
  266. -- Returns a formspec string
  267. function gui.label(args)
  268. if args.textcolor then
  269. return string.format("label[%f,%f;%s%s]",
  270. args.x or 0,
  271. args.y or 0,
  272. minetest.get_color_escape_sequence(args.textcolor),
  273. args.text);
  274. end
  275. return string.format("label[%f,%f;%s]", args.x or 0, args.y or 0, args.text);
  276. end
  277. local function style_internal(selector, properties)
  278. local data = "[" .. selector;
  279. for name,value in pairs(properties) do
  280. data = data .. string.format(";%s=%s", name, tostring(value));
  281. end
  282. return data .. "]";
  283. end
  284. -- Create a formspec style
  285. --
  286. -- selector: A valid comma-separated list of id-based style selectors
  287. -- properties: A table of property names and values
  288. --
  289. -- Returns a formspec string
  290. function gui.style(args)
  291. return "style" .. style_internal(args.selector, args.properties);
  292. end
  293. -- Create a formspec style
  294. --
  295. -- selector: A valid comma-separated list of type-based style selectors
  296. -- properties: A table of property names and values
  297. --
  298. -- Returns a formspec string
  299. function gui.style_type(args)
  300. return "style_type" .. style_internal(args.selector, args.properties);
  301. end
  302. -- Create a formspec tooltip element
  303. --
  304. -- text: The text of the tooltip
  305. -- (Optional) id: The ID of the element to display on
  306. --
  307. -- (Required when id == nil) x: The x position of the element
  308. -- (Required when id == nil) y: The y position of the element
  309. -- (Required when id == nil) w: The width of the element
  310. -- (Required when id == nil) h: The height of the element
  311. --
  312. -- Returns a formspec string
  313. function gui.tooltip(args)
  314. if args.id then
  315. return string.format("tooltip[%s;%s]", args.id, args.text);
  316. else
  317. return string.format("tooltip[%f,%f;%f,%f;%s]", args.x, args.y, args.w, args.h, args.text);
  318. end
  319. end
  320. return gui;