internal.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. local S = unified_inventory.gettext
  2. local F = unified_inventory.fgettext
  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. local formspec = {
  52. "size[14,10]",
  53. "background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]" -- Background
  54. }
  55. local n = 3
  56. if draw_lite_mode then
  57. formspec[1] = "size[11,7.7]"
  58. formspec[2] = "background[-0.19,-0.2;11.4,8.4;ui_form_bg.png]"
  59. end
  60. if unified_inventory.is_creative(player_name)
  61. and page == "craft" then
  62. formspec[n] = "background[0,"..(ui_peruser.formspec_y + 2)..";1,1;ui_single_slot.png]"
  63. n = n+1
  64. end
  65. -- Current page
  66. if not unified_inventory.pages[page] then
  67. return "" -- Invalid page name
  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] = minetest.formspec_escape(def.image)..";"
  93. formspec[n+4] = minetest.formspec_escape(def.name)..";]"
  94. formspec[n+5] = "tooltip["..minetest.formspec_escape(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] = minetest.formspec_escape(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("First page") .. "]"
  122. .. "image_button[" .. (start_x + 0.6 * 1)
  123. .. ",9;.8,.8;ui_doubleleft_icon.png;rewind3;]"
  124. .. "tooltip[rewind3;" .. F("Back three pages") .. "]"
  125. .. "image_button[" .. (start_x + 0.6 * 2)
  126. .. ",9;.8,.8;ui_left_icon.png;rewind1;]"
  127. .. "tooltip[rewind1;" .. F("Back one page") .. "]"
  128. .. "image_button[" .. (start_x + 0.6 * 3)
  129. .. ",9;.8,.8;ui_right_icon.png;forward1;]"
  130. .. "tooltip[forward1;" .. F("Forward one page") .. "]"
  131. .. "image_button[" .. (start_x + 0.6 * 4)
  132. .. ",9;.8,.8;ui_doubleright_icon.png;forward3;]"
  133. .. "tooltip[forward3;" .. F("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("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("First page") .. "]"
  142. .. "image_button[" .. (8.2 + 0.65 * 1)
  143. .. ",5.8;.8,.8;ui_left_icon.png;rewind1;]"
  144. .. "tooltip[rewind1;" .. F("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("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("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. .. minetest.formspec_escape(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("Search") .. "]"
  161. formspec[n+2] = "image_button[12.9,8.1;.8,.8;ui_reset_icon.png;searchresetbutton;]"
  162. .. "tooltip[searchbutton;" ..F("Search") .. "]"
  163. .. "tooltip[searchresetbutton;" ..F("Reset search and display everything") .. "]"
  164. else
  165. formspec[n] = "field[8.5,5.225;2.2,1;searchbox;;"
  166. .. minetest.formspec_escape(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("Search") .. "]"
  169. formspec[n+2] = "image_button[11,5;.8,.8;ui_reset_icon.png;searchresetbutton;]"
  170. .. "tooltip[searchbutton;" ..F("Search") .. "]"
  171. .. "tooltip[searchresetbutton;" ..F("Reset search and display everything") .. "]"
  172. end
  173. n = n+3
  174. local no_matches = "No matching items"
  175. if draw_lite_mode then
  176. no_matches = "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. if minetest.registered_items[name] 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. formspec[n] = "item_image_button["
  206. ..(8.2 + x * 0.7)..","
  207. ..(ui_peruser.formspec_y + ui_peruser.page_y + y * 0.7)..";.81,.81;"
  208. ..name..";item_button_"..dir.."_"
  209. ..unified_inventory.mangle_for_formspec(name)..";]"
  210. n = n+1
  211. list_index = list_index + 1
  212. end
  213. end
  214. end
  215. formspec[n] = "label[8.2,"..ui_peruser.form_header_y..";"..F("Page") .. ": "
  216. .. S("%s of %s"):format(page,pagemax).."]"
  217. end
  218. n= n+1
  219. if unified_inventory.activefilter[player_name] ~= "" then
  220. formspec[n] = "label[8.2,"..(ui_peruser.form_header_y + 0.4)..";" .. F("Filter") .. ":]"
  221. formspec[n+1] = "label[9.1,"..(ui_peruser.form_header_y + 0.4)..";"..minetest.formspec_escape(unified_inventory.activefilter[player_name]).."]"
  222. end
  223. return table.concat(formspec, "")
  224. end
  225. function unified_inventory.set_inventory_formspec(player, page)
  226. if player then
  227. player:set_inventory_formspec(unified_inventory.get_formspec(player, page))
  228. end
  229. end
  230. --apply filter to the inventory list (create filtered copy of full one)
  231. function unified_inventory.apply_filter(player, filter, search_dir)
  232. if not player then
  233. return false
  234. end
  235. local player_name = player:get_player_name()
  236. local lfilter = string.lower(filter)
  237. local ffilter
  238. if lfilter:sub(1, 6) == "group:" then
  239. local groups = lfilter:sub(7):split(",")
  240. ffilter = function(name, def)
  241. for _, group in ipairs(groups) do
  242. if not def.groups[group]
  243. or def.groups[group] <= 0 then
  244. return false
  245. end
  246. end
  247. return true
  248. end
  249. else
  250. ffilter = function(name, def)
  251. local lname = string.lower(name)
  252. local ldesc = string.lower(def.description)
  253. return string.find(lname, lfilter, 1, true) or string.find(ldesc, lfilter, 1, true)
  254. end
  255. end
  256. unified_inventory.filtered_items_list[player_name]={}
  257. for name, def in pairs(minetest.registered_items) do
  258. if (not def.groups.not_in_creative_inventory
  259. or def.groups.not_in_creative_inventory == 0)
  260. and def.description
  261. and def.description ~= ""
  262. and ffilter(name, def)
  263. and (unified_inventory.is_creative(player_name)
  264. or unified_inventory.crafts_for.recipe[def.name]) then
  265. table.insert(unified_inventory.filtered_items_list[player_name], name)
  266. end
  267. end
  268. table.sort(unified_inventory.filtered_items_list[player_name])
  269. unified_inventory.filtered_items_list_size[player_name] = #unified_inventory.filtered_items_list[player_name]
  270. unified_inventory.current_index[player_name] = 1
  271. unified_inventory.activefilter[player_name] = filter
  272. unified_inventory.active_search_direction[player_name] = search_dir
  273. unified_inventory.set_inventory_formspec(player,
  274. unified_inventory.current_page[player_name])
  275. end
  276. function unified_inventory.items_in_group(groups)
  277. local items = {}
  278. for name, item in pairs(minetest.registered_items) do
  279. for _, group in pairs(groups:split(',')) do
  280. if item.groups[group] then
  281. table.insert(items, name)
  282. end
  283. end
  284. end
  285. return items
  286. end
  287. function unified_inventory.sort_inventory(inv)
  288. local inlist = inv:get_list("main")
  289. local typecnt = {}
  290. local typekeys = {}
  291. for _, st in ipairs(inlist) do
  292. if not st:is_empty() then
  293. local n = st:get_name()
  294. local w = st:get_wear()
  295. local m = st:get_metadata()
  296. local k = string.format("%s %05d %s", n, w, m)
  297. if not typecnt[k] then
  298. typecnt[k] = {
  299. name = n,
  300. wear = w,
  301. metadata = m,
  302. stack_max = st:get_stack_max(),
  303. count = 0,
  304. }
  305. table.insert(typekeys, k)
  306. end
  307. typecnt[k].count = typecnt[k].count + st:get_count()
  308. end
  309. end
  310. table.sort(typekeys)
  311. local outlist = {}
  312. for _, k in ipairs(typekeys) do
  313. local tc = typecnt[k]
  314. while tc.count > 0 do
  315. local c = math.min(tc.count, tc.stack_max)
  316. table.insert(outlist, ItemStack({
  317. name = tc.name,
  318. wear = tc.wear,
  319. metadata = tc.metadata,
  320. count = c,
  321. }))
  322. tc.count = tc.count - c
  323. end
  324. end
  325. if #outlist > #inlist then return end
  326. while #outlist < #inlist do
  327. table.insert(outlist, ItemStack(nil))
  328. end
  329. inv:set_list("main", outlist)
  330. end