From 9d107da8d9b9380ec91d9a4509b7f19107f2a53b Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 8 Jun 2022 14:16:05 -0400 Subject: [PATCH] #63 horizontal fill bar indicator --- graphics/elements/hbar.lua | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 graphics/elements/hbar.lua diff --git a/graphics/elements/hbar.lua b/graphics/elements/hbar.lua new file mode 100644 index 0000000..f200bee --- /dev/null +++ b/graphics/elements/hbar.lua @@ -0,0 +1,84 @@ +-- Horizontal Bar Graphics Element + +local util = require("scada-common.util") + +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 +---@field width? integer parent width if omitted +---@field fg_bg cpair foreground/background colors + +-- new horizontal bar +---@param args hbar_args +local function hbar(args) + local bkg = "" + local last_num_bars = -1 + + -- create new graphics element base object + local e = element.new(args) + + -- bar width is width - 5 characters for " 100%" + local bar_width = e.frame.w - 5 + + assert(bar_width > 0, "graphics.elements.hbar: too small for bar") + + -- set background blit string + for _ = 1, bar_width do + bkg = bkg .. args.bar_fg_bg.blit_bkg + end + + -- 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) / (bar_width * 2)) + + -- redraw bar if changed + if num_bars ~= last_num_bars then + last_num_bars = num_bars + + local bar = "" + local spaces = "" + + -- fill percentage + for _ = 1, num_bars / 2 do + spaces = spaces .. " " + bar = bar .. args.bar_fg_bg.blit_fgd + end + + -- add fractional bar if needed + if num_bars % 2 == 1 then + spaces = spaces .. "\x95" + bar = bar .. args.bar_fg_bg.blit_fgd + end + + -- pad background + for _ = 1, bar_width - ((num_bars / 2) + num_bars % 2) do + spaces = spaces .. " " + bar = bar .. args.bar_fg_bg.blit_bkg + end + + e.window.setCursorPos(1, 1) + e.window.blit(spaces, bar, bkg) + end + + -- update percentage + e.window.setCursorPos(bar_width + 1, 1) + e.window.write(util.sprintf("%3.0f%%", fraction * 100)) + end + + return e.get() +end + +return hbar