internal.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. function unified_inventory.get_formspec(player, page)
  2. if not player then
  3. return ""
  4. end
  5. local player_name = player:get_player_name()
  6. unified_inventory.current_page[player_name] = page
  7. local pagedef = unified_inventory.pages[page]
  8. local formspec = "size[14,10]"
  9. local fsdata = nil
  10. -- Background
  11. formspec = formspec .. "background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]"
  12. -- Current page
  13. if unified_inventory.pages[page] then
  14. fsdata = pagedef.get_formspec(player)
  15. formspec = formspec .. fsdata.formspec
  16. else
  17. return "" -- Invalid page name
  18. end
  19. -- Main buttons
  20. for i, def in pairs(unified_inventory.buttons) do
  21. if def.type == "image" then
  22. formspec = formspec.."image_button["
  23. ..(0.65 * (i - 1))..",9;0.8,0.8;"
  24. ..minetest.formspec_escape(def.image)..";"
  25. ..minetest.formspec_escape(def.name)..";]"
  26. end
  27. end
  28. if fsdata.draw_inventory ~= false then
  29. -- Player inventory
  30. formspec = formspec.."listcolors[#00000000;#00000000]"
  31. formspec = formspec .. "list[current_player;main;0,4.5;8,4;]"
  32. end
  33. if fsdata.draw_item_list == false then
  34. return formspec
  35. end
  36. -- Controls to flip items pages
  37. local start_x = 9.2
  38. formspec = formspec .. "image_button["..(start_x + 0.6 * 0)..",9;.8,.8;ui_skip_backward_icon.png;start_list;]"
  39. formspec = formspec .. "image_button["..(start_x + 0.6 * 1)..",9;.8,.8;ui_doubleleft_icon.png;rewind3;]"
  40. formspec = formspec .. "image_button["..(start_x + 0.6 * 2)..",9;.8,.8;ui_left_icon.png;rewind1;]"
  41. formspec = formspec .. "image_button["..(start_x + 0.6 * 3)..",9;.8,.8;ui_right_icon.png;forward1;]"
  42. formspec = formspec .. "image_button["..(start_x + 0.6 * 4)..",9;.8,.8;ui_doubleright_icon.png;forward3;]"
  43. formspec = formspec .. "image_button["..(start_x + 0.6 * 5)..",9;.8,.8;ui_skip_forward_icon.png;end_list;]"
  44. -- Search box
  45. formspec = formspec .. "field[9.5,8.325;3,1;searchbox;;]"
  46. formspec = formspec .. "image_button[12.2,8.1;.8,.8;ui_search_icon.png;searchbutton;]"
  47. -- Items list
  48. local list_index = unified_inventory.current_index[player_name]
  49. local page = math.floor(list_index / (80) + 1)
  50. local pagemax = math.floor(
  51. (#unified_inventory.filtered_items_list[player_name] - 1)
  52. / (80) + 1)
  53. local item = {}
  54. for y = 0, 9 do
  55. for x = 0, 7 do
  56. local name = unified_inventory.filtered_items_list[player_name][list_index]
  57. if minetest.registered_items[name] then
  58. formspec = formspec.."item_image_button["
  59. ..(8.2 + x * 0.7)..","
  60. ..(1 + y * 0.7)..";.81,.81;"
  61. ..name..";item_button_"
  62. ..name..";]"
  63. list_index = list_index + 1
  64. end
  65. end
  66. end
  67. formspec = formspec.."label[8.2,0;Page:]"
  68. formspec = formspec.."label[9,0;"..page.." of "..pagemax.."]"
  69. formspec = formspec.."label[8.2,0.4;Filter:]"
  70. formspec = formspec.."label[9,0.4;"..unified_inventory.activefilter[player_name].."]"
  71. return formspec
  72. end
  73. function unified_inventory.set_inventory_formspec(player, page)
  74. if player then
  75. local formspec = unified_inventory.get_formspec(player, page)
  76. player:set_inventory_formspec(formspec)
  77. end
  78. end
  79. --apply filter to the inventory list (create filtered copy of full one)
  80. function unified_inventory.apply_filter(player, filter)
  81. local player_name = player:get_player_name()
  82. local size = 0
  83. local lfilter = string.lower(filter)
  84. if not pcall(function() ("technic:test"):find(lfilter) end) then
  85. -- Filter is invalid
  86. lfilter = ""
  87. end
  88. unified_inventory.filtered_items_list[player_name]={}
  89. if lfilter:sub(1, 6) == "group:" then
  90. local groups = lfilter:sub(7):split(",")
  91. for name, def in pairs(minetest.registered_items) do
  92. if def.groups then
  93. local all = true
  94. for _, group in ipairs(groups) do
  95. if not (def.groups[group] and (def.groups[group] > 0)) then
  96. all = false
  97. break
  98. end
  99. end
  100. if all then
  101. table.insert(unified_inventory.filtered_items_list[player_name], name)
  102. size = size + 1
  103. end
  104. end
  105. end
  106. else
  107. for name, def in pairs(minetest.registered_items) do
  108. if (not def.groups.not_in_creative_inventory or
  109. def.groups.not_in_creative_inventory == 0)
  110. and def.description and def.description ~= "" then
  111. local lname = string.lower(name)
  112. local ldesc = string.lower(def.description)
  113. if string.find(lname, lfilter) or string.find(ldesc, lfilter) then
  114. table.insert(unified_inventory.filtered_items_list[player_name], name)
  115. size = size + 1
  116. end
  117. end
  118. end
  119. end
  120. table.sort(unified_inventory.filtered_items_list[player_name])
  121. unified_inventory.filtered_items_list_size[player_name] = size
  122. unified_inventory.current_index[player_name] = 1
  123. unified_inventory.activefilter[player_name] = filter
  124. unified_inventory.set_inventory_formspec(player,
  125. unified_inventory.current_page[player_name])
  126. end
  127. function unified_inventory.items_in_group(groups)
  128. local items = {}
  129. for name, item in pairs(minetest.registered_items) do
  130. for _, group in pairs(groups:split(',')) do
  131. if item.groups[group] then
  132. table.insert(items, name)
  133. end
  134. end
  135. end
  136. return items
  137. end