#63 added indicator icon/light, added util.strrep string repeater

This commit is contained in:
Mikayla Fischler 2022-06-08 17:16:53 -04:00
parent 6f645579f8
commit d8bbe4b459
3 changed files with 122 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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