From 11e4d89b1da85788412dd7e5b1945b5119d062c2 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Thu, 9 Jun 2022 10:18:37 -0400 Subject: [PATCH] #63 vertical fill bar indicator --- graphics/element.lua | 7 ++++ graphics/elements/hbar.lua | 1 - graphics/elements/vbar.lua | 82 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 graphics/elements/vbar.lua diff --git a/graphics/element.lua b/graphics/element.lua index e151227..da296cb 100644 --- a/graphics/element.lua +++ b/graphics/element.lua @@ -32,6 +32,7 @@ function element.new(args) -- get the parent window self.p_window = args.window or args.parent.window() + -- check window assert(self.p_window, "graphics.element: no parent window provided") -- get frame coordinates/size @@ -48,6 +49,12 @@ function element.new(args) protected.frame.h = args.height or h end + -- check frame + assert(protected.frame.x >= 1, "graphics.element: frame x not >= 1") + assert(protected.frame.y >= 1, "graphics.element: frame y not >= 1") + assert(protected.frame.w >= 1, "graphics.element: frame width not >= 1") + assert(protected.frame.h >= 1, "graphics.element: frame height not >= 1") + -- create window local f = protected.frame protected.window = window.create(self.p_window, f.x, f.y, f.w, f.h, true) diff --git a/graphics/elements/hbar.lua b/graphics/elements/hbar.lua index 37e1550..189a429 100644 --- a/graphics/elements/hbar.lua +++ b/graphics/elements/hbar.lua @@ -6,7 +6,6 @@ local element = require("graphics.element") ---@class hbar_args ---@field bar_fg_bg cpair bar foreground/background colors ----@field border? graphics_border ---@field parent graphics_element ---@field x? integer 1 if omitted ---@field y? integer 1 if omitted diff --git a/graphics/elements/vbar.lua b/graphics/elements/vbar.lua new file mode 100644 index 0000000..d626575 --- /dev/null +++ b/graphics/elements/vbar.lua @@ -0,0 +1,82 @@ +-- Horizontal Bar Graphics Element + +local util = require("scada-common.util") + +local element = require("graphics.element") + +---@class vbar_args +---@field parent graphics_element +---@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 +---@field fg_bg cpair foreground/background colors + +-- new vertical bar +---@param args vbar_args +local function vbar(args) + -- last state + 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) + local spaces = util.strrep(" ", e.frame.w) + 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 + local num_bars = util.round((fraction * 100) / (e.frame.h * 3)) + + -- redraw only if number of bars has changed + if num_bars ~= last_num_bars then + last_num_bars = num_bars + + -- start bottom up + local y = e.window.h + + -- 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 + + return e.get() +end + +return vbar