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

@ -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: * `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` * `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" }` * `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
Example (mostly) from `hbarmor` mod: Example (mostly) from `hbarmor` mod:

@ -58,11 +58,19 @@ local function make_label(format_string, format_string_config, label, start_valu
if order[o] == "label" then if order[o] == "label" then
table.insert(params, label) table.insert(params, label)
elseif order[o] == "value" then elseif order[o] == "value" then
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) table.insert(params, start_value)
end
elseif order[o] == "max_value" then elseif order[o] == "max_value" then
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) table.insert(params, max_value)
end end
end end
end
local ret local ret
if format_string_config.textdomain then if format_string_config.textdomain then
ret = minetest.translate(format_string_config.textdomain, format_string, unpack(params)) ret = minetest.translate(format_string_config.textdomain, format_string, unpack(params))
@ -148,7 +156,16 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
format_string = N("@1: @2/@3") format_string = N("@1: @2/@3")
end end
if format_string_config == nil then 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 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)