Compare commits

..

1 Commits

Author SHA1 Message Date
103624bc28 Fix item positioning for large recipes 2020-04-16 18:39:39 +02:00
3 changed files with 68 additions and 92 deletions

160
init.lua
View File

@ -7,6 +7,7 @@ local searches = {}
local recipes_cache = {} local recipes_cache = {}
local usages_cache = {} local usages_cache = {}
local fuel_cache = {} local fuel_cache = {}
local toolrepair local toolrepair
local progressive_mode = core.settings:get_bool "craftguide_progressive_mode" local progressive_mode = core.settings:get_bool "craftguide_progressive_mode"
@ -15,7 +16,6 @@ local autocache = core.settings:get_bool "craftguide_autocache"
local http = core.request_http_api() local http = core.request_http_api()
local storage = core.get_mod_storage() local storage = core.get_mod_storage()
local singleplayer = core.is_singleplayer()
local reg_items = core.registered_items local reg_items = core.registered_items
local reg_tools = core.registered_tools local reg_tools = core.registered_tools
@ -111,20 +111,6 @@ local function get_fs_version(name)
return info and info.formspec_version or 1 return info and info.formspec_version or 1
end end
local function outdated(name)
local fs = fmt([[
size[6.6,1.3]
image[0,0;1,1;%s]
label[1,0;%s]
button_exit[2.8,0.8;1,1;;OK]
]],
PNG.book,
"Your Minetest client is outdated.\n" ..
"Get the latest version on minetest.net to use the Crafting Guide.")
return show_formspec(name, "craftguide", fs)
end
local function mul_elem(elem, n) local function mul_elem(elem, n)
local fstr, elems = "", {} local fstr, elems = "", {}
@ -711,10 +697,6 @@ local function is_fav(data)
return fav, i return fav, i
end end
local function check_newline(def)
return def and def.description and find(def.description, "\n")
end
local function get_desc(name) local function get_desc(name)
if sub(name, 1, 1) == "_" then if sub(name, 1, 1) == "_" then
name = sub(name, 2) name = sub(name, 2)
@ -832,15 +814,11 @@ local function get_output_fs(data, fs, L)
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]", fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
output_X, Y, 1.1, 1.1, item, _name, "") output_X, Y, 1.1, 1.1, item, _name, "")
local def = reg_items[name]
local infos = { local infos = {
unknown = not def or nil, unknown = not reg_items[name] or nil,
burntime = fuel_cache[name], burntime = fuel_cache[name],
repair = repairable(name), repair = repairable(name),
rarity = L.rarity, rarity = L.rarity,
newline = check_newline(def),
} }
if next(infos) then if next(infos) then
@ -947,15 +925,12 @@ local function get_grid_fs(data, fs, rcp, spacing)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
X, Y, btn_size, btn_size, item, item, label) X, Y, btn_size, btn_size, item, item, label)
local def = reg_items[name]
local infos = { local infos = {
unknown = not def or nil, unknown = not reg_items[name] or nil,
groups = groups, groups = groups,
burntime = fuel_cache[name], burntime = fuel_cache[name],
cooktime = cooktime, cooktime = cooktime,
replace = replace, replace = replace,
newline = check_newline(def),
} }
if next(infos) then if next(infos) then
@ -1123,7 +1098,6 @@ local function make_fs(data)
style[clear;fgimg=%s;fgimg_hovered=%s] style[clear;fgimg=%s;fgimg_hovered=%s]
style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s] style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[next_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s] style[next_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[pagenum;border=false]
]], ]],
PNG.selected, PNG.selected, PNG.selected, PNG.selected,
PNG.search, PNG.search_hover, PNG.search, PNG.search_hover,
@ -1139,9 +1113,9 @@ local function make_fs(data)
data.pagemax = max(1, ceil(#data.items / IPP)) data.pagemax = max(1, ceil(#data.items / IPP))
fs[#fs + 1] = fmt(FMT.button, fs[#fs + 1] = fmt("label[%f,%f;%s / %u]",
sfinv_only and 5.85 or 6, -0.1, sfinv_only and 1.82 or 1.62, 1, "pagenum", sfinv_only and 6.35 or (9 * 7.85) / 11,
fmt("%u / %u", data.pagenum, data.pagemax)) 0.06, clr("#ff0", data.pagenum), data.pagemax)
if #data.items == 0 then if #data.items == 0 then
local no_item = ES"No item to show" local no_item = ES"No item to show"
@ -1170,7 +1144,8 @@ local function make_fs(data)
fs[#fs + 1] = fmt(FMT.image, X, Y, 1, 1, PNG.selected) fs[#fs + 1] = fmt(FMT.image, X, Y, 1, 1, PNG.selected)
end end
fs[#fs + 1] = fmt("item_image_button[%f,%f;1,1;%s;%s_inv;]", X, Y, item, item) fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]",
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
@ -1191,12 +1166,12 @@ end
craftguide.register_craft_type("digging", { craftguide.register_craft_type("digging", {
description = ES"Digging", description = ES"Digging",
icon = "craftguide_steelpick.png", icon = "default_tool_steelpick.png",
}) })
craftguide.register_craft_type("digging_chance", { craftguide.register_craft_type("digging_chance", {
description = ES"Digging Chance", description = ES"Digging Chance",
icon = "craftguide_mesepick.png", icon = "default_tool_mesepick.png",
}) })
local function search(data) local function search(data)
@ -1574,10 +1549,6 @@ on_mods_loaded(get_init_items)
on_joinplayer(function(player) on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
init_data(name) init_data(name)
if pdata[name].fs_version < FORMSPEC_MINIMAL_VERSION then
outdated(name)
end
end) end)
local function fields(player, _f) local function fields(player, _f)
@ -1682,8 +1653,9 @@ if sfinv_only then
get = function(self, player, context) get = function(self, player, context)
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
local formspec = make_fs(data)
return sfinv.make_formspec(player, context, make_fs(data)) return sfinv.make_formspec(player, context, formspec)
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
@ -1712,7 +1684,17 @@ else
local data = pdata[name] local data = pdata[name]
if data.fs_version < FORMSPEC_MINIMAL_VERSION then if data.fs_version < FORMSPEC_MINIMAL_VERSION then
return outdated(name) local fs = fmt([[
size[6.6,1.3]
image[0,0;1,1;%s]
label[1,0;%s]
button_exit[2.8,0.8;1,1;;OK]
]],
PNG.nothing,
"Your Minetest client is outdated.\n" ..
"Get the latest version on minetest.net to use the Crafting Guide.")
return show_formspec(name, "craftguide", fs)
end end
if next(recipe_filters) then if next(recipe_filters) then
@ -1743,12 +1725,7 @@ else
paramtype = "light", paramtype = "light",
paramtype2 = "wallmounted", paramtype2 = "wallmounted",
sunlight_propagates = true, sunlight_propagates = true,
groups = { groups = {choppy = 1, attached_node = 1, oddly_breakable_by_hand = 1, flammable = 3},
choppy = 1,
attached_node = 1,
oddly_breakable_by_hand = 1,
flammable = 3,
},
node_box = { node_box = {
type = "wallmounted", type = "wallmounted",
wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5}, wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5},
@ -1793,7 +1770,7 @@ else
if rawget(_G, "sfinv_buttons") then if rawget(_G, "sfinv_buttons") then
sfinv_buttons.register_button("craftguide", { sfinv_buttons.register_button("craftguide", {
title = S"Crafting Guide", title = S"Crafting Guide",
tooltip = S"Shows a list of available crafting recipes", tooltip = S"Shows a list of available crafting recipes, cooking recipes and fuels",
image = PNG.book, image = PNG.book,
action = function(player) action = function(player)
on_use(player) on_use(player)
@ -1803,6 +1780,7 @@ else
end end
if progressive_mode then if progressive_mode then
local PLAYERS = {}
local POLL_FREQ = 0.25 local POLL_FREQ = 0.25
local HUD_TIMER_MAX = 1.5 local HUD_TIMER_MAX = 1.5
@ -1889,34 +1867,6 @@ if progressive_mode then
return inv_items return inv_items
end end
local function init_hud(player, data)
data.hud = {
bg = player:hud_add{
hud_elem_type = "image",
position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
text = PNG.bg,
},
book = player:hud_add{
hud_elem_type = "image",
position = {x = 0.79, y = 1.02},
alignment = {x = 1, y = 1},
scale = {x = 4, y = 4},
text = PNG.book,
},
text = player:hud_add{
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
},
}
end
local function show_hud_success(player, data) local function show_hud_success(player, data)
-- It'd better to have an engine function `hud_move` to only need -- It'd better to have an engine function `hud_move` to only need
-- 2 calls for the notification's back and forth. -- 2 calls for the notification's back and forth.
@ -1964,9 +1914,8 @@ if progressive_mode then
-- Workaround. Need an engine call to detect when the contents of -- Workaround. Need an engine call to detect when the contents of
-- the player inventory changed, instead. -- the player inventory changed, instead.
local function poll_new_items() local function poll_new_items()
local players = get_players() for i = 1, #PLAYERS do
for i = 1, #players do local player = PLAYERS[i]
local player = players[i]
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
@ -1999,13 +1948,12 @@ if progressive_mode then
poll_new_items() poll_new_items()
globalstep(function() globalstep(function()
local players = get_players() for i = 1, #PLAYERS do
for i = 1, #players do local player = PLAYERS[i]
local player = players[i]
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
if data.show_hud ~= nil and singleplayer then if data.show_hud ~= nil then
show_hud_success(player, data) show_hud_success(player, data)
end end
end end
@ -2014,6 +1962,8 @@ if progressive_mode then
craftguide.add_recipe_filter("Default progressive filter", progressive_filter) craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
on_joinplayer(function(player) on_joinplayer(function(player)
PLAYERS = get_players()
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
@ -2021,9 +1971,31 @@ if progressive_mode then
data.inv_items = dslz(meta:get_string "inv_items") or {} data.inv_items = dslz(meta:get_string "inv_items") or {}
data.known_recipes = dslz(meta:get_string "known_recipes") or 0 data.known_recipes = dslz(meta:get_string "known_recipes") or 0
if singleplayer then data.hud = {
init_hud(player, data) bg = player:hud_add{
end hud_elem_type = "image",
position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
text = PNG.bg,
},
book = player:hud_add{
hud_elem_type = "image",
position = {x = 0.79, y = 1.02},
alignment = {x = 1, y = 1},
scale = {x = 4, y = 4},
text = PNG.book,
},
text = player:hud_add{
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
},
}
end) end)
local to_save = {"inv_items", "known_recipes"} local to_save = {"inv_items", "known_recipes"}
@ -2039,12 +2011,14 @@ if progressive_mode then
end end
end end
on_leaveplayer(save_meta) on_leaveplayer(function(player)
PLAYERS = get_players()
save_meta(player)
end)
on_shutdown(function() on_shutdown(function()
local players = get_players() for i = 1, #PLAYERS do
for i = 1, #players do local player = PLAYERS[i]
local player = players[i]
save_meta(player) save_meta(player)
end end
end) end)
@ -2072,11 +2046,13 @@ function craftguide.show(name, item, show_usages)
if not recipes and not usages then if not recipes and not usages then
if not recipes_cache[item] and not usages_cache[item] then if not recipes_cache[item] and not usages_cache[item] then
return false, msg(name, fmt("%s: %s", return false, msg(name, fmt("%s: %s",
S"No recipe or usage for this item", get_desc(item))) S"No recipe or usage for this item",
get_desc(item)))
end end
return false, msg(name, fmt("%s: %s", return false, msg(name, fmt("%s: %s",
S"You don't know a recipe or usage for this item", get_desc(item))) S"You don't know a recipe or usage for this item",
get_desc(item)))
end end
data.query_item = item data.query_item = item

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB