Update sfinv

This commit is contained in:
rubenwardy 2017-10-06 01:35:51 +01:00
parent 1e0f007264
commit 0543f15de8
6 changed files with 146 additions and 176 deletions

View file

@ -1,11 +1,3 @@
local fs = [[
size[8,8.6]
bgcolor[#080808BB;true]
background[5,5;1,1;gui_formbg.png;true]
{{ nav }}
textlist[0,0;7.85,8.5;help;
]]
local items = { local items = {
"Tips", "Tips",
"", "",
@ -26,12 +18,15 @@ local items = {
for i = 1, #items do for i = 1, #items do
items[i] = minetest.formspec_escape(items[i]) items[i] = minetest.formspec_escape(items[i])
end end
fs = fs .. table.concat(items, ",") .. "]"
local fs = [[
textlist[0,0;7.85,8.5;help;
]] .. table.concat(items, ",") .. "]"
sfinv.register_page("ctf_inventory:help", { sfinv.register_page("ctf_inventory:help", {
title = "Help", title = "Help",
get = function(player, context) get = function(self, player, context)
return fs return sfinv.make_formspec(player, context, fs, false)
end end
}) })

View file

@ -140,12 +140,7 @@ if minetest.global_exists("sfinv") then
title = "Inbox", title = "Inbox",
get = function(self, player, context) get = function(self, player, context)
local name = player:get_player_name() local name = player:get_player_name()
return ([[ return sfinv.make_formspec(player, context, email.get_formspec(name), false, "size[12,8]")
size[12,8]
bgcolor[#080808BB;true]
background[5,5;1,1;gui_formbg.png;true]
{{ nav }}
]]) .. email.get_formspec(name)
end end
}) })
end end

View file

@ -1,84 +1,21 @@
# Simple Fast Inventory (WIP) Simple Fast Inventory
====================
![SFINV Screeny](https://cdn.pbrd.co/images/1yQhd1TI.png) ![SFINV Screeny](https://cdn.pbrd.co/images/1yQhd1TI.png)
A cleaner, simpler, solution to having an advanced inventory in Minetest. A cleaner, simpler, solution to having an advanced inventory in Minetest.
Formspec style based on the creative inventory.
Written by rubenwardy. Written by rubenwardy.
License: WTFPL License: MIT
## Aims See game_api.txt for this mod's API
* Unified Inventory API compatible (a mod using UI's api will work with this) License of source code and media files:
* Themable. ---------------------------------------
* Clean API. Copyright (C) 2016 rubenwardy <rubenwardy@gmail.com>
# API Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
## Formspec Parser The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
sfinv has a variable based parser. Here is the formspec of the crafting tab: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{{ layout }}
list[current_player;craft;1.75,0.5;3,3;]
list[current_player;craftpreview;5.75,1.5;1,1;]
image[4.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]
listring[current_player;main]
listring[current_player;craft]
image[0,4.25;1,1;gui_hb_bg.png]
image[1,4.25;1,1;gui_hb_bg.png]
image[2,4.25;1,1;gui_hb_bg.png]
image[3,4.25;1,1;gui_hb_bg.png]
image[4,4.25;1,1;gui_hb_bg.png]
image[5,4.25;1,1;gui_hb_bg.png]
image[6,4.25;1,1;gui_hb_bg.png]
image[7,4.25;1,1;gui_hb_bg.png]
`{{ layout }}` will be replaced by the following:
size[8,8.6]
bgcolor[#080808BB;true]
background[5,5;1,1;gui_formbg.png;true]
{{ nav }}
listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]
list[current_player;main;0,4.25;8,1;]
list[current_player;main;0,5.5;8,3;8]
and `{{ nav }}` will be replaced by something like the following:
tabheader[0,0;tabs;Crafting,Page 1, Page 2;1]
Only two levels of variable parsing is guaranteed to succeed, as only two
passes are done.
Here is another example, this time a page with no inventory:
size[8,8.6]
bgcolor[#080808BB;true]
background[5,5;1,1;gui_formbg.png;true]
{{ nav }}
textlist[0,0;7.85,8.5;help;one,two,three]
The following variables are provided by the API:
* `name` - name of the player viewing
* `nav` - the navigation, probably a tabset
* `layout` - a default layout which has the players inventory at the bottom.
## sfinv.register_page
sfinv.register_page(name, def)
def is a table containing:
* `title(player, context)` - human readable page name (required)
* `get(player, context)` - returns a formspec string. See formspec variables. (required)
* `is_in_nav(player, context)` - return true if it appears in tab header
* `on_player_receive_fields(player, context, fields)` - on formspec submit
planned:
* `on_enter(player, context)` - when coming to this page from another
* `on_leave(player, context)` - when leaving this page to go to another

View file

@ -1,90 +1,144 @@
local theme = [[size[8,8.6]
bgcolor[#080808BB;true]
background[5,5;1,1;gui_formbg.png;true]
{{ nav }}
listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]
list[current_player;main;0,4.25;8,1;]
list[current_player;main;0,5.5;8,3;8] ]]
sfinv = { sfinv = {
pages = {}, pages = {},
pages_unordered = {}, pages_unordered = {},
homepage_name = "sfinv:crafting", contexts = {},
contexts = {} enabled = true
} }
function sfinv.register_page(name, def) function sfinv.register_page(name, def)
if not name or not def or not def.get then assert(name, "Invalid sfinv page. Requires a name")
error("Invalid sfinv page. Requires a name & def, and a get function in def") assert(def, "Invalid sfinv page. Requires a def[inition] table")
end 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))
sfinv.pages[name] = def sfinv.pages[name] = def
def.name = name def.name = name
table.insert(sfinv.pages_unordered, def) table.insert(sfinv.pages_unordered, def)
end end
function sfinv.parse_variables(fs, vars) function sfinv.override_page(name, def)
local ret = fs assert(name, "Invalid sfinv page override. Requires a name")
for key, value in pairs(vars) do assert(def, "Invalid sfinv page override. Requires a def[inition] table")
ret = string.gsub(ret, "{{([ ]+)" .. key .. "([ ]+)}}", value) 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
return ret
end end
function sfinv.get(player, context) function sfinv.get_nav_fs(player, context, nav, current_idx)
local page = sfinv.pages[context.page] -- Only show tabs if there is more than one page
if not page then if #nav > 1 then
page = sfinv.pages["404"] return "tabheader[0,0;tabs;" .. table.concat(nav, ",") .. ";" .. current_idx .. ";true;false]"
else
return ""
end end
end
local fs = page:get(player, context) local theme_main = "bgcolor[#080808BB;true]" .. default.gui_bg ..
default.gui_bg_img
local theme_inv = default.gui_slots .. [[
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]",
theme_main,
sfinv.get_nav_fs(player, context, context.nav_titles, context.nav_idx),
content
}
if show_inv then
tmp[#tmp + 1] = theme_inv
end
return table.concat(tmp, "")
end
function sfinv.get_homepage_name(player)
return "sfinv:crafting"
end
function sfinv.get_formspec(player, context)
-- Generate navigation tabs
local nav = {} local nav = {}
local nav_ids = {} local nav_ids = {}
local current_idx = 1 local current_idx = 1
for i, pdef in pairs(sfinv.pages_unordered) do for i, pdef in pairs(sfinv.pages_unordered) do
if not pdef.is_in_nav or pdef.is_in_nav(player, context) then if not pdef.is_in_nav or pdef:is_in_nav(player, context) then
nav[#nav + 1] = pdef.title nav[#nav + 1] = pdef.title
nav_ids[#nav_ids + 1] = pdef.name nav_ids[#nav_ids + 1] = pdef.name
if pdef.name == context.page then if pdef.name == context.page then
current_idx = i current_idx = #nav_ids
end end
end end
end end
context.nav = nav_ids context.nav = nav_ids
context.nav_titles = nav
context.nav_idx = current_idx
local vars = { -- Generate formspec
layout = theme, local page = sfinv.pages[context.page] or sfinv.pages["404"]
name = player:get_player_name(), if page then
nav = "tabheader[0,0;tabs;" .. table.concat(nav, ",") .. ";" .. current_idx .. ";true;false]" return page:get(player, context)
} else
fs = sfinv.parse_variables(fs, vars) local old_page = context.page
fs = sfinv.parse_variables(fs, vars) context.page = sfinv.get_homepage_name(player)
return fs assert(sfinv.pages[context.page], "[sfinv] Invalid homepage")
minetest.log("warning", "[sfinv] Couldn't find " .. dump(old_page) .. " so using switching to homepage")
return sfinv.get_formspec(player, context)
end
end end
function sfinv.set(player, context) function sfinv.get_or_create_context(player)
local name = player:get_player_name()
local context = sfinv.contexts[name]
if not context then if not context then
local name = player:get_player_name() context = {
context = sfinv.contexts[name] page = sfinv.get_homepage_name(player)
if not context then }
context = { sfinv.contexts[name] = context
page = sfinv.homepage_name
}
sfinv.contexts[name] = context
end
end end
return context
end
local fs = sfinv.get(player, context) function sfinv.set_context(player, context)
sfinv.contexts[player:get_player_name()] = context
end
function sfinv.set_player_inventory_formspec(player, context)
local fs = sfinv.get_formspec(player,
context or sfinv.get_or_create_context(player))
player:set_inventory_formspec(fs) player:set_inventory_formspec(fs)
end end
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
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
minetest.after(0.5, function() if sfinv.enabled then
sfinv.set(player) sfinv.set_player_inventory_formspec(player)
end) end
end)
minetest.register_on_leaveplayer(function(player)
sfinv.contexts[player:get_player_name()] = nil
end) end)
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "" then if formname ~= "" or not sfinv.enabled then
return false return false
end end
@ -92,29 +146,25 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name() local name = player:get_player_name()
local context = sfinv.contexts[name] local context = sfinv.contexts[name]
if not context then if not context then
sfinv.set(player) sfinv.set_player_inventory_formspec(player)
return false return false
end end
-- Handle Events -- Was a tab selected?
if fields.tabs and context.nav then if fields.tabs and context.nav then
local tid = tonumber(fields.tabs) local tid = tonumber(fields.tabs)
if tid and tid > 0 then if tid and tid > 0 then
local id = context.nav[tid] local id = context.nav[tid]
if id and sfinv.pages[id] then local page = sfinv.pages[id]
print(name .. " views sfinv/" .. id) if id and page then
sfinv.set_page(player, id)
-- TODO: on_leave
context.page = id
sfinv.set(player, context)
end end
end end
return else
end -- Pass event to page
local page = sfinv.pages[context.page]
-- Pass to page if page and page.on_player_receive_fields then
local page = sfinv.pages[context.page] return page:on_player_receive_fields(player, context, fields)
if page and page.on_player_receive_fields then end
return page.on_player_receive_fields(player, context, fields)
end end
end) end)

1
mods/sfinv/depends.txt Normal file
View file

@ -0,0 +1 @@
default

View file

@ -2,29 +2,21 @@ dofile(minetest.get_modpath("sfinv") .. "/api.lua")
sfinv.register_page("sfinv:crafting", { sfinv.register_page("sfinv:crafting", {
title = "Crafting", title = "Crafting",
is_in_nav = function(player, context)
return true
end,
get = function(self, player, context) get = function(self, player, context)
return [[ {{ layout }} return sfinv.make_formspec(player, context, [[
list[current_player;craft;1.75,0.5;3,3;] list[current_player;craft;1.75,0.5;3,3;]
list[current_player;craftpreview;5.75,1.5;1,1;] list[current_player;craftpreview;5.75,1.5;1,1;]
image[4.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270] image[4.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]
listring[current_player;main] listring[current_player;main]
listring[current_player;craft] listring[current_player;craft]
image[0,4.25;1,1;gui_hb_bg.png] image[0,4.75;1,1;gui_hb_bg.png]
image[1,4.25;1,1;gui_hb_bg.png] image[1,4.75;1,1;gui_hb_bg.png]
image[2,4.25;1,1;gui_hb_bg.png] image[2,4.75;1,1;gui_hb_bg.png]
image[3,4.25;1,1;gui_hb_bg.png] image[3,4.75;1,1;gui_hb_bg.png]
image[4,4.25;1,1;gui_hb_bg.png] image[4,4.75;1,1;gui_hb_bg.png]
image[5,4.25;1,1;gui_hb_bg.png] image[5,4.75;1,1;gui_hb_bg.png]
image[6,4.25;1,1;gui_hb_bg.png] image[6,4.75;1,1;gui_hb_bg.png]
image[7,4.25;1,1;gui_hb_bg.png] ]] image[7,4.75;1,1;gui_hb_bg.png]
end, ]], true)
on_player_receive_fields = function(player, context, fields)
print("Received!")
end,
on_leave = function(player, context)
print("Left page!")
end end
}) })