splitted smart_sfinv_navfs into smart_sfinv_api and smart_sfinv_creative_sitebar

This commit is contained in:
Alexander Weber 2018-12-19 07:23:14 +01:00
parent 9c85054d6d
commit 2707fb02ea
5 changed files with 217 additions and 134 deletions

125
smart_sfinv_api/init.lua Normal file
View File

@ -0,0 +1,125 @@
smart_sfinv_api = {
registered_enhancements = {}
}
----------------------------------------------
-- API: register new enhancement
----------------------------------------------
--[[
Methods:
- make_formspec(enh, player, context)
- get_nav_fs(enh, player, context, nav, current_idx)
- receive_fields(handler, player, context, fields)
Method does set Attributes:
- enh.formspec_resize
- enh.formspec_before_navfs
- enh.formspec_after_navfs
- enh.formspec_after_content
- enh.custom_nav_fs - if set, default is skipped
]]
function smart_sfinv_api.register_enhancement(def)
table.insert(smart_sfinv_api.registered_enhancements, def)
end
----------------------------------------------
-- Enhancement handler
----------------------------------------------
local enh_handler_class = {
formspec_before_navfs = "",
formspec_after_navfs = "",
formspec_after_content = ""
}
enh_handler_class_meta = {__index = enh_handler_class }
function enh_handler_class:run_enhancements(enh_method, ...)
for _, enh in ipairs(smart_sfinv_api.registered_enhancements) do
if enh[enh_method] then
enh[enh_method](self, ...)
end
end
end
----------------------------------------------
-- Get the enhancement handler
----------------------------------------------
function smart_sfinv_api.get_handler( context )
context.sfinv_navfs_handler = context.sfinv_navfs_handler or setmetatable( {}, enh_handler_class_meta)
return context.sfinv_navfs_handler
end
----------------------------------------------
-- Redefnition sfinv.get_nav_fs
----------------------------------------------
local orig_get_nav_fs = sfinv.get_nav_fs
function sfinv.get_nav_fs(player, context, nav, current_idx)
-- Dummy access
if not nav then
return ""
end
local handler = smart_sfinv_api.get_handler( context )
handler:run_enhancements("get_nav_fs", player, context, nav, current_idx)
if handler.custom_nav_fs then
return handler.custom_nav_fs
else
return orig_get_nav_fs(player, context, nav, current_idx)
end
end
----------------------------------------------
-- Redefinition sfinv.make_formspec
----------------------------------------------
local orig_make_formspec = sfinv.make_formspec
function sfinv.make_formspec(player, context, content, show_inv, size)
context.sfinv_navfs_handler = nil -- initialize handler
local handler = smart_sfinv_api.get_handler( context )
local nav_fs = sfinv.get_nav_fs(player, context, context.nav_titles, context.nav_idx)
handler:run_enhancements("make_formspec", player, context)
local tmp = {
handler.formspec_resize or size or smart_sfinv_api.default_size,
smart_sfinv_api.theme_main,
handler.formspec_before_navfs,
nav_fs,
handler.formspec_after_navfs,
content,
handler.formspec_after_content
}
if show_inv then
tmp[#tmp + 1] = smart_sfinv_api.theme_inv
end
return table.concat(tmp, "")
end
----------------------------------------------
-- Additional on_player_receive_fields
----------------------------------------------
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "" or not sfinv.enabled then
return false
end
-- Get Context
local name = player:get_player_name()
local context = sfinv.contexts[name]
if not context then
sfinv.set_player_inventory_formspec(player)
return false
end
local handler = smart_sfinv_api.get_handler( context )
handler:run_enhancements("receive_fields", player, context, fields)
end)
----------------------------------------------
-- Initialization: hacky access to some default variables
----------------------------------------------
local _dummy_page = orig_make_formspec(nil, {}, "|", true, nil)
smart_sfinv_api.default_size, smart_sfinv_api.theme_main, smart_sfinv_api.theme_inv = _dummy_page:match("(size%[[%d.,]+%]+)([^|]+)|([^|]+)")

View File

@ -0,0 +1 @@
smart_sfinv_api

View File

@ -0,0 +1,91 @@
smart_sfinv_api.register_enhancement({
----------------------------------------------
-- Rewrite get_nav_fs
----------------------------------------------
get_nav_fs = function(handler, player, context, nav, current_idx)
-- Only show tabs if there is more than one page
if #nav < 2 then
return
end
local nav_titles_above = {}
local current_idx_above = -1
context.nav_above = {}
local nav_titles_site = {}
context.current_idx_site = context.current_idx_site or 0
context.nav_site = {}
for idx, page in ipairs(context.nav) do
if page:sub(1,9) == "creative:" then
table.insert(nav_titles_site, nav[idx])
table.insert(context.nav_site, page)
if idx == current_idx then
context.current_idx_site = #nav_titles_site
end
else
table.insert(nav_titles_above, nav[idx])
table.insert(context.nav_above, page)
if idx == current_idx then
current_idx_above = #nav_titles_above
end
end
end
-- Add the creative tab. Select it if any creative is selected
if #nav_titles_site > 0 then
table.insert(nav_titles_above, 2, "Creative")
table.insert(context.nav_above, 2, "Creative")
if current_idx_above == -1 then
current_idx_above = 2 -- Creative
handler.formspec_before_navfs = "textlist[0,0;2.8,8.6;smart_sfinv_nav_site;" .. table.concat(nav_titles_site, ",") ..
";" .. context.current_idx_site .. ";true]container[3.5,0]"..handler.formspec_before_navfs
handler.formspec_after_content = handler.formspec_after_content.."container_end[]"
handler.formspec_resize = 'size[11,8.6]'
elseif current_idx_above >= 2 then
-- Because "Creative" is inserted, the index needs to be adjusted
current_idx_above = current_idx_above + 1
end
end
if #nav_titles_above > 0 then
handler.custom_nav_fs = "tabheader[0,0;smart_sfinv_nav_tabs_above;" .. table.concat(nav_titles_above, ",") ..
";" .. current_idx_above .. ";true;false]"
else
handler.custom_nav_fs = ""
end
end,
------------------------------------------------------------------------
-- Process input for enhanced navfs
------------------------------------------------------------------------
receive_fields = function(handler, player, context, fields)
-- Was a header tab selected?
if fields.smart_sfinv_nav_tabs_above and context.nav_above then
local tid = tonumber(fields.smart_sfinv_nav_tabs_above)
if tid and tid > 0 then
local id = context.nav_above[tid]
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
elseif id == "Creative" then
local id = context.nav_site[context.current_idx_site]
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
end
end
end
-- Was a site table selected?
elseif fields.smart_sfinv_nav_site and context.nav_site then
local tid = minetest.explode_textlist_event(fields.smart_sfinv_nav_site).index
if tid and tid > 0 then
local id = context.nav_site[tid]
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
end
end
end
end
})

View File

@ -1,134 +0,0 @@
local orig_make_formspec = sfinv.make_formspec
function sfinv.make_formspec(player, context, content, show_inv, size)
context.site_pane_size = 3
context.page_size_x = 8
context.page_size_y = 8.6
if size then
context.page_size_x, context.page_size_y = size:sub(6,-2):match("([^,]+),([^,]+)")
context.page_size_x = tonumber(context.page_size_x)
context.page_size_y = tonumber(context.page_size_y)
else
size = 'size['..context.page_size_x..','..context.page_size_y..']'
end
local formspec = orig_make_formspec(player, context, content, show_inv, size)
if context.nav_site_enabled then
local newsize = 'size['..(context.page_size_x+context.site_pane_size+1)..','..(context.page_size_y)..']'
formspec = formspec:gsub(size:gsub("([^%w])", "%%%1"), newsize)
formspec = formspec .. "container_end[]"
end
return formspec
end
------------------------------------------------------------------------
-- Build up the enhanced nav_fs
------------------------------------------------------------------------
function sfinv.get_nav_fs(player, context, nav, current_idx)
-- Only show tabs if there is more than one page
if #nav < 2 then
return ""
end
local nav_titles_above = {}
local current_idx_above = -1
context.nav_above = {}
local nav_titles_site = {}
context.current_idx_site = context.current_idx_site or 0
context.nav_site = {}
context.nav_site_enabled = false
for idx, page in ipairs(context.nav) do
if page:sub(1,9) == "creative:" then
table.insert(nav_titles_site, nav[idx])
table.insert(context.nav_site, page)
if idx == current_idx then
context.current_idx_site = #nav_titles_site
end
else
table.insert(nav_titles_above, nav[idx])
table.insert(context.nav_above, page)
if idx == current_idx then
current_idx_above = #nav_titles_above
end
end
end
local formspec = ""
-- Add the creative tab. Select it if any creative is selected
if #nav_titles_site > 0 then
table.insert(nav_titles_above, 2, "Creative")
table.insert(context.nav_above, 2, "Creative")
if current_idx_above == -1 then
context.nav_site_enabled = true
current_idx_above = 2 -- Creative
formspec = formspec.."textlist[0,0;" ..(context.site_pane_size-0.2).. "," .. context.page_size_y ..
";smart_sfinv_nav_site;" .. table.concat(nav_titles_site, ",") ..
";" .. context.current_idx_site .. ";true]container["..(context.site_pane_size+0.5)..",0]"
elseif current_idx_above >= 2 then
-- Because "Creative" is inserted, the index needs to be adjusted
current_idx_above = current_idx_above + 1
end
end
if #nav_titles_above > 0 then
formspec = formspec.."tabheader[0,0;smart_sfinv_nav_tabs_above;" .. table.concat(nav_titles_above, ",") ..
";" .. current_idx_above .. ";true;false]"
end
return formspec
end
------------------------------------------------------------------------
-- Process input for enhanced navfs
------------------------------------------------------------------------
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "" or not sfinv.enabled then
return false
end
-- Get Context
local name = player:get_player_name()
local context = sfinv.contexts[name]
if not context then
sfinv.set_player_inventory_formspec(player)
return false
end
-- Was a header tab selected?
if fields.smart_sfinv_nav_button_above and context.nav_above then
local id = context.nav_above[1]
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
end
elseif fields.smart_sfinv_nav_tabs_above and context.nav_above then
local tid = tonumber(fields.smart_sfinv_nav_tabs_above)
if tid and tid > 0 then
local id = context.nav_above[tid]
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
elseif id == "Creative" then
local id = context.nav_site[context.current_idx_site]
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
end
end
end
-- Was a site table selected?
elseif fields.smart_sfinv_nav_site and context.nav_site then
local tid = minetest.explode_textlist_event(fields.smart_sfinv_nav_site).index
if tid and tid > 0 then
local id = context.nav_site[tid]
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
end
end
end
end)