From ac7a1fb746966ec7257b9e774e8a9d4a45f18f42 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Thu, 19 Jan 2023 16:40:31 +0100 Subject: [PATCH] Complete minitabs API --- API.md | 81 ++++++++++++++++++++++++--------------------- src/api.lua | 33 +++++++++++++++++- src/common.lua | 13 ++------ src/gui.lua | 9 ++--- src/progressive.lua | 3 ++ 5 files changed, 85 insertions(+), 54 deletions(-) diff --git a/API.md b/API.md index 40ecc20..916b71e 100644 --- a/API.md +++ b/API.md @@ -164,6 +164,49 @@ i3.register_craft { --- +### Minitabs + +Manage the tabs on the right panel of the inventory. +Allow to make a sensible list sorted by specific groups of items. + +#### `i3.new_minitab(name, def)` + +Add a new minitab (limited to 6). + +- `name` is the tab name. +- `def` is the definition table. + +Example: + +```Lua +i3.new_minitab("Test", { + access = function(player, data) + -- Whether a player can access this tab or not. Optional. + return player:get_player_name() == "singleplayer" + end, + + sorter = function(item, data) + -- Whether a specific item is shown in the list or not. + return item:find"wood" + end +}) + +``` + +- `player` is an `ObjectRef` to the user. +- `data` are the user data. +- `item` is an item name string. + +#### `i3.remove_minitab(name)` + +Remove a minitab. + +#### `i3.minimap` + +A list of registered minitabs. + +--- + ### Recipe filters Recipe filters can be used to filter the recipes shown to players. Progressive @@ -309,44 +352,6 @@ A map of all compressed item groups, indexed by stereotypes. --- -### Minitabs - -Allow to add a minitab on the right panel to sort your item list. - -#### `i3.new_minitab(name, def)` - -Add a new minitab (limited to 6). - -- `name` is the tab name. -- `def` is the definition table. - -Example: - -```Lua -i3.new_minitab("Test", { - access = function(player, data) - -- Whether a player can access this tab or not. Optional. - return player:get_player_name() == "singleplayer" - end, - - sorter = function(item, data) - -- Whether a specific item is shown in the list or not. - return item:find"wood" - end -}) - -``` - -- `player` is an `ObjectRef` to the user. -- `data` are the user data. -- `item` is an item name string. - -#### `i3.minimap` - -A list of registered minitabs. - ---- - ### Waypoints `i3` allows you to manage the waypoints of a specific player. diff --git a/src/api.lua b/src/api.lua index 6194465..73f7eb5 100644 --- a/src/api.lua +++ b/src/api.lua @@ -435,10 +435,41 @@ function i3.new_minitab(name, def) if #i3.minitabs == 6 then return err "i3.new_minitab: limit reached (6)" elseif not true_str(name) then - return err "i3.new_minitab: minitab name missing" + return err "i3.new_minitab: name missing" elseif not true_table(def) then return err "i3.new_minitab: definition missing" end insert(i3.minitabs, {name = name, def = def}) end + +function i3.remove_minitab(name) + if not true_str(name) then + return err "i3.remove_minitab: name missing" + end + + for i = #i3.minitabs, 1, -1 do + local v = i3.minitabs[i] + if v and v.name == name then + remove(i3.minitabs, i) + end + end +end + +i3.new_minitab("All", { + sorter = function() + return true + end +}) + +i3.new_minitab("Nodes", { + sorter = function(item) + return core.registered_nodes[item] + end +}) + +i3.new_minitab("Items", { + sorter = function(item) + return core.registered_craftitems[item] or core.registered_tools[item] + end +}) diff --git a/src/common.lua b/src/common.lua index 6e72365..fcddb16 100644 --- a/src/common.lua +++ b/src/common.lua @@ -360,17 +360,8 @@ local function sort_by_category(data) for i = 1, #items do local item = items[i] - local to_add = true - local top_tap = i3.minitabs[data.itab - 3] - local custom_sorter = top_tap and top_tap.def.sorter - - if data.itab == 2 then - to_add = core.registered_nodes[item] - elseif data.itab == 3 then - to_add = core.registered_craftitems[item] or core.registered_tools[item] - elseif custom_sorter then - to_add = custom_sorter(item, data) - end + local tab = i3.minitabs[data.itab] + local to_add = tab.def.sorter(item, data) if to_add then insert(new, item) diff --git a/src/gui.lua b/src/gui.lua index f3638b0..ddb9d9b 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -1391,7 +1391,8 @@ local function get_header_items_fs(fs, data) end box(X + 1, 0.2, 0.01, 0.5, "#bababa50") - local cat = {{"all", "all items"}, {"node", "nodes only"}, {"item", "items only"}} + local cat = recipe_filter_set() and {{"all", "all items"}} or + {{"all", "all items"}, {"node", "nodes only"}, {"item", "items only"}} for i in ipairs(cat) do local name, desc = unpack(cat[i]) @@ -1453,13 +1454,13 @@ local function get_header_items_fs(fs, data) end local function get_minitabs(fs, data, player, full_height) - local minitabs = {"All", "Nodes", "Items"} + local minitabs = {} for i, v in ipairs(i3.minitabs) do local access = v.def.access if access == nil or access(player, data) then - minitabs[i + 3] = v.name + minitabs[i] = v.name end end @@ -1530,7 +1531,7 @@ local function get_items_fs(fs, data, player, full_height) local item_btn = fmt("item_image_button", X, Y, size, size, name, item, "") - if recipe_filter_set() and data.itab <= 3 then + if recipe_filter_set() and data.itab == 1 then if data.items_progress[item] then insert(fs, item_btn) else diff --git a/src/progressive.lua b/src/progressive.lua index 88dec35..c6a7e93 100644 --- a/src/progressive.lua +++ b/src/progressive.lua @@ -6,6 +6,9 @@ IMPORT("reg_items", "reg_nodes") IMPORT("fmt", "search", "table_merge", "array_diff") IMPORT("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters") +i3.remove_minitab"Nodes" +i3.remove_minitab"Items" + i3.new_minitab("Unlocked", { sorter = function(item, data) return data.items_progress[item]