firefly/mods/firefly_hudbars/init.lua
2026-02-14 12:37:11 -05:00

132 lines
5 KiB
Lua

local ns = firefly
Player:listen('init', function(m)
m.hud_bars = {}
m.hud_bars_list = {}
end)
function ns.add_hud_bar(m, name, def)
if m.hud_bars[name] then
ns.remove_hud_bar(m, name)
end
if not def.color then def.color = "#48d" end
local ypos = #m.hud_bars_list *50
local pixel_scale = 3
local hud = {
left = ns.hud_add(m, {
type = "image",
image = "firefly_hud_bar_left"..(def.value == 0 and "_bg" or "")..".png^[multiply:"..def.color,
pos = {x = 0.5, y = 0},
offset = {x = -400 -pixel_scale, y = ypos +36},
scale = {x = pixel_scale, y = pixel_scale}
}),
center = ns.hud_add(m, {
type = "image",
image = "firefly_hud_bar_center_bg.png^[multiply:"..def.color,
pos = {x = 0.5, y = 0},
offset = {x = 0, y = ypos +36},
scale = {x = 800, y = pixel_scale}
}),
overlay = ns.hud_add(m, {
type = "image",
image = "firefly_hud_bar_center.png^[multiply:"..def.color,
pos = {x = 0.5, y = 0},
offset = {x = -400, y = ypos +36},
align = {x = 1, y = 0},
scale = {x = 800 *(def.value /def.max), y = pixel_scale}
}),
right = ns.hud_add(m, {
type = "image",
image = "firefly_hud_bar_right"..(def.value == def.max and "" or "_bg")..".png^[multiply:"..def.color,
pos = {x = 0.5, y = 0},
offset = {x = 400 +pixel_scale, y = ypos +36},
scale = {x = pixel_scale, y = pixel_scale}
}),
title = ns.hud_add(m, {
type = "text",
text = def.title or "",
pos = {x = 0.5, y = 0},
offset = {x = 0, y = ypos +4},
align = {x = 0, y = 1},
color = minetest.colorspec_to_table(def.color)
})
}
m.hud_bars[name] = {
def = def,
hud = hud,
index = #m.hud_bars_list +1
}
table.insert(m.hud_bars_list, name)
end
function ns.change_hud_bar(m, name, def)
local bar = m.hud_bars[name]
if not bar then return end
local last_value = bar.def.value
local last_title = bar.def.title
local last_color = bar.def.color
extend(bar.def, def)
if def.value or def.max then
if last_color ~= bar.def.color then
bar.hud.right:update(m, {text = "firefly_hud_bar_right"..(def.value == def.max and "" or "_bg")..".png^[multiply:"..def.color})
bar.hud.left:update(m, {text = "firefly_hud_bar_left"..(def.value == 0 and "_bg" or "")..".png^[multiply:"..def.color})
bar.hud.center:update(m, {text = "firefly_hud_bar_center_bg.png^[multiply:"..def.color})
bar.hud.overlay:update(m, {text = "firefly_hud_bar_center.png^[multiply:"..def.color})
bar.hud.title:update(m, {color = minetest.colorspec_to_table(def.color)})
end
if bar.def.value == bar.def.max and last_value ~= bar.def.max then
minetest.after(def.ease or 0.3, function()
bar.hud.right:update(m, {text = "firefly_hud_bar_right.png^[multiply:"..bar.def.color})
end)
elseif last_value == bar.def.max then
bar.hud.right:update(m, {text = "firefly_hud_bar_right_bg.png^[multiply:"..bar.def.color})
end
if bar.def.value == 0 and last_value ~= 0 then
minetest.after(def.ease or 0.3, function()
bar.hud.left:update(m, {image = "firefly_hud_bar_left_bg.png^[multiply:"..bar.def.color})
end)
elseif last_value == 0 then
bar.hud.left:update(m, {image = "firefly_hud_bar_left.png^[multiply:"..bar.def.color})
end
if last_title ~= bar.def.title then
bar.hud.title:update(m, {text = bar.def.title})
end
bar.hud.overlay:animate {
scale = {
value = {x = 800 *(bar.def.value /bar.def.max), y = 3},
duration = def.ease or 0.3,
ease_fn = {0.42, 0, 0.58, 1.06}
}
}
end
end
function ns.remove_hud_bar(m, name)
if not m.hud_bars[name] then return end
for _, x in pairs(m.hud_bars[name].hud) do
x:remove(m)
end
m.hud_bars[name] = nil
table.remove(m.hud_bars_list, table.indexof(m.hud_bars_list, name))
-- Reposition all other HUD bars so there are no gaps.
for i = 1, #m.hud_bars_list do
local bar = m.hud_bars[m.hud_bars_list[i]]
if bar.index ~= i then
local dist = i *50
bar.hud.right:update(m, {offset = {x = bar.hud.right.offset.x, y = dist +36}})
bar.hud.left:update(m, {offset = {x = bar.hud.left.offset.x, y = dist +36}})
bar.hud.center:update(m, {offset = {x = bar.hud.center.offset.x, y = dist +36}})
bar.hud.overlay:update(m, {offset = {x = bar.hud.overlay.offset.x, y = dist +36}})
bar.hud.title:update(m, {offset = {x = bar.hud.title.offset.x, y = dist +4}})
end
end
end