2022-06-09 15:59:55 +00:00
|
|
|
-- 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
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field id? string element id
|
2022-06-09 14:18:37 +00:00
|
|
|
---@field x? integer 1 if omitted
|
|
|
|
---@field y? integer 1 if omitted
|
|
|
|
---@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
|
2022-06-09 14:18:37 +00:00
|
|
|
|
|
|
|
-- new vertical bar
|
|
|
|
---@param args vbar_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-06-09 14:18:37 +00:00
|
|
|
local function vbar(args)
|
2022-06-09 15:59:55 +00:00
|
|
|
-- properties/state
|
2022-06-09 14:18:37 +00:00
|
|
|
local last_num_bars = -1
|
|
|
|
|
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
|
|
|
-- blit strings
|
|
|
|
local fgd = util.strrep(e.fg_bg.blit_fgd, e.frame.w)
|
|
|
|
local bkg = util.strrep(e.fg_bg.blit_bkg, e.frame.w)
|
2022-06-11 16:20:49 +00:00
|
|
|
local spaces = util.spaces(e.frame.w)
|
2022-06-09 14:18:37 +00:00
|
|
|
local one_third = util.strrep("\x8f", e.frame.w)
|
|
|
|
local two_thirds = util.strrep("\x83", e.frame.w)
|
|
|
|
|
|
|
|
-- handle data changes
|
|
|
|
function e.on_update(fraction)
|
|
|
|
-- 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
|
|
|
|
|
|
|
|
-- start bottom up
|
2022-06-25 20:20:58 +00:00
|
|
|
local y = e.frame.h
|
2022-06-09 14:18:37 +00:00
|
|
|
|
|
|
|
-- start at base of vertical bar
|
|
|
|
e.window.setCursorPos(1, y)
|
|
|
|
|
|
|
|
-- fill percentage
|
|
|
|
for _ = 1, num_bars / 3 do
|
|
|
|
e.window.blit(spaces, bkg, fgd)
|
|
|
|
y = y - 1
|
|
|
|
e.window.setCursorPos(1, y)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add fractional bar if needed
|
|
|
|
if num_bars % 3 == 1 then
|
|
|
|
e.window.blit(one_third, bkg, fgd)
|
|
|
|
y = y - 1
|
|
|
|
elseif num_bars % 3 == 2 then
|
|
|
|
e.window.blit(two_thirds, bkg, fgd)
|
|
|
|
y = y - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- fill the rest blank
|
|
|
|
while y > 0 do
|
|
|
|
e.window.setCursorPos(1, y)
|
|
|
|
e.window.blit(spaces, fgd, bkg)
|
|
|
|
y = y - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 14:09:34 +00:00
|
|
|
return e.complete()
|
2022-06-09 14:18:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return vbar
|