diff --git a/api.lua b/api.lua index 8cc5532..64efe0b 100644 --- a/api.lua +++ b/api.lua @@ -43,7 +43,7 @@ minetest.after(0.01, function() end table.sort(ui.items_list) ui.items_list_size = #ui.items_list - print("Unified Inventory. inventory size: "..ui.items_list_size) + print("Unified Inventory. Inventory size: "..ui.items_list_size) for _, name in ipairs(ui.items_list) do local def = minetest.registered_items[name] -- Simple drops @@ -133,19 +133,61 @@ minetest.after(0.01, function() end end end + + -- Step 1: group-indexed lookup table for items + local spec_matcher = {} + for _, name in ipairs(ui.items_list) do + -- we only need to care about groups, exact items are handled separately + for group, value in pairs(minetest.registered_items[name].groups) do + if value and value ~= 0 then + if not spec_matcher[group] then + spec_matcher[group] = {} + end + spec_matcher[group][name] = true + end + end + end + + -- Step 2: Find all matching items for the given spec (groups) + local function get_matching_spec_items(specname) + if specname:sub(1,6) ~= "group:" then + return { specname } + end + + local accepted = {} + for i, group in ipairs(specname:sub(7):split(",")) do + if i == 1 then + -- First step: Copy all possible item names in this group + for name, _ in pairs(spec_matcher[group] or {}) do + accepted[name] = true + end + else + -- Perform filtering + if spec_matcher[group] then + for name, _ in pairs(accepted) do + accepted[name] = spec_matcher[group][name] + end + else + -- No matching items + return {} + end + end + end + return accepted + end + for _, recipes in pairs(ui.crafts_for.recipe) do for _, recipe in ipairs(recipes) do local ingredient_items = {} for _, spec in pairs(recipe.items) do - local matches_spec = ui.canonical_item_spec_matcher(spec) - for _, name in ipairs(ui.items_list) do - if matches_spec(name) then - ingredient_items[name] = true - end + -- Get items that fit into this spec (group or item name) + local specname = ItemStack(spec):get_name() + for item_name, _ in pairs(get_matching_spec_items(specname)) do + ingredient_items[item_name] = true end end for name, _ in pairs(ingredient_items) do - if ui.crafts_for.usage[name] == nil then + if not ui.crafts_for.usage[name] then ui.crafts_for.usage[name] = {} end table.insert(ui.crafts_for.usage[name], recipe) diff --git a/callbacks.lua b/callbacks.lua index 5874a0f..e92175d 100644 --- a/callbacks.lua +++ b/callbacks.lua @@ -1,3 +1,5 @@ +local ui = unified_inventory + local function default_refill(stack) stack:set_count(stack:get_stack_max()) local itemdef = minetest.registered_items[stack:get_name()] @@ -12,9 +14,9 @@ end minetest.register_on_joinplayer(function(player) local player_name = player:get_player_name() unified_inventory.players[player_name] = {} - unified_inventory.current_index[player_name] = 1 + unified_inventory.current_index[player_name] = 1 -- Item (~page) index unified_inventory.filtered_items_list[player_name] = - unified_inventory.items_list + unified_inventory.items_list unified_inventory.activefilter[player_name] = "" unified_inventory.active_search_direction[player_name] = "nochange" unified_inventory.apply_filter(player, "", "nochange") @@ -48,11 +50,11 @@ end) local function apply_new_filter(player, search_text, new_dir) local player_name = player:get_player_name() + minetest.sound_play("click", {to_player=player_name, gain = 0.1}) - unified_inventory.apply_filter(player, search_text, new_dir) - unified_inventory.current_searchbox[player_name] = search_text - unified_inventory.set_inventory_formspec(player, - unified_inventory.current_page[player_name]) + ui.apply_filter(player, search_text, new_dir) + ui.current_searchbox[player_name] = search_text + ui.set_inventory_formspec(player, ui.current_page[player_name]) end minetest.register_on_player_receive_fields(function(player, formname, fields) @@ -65,9 +67,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end -- always take new search text, even if not searching on it yet + local dirty_search_filter = false + if fields.searchbox and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then unified_inventory.current_searchbox[player_name] = fields.searchbox + dirty_search_filter = true end @@ -88,19 +93,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) unified_inventory.current_page[player_name]) end - if fields.next_category then - local scroll = math.min(#unified_inventory.category_list-ui_peruser.pagecols, unified_inventory.current_category_scroll[player_name] + 1) - if scroll ~= unified_inventory.current_category_scroll[player_name] then - unified_inventory.current_category_scroll[player_name] = scroll - unified_inventory.set_inventory_formspec(player, - unified_inventory.current_page[player_name]) - end - end - if fields.prev_category then - local scroll = math.max(0, unified_inventory.current_category_scroll[player_name] - 1) - if scroll ~= unified_inventory.current_category_scroll[player_name] then - unified_inventory.current_category_scroll[player_name] = scroll - unified_inventory.set_inventory_formspec(player, + if fields.next_category or fields.prev_category then + local step = fields.next_category and 1 or -1 + local scroll_old = ui.current_category_scroll[player_name] + local scroll_new = math.max(0, math.min(#ui.category_list - ui_peruser.pagecols, scroll_old + step)) + + if scroll_old ~= scroll_new then + ui.current_category_scroll[player_name] = scroll_new + ui.set_inventory_formspec(player, unified_inventory.current_page[player_name]) end end @@ -197,13 +197,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.searchbutton or fields.key_enter_field == "searchbox" then - unified_inventory.apply_filter(player, unified_inventory.current_searchbox[player_name], "nochange") - unified_inventory.set_inventory_formspec(player, - unified_inventory.current_page[player_name]) - minetest.sound_play("paperflip2", - {to_player=player_name, gain = 1.0}) + if dirty_search_filter then + ui.apply_filter(player, ui.current_searchbox[player_name], "nochange") + ui.set_inventory_formspec(player, ui.current_page[player_name]) + minetest.sound_play("paperflip2", + {to_player=player_name, gain = 1.0}) + end elseif fields.searchresetbutton then - apply_new_filter(player, "", "nochange") + if ui.activefilter[player_name] ~= "" then + apply_new_filter(player, "", "nochange") + end end -- alternate buttons @@ -241,11 +244,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) unified_inventory.current_page[player_name]) end) -if minetest.delete_detached_inventory then - minetest.register_on_leaveplayer(function(player) - local player_name = player:get_player_name() - minetest.delete_detached_inventory(player_name.."_bags") - minetest.delete_detached_inventory(player_name.."craftrecipe") - minetest.delete_detached_inventory(player_name.."refill") - end) -end +minetest.register_on_leaveplayer(function(player) + local player_name = player:get_player_name() + minetest.remove_detached_inventory(player_name.."_bags") + minetest.remove_detached_inventory(player_name.."refill") +end) diff --git a/group.lua b/group.lua index 3864267..2bc8e2f 100644 --- a/group.lua +++ b/group.lua @@ -1,30 +1,5 @@ local S = minetest.get_translator("unified_inventory") -function unified_inventory.canonical_item_spec_matcher(spec) - local specname = ItemStack(spec):get_name() - if specname:sub(1, 6) ~= "group:" then - return function (itemname) - return itemname == specname - end - end - - local group_names = specname:sub(7):split(",") - return function (itemname) - local itemdef = minetest.registered_items[itemname] - for _, group_name in ipairs(group_names) do - if (itemdef.groups[group_name] or 0) == 0 then - return false - end - end - return true - end -end - -function unified_inventory.item_matches_spec(item, spec) - local itemname = ItemStack(item):get_name() - return unified_inventory.canonical_item_spec_matcher(spec)(itemname) -end - function unified_inventory.extract_groupnames(groupname) local specname = ItemStack(groupname):get_name() if specname:sub(1, 6) ~= "group:" then @@ -34,22 +9,6 @@ function unified_inventory.extract_groupnames(groupname) return table.concat(group_names, S(" and ")), #group_names end -unified_inventory.registered_group_items = { - mesecon_conductor_craftable = "mesecons:wire_00000000_off", - stone = "default:cobble", - wood = "default:wood", - book = "default:book", - sand = "default:sand", - leaves = "default:leaves", - tree = "default:tree", - vessel = "vessels:glass_bottle", - wool = "wool:white", -} - -function unified_inventory.register_group_item(groupname, itemname) - unified_inventory.registered_group_items[groupname] = itemname -end - -- This is used when displaying craft recipes, where an ingredient is -- specified by group rather than as a specific item. A single-item group diff --git a/init.lua b/init.lua index 5510392..12a14ed 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,11 @@ -- Unified Inventory +if not minetest.features.formspec_version_element then + -- At least formspec_version[] is the minimal feature requirement + error("Unified Inventory requires Minetest version 5.4.0 or newer.\n" .. + " Please update Minetest or use an older version of Unified Inventory.") +end + local modpath = minetest.get_modpath(minetest.get_current_modname()) local worldpath = minetest.get_worldpath() @@ -46,7 +52,7 @@ unified_inventory = { list_img_offset = 0.13, standard_background = "background9[0,0;1,1;ui_formbg_9_sliced.png;true;16]", - version = 3 + version = 4 } local ui = unified_inventory @@ -59,10 +65,16 @@ ui.style_full = { formspec_y = 1, formw = 17.75, formh = 12.25, + -- Item browser size, pos pagecols = 8, pagerows = 9, page_x = 10.75, page_y = 2.30, + -- Item browser controls + page_buttons_x = 11.60, + page_buttons_y = 10.15, + searchwidth = 3.4, + -- Crafting grid positions craft_x = 2.8, craft_y = 1.15, craftresult_x = 7.8, @@ -74,13 +86,15 @@ ui.style_full = { craft_guide_resultstr_x = 0.3, craft_guide_resultstr_y = 0.6, give_btn_x = 0.25, + -- Tab switching buttons main_button_x = 0.4, main_button_y = 11.0, - page_buttons_x = 11.60, - page_buttons_y = 10.15, - searchwidth = 3.4, + main_button_cols = 12, + main_button_rows = 1, + -- Tab title position form_header_x = 0.4, form_header_y = 0.4, + -- Generic sizes btn_spc = 0.85, btn_size = 0.75, std_inv_x = 0.3, @@ -92,10 +106,16 @@ ui.style_lite = { formspec_y = 0.6, formw = 14, formh = 9.75, + -- Item browser size, pos pagecols = 4, pagerows = 5, page_x = 10.5, page_y = 2.15, + -- Item browser controls + page_buttons_x = 10.5, + page_buttons_y = 6.15, + searchwidth = 1.6, + -- Crafting grid positions craft_x = 2.6, craft_y = 0.75, craftresult_x = 5.75, @@ -107,13 +127,15 @@ ui.style_lite = { craft_guide_resultstr_x = 0.15, craft_guide_resultstr_y = 0.35, give_btn_x = 0.15, + -- Tab switching buttons main_button_x = 10.5, main_button_y = 8.15, - page_buttons_x = 10.5, - page_buttons_y = 6.15, - searchwidth = 1.6, + main_button_cols = 4, + main_button_rows = 2, + -- Tab title position form_header_x = 0.2, form_header_y = 0.2, + -- Generic sizes btn_spc = 0.8, btn_size = 0.7, std_inv_x = 0.1, @@ -170,4 +192,6 @@ end dofile(modpath.."/item_names.lua") dofile(modpath.."/waypoints.lua") +dofile(modpath.."/legacy.lua") -- mod compatibility + minetest.log("action", "[unified_inventory] loaded.") diff --git a/internal.lua b/internal.lua index 75b9b7b..b264d22 100644 --- a/internal.lua +++ b/internal.lua @@ -18,7 +18,7 @@ function ui.demangle_for_formspec(str) return string.gsub(str, "_([0-9]+)_", function (v) return string.char(v) end) end - +-- Get the player-specific unified_inventory style function ui.get_per_player_formspec(player_name) local draw_lite_mode = ui.lite_mode and not minetest.check_player_privs(player_name, {ui_full=true}) @@ -27,6 +27,7 @@ function ui.get_per_player_formspec(player_name) return style end +-- Creates an item image or regular image button with a tooltip local function formspec_button(ui_peruser, name, image, offset, pos, scale, label) local element = 'image_button' if minetest.registered_items[image] then @@ -43,9 +44,8 @@ local function formspec_button(ui_peruser, name, image, offset, pos, scale, labe string.format("tooltip[%s;%s]", name, F(label or name)) end -local function formspec_add_filters(player, formspec, style) - local button_row = 0 - local button_col = 0 +-- Add registered buttons (tabs) +local function formspec_tab_buttons(player, formspec, style) local n = #formspec + 1 -- Main buttons @@ -58,37 +58,50 @@ local function formspec_add_filters(player, formspec, style) end end - local j = 1 --Modif NALC (sys4 20/11/2018) 12 buttons max by row + local needs_scrollbar = #filtered_inv_buttons > style.main_button_cols * style.main_button_rows + + formspec[n] = ("scroll_container[%g,%g;%g,%g;tabbtnscroll;vertical]"):format( + style.main_button_x, style.main_button_y, -- position + style.main_button_cols * style.btn_spc, style.main_button_rows -- size + ) + n = n + 1 + for i, def in pairs(filtered_inv_buttons) do - if style.is_lite_mode and i > 4 then - button_row = 1 - button_col = 1 - elseif not draw_lite_mode and j > 12 then - button_row = 1 - j = 1 - end + local pos_x = ((i - 1) % style.main_button_cols) * style.btn_spc + local pos_y = math.floor((i - 1) / style.main_button_cols) * style.btn_spc if def.type == "image" then - local pos_x = style.main_button_x + style.btn_spc * (j - 1) - button_col * style.btn_spc * 4 - local pos_y = style.main_button_y + button_row * style.btn_spc if (def.condition == nil or def.condition(player) == true) then - formspec[n] = string.format("image_button[%f,%f;%f,%f;%s;%s;]", + formspec[n] = string.format("image_button[%g,%g;%g,%g;%s;%s;]", pos_x, pos_y, style.btn_size, style.btn_size, F(def.image), F(def.name)) formspec[n+1] = "tooltip["..F(def.name)..";"..(def.tooltip or "").."]" n = n+2 else - formspec[n] = string.format("image[%f,%f;%f,%f;%s^[colorize:#808080:alpha]", + formspec[n] = string.format("image[%g,%g;%g,%g;%s^[colorize:#808080:alpha]", pos_x, pos_y, style.btn_size, style.btn_size, def.image) n = n+1 end end - j = j + 1 -- Modif NALC + end + formspec[n] = "scroll_container_end[]" + if needs_scrollbar then + formspec[n+1] = ("scrollbaroptions[max=%i;arrows=hide]"):format( + -- This calculation is not 100% accurate but "good enough" + math.ceil((#filtered_inv_buttons - 1) / style.main_button_cols) * style.btn_spc * 5 + ) + formspec[n+2] = ("scrollbar[%g,%g;0.4,%g;vertical;tabbtnscroll;0]"):format( + style.main_button_x + style.main_button_cols * style.btn_spc - 0.1, -- x pos + style.main_button_y, -- y pos + style.main_button_rows * style.btn_spc -- height + ) + formspec[n+3] = "scrollbaroptions[max=1000;arrows=default]" end end +-- Add category GUI elements (top right) local function formspec_add_categories(player, formspec, ui_peruser) local player_name = player:get_player_name() local n = #formspec + 1 @@ -285,7 +298,7 @@ function ui.get_formspec(player, page) fs[#fs + 1] = fsdata.formspec - formspec_add_filters(player, fs, ui_peruser) + formspec_tab_buttons(player, fs, ui_peruser) if fsdata.draw_inventory ~= false then -- Player inventory @@ -383,3 +396,13 @@ function ui.apply_filter(player, filter, search_dir) ui.active_search_direction[player_name] = search_dir ui.set_inventory_formspec(player, ui.current_page[player_name]) end + +-- Inform players about potential visual issues +minetest.register_on_joinplayer(function(player) + local player_name = player:get_player_name() + local info = minetest.get_player_information(player_name) + if info and (info.formspec_version or 0) < 4 then + minetest.chat_send_player(player_name, S("Unified Inventory: Your game version is too old" + .. " and does not support the GUI requirements. You might experience visual issues.")) + end +end) diff --git a/legacy.lua b/legacy.lua new file mode 100644 index 0000000..5b62894 --- /dev/null +++ b/legacy.lua @@ -0,0 +1,55 @@ +-- Inefficient pattern matching + +local warned_funcs = {} +local function LOG_ONCE(funcname) + if warned_funcs[funcname] then return end + warned_funcs[funcname] = true + minetest.log("error", "Call to undocumented, deprecated API '" .. funcname .. "'." + .. " In a future version of Unified Inventory this will result in a real error.") +end + +function unified_inventory.canonical_item_spec_matcher(spec) + LOG_ONCE("canonical_item_spec_matcher") + local specname = ItemStack(spec):get_name() + if specname:sub(1, 6) ~= "group:" then + return function (itemname) + return itemname == specname + end + end + + local group_names = specname:sub(7):split(",") + return function (itemname) + local itemdef = minetest.registered_items[itemname] + for _, group_name in ipairs(group_names) do + if (itemdef.groups[group_name] or 0) == 0 then + return false + end + end + return true + end +end + +function unified_inventory.item_matches_spec(item, spec) + LOG_ONCE("item_matches_spec") + local itemname = ItemStack(item):get_name() + return unified_inventory.canonical_item_spec_matcher(spec)(itemname) +end + + +unified_inventory.registered_group_items = { + mesecon_conductor_craftable = "mesecons:wire_00000000_off", + stone = "default:cobble", + wood = "default:wood", + book = "default:book", + sand = "default:sand", + leaves = "default:leaves", + tree = "default:tree", + vessel = "vessels:glass_bottle", + wool = "wool:white", +} + +function unified_inventory.register_group_item(groupname, itemname) + LOG_ONCE("register_group_item") + unified_inventory.registered_group_items[groupname] = itemname +end +