Complete minitabs API

This commit is contained in:
Jean-Patrick Guerrero 2023-01-19 16:40:31 +01:00
parent b95b179a5a
commit ac7a1fb746
5 changed files with 85 additions and 54 deletions

81
API.md
View File

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

View File

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

View File

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

View File

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

View File

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