Make format string params changable

This commit is contained in:
Wuzzy 2019-03-30 14:56:36 +01:00
parent a3fede159b
commit 07b9f766d2
1 changed files with 23 additions and 5 deletions

View File

@ -51,6 +51,20 @@ local function player_exists(player)
return player ~= nil and player:is_player()
end
local function make_label(format_string, format_string_config, label, start_value, max_value)
local params = {}
for c=1, #format_string_config do
if format_string_config[c] == "label" then
table.insert(params, label)
elseif format_string_config[c] == "value" then
table.insert(params, start_value)
elseif format_string_config[c] == "max_value" then
table.insert(params, max_value)
end
end
return S(format_string, unpack(params))
end
-- Table which contains all players with active default HUD bars (only for internal use)
hb.players = {}
@ -90,7 +104,7 @@ function hb.get_hudbar_position_index(identifier)
end
end
function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string)
function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config)
minetest.log("action", "hb.register_hudbar: "..tostring(identifier))
local hudtable = {}
local pos, offset
@ -126,6 +140,9 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
if format_string == nil then
format_string = N("@1: @2/@3")
end
if format_string_config == nil then
format_string_config = { "label", "value", "max_value" }
end
hudtable.add_all = function(player, hudtable, start_value, start_max, start_hidden)
if start_value == nil then start_value = hudtable.default_start_value end
@ -149,7 +166,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
iconscale = { x=1, y=1 }
barnumber = hb.value_to_barlength(start_value, start_max)
bgiconnumber = hb.settings.statbar_length
text = S(format_string, label, start_value, start_max)
text = make_label(format_string, format_string_config, label, start_value, start_max)
end
if hb.settings.bar_type == "progress_bar" then
ids.bg = player:hud_add({
@ -239,6 +256,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
hudtable.identifier = identifier
hudtable.format_string = format_string
hudtable.format_string_config = format_string_config
hudtable.label = label
hudtable.hudids = {}
hudtable.hudstate = {}
@ -299,7 +317,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon
end
if new_label ~= nil then
hudtable.label = new_label
local new_text = S(hudtable.format_string, new_label, hudtable.hudstate[name].value, hudtable.hudstate[name].max)
local new_text = make_label(hudtable.format_string, hudtable.format_string_config, new_label, hudtable.hudstate[name].value, hudtable.hudstate[name].max)
player:hud_change(hudtable.hudids[name].text, "text", new_text)
end
if new_text_color ~= nil then
@ -343,7 +361,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon
end
if hb.settings.bar_type == "progress_bar" then
local new_text = S(hudtable.format_string, hudtable.label, new_value, new_max_value)
local new_text = make_label(hudtable.format_string, hudtable.format_string_config, hudtable.label, new_value, new_max_value)
if new_text ~= hudtable.hudstate[name].text then
player:hud_change(hudtable.hudids[name].text, "text", new_text)
hudtable.hudstate[name].text = new_text
@ -390,7 +408,7 @@ function hb.unhide_hudbar(player, identifier)
if hudtable.hudstate[name].max ~= 0 then
player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
end
player:hud_change(hudtable.hudids[name].text, "text", tostring(S(hudtable.format_string, hudtable.label, value, max)))
player:hud_change(hudtable.hudids[name].text, "text", make_label(hudtable.format_string, hudtable.format_string_config, hudtable.label, value, max))
elseif hb.settings.bar_type == "statbar_modern" then
player:hud_change(hudtable.hudids[name].bg, "number", hb.settings.statbar_length)
end