An extensible inventory mod which allows searching crafting and browsing for recipes in the same dialogue. https://content.minetest.net/packages/RealBadAngel/unified_inventory/
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.

374 lines
12KB

  1. local S = minetest.get_translator("unified_inventory")
  2. local F = minetest.formspec_escape
  3. -- This pair of encoding functions is used where variable text must go in
  4. -- button names, where the text might contain formspec metacharacters.
  5. -- We can escape button names for the formspec, to avoid screwing up
  6. -- form structure overall, but they then don't get de-escaped, and so
  7. -- the input we get back from the button contains the formspec escaping.
  8. -- This is a game engine bug, and in the anticipation that it might be
  9. -- fixed some day we don't want to rely on it. So for safety we apply
  10. -- an encoding that avoids all formspec metacharacters.
  11. function unified_inventory.mangle_for_formspec(str)
  12. return string.gsub(str, "([^A-Za-z0-9])", function (c) return string.format("_%d_", string.byte(c)) end)
  13. end
  14. function unified_inventory.demangle_for_formspec(str)
  15. return string.gsub(str, "_([0-9]+)_", function (v) return string.char(v) end)
  16. end
  17. function unified_inventory.get_per_player_formspec(player_name)
  18. local lite = unified_inventory.lite_mode and not minetest.check_player_privs(player_name, {ui_full=true})
  19. local ui = {}
  20. ui.pagecols = unified_inventory.pagecols
  21. ui.pagerows = unified_inventory.pagerows
  22. ui.page_y = unified_inventory.page_y
  23. ui.formspec_y = unified_inventory.formspec_y
  24. ui.main_button_x = unified_inventory.main_button_x
  25. ui.main_button_y = unified_inventory.main_button_y
  26. ui.craft_result_x = unified_inventory.craft_result_x
  27. ui.craft_result_y = unified_inventory.craft_result_y
  28. ui.form_header_y = unified_inventory.form_header_y
  29. if lite then
  30. ui.pagecols = 4
  31. ui.pagerows = 6
  32. ui.page_y = 0.25
  33. ui.formspec_y = 0.47
  34. ui.main_button_x = 8.2
  35. ui.main_button_y = 6.5
  36. ui.craft_result_x = 2.8
  37. ui.craft_result_y = 3.4
  38. ui.form_header_y = -0.1
  39. end
  40. ui.items_per_page = ui.pagecols * ui.pagerows
  41. return ui, lite
  42. end
  43. function unified_inventory.get_formspec(player, page)
  44. if not player then
  45. return ""
  46. end
  47. local player_name = player:get_player_name()
  48. local ui_peruser,draw_lite_mode = unified_inventory.get_per_player_formspec(player_name)
  49. unified_inventory.current_page[player_name] = page
  50. local pagedef = unified_inventory.pages[page]
  51. if not pagedef then
  52. return "" -- Invalid page name
  53. end
  54. local formspec = {
  55. "size[14,10]",
  56. pagedef.formspec_prepend and "" or "no_prepend[]",
  57. "background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]" -- Background
  58. }
  59. local n = 4
  60. if draw_lite_mode then
  61. formspec[1] = "size[11,7.7]"
  62. formspec[3] = "background[-0.19,-0.2;11.4,8.4;ui_form_bg.png]"
  63. end
  64. if unified_inventory.is_creative(player_name)
  65. and page == "craft" then
  66. formspec[n] = "background[0,"..(ui_peruser.formspec_y + 2)..";1,1;ui_single_slot.png]"
  67. n = n+1
  68. end
  69. local perplayer_formspec = unified_inventory.get_per_player_formspec(player_name)
  70. local fsdata = pagedef.get_formspec(player, perplayer_formspec)
  71. formspec[n] = fsdata.formspec
  72. n = n+1
  73. local button_row = 0
  74. local button_col = 0
  75. -- Main buttons
  76. local filtered_inv_buttons = {}
  77. for i, def in pairs(unified_inventory.buttons) do
  78. if not (draw_lite_mode and def.hide_lite) then
  79. table.insert(filtered_inv_buttons, def)
  80. end
  81. end
  82. for i, def in pairs(filtered_inv_buttons) do
  83. if draw_lite_mode and i > 4 then
  84. button_row = 1
  85. button_col = 1
  86. end
  87. if def.type == "image" then
  88. if (def.condition == nil or def.condition(player) == true) then
  89. formspec[n] = "image_button["
  90. formspec[n+1] = ( ui_peruser.main_button_x + 0.65 * (i - 1) - button_col * 0.65 * 4)
  91. formspec[n+2] = ","..(ui_peruser.main_button_y + button_row * 0.7)..";0.8,0.8;"
  92. formspec[n+3] = F(def.image)..";"
  93. formspec[n+4] = F(def.name)..";]"
  94. formspec[n+5] = "tooltip["..F(def.name)
  95. formspec[n+6] = ";"..(def.tooltip or "").."]"
  96. n = n+7
  97. else
  98. formspec[n] = "image["
  99. formspec[n+1] = ( ui_peruser.main_button_x + 0.65 * (i - 1) - button_col * 0.65 * 4)
  100. formspec[n+2] = ","..(ui_peruser.main_button_y + button_row * 0.7)..";0.8,0.8;"
  101. formspec[n+3] = F(def.image).."^[colorize:#808080:alpha]"
  102. n = n+4
  103. end
  104. end
  105. end
  106. if fsdata.draw_inventory ~= false then
  107. -- Player inventory
  108. formspec[n] = "listcolors[#00000000;#00000000]"
  109. formspec[n+1] = "list[current_player;main;0,"..(ui_peruser.formspec_y + 3.5)..";8,4;]"
  110. n = n+2
  111. end
  112. if fsdata.draw_item_list == false then
  113. return table.concat(formspec, "")
  114. end
  115. -- Controls to flip items pages
  116. local start_x = 9.2
  117. if not draw_lite_mode then
  118. formspec[n] =
  119. "image_button[" .. (start_x + 0.6 * 0)
  120. .. ",9;.8,.8;ui_skip_backward_icon.png;start_list;]"
  121. .. "tooltip[start_list;" .. F(S("First page")) .. "]"
  122. .. "image_button[" .. (start_x + 0.6 * 1)
  123. .. ",9;.8,.8;ui_doubleleft_icon.png;rewind3;]"
  124. .. "tooltip[rewind3;" .. F(S("Back three pages")) .. "]"
  125. .. "image_button[" .. (start_x + 0.6 * 2)
  126. .. ",9;.8,.8;ui_left_icon.png;rewind1;]"
  127. .. "tooltip[rewind1;" .. F(S("Back one page")) .. "]"
  128. .. "image_button[" .. (start_x + 0.6 * 3)
  129. .. ",9;.8,.8;ui_right_icon.png;forward1;]"
  130. .. "tooltip[forward1;" .. F(S("Forward one page")) .. "]"
  131. .. "image_button[" .. (start_x + 0.6 * 4)
  132. .. ",9;.8,.8;ui_doubleright_icon.png;forward3;]"
  133. .. "tooltip[forward3;" .. F(S("Forward three pages")) .. "]"
  134. .. "image_button[" .. (start_x + 0.6 * 5)
  135. .. ",9;.8,.8;ui_skip_forward_icon.png;end_list;]"
  136. .. "tooltip[end_list;" .. F(S("Last page")) .. "]"
  137. else
  138. formspec[n] =
  139. "image_button[" .. (8.2 + 0.65 * 0)
  140. .. ",5.8;.8,.8;ui_skip_backward_icon.png;start_list;]"
  141. .. "tooltip[start_list;" .. F(S("First page")) .. "]"
  142. .. "image_button[" .. (8.2 + 0.65 * 1)
  143. .. ",5.8;.8,.8;ui_left_icon.png;rewind1;]"
  144. .. "tooltip[rewind1;" .. F(S("Back one page")) .. "]"
  145. .. "image_button[" .. (8.2 + 0.65 * 2)
  146. .. ",5.8;.8,.8;ui_right_icon.png;forward1;]"
  147. .. "tooltip[forward1;" .. F(S("Forward one page")) .. "]"
  148. .. "image_button[" .. (8.2 + 0.65 * 3)
  149. .. ",5.8;.8,.8;ui_skip_forward_icon.png;end_list;]"
  150. .. "tooltip[end_list;" .. F(S("Last page")) .. "]"
  151. end
  152. n = n+1
  153. -- Search box
  154. formspec[n] = "field_close_on_enter[searchbox;false]"
  155. n = n+1
  156. if not draw_lite_mode then
  157. formspec[n] = "field[9.5,8.325;3,1;searchbox;;"
  158. .. F(unified_inventory.current_searchbox[player_name]) .. "]"
  159. formspec[n+1] = "image_button[12.2,8.1;.8,.8;ui_search_icon.png;searchbutton;]"
  160. .. "tooltip[searchbutton;" ..F(S("Search")) .. "]"
  161. formspec[n+2] = "image_button[12.9,8.1;.8,.8;ui_reset_icon.png;searchresetbutton;]"
  162. .. "tooltip[searchbutton;" ..F(S("Search")) .. "]"
  163. .. "tooltip[searchresetbutton;" ..F(S("Reset search and display everything")) .. "]"
  164. else
  165. formspec[n] = "field[8.5,5.225;2.2,1;searchbox;;"
  166. .. F(unified_inventory.current_searchbox[player_name]) .. "]"
  167. formspec[n+1] = "image_button[10.3,5;.8,.8;ui_search_icon.png;searchbutton;]"
  168. .. "tooltip[searchbutton;" ..F(S("Search")) .. "]"
  169. formspec[n+2] = "image_button[11,5;.8,.8;ui_reset_icon.png;searchresetbutton;]"
  170. .. "tooltip[searchbutton;" ..F(S("Search")) .. "]"
  171. .. "tooltip[searchresetbutton;" ..F(S("Reset search and display everything")) .. "]"
  172. end
  173. n = n+3
  174. local no_matches = S("No matching items")
  175. if draw_lite_mode then
  176. no_matches = S("No matches.")
  177. end
  178. -- Items list
  179. if #unified_inventory.filtered_items_list[player_name] == 0 then
  180. formspec[n] = "label[8.2,"..ui_peruser.form_header_y..";" .. F(no_matches) .. "]"
  181. else
  182. local dir = unified_inventory.active_search_direction[player_name]
  183. local list_index = unified_inventory.current_index[player_name]
  184. local page2 = math.floor(list_index / (ui_peruser.items_per_page) + 1)
  185. local pagemax = math.floor(
  186. (#unified_inventory.filtered_items_list[player_name] - 1)
  187. / (ui_peruser.items_per_page) + 1)
  188. for y = 0, ui_peruser.pagerows - 1 do
  189. for x = 0, ui_peruser.pagecols - 1 do
  190. local name = unified_inventory.filtered_items_list[player_name][list_index]
  191. local item = minetest.registered_items[name]
  192. if item then
  193. -- Clicked on current item: Flip crafting direction
  194. if name == unified_inventory.current_item[player_name] then
  195. local cdir = unified_inventory.current_craft_direction[player_name]
  196. if cdir == "recipe" then
  197. dir = "usage"
  198. elseif cdir == "usage" then
  199. dir = "recipe"
  200. end
  201. else
  202. -- Default: use active search direction by default
  203. dir = unified_inventory.active_search_direction[player_name]
  204. end
  205. local button_name = "item_button_" .. dir .. "_"
  206. .. unified_inventory.mangle_for_formspec(name)
  207. formspec[n] = ("item_image_button[%f,%f;.81,.81;%s;%s;]"):format(
  208. 8.2 + x * 0.7, ui_peruser.formspec_y + ui_peruser.page_y + y * 0.7,
  209. name, button_name
  210. )
  211. formspec[n + 1] = ("tooltip[%s;%s \\[%s\\]]"):format(
  212. button_name, minetest.formspec_escape(item.description),
  213. item.mod_origin or "??"
  214. )
  215. n = n + 2
  216. list_index = list_index + 1
  217. end
  218. end
  219. end
  220. formspec[n] = "label[8.2,"..ui_peruser.form_header_y..";"..F(S("Page")) .. ": "
  221. .. S("@1 of @2",page2,pagemax).."]"
  222. end
  223. n= n+1
  224. if unified_inventory.activefilter[player_name] ~= "" then
  225. formspec[n] = "label[8.2,"..(ui_peruser.form_header_y + 0.4)..";" .. F(S("Filter")) .. ":]"
  226. formspec[n+1] = "label[9.1,"..(ui_peruser.form_header_y + 0.4)..";"..F(unified_inventory.activefilter[player_name]).."]"
  227. end
  228. return table.concat(formspec, "")
  229. end
  230. function unified_inventory.set_inventory_formspec(player, page)
  231. if player then
  232. player:set_inventory_formspec(unified_inventory.get_formspec(player, page))
  233. end
  234. end
  235. --apply filter to the inventory list (create filtered copy of full one)
  236. function unified_inventory.apply_filter(player, filter, search_dir)
  237. if not player then
  238. return false
  239. end
  240. local player_name = player:get_player_name()
  241. local lfilter = string.lower(filter)
  242. local ffilter
  243. if lfilter:sub(1, 6) == "group:" then
  244. local groups = lfilter:sub(7):split(",")
  245. ffilter = function(name, def)
  246. for _, group in ipairs(groups) do
  247. if not def.groups[group]
  248. or def.groups[group] <= 0 then
  249. return false
  250. end
  251. end
  252. return true
  253. end
  254. else
  255. ffilter = function(name, def)
  256. local lname = string.lower(name)
  257. local ldesc = string.lower(def.description)
  258. return string.find(lname, lfilter, 1, true) or string.find(ldesc, lfilter, 1, true)
  259. end
  260. end
  261. unified_inventory.filtered_items_list[player_name]={}
  262. for name, def in pairs(minetest.registered_items) do
  263. if (not def.groups.not_in_creative_inventory
  264. or def.groups.not_in_creative_inventory == 0)
  265. and def.description
  266. and def.description ~= ""
  267. and ffilter(name, def) then
  268. table.insert(unified_inventory.filtered_items_list[player_name], name)
  269. end
  270. end
  271. table.sort(unified_inventory.filtered_items_list[player_name])
  272. unified_inventory.filtered_items_list_size[player_name] = #unified_inventory.filtered_items_list[player_name]
  273. unified_inventory.current_index[player_name] = 1
  274. unified_inventory.activefilter[player_name] = filter
  275. unified_inventory.active_search_direction[player_name] = search_dir
  276. unified_inventory.set_inventory_formspec(player,
  277. unified_inventory.current_page[player_name])
  278. end
  279. function unified_inventory.items_in_group(groups)
  280. local items = {}
  281. for name, item in pairs(minetest.registered_items) do
  282. for _, group in pairs(groups:split(',')) do
  283. if item.groups[group] then
  284. table.insert(items, name)
  285. end
  286. end
  287. end
  288. return items
  289. end
  290. function unified_inventory.sort_inventory(inv)
  291. local inlist = inv:get_list("main")
  292. local typecnt = {}
  293. local typekeys = {}
  294. for _, st in ipairs(inlist) do
  295. if not st:is_empty() then
  296. local n = st:get_name()
  297. local w = st:get_wear()
  298. local m = st:get_metadata()
  299. local k = string.format("%s %05d %s", n, w, m)
  300. if not typecnt[k] then
  301. typecnt[k] = {
  302. name = n,
  303. wear = w,
  304. metadata = m,
  305. stack_max = st:get_stack_max(),
  306. count = 0,
  307. }
  308. table.insert(typekeys, k)
  309. end
  310. typecnt[k].count = typecnt[k].count + st:get_count()
  311. end
  312. end
  313. table.sort(typekeys)
  314. local outlist = {}
  315. for _, k in ipairs(typekeys) do
  316. local tc = typecnt[k]
  317. while tc.count > 0 do
  318. local c = math.min(tc.count, tc.stack_max)
  319. table.insert(outlist, ItemStack({
  320. name = tc.name,
  321. wear = tc.wear,
  322. metadata = tc.metadata,
  323. count = c,
  324. }))
  325. tc.count = tc.count - c
  326. end
  327. end
  328. if #outlist > #inlist then return end
  329. while #outlist < #inlist do
  330. table.insert(outlist, ItemStack(nil))
  331. end
  332. inv:set_list("main", outlist)
  333. end