147 lines
4.2 KiB
Lua
Raw Normal View History

2017-02-05 13:51:48 +01:00
local modpath = minetest.get_modpath(minetest.get_current_modname())
smart_inventory = {}
smart_inventory.skins_mod = minetest.get_modpath("skins")
smart_inventory.armor_mod = minetest.get_modpath("3d_armor")
smart_inventory.doc_items_mod = minetest.get_modpath("doc_items")
2017-02-05 13:51:48 +01:00
smart_inventory.registered_pages = {}
smart_inventory.smartfs = dofile(modpath.."/smartfs.lua")
smart_inventory.smartfs_elements = dofile(modpath.."/smartfs-elements.lua")
2017-02-05 13:51:48 +01:00
smartfs = smart_inventory.smartfs
-- start with empty group items replacement table.
-- Will be filled at runtime with used items. partially independend on user
2017-02-05 13:51:48 +01:00
local inventory_form = smartfs.create("smart_inventory:main", function(state)
-- tabbed view controller
local tab_controller = {
_tabs = {},
active_name = nil,
set_active = function(self, tabname)
for name, def in pairs(self._tabs) do
if name == tabname then
def.button:setBackground("halo.png")
2017-02-14 23:26:48 +01:00
def.view:setVisible(true)
2017-02-05 13:51:48 +01:00
else
def.button:setBackground(nil)
2017-02-14 23:26:48 +01:00
def.view:setVisible(false)
2017-02-05 13:51:48 +01:00
end
end
self.active_name = tabname
end,
tab_add = function(self, name, def)
2017-02-10 22:30:31 +01:00
def.viewstate:size(20,10) --size of tab view
2017-02-05 13:51:48 +01:00
self._tabs[name] = def
end,
get_active_name = function(self)
return self.active_name
end,
}
--set screen size
2017-02-10 22:30:31 +01:00
state:size(20,12)
2017-02-05 13:51:48 +01:00
state:label(1,0.2,"header","Smart Inventory")
state:image(0,0,1,1,"header_logo", "logo.png")
-- state:image_button(19,0,1,1,"exit", "Exit","???.png", true)
state:button(19,0,1,1,"exit", "Exit", true)
2017-02-05 13:51:48 +01:00
local button_x = 0.1
table.sort(smart_inventory.registered_pages, function(a,b)
if not a.sequence then
return false
elseif not b.sequence then
return true
elseif a.sequence > b.sequence then
return false
else
return true
end
end)
for _, def in ipairs(smart_inventory.registered_pages) do
2017-02-05 13:51:48 +01:00
assert(def.smartfs_callback, "Callback function needed")
assert(def.name, "Name is needed")
2017-02-05 13:51:48 +01:00
local tabdef = {}
local label
if not def.label then
label = ""
else
label = def.label
end
2017-02-10 22:30:31 +01:00
tabdef.button = state:button(button_x,11.2,1,1,def.name.."_button",label)
2017-02-05 13:51:48 +01:00
if def.icon then
tabdef.button:setImage(def.icon)
end
2017-02-13 22:26:43 +01:00
tabdef.button:setTooltip(def.tooltip)
2017-02-05 13:51:48 +01:00
tabdef.button:onClick(function(self)
tab_controller:set_active(def.name)
2017-02-10 22:30:31 +01:00
if def.on_button_click then
def.on_button_click(tabdef.viewstate)
end
2017-02-05 13:51:48 +01:00
end)
tabdef.view = state:container(0,1,def.name.."_container")
2017-02-05 13:51:48 +01:00
tabdef.viewstate = tabdef.view:getContainerState()
def.smartfs_callback(tabdef.viewstate)
tab_controller:tab_add(def.name, tabdef)
button_x = button_x + 1
2017-02-05 13:51:48 +01:00
end
tab_controller:set_active(smart_inventory.registered_pages[1].name)
2017-02-05 13:51:48 +01:00
end)
smartfs.set_player_inventory(inventory_form)
function smart_inventory.register_page(def)
table.insert(smart_inventory.registered_pages, def)
2017-02-05 13:51:48 +01:00
end
function smart_inventory.get_page_state(pagename, playername)
local rootstate = smart_inventory.smartfs.inv[playername]
if not rootstate then
return
end
local view = rootstate:get(pagename.."_container")
if not view then
return
end
return view:getContainerState()
end
function smart_inventory.get_registered_page(pagename)
for _, registred_page in ipairs(smart_inventory.registered_pages) do
if registred_page.name == pagename then
return registred_page
end
end
end
-- get language for texts
local LANG = minetest.setting_get("language")
if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end
if not (LANG and (LANG ~= "")) then LANG = "en" end
local txtfile = modpath.."/classify_description_"..LANG:sub(1,2)..".lua"
-- build up caches
local f=io.open(txtfile,"r")
if f~=nil then
io.close(f)
smart_inventory.txt = dofile(txtfile)
else
smart_inventory.txt = dofile(modpath.."/classify_description_en.lua")
end
smart_inventory.filter = dofile(modpath.."/filter.lua")
smart_inventory.doc_addon = dofile(modpath.."/doc_addon.lua")
smart_inventory.cache = dofile(modpath.."/cache.lua")
2017-02-21 23:17:53 +01:00
smart_inventory.ui_tools = dofile(modpath.."/ui_tools.lua")
-- register pages
2017-02-23 09:16:06 +01:00
dofile(modpath.."/crafting.lua")
2017-02-21 23:17:53 +01:00
if minetest.setting_getbool("creative_mode") then
dofile(modpath.."/creative.lua")
end
2017-02-12 23:28:21 +01:00
if smart_inventory.skins_mod or smart_inventory.armor_mod then
dofile(modpath.."/player.lua")
end
2017-02-12 23:28:21 +01:00