Fix builtin statbar backgrounds

see #12000
This commit is contained in:
Lars Mueller 2022-01-28 17:50:51 +01:00 committed by Hugues Ross
parent 80812b86d6
commit 1e4d6672be
2 changed files with 37 additions and 38 deletions

@ -1,7 +1,8 @@
-- cache setting -- cache setting
local enable_damage = core.settings:get_bool("enable_damage") local enable_damage = core.settings:get_bool("enable_damage")
local health_bar_definition = { local bar_definitions = {
hp = {
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = {x = 0.5, y = 1}, position = {x = 0.5, y = 1},
text = "heart.png", text = "heart.png",
@ -11,29 +12,28 @@ local health_bar_definition = {
direction = 0, direction = 0,
size = {x = 24, y = 24}, size = {x = 24, y = 24},
offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)}, offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)},
} },
breath = {
local breath_bar_definition = {
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = {x = 0.5, y = 1}, position = {x = 0.5, y = 1},
text = "bubble.png", text = "bubble.png",
text2 = "bubble_gone.png", text2 = "bubble_gone.png",
number = core.PLAYER_MAX_BREATH_DEFAULT, number = core.PLAYER_MAX_BREATH_DEFAULT * 2,
item = core.PLAYER_MAX_BREATH_DEFAULT * 2, item = core.PLAYER_MAX_BREATH_DEFAULT * 2,
direction = 0, direction = 0,
size = {x = 24, y = 24}, size = {x = 24, y = 24},
offset = {x = 25, y= -(48 + 24 + 16)}, offset = {x = 25, y= -(48 + 24 + 16)},
},
} }
local hud_ids = {} local hud_ids = {}
local function scaleToDefault(player, field) local function scaleToHudMax(player, field)
-- Scale "hp" or "breath" to the default dimensions -- Scale "hp" or "breath" to the hud maximum dimensions
local current = player["get_" .. field](player) local current = player["get_" .. field](player)
local nominal = core["PLAYER_MAX_" .. field:upper() .. "_DEFAULT"] local nominal = bar_definitions[field].item
local max_display = math.max(nominal, local max_display = math.max(player:get_properties()[field .. "_max"], current)
math.max(player:get_properties()[field .. "_max"], current)) return math.ceil(current / max_display * nominal)
return current / max_display * nominal
end end
local function update_builtin_statbars(player) local function update_builtin_statbars(player)
@ -55,9 +55,9 @@ local function update_builtin_statbars(player)
local immortal = player:get_armor_groups().immortal == 1 local immortal = player:get_armor_groups().immortal == 1
if flags.healthbar and enable_damage and not immortal then if flags.healthbar and enable_damage and not immortal then
local number = scaleToDefault(player, "hp") local number = scaleToHudMax(player, "hp")
if hud.id_healthbar == nil then if hud.id_healthbar == nil then
local hud_def = table.copy(health_bar_definition) local hud_def = table.copy(bar_definitions.hp)
hud_def.number = number hud_def.number = number
hud.id_healthbar = player:hud_add(hud_def) hud.id_healthbar = player:hud_add(hud_def)
else else
@ -73,9 +73,9 @@ local function update_builtin_statbars(player)
local breath = player:get_breath() local breath = player:get_breath()
local breath_max = player:get_properties().breath_max local breath_max = player:get_properties().breath_max
if show_breathbar and breath <= breath_max then if show_breathbar and breath <= breath_max then
local number = 2 * scaleToDefault(player, "breath") local number = scaleToHudMax(player, "breath")
if not hud.id_breathbar and breath < breath_max then if not hud.id_breathbar and breath < breath_max then
local hud_def = table.copy(breath_bar_definition) local hud_def = table.copy(bar_definitions.breath)
hud_def.number = number hud_def.number = number
hud.id_breathbar = player:hud_add(hud_def) hud.id_breathbar = player:hud_add(hud_def)
elseif hud.id_breathbar then elseif hud.id_breathbar then
@ -145,7 +145,7 @@ function core.hud_replace_builtin(hud_name, definition)
end end
if hud_name == "health" then if hud_name == "health" then
health_bar_definition = definition bar_definitions.hp = definition
for name, ids in pairs(hud_ids) do for name, ids in pairs(hud_ids) do
local player = core.get_player_by_name(name) local player = core.get_player_by_name(name)
@ -159,7 +159,7 @@ function core.hud_replace_builtin(hud_name, definition)
end end
if hud_name == "breath" then if hud_name == "breath" then
breath_bar_definition = definition bar_definitions.breath = definition
for name, ids in pairs(hud_ids) do for name, ids in pairs(hud_ids) do
local player = core.get_player_by_name(name) local player = core.get_player_by_name(name)

@ -676,7 +676,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
// Rectangles for 1/2 the "off state" texture // Rectangles for 1/2 the "off state" texture
core::rect<s32> srchalfrect2, dsthalfrect2; core::rect<s32> srchalfrect2, dsthalfrect2;
if (count % 2 == 1) { if (count % 2 == 1 || maxcount % 2 == 1) {
// Need to draw halves: Calculate rectangles // Need to draw halves: Calculate rectangles
srchalfrect = calculate_clipping_rect(srcd, steppos); srchalfrect = calculate_clipping_rect(srcd, steppos);
dsthalfrect = calculate_clipping_rect(dstd, steppos); dsthalfrect = calculate_clipping_rect(dstd, steppos);
@ -711,7 +711,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
} }
} }
if (stat_texture_bg && maxcount > count / 2) { if (stat_texture_bg && maxcount > count) {
// Draw "off state" textures // Draw "off state" textures
s32 start_offset; s32 start_offset;
if (count % 2 == 1) if (count % 2 == 1)
@ -731,8 +731,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
if (maxcount % 2 == 1) { if (maxcount % 2 == 1) {
draw2DImageFilterScaled(driver, stat_texture_bg, draw2DImageFilterScaled(driver, stat_texture_bg,
dsthalfrect + p, srchalfrect, dsthalfrect + p, srchalfrect, NULL, colors, true);
NULL, colors, true);
} }
} }
} }