internal.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 page = 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. local item = {}
  189. for y = 0, ui_peruser.pagerows - 1 do
  190. for x = 0, ui_peruser.pagecols - 1 do
  191. local name = unified_inventory.filtered_items_list[player_name][list_index]
  192. local item = minetest.registered_items[name]
  193. if item then
  194. -- Clicked on current item: Flip crafting direction
  195. if name == unified_inventory.current_item[player_name] then
  196. local cdir = unified_inventory.current_craft_direction[player_name]
  197. if cdir == "recipe" then
  198. dir = "usage"
  199. elseif cdir == "usage" then
  200. dir = "recipe"
  201. end
  202. else
  203. -- Default: use active search direction by default
  204. dir = unified_inventory.active_search_direction[player_name]
  205. end
  206. local button_name = "item_button_" .. dir .. "_"
  207. .. unified_inventory.mangle_for_formspec(name)
  208. formspec[n] = ("item_image_button[%f,%f;.81,.81;%s;%s;]"):format(
  209. 8.2 + x * 0.7, ui_peruser.formspec_y + ui_peruser.page_y + y * 0.7,
  210. name, button_name
  211. )
  212. formspec[n + 1] = ("tooltip[%s;%s \\[%s\\]]"):format(
  213. button_name, minetest.formspec_escape(item.description),
  214. item.mod_origin or "??"
  215. )
  216. n = n + 2
  217. list_index = list_index + 1
  218. end
  219. end
  220. end
  221. formspec[n] = "label[8.2,"..ui_peruser.form_header_y..";"..F(S("Page")) .. ": "
  222. .. S("@1 of @2",page,pagemax).."]"
  223. end
  224. n= n+1
  225. if unified_inventory.activefilter[player_name] ~= "" then
  226. formspec[n] = "label[8.2,"..(ui_peruser.form_header_y + 0.4)..";" .. F(S("Filter")) .. ":]"
  227. formspec[n+1] = "label[9.1,"..(ui_peruser.form_header_y + 0.4)..";"..F(unified_inventory.activefilter[player_name]).."]"
  228. end
  229. return table.concat(formspec, "")
  230. end
  231. function unified_inventory.set_inventory_formspec(player, page)
  232. if player then
  233. player:set_inventory_formspec(unified_inventory.get_formspec(player, page))
  234. end
  235. end
  236. --apply filter to the inventory list (create filtered copy of full one)
  237. function unified_inventory.apply_filter(player, filter, search_dir)
  238. if not player then
  239. return false
  240. end
  241. local player_name = player:get_player_name()
  242. local lfilter = string.lower(filter)
  243. local ffilter
  244. if lfilter:sub(1, 6) == "group:" then
  245. local groups = lfilter:sub(7):split(",")
  246. ffilter = function(name, def)
  247. for _, group in ipairs(groups) do
  248. if not def.groups[group]
  249. or def.groups[group] <= 0 then
  250. return false
  251. end
  252. end
  253. return true
  254. end
  255. else
  256. ffilter = function(name, def)
  257. local lname = string.lower(name)
  258. local ldesc = string.lower(def.description)
  259. return string.find(lname, lfilter, 1, true) or string.find(ldesc, lfilter, 1, true)
  260. end
  261. end
  262. unified_inventory.filtered_items_list[player_name]={}
  263. for name, def in pairs(minetest.registered_items) do
  264. if (not def.groups.not_in_creative_inventory
  265. or def.groups.not_in_creative_inventory == 0)
  266. and def.description
  267. and def.description ~= ""
  268. and ffilter(name, def) then
  269. table.insert(unified_inventory.filtered_items_list[player_name], name)
  270. end
  271. end
  272. table.sort(unified_inventory.filtered_items_list[player_name])
  273. unified_inventory.filtered_items_list_size[player_name] = #unified_inventory.filtered_items_list[player_name]
  274. unified_inventory.current_index[player_name] = 1
  275. unified_inventory.activefilter[player_name] = filter
  276. unified_inventory.active_search_direction[player_name] = search_dir
  277. unified_inventory.set_inventory_formspec(player,
  278. unified_inventory.current_page[player_name])
  279. end
  280. function unified_inventory.items_in_group(groups)
  281. local items = {}
  282. for name, item in pairs(minetest.registered_items) do
  283. for _, group in pairs(groups:split(',')) do
  284. if item.groups[group] then
  285. table.insert(items, name)
  286. end
  287. end
  288. end
  289. return items
  290. end
  291. function unified_inventory.sort_inventory(inv)
  292. local inlist = inv:get_list("main")
  293. local typecnt = {}
  294. local typekeys = {}
  295. for _, st in ipairs(inlist) do
  296. if not st:is_empty() then
  297. local n = st:get_name()
  298. local w = st:get_wear()
  299. local m = st:get_metadata()
  300. local k = string.format("%s %05d %s", n, w, m)
  301. if not typecnt[k] then
  302. typecnt[k] = {
  303. name = n,
  304. wear = w,
  305. metadata = m,
  306. stack_max = st:get_stack_max(),
  307. count = 0,
  308. }
  309. table.insert(typekeys, k)
  310. end
  311. typecnt[k].count = typecnt[k].count + st:get_count()
  312. end
  313. end
  314. table.sort(typekeys)
  315. local outlist = {}
  316. for _, k in ipairs(typekeys) do
  317. local tc = typecnt[k]
  318. while tc.count > 0 do
  319. local c = math.min(tc.count, tc.stack_max)
  320. table.insert(outlist, ItemStack({
  321. name = tc.name,
  322. wear = tc.wear,
  323. metadata = tc.metadata,
  324. count = c,
  325. }))
  326. tc.count = tc.count - c
  327. end
  328. end
  329. if #outlist > #inlist then return end
  330. while #outlist < #inlist do
  331. table.insert(outlist, ItemStack(nil))
  332. end
  333. inv:set_list("main", outlist)
  334. end