gui.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. -- Crafting Mod - semi-realistic crafting in minetest
  2. -- Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
  3. --
  4. -- This library is free software; you can redistribute it and/or
  5. -- modify it under the terms of the GNU Lesser General Public
  6. -- License as published by the Free Software Foundation; either
  7. -- version 2.1 of the License, or (at your option) any later version.
  8. --
  9. -- This library 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 GNU
  12. -- Lesser General Public License for more details.
  13. --
  14. -- You should have received a copy of the GNU Lesser General Public
  15. -- License along with this library; if not, write to the Free Software
  16. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. local S = minetest.get_translator("crafting")
  18. local function get_item_description(name)
  19. if name:sub(1, 6) == "group:" then
  20. local group = name:sub(7, #name):gsub("%_", " ")
  21. return S("Any") .. " " .. S(group)
  22. else
  23. local def = minetest.registered_items[name] or {}
  24. return def.description or name
  25. end
  26. end
  27. function crafting.make_result_selector(player, type, level, size, context)
  28. local page = context.crafting_page or 1
  29. local full_recipes = crafting.get_all_for_player(player, type, level)
  30. local recipes
  31. if context.crafting_query then
  32. recipes = {}
  33. for i = 1, #full_recipes do
  34. local output = full_recipes[i].recipe.output
  35. local desc = get_item_description(output):lower()
  36. if string.find(output, context.crafting_query, 1, true) or
  37. string.find(desc, context.crafting_query, 1, true) then
  38. recipes[#recipes + 1] = full_recipes[i]
  39. end
  40. end
  41. else
  42. recipes = full_recipes
  43. end
  44. local num_per_page = size.x * size.y
  45. local max_pages = math.floor(0.999 + #recipes / num_per_page)
  46. if page > max_pages or page < 1 then
  47. page = ((page - 1) % max_pages) + 1
  48. context.crafting_page = page
  49. end
  50. local start_i = (page - 1) * num_per_page + 1
  51. local formspec = {}
  52. formspec[#formspec + 1] = "container["
  53. formspec[#formspec + 1] = tostring(size.x)
  54. formspec[#formspec + 1] = ","
  55. formspec[#formspec + 1] = tostring(size.y)
  56. formspec[#formspec + 1] = "]"
  57. formspec[#formspec + 1] = "field_close_on_enter[query;false]"
  58. formspec[#formspec + 1] = "field[-4.75,0.81;3,0.8;query;;"
  59. formspec[#formspec + 1] = context.crafting_query
  60. formspec[#formspec + 1] = "]button[-2.2,0.5;0.8,0.8;search;?]"
  61. formspec[#formspec + 1] = "button[-1.4,0.5;0.8,0.8;prev;<]"
  62. formspec[#formspec + 1] = "button[-0.8,0.5;0.8,0.8;next;>]"
  63. formspec[#formspec + 1] = "container_end[]"
  64. formspec[#formspec + 1] = "label[0,-0.25;"
  65. formspec[#formspec + 1] = minetest.formspec_escape(S("Page") .. ": " ..
  66. page .. "/" .. max_pages ..
  67. " | " .. S("Unlocked") .. ": " .. #full_recipes .. " / " .. #crafting.recipes[type])
  68. formspec[#formspec + 1] = "]"
  69. local x = 0
  70. local y = 0
  71. local y_offset = 0.2
  72. for i = start_i, math.min(#recipes, start_i * num_per_page) do
  73. local result = recipes[i]
  74. local recipe = result.recipe
  75. local itemname = ItemStack(recipe.output):get_name()
  76. local item_description = get_item_description(itemname)
  77. formspec[#formspec + 1] = "item_image_button["
  78. formspec[#formspec + 1] = x
  79. formspec[#formspec + 1] = ","
  80. formspec[#formspec + 1] = y + y_offset
  81. formspec[#formspec + 1] = ";1,1;"
  82. formspec[#formspec + 1] = recipe.output
  83. formspec[#formspec + 1] = ";result_"
  84. formspec[#formspec + 1] = tostring(recipe.id)
  85. formspec[#formspec + 1] = ";]"
  86. formspec[#formspec + 1] = "tooltip[result_"
  87. formspec[#formspec + 1] = tostring(recipe.id)
  88. formspec[#formspec + 1] = ";"
  89. formspec[#formspec + 1] = minetest.formspec_escape(item_description .. "\n")
  90. for j, item in pairs(result.items) do
  91. local color = item.have >= item.need and "#6f6" or "#f66"
  92. local itemtab = {
  93. "\n",
  94. minetest.get_color_escape_sequence(color),
  95. get_item_description(item.name), ": ",
  96. item.have, "/", item.need
  97. }
  98. formspec[#formspec + 1] = minetest.formspec_escape(table.concat(itemtab, ""))
  99. end
  100. formspec[#formspec + 1] = minetest.get_color_escape_sequence("#ffffff")
  101. formspec[#formspec + 1] = "]"
  102. formspec[#formspec + 1] = "image["
  103. formspec[#formspec + 1] = x
  104. formspec[#formspec + 1] = ","
  105. formspec[#formspec + 1] = y + y_offset
  106. if result.craftable then
  107. formspec[#formspec + 1] = ";1,1;crafting_slot_craftable.png]"
  108. else
  109. formspec[#formspec + 1] = ";1,1;crafting_slot_uncraftable.png]"
  110. end
  111. x = x + 1
  112. if x == size.x then
  113. x = 0
  114. y = y + 1
  115. end
  116. if y == size.y then
  117. break
  118. end
  119. end
  120. while y < size.y do
  121. x = 0
  122. y = y + 1
  123. end
  124. return table.concat(formspec, "")
  125. end
  126. function crafting.result_select_on_receive_results(player, type, level, context, fields)
  127. if fields.prev then
  128. context.crafting_page = (context.crafting_page or 1) - 1
  129. return true
  130. elseif fields.next then
  131. context.crafting_page = (context.crafting_page or 1) + 1
  132. return true
  133. elseif fields.search or fields.key_enter_field == "query" then
  134. context.crafting_query = fields.query:trim():lower()
  135. context.crafting_page = 1
  136. if context.crafting_query == "" then
  137. context.crafting_query = nil
  138. end
  139. return true
  140. end
  141. for key, value in pairs(fields) do
  142. if key:sub(1, 7) == "result_" then
  143. local num = string.match(key, "result_([0-9]+)")
  144. if num then
  145. local inv = player:get_inventory()
  146. local recipe = crafting.get_recipe(tonumber(num))
  147. local name = player:get_player_name()
  148. if not crafting.can_craft(name, type, level, recipe) then
  149. minetest.log("error", "[crafting] Player clicked a button they shouldn't have been able to")
  150. return true
  151. elseif crafting.perform_craft(name, inv, "main", "main", recipe) then
  152. return true -- crafted
  153. else
  154. minetest.chat_send_player(name, S("Missing required items!"))
  155. return false
  156. end
  157. end
  158. end
  159. end
  160. end
  161. if minetest.global_exists("sfinv") then
  162. sfinv.override_page("sfinv:crafting", {
  163. get = function(self, player, context)
  164. local formspec = crafting.make_result_selector(player, "inv", 1, { x = 8, y = 3 }, context)
  165. formspec = formspec .. "list[detached:creative_trash;main;0,3.4;1,1;]" ..
  166. "image[0.05,3.5;0.8,0.8;creative_trash_icon.png]"
  167. return sfinv.make_formspec(player, context, formspec, true)
  168. end,
  169. on_player_receive_fields = function(self, player, context, fields)
  170. if crafting.result_select_on_receive_results(player, "inv", 1, context, fields) then
  171. sfinv.set_player_inventory_formspec(player)
  172. end
  173. return true
  174. end
  175. })
  176. end
  177. local node_fs_context = {}
  178. local node_serial = 0
  179. function crafting.make_on_rightclick(type, level, inv_size)
  180. node_serial = node_serial + 1
  181. local formname = "crafting:node_" .. node_serial
  182. local function show(player, context)
  183. local formspec = crafting.make_result_selector(player, type, level, inv_size, context)
  184. formspec = "size[" .. inv_size.x .. "," .. (inv_size.y + 5.6) ..
  185. "]list[current_player;main;0," .. (inv_size.y + 1.7) ..";8,1;]" ..
  186. "list[current_player;main;0," .. (inv_size.y + 2.85) ..";8,3;8]" .. formspec
  187. minetest.show_formspec(player:get_player_name(), formname, formspec)
  188. end
  189. minetest.register_on_player_receive_fields(function(player, _formname, fields)
  190. if formname ~= _formname then
  191. return
  192. end
  193. local context = node_fs_context[player:get_player_name()]
  194. if not context then
  195. return false
  196. end
  197. if crafting.result_select_on_receive_results(player, type, level, context, fields) then
  198. show(player, context)
  199. end
  200. return true
  201. end)
  202. return function(pos, node, player)
  203. local name = player:get_player_name()
  204. local context = node_fs_context[name] or {}
  205. node_fs_context[name] = context
  206. context.pos = vector.new(pos)
  207. context.type = type
  208. context.level = level
  209. show(player, context)
  210. end
  211. end