cc-mek-scada/graphics/elements/indicators/hbar.lua

129 lines
3.6 KiB
Lua
Raw Normal View History

2022-06-08 18:16:05 +00:00
-- Horizontal Bar Graphics Element
local util = require("scada-common.util")
local element = require("graphics.element")
---@class hbar_args
---@field show_percent? boolean whether or not to show the percent
---@field bar_fg_bg? cpair bar foreground/background colors if showing percent
2022-06-08 18:16:05 +00:00
---@field parent graphics_element
---@field id? string element id
2022-06-08 18:16:05 +00:00
---@field x? integer 1 if omitted
2023-07-10 03:42:44 +00:00
---@field y? integer auto incremented if omitted
2022-06-08 18:16:05 +00:00
---@field width? integer parent width if omitted
---@field height? integer parent height if omitted
---@field gframe? graphics_frame frame instead of x/y/width/height
2022-06-11 21:06:32 +00:00
---@field fg_bg? cpair foreground/background colors
---@field hidden? boolean true to hide on initial draw
2022-06-08 18:16:05 +00:00
-- new horizontal bar
2023-02-25 00:50:01 +00:00
---@nodiscard
2022-06-08 18:16:05 +00:00
---@param args hbar_args
---@return graphics_element element, element_id id
2022-06-08 18:16:05 +00:00
local function hbar(args)
-- create new graphics element base object
local e = element.new(args)
e.value = 0.0
-- bar width is width - 5 characters for " 100%" if showing percent
local bar_width = util.trinary(args.show_percent, e.frame.w - 5, e.frame.w)
2022-06-08 18:16:05 +00:00
2023-09-30 15:46:47 +00:00
element.assert(bar_width > 0, "too small for bar")
local last_num_bars = -1
2022-06-08 18:16:05 +00:00
-- determine bar colors
2022-06-16 15:29:47 +00:00
local bar_bkg = e.fg_bg.blit_bkg
local bar_fgd = e.fg_bg.blit_fgd
if args.bar_fg_bg ~= nil then
2022-06-16 15:29:47 +00:00
bar_bkg = args.bar_fg_bg.blit_bkg
bar_fgd = args.bar_fg_bg.blit_fgd
end
2022-06-08 18:16:05 +00:00
-- handle data changes
2022-09-12 19:58:43 +00:00
---@param fraction number 0.0 to 1.0
2022-06-08 18:16:05 +00:00
function e.on_update(fraction)
e.value = fraction
2022-06-08 18:16:05 +00:00
-- enforce minimum and maximum
if fraction < 0 then
fraction = 0.0
elseif fraction > 1 then
fraction = 1.0
end
-- compute number of bars
local num_bars = util.round(fraction * (bar_width * 2))
2022-06-08 18:16:05 +00:00
-- redraw bar if changed
if num_bars ~= last_num_bars then
last_num_bars = num_bars
local fgd = ""
local bkg = ""
2022-06-08 18:16:05 +00:00
local spaces = ""
-- fill percentage
for _ = 1, num_bars / 2 do
spaces = spaces .. " "
fgd = fgd .. bar_fgd
bkg = bkg .. bar_bkg
2022-06-08 18:16:05 +00:00
end
-- add fractional bar if needed
if num_bars % 2 == 1 then
spaces = spaces .. "\x95"
fgd = fgd .. bar_bkg
bkg = bkg .. bar_fgd
2022-06-08 18:16:05 +00:00
end
-- pad background
for _ = 1, ((bar_width * 2) - num_bars) / 2 do
2022-06-08 18:16:05 +00:00
spaces = spaces .. " "
fgd = fgd .. bar_bkg
bkg = bkg .. bar_bkg
2022-06-08 18:16:05 +00:00
end
-- draw bar
for y = 1, e.frame.h do
2023-08-31 01:11:57 +00:00
e.w_set_cur(1, y)
-- intentionally swapped fgd/bkg since we use spaces as fill, but they are the opposite
2023-08-31 01:11:57 +00:00
e.w_blit(spaces, bkg, fgd)
end
2022-06-08 18:16:05 +00:00
end
-- update percentage
if args.show_percent then
2023-08-31 01:11:57 +00:00
e.w_set_cur(bar_width + 2, math.max(1, math.ceil(e.frame.h / 2)))
e.w_write(util.sprintf("%3.0f%%", fraction * 100))
end
2022-06-08 18:16:05 +00:00
end
-- change bar color
---@param bar_fg_bg cpair new bar colors
function e.recolor(bar_fg_bg)
bar_bkg = bar_fg_bg.blit_bkg
bar_fgd = bar_fg_bg.blit_fgd
e.redraw()
end
2022-09-12 19:58:43 +00:00
-- set the percentage value
---@param val number 0.0 to 1.0
function e.set_value(val) e.on_update(val) end
-- element redraw
function e.redraw()
last_num_bars = -1
e.on_update(e.value)
end
-- initial draw
e.redraw()
return e.complete()
2022-06-08 18:16:05 +00:00
end
return hbar