mirror of
https://repo.or.cz/minetest_hudbars.git
synced 2024-08-20 05:04:12 +02:00
Make format string params changable
This commit is contained in:
parent
a3fede159b
commit
07b9f766d2
28
init.lua
28
init.lua
@ -51,6 +51,20 @@ local function player_exists(player)
|
|||||||
return player ~= nil and player:is_player()
|
return player ~= nil and player:is_player()
|
||||||
end
|
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)
|
-- Table which contains all players with active default HUD bars (only for internal use)
|
||||||
hb.players = {}
|
hb.players = {}
|
||||||
|
|
||||||
@ -90,7 +104,7 @@ function hb.get_hudbar_position_index(identifier)
|
|||||||
end
|
end
|
||||||
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))
|
minetest.log("action", "hb.register_hudbar: "..tostring(identifier))
|
||||||
local hudtable = {}
|
local hudtable = {}
|
||||||
local pos, offset
|
local pos, offset
|
||||||
@ -126,6 +140,9 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||||||
if format_string == nil then
|
if format_string == nil then
|
||||||
format_string = N("@1: @2/@3")
|
format_string = N("@1: @2/@3")
|
||||||
end
|
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)
|
hudtable.add_all = function(player, hudtable, start_value, start_max, start_hidden)
|
||||||
if start_value == nil then start_value = hudtable.default_start_value end
|
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 }
|
iconscale = { x=1, y=1 }
|
||||||
barnumber = hb.value_to_barlength(start_value, start_max)
|
barnumber = hb.value_to_barlength(start_value, start_max)
|
||||||
bgiconnumber = hb.settings.statbar_length
|
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
|
end
|
||||||
if hb.settings.bar_type == "progress_bar" then
|
if hb.settings.bar_type == "progress_bar" then
|
||||||
ids.bg = player:hud_add({
|
ids.bg = player:hud_add({
|
||||||
@ -239,6 +256,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||||||
|
|
||||||
hudtable.identifier = identifier
|
hudtable.identifier = identifier
|
||||||
hudtable.format_string = format_string
|
hudtable.format_string = format_string
|
||||||
|
hudtable.format_string_config = format_string_config
|
||||||
hudtable.label = label
|
hudtable.label = label
|
||||||
hudtable.hudids = {}
|
hudtable.hudids = {}
|
||||||
hudtable.hudstate = {}
|
hudtable.hudstate = {}
|
||||||
@ -299,7 +317,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon
|
|||||||
end
|
end
|
||||||
if new_label ~= nil then
|
if new_label ~= nil then
|
||||||
hudtable.label = new_label
|
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)
|
player:hud_change(hudtable.hudids[name].text, "text", new_text)
|
||||||
end
|
end
|
||||||
if new_text_color ~= nil then
|
if new_text_color ~= nil then
|
||||||
@ -343,7 +361,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon
|
|||||||
end
|
end
|
||||||
|
|
||||||
if hb.settings.bar_type == "progress_bar" then
|
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
|
if new_text ~= hudtable.hudstate[name].text then
|
||||||
player:hud_change(hudtable.hudids[name].text, "text", new_text)
|
player:hud_change(hudtable.hudids[name].text, "text", new_text)
|
||||||
hudtable.hudstate[name].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
|
if hudtable.hudstate[name].max ~= 0 then
|
||||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
|
player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
|
||||||
end
|
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
|
elseif hb.settings.bar_type == "statbar_modern" then
|
||||||
player:hud_change(hudtable.hudids[name].bg, "number", hb.settings.statbar_length)
|
player:hud_change(hudtable.hudids[name].bg, "number", hb.settings.statbar_length)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user