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 = {
"Tips",
"",
@ -26,12 +18,15 @@ local items = {
for i = 1, #items do
items[i] = minetest.formspec_escape(items[i])
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", {
title = "Help",
get = function(player, context)
return fs
get = function(self, player, context)
return sfinv.make_formspec(player, context, fs, false)
end
})

View file

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

View file

@ -1,84 +1,21 @@
# Simple Fast Inventory (WIP)
Simple Fast Inventory
====================
![SFINV Screeny](https://cdn.pbrd.co/images/1yQhd1TI.png)
A cleaner, simpler, solution to having an advanced inventory in Minetest.
Formspec style based on the creative inventory.
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)
* Themable.
* Clean API.
License of source code and media files:
---------------------------------------
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:
{{ 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
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.

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 = {
pages = {},
pages_unordered = {},
homepage_name = "sfinv:crafting",
contexts = {}
contexts = {},
enabled = true
}
function sfinv.register_page(name, def)
if not name or not def or not def.get then
error("Invalid sfinv page. Requires a name & def, and a get function in def")
end
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))
sfinv.pages[name] = def
def.name = name
table.insert(sfinv.pages_unordered, def)
end
function sfinv.parse_variables(fs, vars)
local ret = fs
for key, value in pairs(vars) do
ret = string.gsub(ret, "{{([ ]+)" .. key .. "([ ]+)}}", value)
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
return ret
end
function sfinv.get(player, context)
local page = sfinv.pages[context.page]
if not page then
page = sfinv.pages["404"]
function sfinv.get_nav_fs(player, context, nav, current_idx)
-- Only show tabs if there is more than one page
if #nav > 1 then
return "tabheader[0,0;tabs;" .. table.concat(nav, ",") .. ";" .. current_idx .. ";true;false]"
else
return ""
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_ids = {}
local current_idx = 1
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_ids[#nav_ids + 1] = pdef.name
if pdef.name == context.page then
current_idx = i
current_idx = #nav_ids
end
end
end
context.nav = nav_ids
context.nav_titles = nav
context.nav_idx = current_idx
local vars = {
layout = theme,
name = player:get_player_name(),
nav = "tabheader[0,0;tabs;" .. table.concat(nav, ",") .. ";" .. current_idx .. ";true;false]"
}
fs = sfinv.parse_variables(fs, vars)
fs = sfinv.parse_variables(fs, vars)
return fs
-- 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
context.page = sfinv.get_homepage_name(player)
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
function sfinv.set(player, context)
if not context then
function sfinv.get_or_create_context(player)
local name = player:get_player_name()
context = sfinv.contexts[name]
local context = sfinv.contexts[name]
if not context then
context = {
page = sfinv.homepage_name
page = sfinv.get_homepage_name(player)
}
sfinv.contexts[name] = context
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)
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.after(0.5, function()
sfinv.set(player)
end)
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
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "" then
if formname ~= "" or not sfinv.enabled then
return false
end
@ -92,29 +146,25 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
local context = sfinv.contexts[name]
if not context then
sfinv.set(player)
sfinv.set_player_inventory_formspec(player)
return false
end
-- Handle Events
-- Was a tab selected?
if fields.tabs and context.nav then
local tid = tonumber(fields.tabs)
if tid and tid > 0 then
local id = context.nav[tid]
if id and sfinv.pages[id] then
print(name .. " views sfinv/" .. id)
-- TODO: on_leave
context.page = id
sfinv.set(player, context)
local page = sfinv.pages[id]
if id and page then
sfinv.set_page(player, id)
end
end
return
end
-- Pass to page
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)
return page:on_player_receive_fields(player, context, fields)
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", {
title = "Crafting",
is_in_nav = function(player, context)
return true
end,
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;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] ]]
end,
on_player_receive_fields = function(player, context, fields)
print("Received!")
end,
on_leave = function(player, context)
print("Left page!")
image[0,4.75;1,1;gui_hb_bg.png]
image[1,4.75;1,1;gui_hb_bg.png]
image[2,4.75;1,1;gui_hb_bg.png]
image[3,4.75;1,1;gui_hb_bg.png]
image[4,4.75;1,1;gui_hb_bg.png]
image[5,4.75;1,1;gui_hb_bg.png]
image[6,4.75;1,1;gui_hb_bg.png]
image[7,4.75;1,1;gui_hb_bg.png]
]], true)
end
})