Minor API change

This commit is contained in:
Jean-Patrick Guerrero 2021-11-19 19:35:41 +01:00
parent dc8efbd16c
commit 6c3d2fe5cf
3 changed files with 17 additions and 15 deletions

11
API.md
View File

@ -4,6 +4,9 @@
#### `i3.new_tab(name, def)` #### `i3.new_tab(name, def)`
- `name` is the tab name.
- `def` is the tab definition.
Custom tabs can be added to the `i3` inventory as follow (example): Custom tabs can be added to the `i3` inventory as follow (example):
```Lua ```Lua
@ -238,17 +241,17 @@ A map of search filters, indexed by name.
Sorting methods are used to filter the player's main inventory. Sorting methods are used to filter the player's main inventory.
#### `i3.add_sorting_method(def)` #### `i3.add_sorting_method(name, def)`
Adds a player inventory sorting method. Adds a player inventory sorting method.
- `name` is the method name.
- `def` is the method definition. - `def` is the method definition.
Example: Example:
```Lua ```Lua
i3.add_sorting_method { i3.add_sorting_method("test", {
name = "test",
description = "Cool sorting method", description = "Cool sorting method",
func = function(list, data) func = function(list, data)
-- `list`: inventory list -- `list`: inventory list
@ -259,7 +262,7 @@ i3.add_sorting_method {
-- A list must be returned -- A list must be returned
return list return list
end, end,
} })
``` ```

View File

@ -298,32 +298,31 @@ function i3.compress(item, def)
end end
end end
function i3.add_sorting_method(def) function i3.add_sorting_method(name, def)
if not true_table(def) then if not true_str(name) then
return err "i3.add_sorting_method: definition missing"
elseif not true_str(def.name) then
return err "i3.add_sorting_method: name missing" return err "i3.add_sorting_method: name missing"
elseif not true_table(def) then
return err "i3.add_sorting_method: definition missing"
elseif not is_func(def.func) then elseif not is_func(def.func) then
return err "i3.add_sorting_method: function missing" return err "i3.add_sorting_method: function missing"
end end
def.name = name
insert(i3.sorting_methods, def) insert(i3.sorting_methods, def)
end end
i3.add_sorting_method { i3.add_sorting_method("alphabetical", {
name = "alphabetical",
description = S"Sort items by name (A-Z)", description = S"Sort items by name (A-Z)",
func = function(list, data) func = function(list, data)
sorter(list, data.reverse_sorting, 1) sorter(list, data.reverse_sorting, 1)
return list return list
end end
} })
i3.add_sorting_method { i3.add_sorting_method("numerical", {
name = "numerical",
description = S"Sort items by number of items per stack", description = S"Sort items by number of items per stack",
func = function(list, data) func = function(list, data)
sorter(list, data.reverse_sorting, 2) sorter(list, data.reverse_sorting, 2)
return list return list
end, end,
} })

View File

@ -475,7 +475,7 @@ local function show_popup(fs, data)
fs("image_button", 7.65, 10.6, 0.35, 0.35, "", "next_sort", "") fs("image_button", 7.65, 10.6, 0.35, 0.35, "", "next_sort", "")
fs("style[sort_method;font=bold;font_size=20]") fs("style[sort_method;font=bold;font_size=20]")
fs("button", 2.55, 10.36, 5.1, 0.8, "sort_method", data.sort:gsub("^%l", upper)) fs("button", 2.55, 10.36, 5.1, 0.8, "sort_method", toupper(data.sort))
local idx = get_sorting_idx(data.sort) local idx = get_sorting_idx(data.sort)
local desc = i3.sorting_methods[idx].description local desc = i3.sorting_methods[idx].description