Code cleaning

This commit is contained in:
Jean-Patrick Guerrero 2020-11-18 02:17:59 +01:00
parent e74b485e7c
commit 9d26dc4840

172
init.lua

@ -54,12 +54,12 @@ local maxn, sort, concat, copy, insert, remove =
table.maxn, table.sort, table.concat, table.copy, table.maxn, table.sort, table.concat, table.copy,
table.insert, table.remove table.insert, table.remove
local sprintf, find, gmatch, match, sub, split, upper, lower, rep = local sprintf, find, gmatch, match, sub, split, upper, lower =
string.format, string.find, string.gmatch, string.match, string.format, string.find, string.gmatch, string.match,
string.sub, string.split, string.upper, string.lower, string.rep string.sub, string.split, string.upper, string.lower
local min, max, floor, ceil, abs = math.min, math.max, math.floor, math.ceil, math.abs local min, max, floor, ceil, abs = math.min, math.max, math.floor, math.ceil, math.abs
local pairs, next, type = pairs, next, type local pairs, ipairs, next, type, setmetatable = pairs, ipairs, next, type, setmetatable
local vec_add, vec_mul = vector.add, vector.multiply local vec_add, vec_mul = vector.add, vector.multiply
local ROWS = 9 local ROWS = 9
@ -106,6 +106,7 @@ local fs_elements = {
tooltip = "tooltip[%f,%f;%f,%f;%s]", tooltip = "tooltip[%f,%f;%f,%f;%s]",
hypertext = "hypertext[%f,%f;%f,%f;;%s]", hypertext = "hypertext[%f,%f;%f,%f;;%s]",
item_image = "item_image[%f,%f;%f,%f;%s]", item_image = "item_image[%f,%f;%f,%f;%s]",
bg9 = "background9[%f,%f;%f,%f;%s;false;%u]",
model = "model[%f,%f;%f,%f;%s;%s;%s;0,0;true]", model = "model[%f,%f;%f,%f;%s;%s;%s;0,0;true]",
image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]", image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]",
animated_image = "animated_image[%f,%f;%f,%f;;%s;%u;%u]", animated_image = "animated_image[%f,%f;%f,%f;;%s;%u;%u]",
@ -244,12 +245,8 @@ local function is_group(item)
return sub(item, 1, 6) == "group:" return sub(item, 1, 6) == "group:"
end end
local function fmt(elem, n, ...) local function fmt(elem, ...)
if n == 1 then
return sprintf(fs_elements[elem], ...) return sprintf(fs_elements[elem], ...)
end
return sprintf(rep(fs_elements[elem], n), ...)
end end
local function clean_name(item) local function clean_name(item)
@ -824,7 +821,7 @@ local function toupper(str)
return str:gsub("%f[%w]%l", upper):gsub("_", " ") return str:gsub("%f[%w]%l", upper):gsub("_", " ")
end end
local function nice_strip(str, limit) local function snip(str, limit)
return #str > limit and sprintf("%s...", sub(str, 1, limit - 3)) or str return #str > limit and sprintf("%s...", sub(str, 1, limit - 3)) or str
end end
@ -949,38 +946,35 @@ local function get_output_fs(fs, rcp, shapeless, right, btn_size, _btn_size, spa
local pos_y = YOFFSET + (sfinv_only and 1.55 or -0.45) + spacing local pos_y = YOFFSET + (sfinv_only and 1.55 or -0.45) + spacing
if sub(icon, 1, 18) == "craftguide_furnace" then if sub(icon, 1, 18) == "craftguide_furnace" then
fs[#fs + 1] = fmt("animated_image", 1, fs(fmt("animated_image", pos_x, pos_y, 0.5, 0.5, PNG.furnace_anim, 8, 180))
pos_x, pos_y, 0.5, 0.5, PNG.furnace_anim, 8, 180)
else else
fs[#fs + 1] = fmt("image", 1, pos_x, pos_y, 0.5, 0.5, icon) fs(fmt("image", pos_x, pos_y, 0.5, 0.5, icon))
end end
local tooltip = custom_recipe and custom_recipe.description or local tooltip = custom_recipe and custom_recipe.description or
shapeless and S"Shapeless" or S"Cooking" shapeless and S"Shapeless" or S"Cooking"
fs[#fs + 1] = fmt("tooltip", 1, pos_x, pos_y, 0.5, 0.5, ESC(tooltip)) fs(fmt("tooltip", pos_x, pos_y, 0.5, 0.5, ESC(tooltip)))
end end
local arrow_X = right + (_btn_size or ITEM_BTN_SIZE) local arrow_X = right + (_btn_size or ITEM_BTN_SIZE)
local X = arrow_X + 0.9 local X = arrow_X + 0.9
local Y = YOFFSET + (sfinv_only and 2 or 0) + spacing local Y = YOFFSET + (sfinv_only and 2 or 0) + spacing
fs[#fs + 1] = fmt("image", 1, arrow_X, Y + 0.2, 0.9, 0.7, PNG.arrow) fs(fmt("image", arrow_X, Y + 0.2, 0.9, 0.7, PNG.arrow))
if rcp.type == "fuel" then if rcp.type == "fuel" then
fs[#fs + 1] = fmt("animated_image", 1, fs(fmt("animated_image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180))
X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180)
else else
local item = rcp.output local item = rcp.output
item = clean_name(item) item = clean_name(item)
local name = match(item, "%S*") local name = match(item, "%S*")
fs[#fs + 1] = fmt("image", 1, X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected) fs(fmt("image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected))
local _name = sfinv_only and name or sprintf("_%s", name) local _name = sfinv_only and name or sprintf("_%s", name)
fs[#fs + 1] = sprintf("item_image_button[%f,%f;%f,%f;%s;%s;%s]", fs(fmt("item_image_button", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, _name, ""))
X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, _name, "")
local def = reg_items[name] local def = reg_items[name]
local unknown = not def or nil local unknown = not def or nil
@ -998,17 +992,16 @@ local function get_output_fs(fs, rcp, shapeless, right, btn_size, _btn_size, spa
} }
if next(infos) then if next(infos) then
fs[#fs + 1] = get_tooltip(_name, infos) fs(get_tooltip(_name, infos))
end end
if infos.burntime then if infos.burntime then
fs[#fs + 1] = fmt("image", 1, fs(fmt("image",
X + 1, YOFFSET + (sfinv_only and 2 or 0.1) + spacing, X + 1, YOFFSET + (sfinv_only and 2 or 0.1) + spacing,
0.6, 0.4, PNG.arrow) 0.6, 0.4, PNG.arrow),
fmt("animated_image",
fs[#fs + 1] = fmt("animated_image", 1,
X + 1.6, YOFFSET + (sfinv_only and 1.85 or 0) + spacing, X + 1.6, YOFFSET + (sfinv_only and 1.85 or 0) + spacing,
0.6, 0.6, PNG.fire_anim, 8, 180) 0.6, 0.6, PNG.fire_anim, 8, 180))
end end
end end
end end
@ -1029,10 +1022,10 @@ local function get_grid_fs(fs, rcp, spacing)
local rows = ceil(maxn(rcp.items) / width) local rows = ceil(maxn(rcp.items) / width)
if width > WH_LIMIT or rows > WH_LIMIT then if width > WH_LIMIT or rows > WH_LIMIT then
fs[#fs + 1] = fmt("label", 1, fs(fmt("label",
XOFFSET + (sfinv_only and -1.5 or -1.6), XOFFSET + (sfinv_only and -1.5 or -1.6),
YOFFSET + (sfinv_only and 0.5 or spacing), YOFFSET + (sfinv_only and 0.5 or spacing),
ES("Recipe's too big to be displayed (@1x@2)", width, rows)) ES("Recipe's too big to be displayed (@1x@2)", width, rows)))
return concat(fs) return concat(fs)
end end
@ -1040,7 +1033,7 @@ local function get_grid_fs(fs, rcp, spacing)
local large_recipe = width > 3 or rows > 3 local large_recipe = width > 3 or rows > 3
if large_recipe then if large_recipe then
fs[#fs + 1] = "style_type[item_image_button;border=true]" fs("style_type[item_image_button;border=true]")
end end
for i = 1, width * rows do for i = 1, width * rows do
@ -1100,13 +1093,12 @@ local function get_grid_fs(fs, rcp, spacing)
Y = Y + (sfinv_only and 2 or 0) Y = Y + (sfinv_only and 2 or 0)
if not large_recipe then if not large_recipe then
fs[#fs + 1] = fmt("image", 1, X, Y, btn_size, btn_size, PNG.selected) fs(fmt("image", X, Y, btn_size, btn_size, PNG.selected))
end end
local btn_name = groups and sprintf("group|%s|%s", groups[1], item) or item local btn_name = groups and sprintf("group|%s|%s", groups[1], item) or item
fs[#fs + 1] = fmt("item_image_button", 1, fs(fmt("item_image_button", X, Y, btn_size, btn_size, item, btn_name, label))
X, Y, btn_size, btn_size, item, btn_name, label)
local def = reg_items[name] local def = reg_items[name]
local unknown = not def or nil local unknown = not def or nil
@ -1125,12 +1117,12 @@ local function get_grid_fs(fs, rcp, spacing)
} }
if next(infos) then if next(infos) then
fs[#fs + 1] = get_tooltip(btn_name, infos) fs(get_tooltip(btn_name, infos))
end end
end end
if large_recipe then if large_recipe then
fs[#fs + 1] = "style_type[item_image_button;border=false]" fs("style_type[item_image_button;border=false]")
end end
get_output_fs(fs, rcp, shapeless, right, btn_size, _btn_size, spacing) get_output_fs(fs, rcp, shapeless, right, btn_size, _btn_size, spacing)
@ -1154,9 +1146,9 @@ local function get_rcp_lbl(fs, data, panel, spacing, rn, is_recipe)
local lbl_len = #_lbl:gsub("[\128-\191]", "") -- Count chars, not bytes in UTF-8 strings local lbl_len = #_lbl:gsub("[\128-\191]", "") -- Count chars, not bytes in UTF-8 strings
local shift = min(0.9, abs(12 - max(12, lbl_len)) * 0.1) local shift = min(0.9, abs(12 - max(12, lbl_len)) * 0.1)
fs[#fs + 1] = fmt("label", 1, fs(fmt("label",
XOFFSET + (sfinv_only and 2.3 or 1.6) - shift, XOFFSET + (sfinv_only and 2.3 or 1.6) - shift,
YOFFSET + (sfinv_only and 3.4 or 1.5 + spacing), lbl) YOFFSET + (sfinv_only and 3.4 or 1.5 + spacing), lbl))
if rn > 1 then if rn > 1 then
local btn_suffix = is_recipe and "recipe" or "usage" local btn_suffix = is_recipe and "recipe" or "usage"
@ -1165,9 +1157,8 @@ local function get_rcp_lbl(fs, data, panel, spacing, rn, is_recipe)
local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1) local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1)
local y_arrow = YOFFSET + (sfinv_only and 3.3 or 1.4 + spacing) local y_arrow = YOFFSET + (sfinv_only and 3.3 or 1.4 + spacing)
fs[#fs + 1] = fmt("arrow", 2, fs(fmt("arrow", x_arrow - shift, y_arrow, PNG.prev, prev_name, ""),
x_arrow - shift, y_arrow, PNG.prev, prev_name, "", fmt("arrow", x_arrow + 1.8, y_arrow, PNG.next, next_name, ""))
x_arrow + 1.8, y_arrow, PNG.next, next_name, "")
end end
local rcp = is_recipe and panel.rcp[data.rnum] or panel.rcp[data.unum] local rcp = is_recipe and panel.rcp[data.rnum] or panel.rcp[data.unum]
@ -1175,13 +1166,11 @@ local function get_rcp_lbl(fs, data, panel, spacing, rn, is_recipe)
end end
local function get_title_fs(query_item, favs, lang_code, fs, spacing) local function get_title_fs(query_item, favs, lang_code, fs, spacing)
fs[#fs + 1] = "style_type[label;font=bold;font_size=+6]" fs("style_type[label;font=bold;font_size=+6]",
fs[#fs + 1] = fmt("label", 1, 8.75, fmt("label", 8.75, spacing - 0.1, snip(ESC(get_desc(query_item, lang_code)), 45)),
spacing - 0.1, nice_strip(ESC(get_desc(query_item, lang_code)), 45)) "style_type[label;font=mono;font_size=+0]",
fs[#fs + 1] = "style_type[label;font=mono;font_size=+0]" fmt("label", 8.75, spacing + 0.3, clr("#7bf", snip(query_item, 35))),
fs[#fs + 1] = fmt("label", 1, 8.75, "style_type[label;font=normal]")
spacing + 0.3, clr("#7bf", nice_strip(query_item, 35)))
fs[#fs + 1] = "style_type[label;font=normal]"
local def = reg_items[query_item] local def = reg_items[query_item]
@ -1214,11 +1203,10 @@ local function get_title_fs(query_item, favs, lang_code, fs, spacing)
t[#t + 1] = t[#t] t[#t + 1] = t[#t]
end end
fs[#fs + 1] = fmt("model", 1, fs(fmt("model", 13.5, spacing - 0.2, 1.2, 1.2, "", def.mesh, concat(t, ",")))
13.5, spacing - 0.2, 1.2, 1.2, "", def.mesh, concat(t, ","))
else else
fs[#fs + 1] = fmt("hypertext", 1, 13.8, spacing - 0.15, 1.1, 1.3, fs(fmt("hypertext", 13.8, spacing - 0.15, 1.1, 1.3,
sprintf("<item name=%s width=64 rotate=yes>", query_item)) sprintf("<item name=%s width=64 rotate=yes>", query_item)))
end end
local fav = is_fav(favs, query_item) local fav = is_fav(favs, query_item)
@ -1227,25 +1215,16 @@ local function get_title_fs(query_item, favs, lang_code, fs, spacing)
if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then
local fav_marked = sprintf("craftguide_fav%s.png", fav and "_off" or "") local fav_marked = sprintf("craftguide_fav%s.png", fav and "_off" or "")
fs[#fs + 1] = sprintf( fs(sprintf("style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
"style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
sprintf("craftguide_fav%s.png", fav and "" or "_off"), sprintf("craftguide_fav%s.png", fav and "" or "_off"),
fav_marked, fav_marked) fav_marked, fav_marked),
fmt("image_button", 8.25, spacing + 0.15, 0.5, 0.45, "", "fav", ""),
fs[#fs + 1] = fmt("image_button", 1, 8.25, spacing + 0.15, 0.5, 0.45, "", "fav", "") sprintf("tooltip[fav;%s]", fav and ES"Unmark this item" or ES"Mark this item"))
fs[#fs + 1] = sprintf("tooltip[fav;%s]",
fav and ES"Unmark this item" or ES"Mark this item")
else else
fs[#fs + 1] = sprintf( fs(sprintf("style[fav_no;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
"style[fav_no;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]", "craftguide_fav_off.png", PNG.nothing, PNG.nothing),
"craftguide_fav_off.png", PNG.nothing, PNG.nothing) fmt("image_button", 8.25, spacing + 0.15, 0.5, 0.45, "", "fav_no", ""),
sprintf("tooltip[fav_no;%s]", ES"Cannot mark this item. Bookmark limit reached."))
fs[#fs + 1] = fmt("image_button", 1,
8.25, spacing + 0.15, 0.5, 0.45, "", "fav_no", "")
fs[#fs + 1] = sprintf("tooltip[fav_no;%s]",
ES"Cannot mark this item. Limit of bookmarks reached.")
end end
end end
@ -1279,18 +1258,17 @@ local function get_panels(data, fs)
if sfinv_only then return end if sfinv_only then return end
fs[#fs + 1] = sprintf("background9[8.1,%f;6.6,%f;%s;false;%d]", fs(fmt("bg9", 8.1, -0.2 + spacing, 6.6, panel.height, PNG.bg_full, 10))
-0.2 + spacing, panel.height, PNG.bg_full, 10)
if recipe_or_usage and not rn then if recipe_or_usage and not rn then
local lbl = is_recipe and ES"No recipes" or ES"No usages" local lbl = is_recipe and ES"No recipes" or ES"No usages"
fs[#fs + 1] = fmt("button", 1, 8, YOFFSET + spacing + 0.1, 6.8, 1, "", lbl) fs(fmt("button", 8, YOFFSET + spacing + 0.1, 6.8, 1, "", lbl))
elseif panel.name == "title" then elseif panel.name == "title" then
get_title_fs(data.query_item, data.favs, data.lang_code, fs, spacing) get_title_fs(data.query_item, data.favs, data.lang_code, fs, spacing)
elseif panel.name == "favs" then elseif panel.name == "favs" then
fs[#fs + 1] = fmt("label", 1, 8.3, spacing - 0.15, ES"Bookmarks") fs(fmt("label", 8.3, spacing - 0.15, ES"Bookmarks"))
for i = 1, #data.favs do for i = 1, #data.favs do
local item = data.favs[i] local item = data.favs[i]
@ -1298,61 +1276,58 @@ local function get_panels(data, fs)
local Y = spacing + 0.4 local Y = spacing + 0.4
if data.query_item == item then if data.query_item == item then
fs[#fs + 1] = fmt("image", 1, fs(fmt("image",
X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected) X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected))
end end
fs[#fs + 1] = fmt("item_image_button", 1, fs(fmt("item_image_button",
X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, "") X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, ""))
end end
end end
end end
end end
local function make_fs(data) local function make_fs(data)
local fs = {} local fs = setmetatable({}, {
__call = function(t, ...)
t[#t + 1] = concat({...})
end
})
fs[#fs + 1] = sprintf([[ fs(sprintf([[
size[%f,%f] size[%f,%f]
no_prepend[] no_prepend[]
bgcolor[#0000] bgcolor[#0000]
]], ]],
9 + (data.query_item and 6.7 or 0) - 1.2, LINES - 0.3) 9 + (data.query_item and 6.7 or 0) - 1.2, LINES - 0.3), styles)
if not sfinv_only then if not sfinv_only then
fs[#fs + 1] = sprintf("background9[-0.15,-0.2;%f,%f;%s;false;%d]", fs(fmt("bg9", -0.15, -0.2, 9 - 0.9, LINES + 0.4, PNG.bg_full, 10))
9 - 0.9, LINES + 0.4, PNG.bg_full, 10)
end end
fs[#fs + 1] = styles fs(sprintf([[
fs[#fs + 1] = sprintf([[
field[0.4,0.2;2.6,1;filter;;%s] field[0.4,0.2;2.6,1;filter;;%s]
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
box[0,0;2.5,0.6;#bababa25] box[0,0;2.5,0.6;#bababa25]
]], ]], ESC(data.filter)),
ESC(data.filter)) fmt("image_button", 2.6, -0.06, 0.85, 0.85, "", "search", ""),
fmt("image_button", 3.3, -0.06, 0.85, 0.85, "", "clear", ""))
fs[#fs + 1] = fmt("image_button", 2,
2.6, -0.06, 0.85, 0.85, "", "search", "",
3.3, -0.06, 0.85, 0.85, "", "clear", "")
if sfinv_only then if sfinv_only then
fs[#fs + 1] = "container[0.2,0]" fs("container[0.2,0]")
end end
fs[#fs + 1] = fmt("image_button", 2, fs(fmt("image_button", 5.35, -0.06, 0.85, 0.85, "", "prev_page", ""),
5.35, -0.06, 0.85, 0.85, "", "prev_page", "", fmt("image_button", 7.1, -0.06, 0.85, 0.85, "", "next_page", ""))
7.1, -0.06, 0.85, 0.85, "", "next_page", "")
data.pagemax = max(1, ceil(#data.items / IPP)) data.pagemax = max(1, ceil(#data.items / IPP))
fs[#fs + 1] = fmt("button", 1, fs(fmt("button",
5.97, -0.06, 1.36, 0.85, "pagenum", 5.97, -0.06, 1.36, 0.85, "pagenum",
sprintf("%s / %u", clr("#ff0", data.pagenum), data.pagemax)) sprintf("%s / %u", clr("#ff0", data.pagenum), data.pagemax)))
if sfinv_only then if sfinv_only then
fs[#fs + 1] = "container_end[]" fs("container_end[]")
end end
if #data.items == 0 then if #data.items == 0 then
@ -1362,7 +1337,7 @@ local function make_fs(data)
lbl = ES"Collect items to reveal more recipes" lbl = ES"Collect items to reveal more recipes"
end end
fs[#fs + 1] = fmt("button", 1, -0.25, 3, 8.3, 1, "", lbl) fs(fmt("button", -0.25, 3, 8.3, 1, "", lbl))
end end
local first_item = (data.pagenum - 1) * IPP local first_item = (data.pagenum - 1) * IPP
@ -1377,11 +1352,10 @@ local function make_fs(data)
Y = Y - (Y * 0.08) - 0.15 Y = Y - (Y * 0.08) - 0.15
if data.query_item == item then if data.query_item == item then
fs[#fs + 1] = fmt("image", 1, X, Y, 1, 1, PNG.selected) fs(fmt("image", X, Y, 1, 1, PNG.selected))
end end
fs[#fs + 1] = sprintf("item_image_button[%f,%f;%f,%f;%s;%s_inv;]", fs(fmt("item_image_button", X, Y, 1, 1, item, sprintf("%s_inv", item), ""))
X, Y, 1, 1, item, item)
end end
if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then