internal.lua 11 KB

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