capturetheflag/mods/mtg/sfinv/api.lua

184 lines
4.7 KiB
Lua
Raw Normal View History

2016-02-15 01:27:20 +00:00
sfinv = {
pages = {},
pages_unordered = {},
2017-10-06 00:35:51 +00:00
contexts = {},
enabled = true
2016-02-15 01:27:20 +00:00
}
function sfinv.register_page(name, def)
2017-10-06 00:35:51 +00:00
assert(name, "Invalid sfinv page. Requires a name")
assert(def, "Invalid sfinv page. Requires a def[inition] table")
assert(def.get, "Invalid sfinv page. Def requires a get function.")
assert(not sfinv.pages[name], "Attempt to register already registered sfinv page " .. dump(name))
2016-02-15 01:27:20 +00:00
sfinv.pages[name] = def
def.name = name
table.insert(sfinv.pages_unordered, def)
end
2017-10-06 00:35:51 +00:00
function sfinv.override_page(name, def)
assert(name, "Invalid sfinv page override. Requires a name")
assert(def, "Invalid sfinv page override. Requires a def[inition] table")
local page = sfinv.pages[name]
assert(page, "Attempt to override sfinv page " .. dump(name) .. " which does not exist.")
for key, value in pairs(def) do
page[key] = value
end
end
function sfinv.get_nav_fs(player, context, nav, current_idx)
-- Only show tabs if there is more than one page
if #nav > 1 then
2019-01-08 21:24:35 +00:00
return "tabheader[0,0;sfinv_nav_tabs;" .. table.concat(nav, ",") ..
";" .. current_idx .. ";true;false]"
2017-10-06 00:35:51 +00:00
else
return ""
2016-02-15 01:27:20 +00:00
end
end
2019-01-08 21:24:35 +00:00
local theme_inv = [[
2017-10-06 00:35:51 +00:00
list[current_player;main;0,4.7;8,1;]
list[current_player;main;0,5.85;8,3;8]
]]
function sfinv.make_formspec(player, context, content, show_inv, size)
local tmp = {
size or "size[8,8.6]",
sfinv.get_nav_fs(player, context, context.nav_titles, context.nav_idx),
content
}
if show_inv then
tmp[#tmp + 1] = theme_inv
2016-02-15 01:27:20 +00:00
end
2017-10-06 00:35:51 +00:00
return table.concat(tmp, "")
end
function sfinv.get_homepage_name(player)
return "sfinv:crafting"
end
2016-02-15 01:27:20 +00:00
2017-10-06 00:35:51 +00:00
function sfinv.get_formspec(player, context)
-- Generate navigation tabs
2016-02-15 01:27:20 +00:00
local nav = {}
local nav_ids = {}
local current_idx = 1
for i, pdef in pairs(sfinv.pages_unordered) do
2017-10-06 00:35:51 +00:00
if not pdef.is_in_nav or pdef:is_in_nav(player, context) then
2016-02-15 01:27:20 +00:00
nav[#nav + 1] = pdef.title
nav_ids[#nav_ids + 1] = pdef.name
if pdef.name == context.page then
2017-10-06 00:35:51 +00:00
current_idx = #nav_ids
2016-02-15 01:27:20 +00:00
end
end
end
context.nav = nav_ids
2017-10-06 00:35:51 +00:00
context.nav_titles = nav
context.nav_idx = current_idx
2016-02-15 01:27:20 +00:00
2017-10-06 00:35:51 +00:00
-- Generate formspec
local page = sfinv.pages[context.page] or sfinv.pages["404"]
if page then
return page:get(player, context)
else
local old_page = context.page
2019-01-08 21:24:35 +00:00
local home_page = sfinv.get_homepage_name(player)
if old_page == home_page then
minetest.log("error", "[sfinv] Couldn't find " .. dump(old_page) ..
", which is also the old page")
return ""
end
context.page = home_page
2017-10-06 00:35:51 +00:00
assert(sfinv.pages[context.page], "[sfinv] Invalid homepage")
2019-01-08 21:24:35 +00:00
minetest.log("warning", "[sfinv] Couldn't find " .. dump(old_page) ..
" so switching to homepage")
2017-10-06 00:35:51 +00:00
return sfinv.get_formspec(player, context)
end
2016-02-15 01:27:20 +00:00
end
2017-10-06 00:35:51 +00:00
function sfinv.get_or_create_context(player)
local name = player:get_player_name()
local context = sfinv.contexts[name]
2016-02-15 01:27:20 +00:00
if not context then
2017-10-06 00:35:51 +00:00
context = {
page = sfinv.get_homepage_name(player)
}
sfinv.contexts[name] = context
2016-02-15 01:27:20 +00:00
end
2017-10-06 00:35:51 +00:00
return context
end
function sfinv.set_context(player, context)
sfinv.contexts[player:get_player_name()] = context
end
2016-02-27 04:21:34 +00:00
2017-10-06 00:35:51 +00:00
function sfinv.set_player_inventory_formspec(player, context)
local fs = sfinv.get_formspec(player,
context or sfinv.get_or_create_context(player))
2016-02-15 01:27:20 +00:00
player:set_inventory_formspec(fs)
end
2017-10-06 00:35:51 +00:00
function sfinv.set_page(player, pagename)
local context = sfinv.get_or_create_context(player)
local oldpage = sfinv.pages[context.page]
if oldpage and oldpage.on_leave then
oldpage:on_leave(player, context)
end
context.page = pagename
local page = sfinv.pages[pagename]
if page.on_enter then
page:on_enter(player, context)
end
sfinv.set_player_inventory_formspec(player, context)
end
2019-01-08 21:24:35 +00:00
function sfinv.get_page(player)
local context = sfinv.contexts[player:get_player_name()]
return context and context.page or sfinv.get_homepage_name(player)
end
2016-02-15 01:27:20 +00:00
minetest.register_on_joinplayer(function(player)
2017-10-06 00:35:51 +00:00
if sfinv.enabled then
sfinv.set_player_inventory_formspec(player)
end
end)
minetest.register_on_leaveplayer(function(player)
sfinv.contexts[player:get_player_name()] = nil
2016-02-15 01:27:20 +00:00
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
2017-10-06 00:35:51 +00:00
if formname ~= "" or not sfinv.enabled then
2016-02-15 01:27:20 +00:00
return false
end
-- Get Context
local name = player:get_player_name()
local context = sfinv.contexts[name]
if not context then
2017-10-06 00:35:51 +00:00
sfinv.set_player_inventory_formspec(player)
2016-02-15 01:27:20 +00:00
return false
end
2017-10-06 00:35:51 +00:00
-- Was a tab selected?
2019-01-08 21:24:35 +00:00
if fields.sfinv_nav_tabs and context.nav then
local tid = tonumber(fields.sfinv_nav_tabs)
2016-02-15 01:27:20 +00:00
if tid and tid > 0 then
local id = context.nav[tid]
2017-10-06 00:35:51 +00:00
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
2016-02-15 01:27:20 +00:00
end
end
2017-10-06 00:35:51 +00:00
else
-- Pass event to page
local page = sfinv.pages[context.page]
if page and page.on_player_receive_fields then
return page:on_player_receive_fields(player, context, fields)
end
2016-02-15 01:27:20 +00:00
end
end)