Crafting guide : check if item is craftable on clicking on a button

This commit is contained in:
kilbith 2016-01-11 17:52:15 +01:00
parent 7c09adf120
commit 8bd1628a24

View File

@ -52,7 +52,8 @@ end
function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id) function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
local inv_size = meta:get_int("inv_size") local inv_size = meta:get_int("inv_size")
local pagemax = math.floor((inv_size - 1) / (8*3) + 1) local npp, i, s = 8*3, 0, 0
local pagemax = math.floor((inv_size - 1) / npp + 1)
if pagenum > pagemax then if pagenum > pagemax then
pagenum = 1 pagenum = 1
@ -75,7 +76,6 @@ function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter,
",#FFFFFF,/ "..tostring(pagemax).."]".. ",#FFFFFF,/ "..tostring(pagemax).."]"..
"field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
local npp, i, s = 8*3, 0, 0
for _, name in pairs(worktable.craftguide_main_list(meta, filter, tab_id)) do for _, name in pairs(worktable.craftguide_main_list(meta, filter, tab_id)) do
if s < (pagenum - 1) * npp then if s < (pagenum - 1) * npp then
s = s + 1 s = s + 1
@ -93,13 +93,13 @@ function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter,
if recipe_num > items_num then recipe_num = 1 end if recipe_num > items_num then recipe_num = 1 end
if items_num > 1 then if items_num > 1 then
formspec = formspec.."button[0,5;1.6,1;alternate;Alternate]".. formspec = formspec.."button[0,6;1.6,1;alternate;Alternate]"..
"label[0,4.5;Recipe "..recipe_num.." of "..items_num.."]" "label[0,5.5;Recipe "..recipe_num.." of "..items_num.."]"
end end
local type = minetest.get_all_craft_recipes(item)[recipe_num].type local type = minetest.get_all_craft_recipes(item)[recipe_num].type
if type == "cooking" then if type == "cooking" then
formspec = formspec.."image[4.25,4.6;0.5,0.5;default_furnace_fire_fg.png]" formspec = formspec.."image[3.75,4.6;0.5,0.5;default_furnace_fire_fg.png]"
end end
local items = minetest.get_all_craft_recipes(item)[recipe_num].items local items = minetest.get_all_craft_recipes(item)[recipe_num].items
@ -113,14 +113,13 @@ function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter,
end end
for i, v in pairs(items) do for i, v in pairs(items) do
formspec = formspec.."item_image_button["..((i-1) % width + 5)..",".. formspec = formspec.."item_image_button["..((i-1) % width + 4.5)..","..
(math.floor((i-1) / width + (6 - math.min(2, rows))))..";1,1;".. (math.floor((i-1) / width + (6 - math.min(2, rows))))..";1,1;"..
worktable.get_recipe(v)..";"..worktable.get_recipe(v)..";"..is_group(v).."]" worktable.get_recipe(v)..";"..worktable.get_recipe(v)..";"..is_group(v).."]"
end end
formspec = formspec.."image[4,5;1,1;gui_furnace_arrow_bg.png^[transformR90]".. formspec = formspec.."item_image[2.5,5;1,1;"..item.."]"..
"item_image_button[3,5;1,1;"..item..";"..item..";]".. "image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90]"
"label[0,6.1;"..item:sub(1,30).."]"
end end
meta:set_string("formspec", formspec) meta:set_string("formspec", formspec)
@ -215,8 +214,7 @@ function worktable.fields(pos, _, fields)
local formspec = meta:to_table().fields.formspec local formspec = meta:to_table().fields.formspec
local filter = formspec:match("filter;;([%w_:]+)") or "" local filter = formspec:match("filter;;([%w_:]+)") or ""
local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1 local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
local current_item = formspec:match("item_image_button%[3,5;1,1;([%w_:]+)") or "" local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*]")) or 1
local current_tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*]")) or 1
if fields.back then if fields.back then
worktable.formspecs.main(meta) worktable.formspecs.main(meta)
@ -228,12 +226,13 @@ function worktable.fields(pos, _, fields)
worktable.craftguide_main_list(meta, nil, 1) worktable.craftguide_main_list(meta, nil, 1)
worktable.craftguide_formspec(meta, 1, nil, 1, "", 1) worktable.craftguide_formspec(meta, 1, nil, 1, "", 1)
elseif fields.alternate then elseif fields.alternate then
local item = formspec:match("item_image%[.*;([%w_:]+)%]") or ""
local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
recipe_num = recipe_num + 1 recipe_num = recipe_num + 1
worktable.craftguide_formspec(meta, pagenum, current_item, recipe_num, filter, current_tab_id) worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
elseif fields.search then elseif fields.search then
worktable.craftguide_main_list(meta, fields.filter:lower(), current_tab_id) worktable.craftguide_main_list(meta, fields.filter:lower(), tab_id)
worktable.craftguide_formspec(meta, 1, nil, 1, fields.filter:lower(), current_tab_id) worktable.craftguide_formspec(meta, 1, nil, 1, fields.filter:lower(), tab_id)
elseif fields.tabs then elseif fields.tabs then
worktable.craftguide_main_list(meta, filter, tonumber(fields.tabs)) worktable.craftguide_main_list(meta, filter, tonumber(fields.tabs))
worktable.craftguide_formspec(meta, 1, nil, 1, filter, tonumber(fields.tabs)) worktable.craftguide_formspec(meta, 1, nil, 1, filter, tonumber(fields.tabs))
@ -243,11 +242,12 @@ function worktable.fields(pos, _, fields)
else else
pagenum = pagenum + 1 pagenum = pagenum + 1
end end
worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, current_tab_id) worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, tab_id)
else else
for item in pairs(fields) do for item in pairs(fields) do
if item:match("[%w_]+:[%w_]+") then if item:match("[%w_]+:[%w_]+") and
worktable.craftguide_formspec(meta, pagenum, item, 1, filter, current_tab_id) minetest.get_craft_recipe(item).items then
worktable.craftguide_formspec(meta, pagenum, item, 1, filter, tab_id)
end end
end end
end end