Allow to specify number format strings explicitly

Also, the default number format string is now "%d"
This commit is contained in:
Wuzzy 2019-09-13 19:50:05 +02:00
parent 085a2aa901
commit 48d9fd6a12
2 changed files with 22 additions and 3 deletions

2
API.md
View File

@ -70,6 +70,8 @@ for more information.
* `format_string_config`: Required if `format_string` is set. This allows to change which parameters to use in the format string. It's a table with these fields:
* `textdomain`: Text domain of the format string, used by `minetest.translate`
* `order`: Table that contains the order of the placeholders. It's also possible to remove placeholders. Default order: `{ "label", "value", "max_value" }`
* `format_value`: Format string to apply when displaying `value`. Syntax is same as in `string.format`. Default: `"%d"`
* `format_max_value`: Same as `format_value` but is applied to `max_value`
#### Example
Example (mostly) from `hbarmor` mod:

View File

@ -58,9 +58,17 @@ local function make_label(format_string, format_string_config, label, start_valu
if order[o] == "label" then
table.insert(params, label)
elseif order[o] == "value" then
table.insert(params, start_value)
if format_string_config.format_value then
table.insert(params, string.format(format_string_config.format_value, start_value))
else
table.insert(params, start_value)
end
elseif order[o] == "max_value" then
table.insert(params, max_value)
if format_string_config.format_max_value then
table.insert(params, string.format(format_string_config.format_max_value, max_value))
else
table.insert(params, max_value)
end
end
end
local ret
@ -148,7 +156,16 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
format_string = N("@1: @2/@3")
end
if format_string_config == nil then
format_string_config = { order = { "label", "value", "max_value" } }
format_string_config = {}
end
if format_string_config.order == nil then
format_string_config.order = { "label", "value", "max_value" }
end
if format_string_config.format_value == nil then
format_string_config.format_value = "%d"
end
if format_string_config.format_max_value == nil then
format_string_config.format_max_value = "%d"
end
hudtable.add_all = function(player, hudtable, start_value, start_max, start_hidden)