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

108 lines
2.8 KiB
Lua
Raw Normal View History

-- Vertical Bar Graphics Element
2022-06-09 14:18:37 +00:00
local util = require("scada-common.util")
local element = require("graphics.element")
---@class vbar_args
---@field parent graphics_element
---@field id? string element id
2022-06-09 14:18:37 +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-09 14:18:37 +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-09 14:18:37 +00:00
-- new vertical bar
2023-02-25 00:50:01 +00:00
---@nodiscard
2022-06-09 14:18:37 +00:00
---@param args vbar_args
---@return graphics_element element, element_id id
2022-06-09 14:18:37 +00:00
local function vbar(args)
-- create new graphics element base object
local e = element.new(args)
e.value = 0.0
local last_num_bars = -1
local fgd = string.rep(e.fg_bg.blit_fgd, e.frame.w)
local bkg = string.rep(e.fg_bg.blit_bkg, e.frame.w)
2022-06-11 16:20:49 +00:00
local spaces = util.spaces(e.frame.w)
local one_third = string.rep("\x8f", e.frame.w)
local two_thirds = string.rep("\x83", e.frame.w)
2022-06-09 14:18:37 +00:00
-- handle data changes
2022-09-12 19:58:43 +00:00
---@param fraction number 0.0 to 1.0
2022-06-09 14:18:37 +00:00
function e.on_update(fraction)
e.value = fraction
2022-06-09 14:18:37 +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
2022-06-25 20:20:58 +00:00
local num_bars = util.round(fraction * (e.frame.h * 3))
2022-06-09 14:18:37 +00:00
-- redraw only if number of bars has changed
if num_bars ~= last_num_bars then
last_num_bars = num_bars
2022-06-25 20:20:58 +00:00
local y = e.frame.h
2023-08-31 01:11:57 +00:00
e.w_set_cur(1, y)
2022-06-09 14:18:37 +00:00
-- fill percentage
for _ = 1, num_bars / 3 do
2023-08-31 01:11:57 +00:00
e.w_blit(spaces, bkg, fgd)
2022-06-09 14:18:37 +00:00
y = y - 1
2023-08-31 01:11:57 +00:00
e.w_set_cur(1, y)
2022-06-09 14:18:37 +00:00
end
-- add fractional bar if needed
if num_bars % 3 == 1 then
2023-08-31 01:11:57 +00:00
e.w_blit(one_third, bkg, fgd)
2022-06-09 14:18:37 +00:00
y = y - 1
elseif num_bars % 3 == 2 then
2023-08-31 01:11:57 +00:00
e.w_blit(two_thirds, bkg, fgd)
2022-06-09 14:18:37 +00:00
y = y - 1
end
-- fill the rest blank
while y > 0 do
2023-08-31 01:11:57 +00:00
e.w_set_cur(1, y)
e.w_blit(spaces, fgd, bkg)
2022-06-09 14:18:37 +00:00
y = y - 1
end
end
end
-- 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
-- change bar color
---@param fg_bg cpair new bar colors
function e.recolor(fg_bg)
fgd = string.rep(fg_bg.blit_fgd, e.frame.w)
bkg = string.rep(fg_bg.blit_bkg, e.frame.w)
e.redraw()
end
-- initial draw
e.redraw()
return e.complete()
2022-06-09 14:18:37 +00:00
end
return vbar