moved some methods from cache to ui_tools

This commit is contained in:
Alexander Weber 2017-04-15 21:51:00 +02:00
parent 0ec772192e
commit 8b4e917d50
5 changed files with 173 additions and 173 deletions

View File

@ -13,12 +13,12 @@ smart_inventory.smartfs = dofile(modpath.."/libs/smartfs.lua")
smart_inventory.smartfs_elements = dofile(modpath.."/libs/smartfs-elements.lua")
smart_inventory.doc_addon = dofile(modpath.."/doc_addon.lua")
smart_inventory.ui_tools = dofile(modpath.."/ui_tools.lua")
smart_inventory.filter = dofile(modpath.."/libs/filter.lua")
smart_inventory.cache = dofile(modpath.."/libs/cache.lua")
smart_inventory.maininv = dofile(modpath.."/libs/maininv.lua")
smart_inventory.ui_tools = dofile(modpath.."/ui_tools.lua")
-- register pages
dofile(modpath.."/inventory_framework.lua")
dofile(modpath.."/pages/crafting.lua")

View File

@ -66,7 +66,7 @@ function crecipes.new(recipe)
self._recipe = recipe
self.recipe_type = recipe.type
self._items = {}
self.recipe_items = self._items --???
-- self.recipe_items = self._items --???
-----------------------------------------------------
-- analyze all data. Return false if invalid recipe. true on success
@ -359,167 +359,6 @@ function cache.fill_recipe_cache()
end
end
-----------------------------------------------------
-- Sort items to groups and decide which groups should be displayed
-----------------------------------------------------
function cache.get_list_grouped(itemtable)
local grouped = {}
-- sort the entries to groups
for _, entry in ipairs(itemtable) do
if cache.citems[entry.item] then
for _, group in pairs(cache.citems[entry.item].cgroups) do
if cache.cgroups[group.name].keyword then
if not grouped[group.name] then
local group_info = {}
group_info.name = group.name
group_info.cgroup = cache.cgroups[group.name]
group_info.items = {}
grouped[group.name] = group_info
end
table.insert(grouped[group.name].items, entry)
end
end
end
end
-- magic to calculate relevant groups
local itemcount = #itemtable
local best_group_count = itemcount ^(1/3)
local best_group_size = (itemcount / best_group_count) * 1.5
best_group_count = math.floor(best_group_count)
local sorttab = {}
for k,v in pairs(grouped) do
if #v.items < 3 or #v.items >= itemcount - 3 then
grouped[k] = nil
else
v.group_size = #v.items
v.unique_count = #v.items
v.best_group_size = best_group_size
v.diff = math.abs(v.group_size - v.best_group_size)
table.insert(sorttab, v)
end
end
local outtab = {}
local assigned_items = {}
if best_group_count > 0 then
for i = 1, best_group_count do
-- sort by best size
table.sort(sorttab, function(a,b)
return a.diff < b.diff
end)
local sel = sorttab[1]
if not sel then
break
end
outtab[sel.name] = {
name = sel.name,
group_desc = sel.cgroup.group_desc,
items = sel.items
}
table.remove(sorttab, 1)
for _, item in ipairs(sel.items) do
assigned_items[item.item] = true
-- update the not selected groups
for _, group in pairs(cache.citems[item.item].cgroups) do
if group.name ~= sel.name then
local u = grouped[group.name]
if u and u.unique_count and u.group_size > 0 then
u.unique_count = u.unique_count-1
if (u.group_size < u.best_group_size) or
(u.group_size - u.best_group_size) < (u.best_group_size - u.unique_count) then
sel.diff = u.best_group_size - u.unique_count
end
end
end
end
end
for idx = #sorttab, 1, -1 do
if sorttab[idx].unique_count < 3 or
( sel.cgroup.parent and sel.cgroup.parent.name == sorttab[idx].name ) or
( sel.cgroup.childs and sel.cgroup.childs[sorttab[idx].name] )
then
grouped[sorttab[idx].name] = nil
table.remove(sorttab, idx)
end
end
end
end
-- fill other group
local other = {}
for _, item in ipairs(itemtable) do
if not assigned_items[item.item] then
table.insert(other, item)
end
end
-- default groups
outtab.all = {}
outtab.all.name = "all"
outtab.all.group_desc = txt[outtab.all.name].label
outtab.all.items = itemtable
outtab.other = {}
outtab.other.name = "other"
outtab.other.group_desc = txt[outtab.other.name].label
outtab.other.items = other
return outtab
end
-----------------------------------------------------
-- Get all items available
-----------------------------------------------------
function cache.get_all_items()
local outtab = {}
local outtab_material = {}
for itemname, citem in pairs(cache.citems) do
local entry = {
citem = citem,
-- buttons_grid related
item = itemname,
is_button = true
}
if cache.citems[itemname].cgroups["shape"] then
table.insert(outtab_material, entry)
else
table.insert(outtab, entry)
end
end
return outtab, outtab_material
end
-----------------------------------------------------
-- Get all revealed items available
-----------------------------------------------------
function cache.get_revealed_items(player)
local outtab = {}
for itemname, citem in pairs(cache.citems) do
if doc_addon.is_revealed_item(itemname, player) then
local entry = {
citem = citem,
itemdef = minetest.registered_items[itemname],
recipes = cache.citems[itemname].in_output_recipe,
-- buttons_grid related
item = itemname,
is_button = true
}
table.insert(outtab, entry)
end
end
return outtab
end
-----------------------------------------------------
-- fill the cache after all mods loaded
-----------------------------------------------------

View File

@ -162,7 +162,7 @@ local function update_from_recipelist(state, recipelist)
table.insert(craftable_itemlist, entry)
end
end
state.param.crafting_grouped_items = cache.get_list_grouped(craftable_itemlist)
state.param.crafting_grouped_items = ui_tools.get_list_grouped(craftable_itemlist)
update_group_selection(state, true)
end
@ -370,8 +370,8 @@ local function crafting_callback(state)
local searchfield = state:field(13.3, 4.5, 3, 0.5, "search")
searchfield:setCloseOnEnter(false)
searchfield:onKeyEnter(function(self, state, player)
local filtered_list = ui_tools.search_in_list(cache.get_revealed_items(player), self:getText(), player)
state.param.crafting_grouped_items = cache.get_list_grouped(filtered_list)
local filtered_list = ui_tools.search_in_list(ui_tools.get_revealed_items(player), self:getText(), player)
state.param.crafting_grouped_items = ui_tools.get_list_grouped(filtered_list)
-- reset group selection if proposal mode is changed
if state.param.survival_proposal_mode ~= "search" then
state.param.survival_proposal_mode = "search"

View File

@ -40,7 +40,7 @@ local function update_group_selection(state, changed_group)
groups_sel3:clearItems()
else
-- update group 2
grouped = cache.get_list_grouped(grouped[state.param.creative_group_list1[sel_id]].items)
grouped = ui_tools.get_list_grouped(grouped[state.param.creative_group_list1[sel_id]].items)
if changed_group < 2 or not state.param.creative_group_list2 then
state.param.creative_group_list2 = ui_tools.update_group_selection(grouped, groups_sel2, state.param.creative_group_list2)
end
@ -53,7 +53,7 @@ local function update_group_selection(state, changed_group)
groups_sel3:clearItems()
else
-- update group 3
grouped = cache.get_list_grouped(grouped[state.param.creative_group_list2[sel_id]].items)
grouped = ui_tools.get_list_grouped(grouped[state.param.creative_group_list2[sel_id]].items)
if changed_group < 3 or not state.param.creative_group_list3 then
state.param.creative_group_list3 = ui_tools.update_group_selection(grouped, groups_sel3, state.param.creative_group_list3)
end
@ -114,7 +114,7 @@ local function creative_callback(state)
searchfield:onKeyEnter(function(self, state, player)
local search_string = self:getText()
local filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_all, search_string)
state.param.creative_grouped_items = cache.get_list_grouped(filtered_list)
state.param.creative_grouped_items = ui_tools.get_list_grouped(filtered_list)
filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_material_all, search_string)
state.param.creative_grouped_material_items = filtered_list
update_group_selection(state, 0)
@ -173,8 +173,8 @@ local function creative_callback(state)
end)
-- fill with data
state.param.creative_grouped_items_all, state.param.creative_grouped_items_material_all = cache.get_all_items()
state.param.creative_grouped_items = cache.get_list_grouped(state.param.creative_grouped_items_all)
state.param.creative_grouped_items_all, state.param.creative_grouped_items_material_all = ui_tools.get_all_items()
state.param.creative_grouped_items = ui_tools.get_list_grouped(state.param.creative_grouped_items_all)
state.param.creative_grouped_material_items = state.param.creative_grouped_items_material_all
update_group_selection(state, 0)
end

View File

@ -1,5 +1,8 @@
local ui_tools = {}
local cache = smart_inventory.cache
local txt = smart_inventory.txt
local doc_addon = smart_inventory.doc_addon
local ui_tools = {}
-----------------------------------------------------
-- Group item list and prepare for output
-----------------------------------------------------
@ -88,7 +91,6 @@ function ui_tools.create_trash_inv(state, name)
end
function ui_tools.search_in_list(list, search_string, playername)
local cache = smart_inventory.cache
local filtered_list = {}
search_string = search_string:lower()
for _, entry in ipairs(list) do
@ -150,5 +152,164 @@ function ui_tools.get_tight_groups(cgroups)
return out_list_sorted
end
-----------------------------------------------------
-- Get all items available
-----------------------------------------------------
function ui_tools.get_all_items()
local outtab = {}
local outtab_material = {}
for itemname, citem in pairs(cache.citems) do
local entry = {
citem = citem,
-- buttons_grid related
item = itemname,
is_button = true
}
if cache.citems[itemname].cgroups["shape"] then
table.insert(outtab_material, entry)
else
table.insert(outtab, entry)
end
end
return outtab, outtab_material
end
-----------------------------------------------------
-- Get all revealed items available
-----------------------------------------------------
function ui_tools.get_revealed_items(player)
local outtab = {}
for itemname, citem in pairs(cache.citems) do
if doc_addon.is_revealed_item(itemname, player) then
local entry = {
citem = citem,
itemdef = minetest.registered_items[itemname],
recipes = cache.citems[itemname].in_output_recipe,
-- buttons_grid related
item = itemname,
is_button = true
}
table.insert(outtab, entry)
end
end
return outtab
end
-----------------------------------------------------
-- Sort items to groups and decide which groups should be displayed
-----------------------------------------------------
function ui_tools.get_list_grouped(itemtable)
local grouped = {}
-- sort the entries to groups
for _, entry in ipairs(itemtable) do
if cache.citems[entry.item] then
for _, group in pairs(cache.citems[entry.item].cgroups) do
if cache.cgroups[group.name].keyword then
if not grouped[group.name] then
local group_info = {}
group_info.name = group.name
group_info.cgroup = cache.cgroups[group.name]
group_info.items = {}
grouped[group.name] = group_info
end
table.insert(grouped[group.name].items, entry)
end
end
end
end
-- magic to calculate relevant groups
local itemcount = #itemtable
local best_group_count = itemcount ^(1/3)
local best_group_size = (itemcount / best_group_count) * 1.5
best_group_count = math.floor(best_group_count)
local sorttab = {}
for k,v in pairs(grouped) do
if #v.items < 3 or #v.items >= itemcount - 3 then
grouped[k] = nil
else
v.group_size = #v.items
v.unique_count = #v.items
v.best_group_size = best_group_size
v.diff = math.abs(v.group_size - v.best_group_size)
table.insert(sorttab, v)
end
end
local outtab = {}
local assigned_items = {}
if best_group_count > 0 then
for i = 1, best_group_count do
-- sort by best size
table.sort(sorttab, function(a,b)
return a.diff < b.diff
end)
local sel = sorttab[1]
if not sel then
break
end
outtab[sel.name] = {
name = sel.name,
group_desc = sel.cgroup.group_desc,
items = sel.items
}
table.remove(sorttab, 1)
for _, item in ipairs(sel.items) do
assigned_items[item.item] = true
-- update the not selected groups
for _, group in pairs(cache.citems[item.item].cgroups) do
if group.name ~= sel.name then
local u = grouped[group.name]
if u and u.unique_count and u.group_size > 0 then
u.unique_count = u.unique_count-1
if (u.group_size < u.best_group_size) or
(u.group_size - u.best_group_size) < (u.best_group_size - u.unique_count) then
sel.diff = u.best_group_size - u.unique_count
end
end
end
end
end
for idx = #sorttab, 1, -1 do
if sorttab[idx].unique_count < 3 or
( sel.cgroup.parent and sel.cgroup.parent.name == sorttab[idx].name ) or
( sel.cgroup.childs and sel.cgroup.childs[sorttab[idx].name] )
then
grouped[sorttab[idx].name] = nil
table.remove(sorttab, idx)
end
end
end
end
-- fill other group
local other = {}
for _, item in ipairs(itemtable) do
if not assigned_items[item.item] then
table.insert(other, item)
end
end
-- default groups
outtab.all = {}
outtab.all.name = "all"
outtab.all.group_desc = txt[outtab.all.name].label
outtab.all.items = itemtable
outtab.other = {}
outtab.other.name = "other"
outtab.other.group_desc = txt[outtab.other.name].label
outtab.other.items = other
return outtab
end
--------------------------------
return ui_tools