Animated image buttons for groups (needs polish)

This commit is contained in:
Jean-Patrick Guerrero 2022-06-18 21:50:13 +02:00
parent e17e1080d3
commit a6605263f4
3 changed files with 72 additions and 39 deletions

View File

@ -1,11 +1,10 @@
local PNG = i3.files.styles()
local replacements = {fuel = {}}
local http = ...
IMPORT("maxn", "copy", "insert", "sort", "match", "sub")
IMPORT("is_group", "extract_groups", "item_has_groups", "groups_to_items")
IMPORT("fmt", "reg_items", "reg_aliases", "reg_nodes", "draw_cube", "ItemStack")
IMPORT("true_str", "true_table", "is_table", "valid_item", "table_merge", "table_replace", "rcp_eq")
IMPORT("fmt", "reg_items", "reg_aliases", "reg_nodes", "is_cube", "get_cube", "ItemStack")
IMPORT("true_str", "is_table", "valid_item", "table_merge", "table_replace", "rcp_eq")
local function get_burntime(item)
return core.get_craft_result{method = "fuel", items = {item}}.time
@ -27,8 +26,31 @@ local function cache_groups(groupname, groups)
i3.groups[groupname] = {}
i3.groups[groupname].groups = groups
i3.groups[groupname].items = groups_to_items(groups, true)
local items = i3.groups[groupname].items
local nb_items = #items
if nb_items > 1 then
local px = 256
local sprite = fmt("[combine:%ux%u", px, nb_items + (px * nb_items) - 1)
for i = 1, nb_items do
local item = items[i]
local def = reg_items[item]
local texture = def.inventory_image or def.wield_image
if is_cube(def.drawtype) then
texture = get_cube(def.tiles)
end
sprite = sprite .. fmt(":0,%u=%s", (i - 1) + ((i - 1) * px), texture)
end
i3.groups[groupname].sprite = sprite
end
end
local function get_item_usages(item, recipe, added)
if is_group(item) then
local groupname = item:sub(7)
@ -296,35 +318,6 @@ local function init_recipes()
end
end
local function get_cube(tiles)
if not true_table(tiles) then
return PNG.blank
end
local top = tiles[1] or PNG.blank
if is_table(top) then
top = top.name or top.image
end
local left = tiles[3] or top or PNG.blank
if is_table(left) then
left = left.name or left.image
end
local right = tiles[5] or left or PNG.blank
if is_table(right) then
right = right.name or right.image
end
return draw_cube(top, left, right)
end
local function is_cube(drawtype)
return drawtype == "normal" or drawtype == "liquid" or
sub(drawtype, 1, 9) == "glasslike" or
sub(drawtype, 1, 8) == "allfaces"
end
local function init_cubes()
for name, def in pairs(reg_nodes) do
if def then

View File

@ -279,6 +279,35 @@ local function groups_to_items(groups, get_all)
return get_all and names or ""
end
local function is_cube(drawtype)
return drawtype == "normal" or drawtype == "liquid" or
sub(drawtype, 1, 9) == "glasslike" or
sub(drawtype, 1, 8) == "allfaces"
end
local function get_cube(tiles)
if not true_table(tiles) then
return "i3_blank.png"
end
local top = tiles[1] or "i3_blank.png"
if is_table(top) then
top = top.name or top.image
end
local left = tiles[3] or top or "i3_blank.png"
if is_table(left) then
left = left.name or left.image
end
local right = tiles[5] or left or "i3_blank.png"
if is_table(right) then
right = right.name or right.image
end
return core.inventorycube(top, left, right)
end
local function apply_recipe_filters(recipes, player)
for _, filter in pairs(i3.recipe_filters) do
recipes = filter(recipes, player)
@ -667,6 +696,8 @@ local _ = {
msg = msg,
-- Misc. functions
is_cube = is_cube,
get_cube = get_cube,
ItemStack = ItemStack,
valid_item = valid_item,
spawn_item = spawn_item,

View File

@ -93,8 +93,7 @@ local function get_stack_max(inv, data, is_recipe, rcp)
local def = reg_items[item]
if def then
local groupname = name:sub(7)
local group_cache = i3.groups[groupname]
local group_cache = i3.groups[name:sub(7)]
local groups = group_cache and group_cache.groups or extract_groups(name)
if item_has_groups(def.groups, groups) then
@ -994,10 +993,9 @@ local function get_grid_fs(fs, data, rcp, is_recipe)
end
local groups
local group_cache = i3.groups[name:sub(7)]
if is_group(name) then
local groupname = name:sub(7)
local group_cache = i3.groups[groupname]
groups = group_cache and group_cache.groups or extract_groups(name)
name = group_cache and group_cache.items[1] or groups_to_items(groups)
end
@ -1031,10 +1029,21 @@ local function get_grid_fs(fs, data, rcp, is_recipe)
end
local btn_name = groups and fmt("group!%s!%s", groups[1], name) or name
local _count = count * (is_recipe and data.scrbar_rcp or data.scrbar_usg or 1)
fs("item_image_button", X, Y, btn_size, btn_size,
fmt("%s %u", name, count * (is_recipe and data.scrbar_rcp or data.scrbar_usg or 1)),
btn_name, label)
if group_cache and group_cache.sprite then
local sprite = group_cache.sprite
fs("item_image_button", X, Y, btn_size, btn_size, "", btn_name, "")
fs("animated_image", X + 0.01, Y, 1.87, 1.87, sprite, #group_cache.items, 1000)
fs("label", X + 0.45, Y + 0.18, label)
if _count > 1 then
fs("label", X + 0.8, Y + 0.9, tostring(_count))
end
else
fs("item_image_button", X, Y, btn_size, btn_size, fmt("%s %u", name, _count), btn_name, label)
end
local def = reg_items[name]
local unknown = not def or nil