Add more API funcs

This commit is contained in:
Jean-Patrick Guerrero 2021-01-28 21:00:08 +01:00
parent 7c9785d448
commit eecb3439ab
3 changed files with 77 additions and 16 deletions

10
API.md
View File

@ -41,10 +41,18 @@ i3.new_tab {
- `fs` is the formspec table which is callable with a metamethod. Each call adds a new entry. - `fs` is the formspec table which is callable with a metamethod. Each call adds a new entry.
- `i3.set_fs(player)` must be called to update the formspec. - `i3.set_fs(player)` must be called to update the formspec.
##### `i3.delete_tab(name)` ##### `i3.delete_tab(tabname)`
Deletes a tab by name. Deletes a tab by name.
##### `i3.set_tab(player, tabname)`
Sets the current tab by name. `player` is an `ObjectRef` to the user.
##### `i3.override_tab(tabname, def)`
Overrides a tab by name. `def` is the tab definition like seen in `i3.set_tab`.
##### `i3.get_tabs()` ##### `i3.get_tabs()`
Returns the list of registered tabs. Returns the list of registered tabs.

View File

@ -1969,7 +1969,7 @@ local function get_tabs_fs(player, data, fs, full_height)
local selected = i == data.current_tab local selected = i == data.current_tab
fs(fmt([[style_type[image_button;fgimg=%s;fgimg_hovered=%s;fgimg_middle=14;noclip=true; fs(fmt([[style_type[image_button;bgimg=%s;bgimg_hovered=%s;noclip=true;
font_size=16;textcolor=%s;content_offset=0;sound=i3_tab] ]], font_size=16;textcolor=%s;content_offset=0;sound=i3_tab] ]],
selected and (btm and PNG.tab_hover or PNG.tab_hover_top) or (btm and PNG.tab or PNG.tab_top), selected and (btm and PNG.tab_hover or PNG.tab_hover_top) or (btm and PNG.tab or PNG.tab_top),
btm and PNG.tab_hover or PNG.tab_hover_top, selected and "#fff" or "#ddd")) btm and PNG.tab_hover or PNG.tab_hover_top, selected and "#fff" or "#ddd"))
@ -2024,9 +2024,11 @@ local function make_fs(player, data)
fs(fmt("bg9", 0, 0, data.xoffset, full_height, PNG.bg_full, 10)) fs(fmt("bg9", 0, 0, data.xoffset, full_height, PNG.bg_full, 10))
local tab = tabs[(data.current_tab or 1)] local tab = tabs[data.current_tab]
if tab then
tab.formspec(player, data, fs) tab.formspec(player, data, fs)
end
local hide_panels = tab.hide_panels and tab.hide_panels(player, data) local hide_panels = tab.hide_panels and tab.hide_panels(player, data)
@ -2059,7 +2061,7 @@ function i3.new_tab(def)
end end
if not true_str(def.name) then if not true_str(def.name) then
return err "i3.new_tab: name missing" return err "i3.new_tab: tab name missing"
end end
if not true_str(def.description) then if not true_str(def.description) then
@ -2077,18 +2079,61 @@ function i3.get_tabs()
return tabs return tabs
end end
function i3.delete_tab(name) function i3.delete_tab(tabname)
if not true_str(name) then if not true_str(tabname) then
return err "i3.delete_tab: name missing" return err "i3.delete_tab: tab name missing"
end end
for i, def in ipairs(tabs) do for i, def in ipairs(tabs) do
if name == def.name then if tabname == def.name then
remove(tabs, i) remove(tabs, i)
end end
end end
end end
function i3.set_tab(player, tabname)
if not true_str(tabname) then
return err "i3.set_tab: tab name missing"
end
local name = player:get_player_name()
local data = pdata[name]
local found
for i, def in ipairs(tabs) do
if not found and def.name == tabname then
data.current_tab = i
found = true
end
end
if not found then
return err(fmt("i3.set_tab: tab name '%s' does not exist", tabname))
end
end
local set_tab = i3.set_tab
function i3.override_tab(tabname, newdef)
if not is_table(newdef) or not next(newdef) then
return err "i3.override_tab: tab definition missing"
end
if not true_str(newdef.name) then
return err "i3.override_tab: tab name missing"
end
if not true_str(newdef.description) then
return err "i3.override_tab: description missing"
end
for i, def in ipairs(tabs) do
if def.name == tabname then
tabs[i] = newdef
end
end
end
local function init_data(player, info) local function init_data(player, info)
local name = player:get_player_name() local name = player:get_player_name()
@ -2803,13 +2848,7 @@ on_receive_fields(function(player, formname, fields)
for f in pairs(fields) do for f in pairs(fields) do
if sub(f, 1, 4) == "tab_" then if sub(f, 1, 4) == "tab_" then
local tabname = sub(f, 5) local tabname = sub(f, 5)
set_tab(player, tabname)
for i, def in ipairs(tabs) do
if def.name == tabname then
data.current_tab = i
end
end
break break
end end
end end

View File

@ -45,3 +45,17 @@ i3.new_tab {
i3.set_fs(player) i3.set_fs(player)
end, end,
} }
i3.override_tab("test2", {
name = "test2",
description = "Test override",
image = "i3_mesepick.png",
formspec = function(player, data, fs)
fs("label[3,1;Override!]")
end,
fields = function(player, data, fields)
i3.set_fs(player)
end,
})