From b95b179a5a558b63f6b144cafd714c0a85212342 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Thu, 19 Jan 2023 01:24:45 +0100 Subject: [PATCH] Add an API to add minitabs --- API.md | 38 ++++++++++++++++++++++++++++++++++++++ init.lua | 1 + src/api.lua | 12 ++++++++++++ src/common.lua | 4 ++++ src/gui.lua | 37 ++++++++++++++++++++++++++----------- src/progressive.lua | 6 ++++++ 6 files changed, 87 insertions(+), 11 deletions(-) diff --git a/API.md b/API.md index ce17dc6..40ecc20 100644 --- a/API.md +++ b/API.md @@ -309,6 +309,44 @@ 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/init.lua b/init.lua index a46cb63..986f9b1 100644 --- a/init.lua +++ b/init.lua @@ -101,6 +101,7 @@ i3 = { groups = {}, plants = {}, modules = {}, + minitabs = {}, craft_types = {}, recipe_filters = {}, diff --git a/src/api.lua b/src/api.lua index 0e71fa7..6194465 100644 --- a/src/api.lua +++ b/src/api.lua @@ -430,3 +430,15 @@ function i3.get_waypoints(player_name) return data.waypoints end + +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" + elseif not true_table(def) then + return err "i3.new_minitab: definition missing" + end + + insert(i3.minitabs, {name = name, def = def}) +end diff --git a/src/common.lua b/src/common.lua index 5ab144f..6e72365 100644 --- a/src/common.lua +++ b/src/common.lua @@ -361,11 +361,15 @@ 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 if to_add then diff --git a/src/gui.lua b/src/gui.lua index 365e6c6..f3638b0 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -1452,22 +1452,37 @@ local function get_header_items_fs(fs, data) end end -local function get_minitabs(fs, data, full_height) - local _tabs = {"All", "Nodes", "Items"} - local tab_len, tab_hgh = 1.8, 0.5 +local function get_minitabs(fs, data, player, full_height) + local minitabs = {"All", "Nodes", "Items"} - for i, title in ipairs(_tabs) do - local selected = i == data.itab + 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 + end + end + + local tab_len, tab_hgh, i = 1.8, 0.5, 0 + + for id, title in pairs(minitabs) do + i++ + local X = i > 3 and i - 3 or i + local selected = id == data.itab local hover_texture = selected and PNG.tab_small_hover or PNG.tab_small + local top = i > 3 + local flip = top and "^[transformFY" or "" - fs([[ style_type[image_button;bgimg=%s;bgimg_hovered=%s; - bgimg_middle=14,0,-14,-14;padding=-14,0,14,14] ]], hover_texture, PNG.tab_small_hover) + fs([[ style_type[image_button;bgimg=%s%s;bgimg_hovered=%s%s; + bgimg_middle=14,0,-14,-14;padding=-14,0,14,14] ]], + hover_texture, flip, PNG.tab_small_hover, flip) fs([[ style_type[image_button;noclip=true;font=bold;font_size=16; textcolor=%s;content_offset=0;sound=i3_tab] ]], selected and "#fff" or "#bbb") fs"style_type[image_button:hovered;textcolor=#fff]" - image_button((data.inv_width - 0.65) + (i * (tab_len + 0.1)), - full_height, tab_len, tab_hgh, "", fmt("itab_%u", i), title) + + image_button((data.inv_width - 0.65) + (X * (tab_len + 0.1)), + top and -tab_hgh or full_height, tab_len, tab_hgh, "", fmt("itab_%u", id), title) end end @@ -1515,7 +1530,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() then + if recipe_filter_set() and data.itab <= 3 then if data.items_progress[item] then insert(fs, item_btn) else @@ -1706,7 +1721,7 @@ local function make_fs(player, data) get_items_fs(fs, data, player, full_height) if not data.hide_tabs then - get_minitabs(fs, data, full_height) + get_minitabs(fs, data, player, full_height) end end diff --git a/src/progressive.lua b/src/progressive.lua index 1797aa1..88dec35 100644 --- a/src/progressive.lua +++ b/src/progressive.lua @@ -6,6 +6,12 @@ IMPORT("reg_items", "reg_nodes") IMPORT("fmt", "search", "table_merge", "array_diff") IMPORT("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters") +i3.new_minitab("Unlocked", { + sorter = function(item, data) + return data.items_progress[item] + end +}) + local function get_filtered_items(player, data) local items, known = {}, 0