Add option in Style settings to show wielditem descriptions in HUD

master^2
Jean-Patrick Guerrero 2022-08-28 14:18:05 +02:00
parent 75fdd57f2a
commit 911bed3911
5 changed files with 71 additions and 1 deletions

View File

@ -10,6 +10,7 @@ read_globals = {
"armor",
"skins",
"awards",
"hb",
"vector",
"string",
"table",

View File

@ -30,6 +30,7 @@ i3 = {
min_fs_version = 6,
item_btn_size = 1.1,
drop_bag_on_die = true,
wielditem_fade_after = 3,
save_interval = 600, -- Player data save interval (in seconds)
hud_speed = 1,
@ -56,6 +57,7 @@ i3 = {
waypoints = true,
inv_items = true,
known_recipes = true,
wielditem_hud = true,
legacy_inventory = true,
},

View File

@ -148,6 +148,10 @@ if core.global_exists"skins" then
i3.modules.skins = true
end
if core.global_exists"hb" then
i3.modules.hudbars = true
end
if core.global_exists"awards" then
i3.modules.awards = true

View File

@ -629,6 +629,7 @@ local function show_settings(fs, data)
elseif show_style then
checkbox(2.6, 9.95, "cb_hide_tabs", "Hide tabs", tostring(data.hide_tabs))
checkbox(2.6, 10.4, "cb_legacy_inventory", "Legacy inventory", tostring(data.legacy_inventory))
checkbox(2.6, 10.85, "cb_wielditem_hud", "HUD description", tostring(data.wielditem_hud))
local sign = (data.font_size > 0 and "+") or (data.font_size > 0 and "-") or ""
label(5.3, 9.95, ES"Font size" .. fmt(": %s", sign .. data.font_size))
@ -637,6 +638,13 @@ local function show_settings(fs, data)
fs(fmt("scrollbaroptions[min=-%u;max=%u;smallstep=1;largestep=1;thumbsize=2]", range, range))
fs(fmt("scrollbar[5.3,10.25;2.45,0.3;horizontal;sb_font_size;%d]", data.font_size))
fs(fmt("tooltip[cb_hide_tabs;%s;#707070;#fff]",
ES"Enable this option to change the style of the right panel"),
fmt("tooltip[cb_legacy_inventory;%s;#707070;#fff]",
ES"Enable this option to set the classic inventory size in Minetest"),
fmt("tooltip[cb_wielditem_hud;%s;#707070;#fff]",
ES"Enable this option to show the wielded item description in your HUD"))
elseif show_sorting then
checkbox(2.6, 9.95, "cb_inv_compress", "Compression", tostring(data.inv_compress))
checkbox(2.6, 10.4, "cb_reverse_sorting", "Reverse mode", tostring(data.reverse_sorting))

View File

@ -1,4 +1,4 @@
IMPORT("get_connected_players", "str_to_pos", "add_hud_waypoint")
IMPORT("ceil", "get_connected_players", "str_to_pos", "add_hud_waypoint")
local function init_hud(player)
local name = player:get_player_name()
@ -32,6 +32,17 @@ local function init_hud(player)
z_index = 0xDEAD,
style = 1,
},
wielditem = player:hud_add {
hud_elem_type = "text",
position = {x = 0.5, y = 1},
offset = {x = 0, y = -65 - (i3.modules.hudbars and (ceil(hb.hudbars_count / 2) * 25) or 25)},
alignment = {x = 0, y = -1},
number = 0xffffff,
text = "",
z_index = 0xDEAD,
style = 1,
},
}
end
@ -84,6 +95,50 @@ local function show_hud(player, data)
end
end
core.register_globalstep(function(dt)
local players = get_connected_players()
players[0] = #players
for i = 1, players[0] do
local player = players[i]
local name = player:get_player_name()
local data = i3.data[name]
if not data then return end
local function reset()
player:hud_change(data.hud.wielditem, "text", "")
data.timer = 0
end
if not data.wielditem_hud then
return reset()
end
data.timer = (data.timer or 0) + dt
local wielditem = player:get_wielded_item()
local wieldname = wielditem:get_name()
if wieldname == data.old_wielditem then
if data.timer >= i3.settings.wielditem_fade_after then
return reset()
end
return
end
data.old_wielditem = wieldname
local meta = wielditem:get_meta()
local meta_desc = meta:get_string"short_description"
meta_desc = meta_desc:gsub("\27", "")
meta_desc = core.strip_colors(meta_desc)
local desc = meta_desc ~= "" and meta_desc or wielditem:get_short_description()
player:hud_change(data.hud.wielditem, "text", desc:trim())
end
end)
core.register_globalstep(function()
local players = get_connected_players()
players[0] = #players