Add right-to-left statbars, for hunger and air.

This commit is contained in:
iliekprogrammar 2021-04-07 20:39:49 +08:00
parent c451866a36
commit 61cef21cd1
4 changed files with 28 additions and 18 deletions

View file

@ -45,7 +45,7 @@ a vertical gradient.
### Icon ### Icon
A 16×16 image shown left of the HUD bar. This is optional. A 16×16 image shown left of the HUD bar. This is optional.
### `hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config)` ### `hb.register_hudbar(identifier, text_color, label, textures, direction, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config)`
This function registers a new custom HUD bar definition to the HUD bars mod, so it can be later used to be displayed, changed, hidden This function registers a new custom HUD bar definition to the HUD bars mod, so it can be later used to be displayed, changed, hidden
and unhidden on a per-player basis. and unhidden on a per-player basis.
Note this does not yet display the HUD bar. Note this does not yet display the HUD bar.
@ -63,6 +63,7 @@ for more information.
* `bar`: The file name of the bar image (as string). This is only used for the `progress_bar` bar type (see `README.txt`, settings section). * `bar`: The file name of the bar image (as string). This is only used for the `progress_bar` bar type (see `README.txt`, settings section).
* `icon`: The file name of the icon, as string. For the `progress_bar` type, it is shown as single image left of the bar, for the two statbar bar types, it is used as the statbar icon and will be repeated. This field can be `nil`, in which case no icon will be used, but this is not recommended, because the HUD bar will be invisible if the one of the statbar bar types is used. * `icon`: The file name of the icon, as string. For the `progress_bar` type, it is shown as single image left of the bar, for the two statbar bar types, it is used as the statbar icon and will be repeated. This field can be `nil`, in which case no icon will be used, but this is not recommended, because the HUD bar will be invisible if the one of the statbar bar types is used.
* `bgicon`: The file name of the background icon, it is used as the background for the modern statbar mode only. This field can be `nil`, in which case no background icon will be displayed in this mode. * `bgicon`: The file name of the background icon, it is used as the background for the modern statbar mode only. This field can be `nil`, in which case no background icon will be displayed in this mode.
* `direction`: Either left to right(0), or right to left(1).
* `default_start_value`: If this HUD bar is added to a player, and no initial value is specified, this value will be used as initial current value * `default_start_value`: If this HUD bar is added to a player, and no initial value is specified, this value will be used as initial current value
* `default_max_value`: If this HUD bar is added to a player, and no initial maximum value is specified, this value will be used as initial maximum value * `default_max_value`: If this HUD bar is added to a player, and no initial maximum value is specified, this value will be used as initial maximum value
* `default_start_hidden`: The HUD bar will be initially start hidden by default when added to a player. Use `hb.unhide_hudbar` to unhide it. * `default_start_hidden`: The HUD bar will be initially start hidden by default when added to a player. Use `hb.unhide_hudbar` to unhide it.

View file

@ -124,7 +124,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, format_string_config) function hb.register_hudbar(identifier, text_color, label, textures, direction, 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
@ -133,30 +133,33 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
if hb.settings.alignment_pattern == "stack_up" then if hb.settings.alignment_pattern == "stack_up" then
pos = hb.settings.pos_left pos = hb.settings.pos_left
offset = { offset = {
x = hb.settings.start_offset_left.x, x = direction == 0 and hb.settings.start_offset_left.x or -hb.settings.start_offset_right.x,
y = hb.settings.start_offset_left.y - hb.settings.vmargin * index y = hb.settings.start_offset_left.y - hb.settings.vmargin * index
} }
elseif hb.settings.alignment_pattern == "stack_down" then elseif hb.settings.alignment_pattern == "stack_down" then
pos = hb.settings.pos_left pos = hb.settings.pos_left
offset = { offset = {
x = hb.settings.start_offset_left.x, x = direction == 0 and hb.settings.start_offset_right.x or -hb.settings.start_offset_left.x,
y = hb.settings.start_offset_left.y + hb.settings.vmargin * index y = hb.settings.start_offset_left.y + hb.settings.vmargin * index
} }
else else -- zigzag
if index % 2 == 0 then if index % 2 == 0 then
pos = hb.settings.pos_left pos = hb.settings.pos_left
offset = { offset = {
x = hb.settings.start_offset_left.x, -- -(24+18) = -42. using linear eq, -42 = -258m - 24.
x = direction == 0 and hb.settings.start_offset_left.x or (-42+24)/(-258.0) * hb.settings.start_offset_left.x - 24,
y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index/2) y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index/2)
} }
else else
pos = hb.settings.pos_right pos = hb.settings.pos_right
offset = { offset = {
x = hb.settings.start_offset_right.x, -- 24*10+30 - 24 = 234. using linear eq, 234 = 16m - 24.
x = direction == 0 and hb.settings.start_offset_right.x or (234+24)/(16) * hb.settings.start_offset_right.x - 24,
y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index-1)/2) y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index-1)/2)
} }
end end
end end
if format_string == nil then if format_string == nil then
format_string = N("@1: @2/@3") format_string = N("@1: @2/@3")
end end
@ -181,6 +184,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
local state = {} local state = {}
local name = player:get_player_name() local name = player:get_player_name()
local bgscale, iconscale, text, barnumber, bgiconnumber local bgscale, iconscale, text, barnumber, bgiconnumber
if start_max == 0 or start_hidden then if start_max == 0 or start_hidden then
bgscale = { x=0, y=0 } bgscale = { x=0, y=0 }
else else
@ -197,6 +201,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
bgiconnumber = hb.settings.statbar_length bgiconnumber = hb.settings.statbar_length
text = make_label(format_string, format_string_config, 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({
hud_elem_type = "image", hud_elem_type = "image",
@ -219,6 +224,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
}) })
end end
end end
local bar_image, bgicon, bar_size local bar_image, bgicon, bar_size
if hb.settings.bar_type == "progress_bar" then if hb.settings.bar_type == "progress_bar" then
bar_image = textures.bar bar_image = textures.bar
@ -234,10 +240,12 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
bgicon = textures.bgicon bgicon = textures.bgicon
bar_size = {x=24, y=24} bar_size = {x=24, y=24}
end end
local text2 local text2
if hb.settings.bar_type == "statbar_modern" then if hb.settings.bar_type == "statbar_modern" then
text2 = bgicon text2 = bgicon
end end
ids.bar = player:hud_add({ ids.bar = player:hud_add({
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = pos, position = pos,
@ -247,7 +255,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
item = bgiconnumber, item = bgiconnumber,
alignment = {x=-1,y=-1}, alignment = {x=-1,y=-1},
offset = offset, offset = offset,
direction = 0, direction = direction,
size = bar_size, size = bar_size,
z_index = 1, z_index = 1,
}) })
@ -258,7 +266,7 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
text = text, text = text,
alignment = {x=1,y=1}, alignment = {x=1,y=1},
number = text_color, number = text_color,
direction = 0, direction = direction,
offset = { x = offset.x + 2, y = offset.y - 1}, offset = { x = offset.x + 2, y = offset.y - 1},
z_index = 2, z_index = 2,
}) })
@ -359,6 +367,7 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon
if new_text_color ~= nil then if new_text_color ~= nil then
player:hud_change(hudtable.hudids[name].text, "number", new_text_color) player:hud_change(hudtable.hudids[name].text, "number", new_text_color)
end end
else else
if new_icon ~= nil and hudtable.hudids[name].bar ~= nil then if new_icon ~= nil and hudtable.hudids[name].bar ~= nil then
player:hud_change(hudtable.hudids[name].bar, "text", new_icon) player:hud_change(hudtable.hudids[name].bar, "text", new_icon)
@ -474,8 +483,8 @@ end
--register built-in HUD bars --register built-in HUD bars
if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_hudbars then if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_hudbars then
hb.register_hudbar("health", 0xFFFFFF, S("Health"), { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png", bgicon = "hudbars_bgicon_health.png" }, 20, 20, false) hb.register_hudbar("health", 0xFFFFFF, S("Health"), { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png", bgicon = "hudbars_bgicon_health.png" }, 0, 20, 20, false)
hb.register_hudbar("breath", 0xFFFFFF, S("Breath"), { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png", bgicon = "hudbars_bgicon_breath.png" }, 10, 10, true) hb.register_hudbar("breath", 0xFFFFFF, S("Breath"), { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png", bgicon = "hudbars_bgicon_breath.png" }, 1, 10, 10, true)
end end
local function hide_builtin(player) local function hide_builtin(player)

View file

@ -57,7 +57,7 @@ local function custom_hud(player)
end end
--register and define armor HUD bar --register and define armor HUD bar
hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 20, mcl_hbarmor.autohide) hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 0, 20, mcl_hbarmor.autohide)
function mcl_hbarmor.get_armor(player) function mcl_hbarmor.get_armor(player)
if not player or not armor.def then if not player or not armor.def then

View file

@ -89,10 +89,10 @@ function mcl_hunger.update_exhaustion_hud(player, exhaustion)
end end
-- register saturation hudbar -- register saturation hudbar
hb.register_hudbar("hunger", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false) hb.register_hudbar("hunger", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 1, 20, 20, false)
if mcl_hunger.debug then if mcl_hunger.debug then
hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %.1f/%d")) hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, 1, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %.1f/%d"))
hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d")) hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 1, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d"))
end end
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)