From d8bbe4b459251cf3acd63fa8fb9e26f8b80e8921 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 8 Jun 2022 17:16:53 -0400 Subject: [PATCH] #63 added indicator icon/light, added util.strrep string repeater --- graphics/elements/indicator_icon.lua | 61 +++++++++++++++++++++++++++ graphics/elements/indicator_light.lua | 49 +++++++++++++++++++++ scada-common/util.lua | 12 ++++++ 3 files changed, 122 insertions(+) create mode 100644 graphics/elements/indicator_icon.lua create mode 100644 graphics/elements/indicator_light.lua diff --git a/graphics/elements/indicator_icon.lua b/graphics/elements/indicator_icon.lua new file mode 100644 index 0000000..d36dbe7 --- /dev/null +++ b/graphics/elements/indicator_icon.lua @@ -0,0 +1,61 @@ +-- Icon Indicator Graphics Element + +local util = require("scada-common.util") + +local element = require("graphics.element") + +---@class icon_sym_color +---@field color cpair +---@field symbol string + +---@class icon_indicator_args +---@field text string indicator text +---@field states table state color and symbol table +---@field default? integer default state, defaults to 1 +---@field min_text_width? integer text length if omitted +---@field parent graphics_element +---@field x? integer 1 if omitted +---@field y? integer 1 if omitted +---@field height? integer parent height if omitted +---@field fg_bg cpair foreground/background colors + +-- new icon indicator +---@param args icon_indicator_args +local function icon_indicator(args) + -- determine width + args.width = (args.min_text_width or string.len(args.text)) + 4 + + -- create new graphics element base object + local e = element.new(args) + + -- state blit strings + local state_blit_cmds = {} + for i = 1, #args.states do + local sym_color = args.states[i] ---@type icon_sym_color + + table.insert(state_blit_cmds, { + text = " " .. sym_color.symbol .. " ", + fgd = util.strrep(sym_color.color.blit_fgd, 3), + bkg = util.strrep(sym_color.color.blit_bkg, 3) + }) + end + + -- write text and initial indicator light + e.setCursorPos(5, 1) + e.window.write(args.text) + + -- on state change + ---@param new_state integer indicator state + function e.on_update(new_state) + local blit_cmd = state_blit_cmds[new_state] + e.window.setCursorPos(1, 1) + e.window.blit(blit_cmd.text, blit_cmd.fgd, blit_cmd.bkg) + end + + -- initial icon draw + e.on_update(args.default or 1) + + return e.get() +end + +return icon_indicator diff --git a/graphics/elements/indicator_light.lua b/graphics/elements/indicator_light.lua new file mode 100644 index 0000000..b3357e8 --- /dev/null +++ b/graphics/elements/indicator_light.lua @@ -0,0 +1,49 @@ +-- Indicator Light Graphics Element + +local util = require("scada-common.util") + +local element = require("graphics.element") + +---@class indicator_light_args +---@field text string indicator text +---@field colors cpair on/off colors (a/b respectively) +---@field min_text_width? integer text length if omitted +---@field parent graphics_element +---@field x? integer 1 if omitted +---@field y? integer 1 if omitted +---@field height? integer parent height if omitted +---@field fg_bg cpair foreground/background colors + +-- new indicator light +---@param args indicator_light_args +local function indicator_light(args) + -- determine width + args.width = (args.min_text_width or string.len(args.text)) + 3 + + -- create new graphics element base object + local e = element.new(args) + + -- on/off blit strings + local on_blit = util.strrep(args.colors.blit_a, 2) + local off_blit = util.strrep(args.colors.blit_b, 2) + + -- write text and initial indicator light + e.setCursorPos(1, 1) + e.window.blit(" ", "000", off_blit .. e.fg_bg.blit_bkg) + e.window.write(args.text) + + -- on state change + ---@param new_state boolean indicator state + function e.on_update(new_state) + e.window.setCursorPos(1, 1) + if new_state then + e.window.blit(" ", "00", on_blit) + else + e.window.blit(" ", "00", off_blit) + end + end + + return e.get() +end + +return indicator_light diff --git a/scada-common/util.lua b/scada-common/util.lua index fc194ca..30498d6 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -56,6 +56,18 @@ function util.strval(val) end end +-- repeat a string n times +---@param str string +---@param n integer +---@return string +function util.strrep(str, n) + local repeated = "" + for _ = 1, n do + repeated = repeated .. str + end + return repeated +end + -- concatenation with built-in to string ---@vararg any ---@return string