Add an API to add minitabs

This commit is contained in:
Jean-Patrick Guerrero 2023-01-19 01:24:45 +01:00
parent 38f1d7c960
commit b95b179a5a
6 changed files with 87 additions and 11 deletions

38
API.md
View File

@ -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.

View File

@ -101,6 +101,7 @@ i3 = {
groups = {},
plants = {},
modules = {},
minitabs = {},
craft_types = {},
recipe_filters = {},

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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