2022-06-08 21:16:53 +00:00
|
|
|
-- 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
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field label string indicator label
|
2022-06-08 21:16:53 +00:00
|
|
|
---@field states table state color and symbol table
|
2022-06-16 15:19:32 +00:00
|
|
|
---@field value? integer default state, defaults to 1
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field min_label_width? integer label length if omitted
|
2022-06-08 21:16:53 +00:00
|
|
|
---@field parent graphics_element
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field id? string element id
|
2022-06-08 21:16:53 +00:00
|
|
|
---@field x? integer 1 if omitted
|
|
|
|
---@field y? integer 1 if omitted
|
2022-06-11 21:06:32 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2023-05-25 21:40:16 +00:00
|
|
|
---@field hidden? boolean true to hide on initial draw
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
-- new icon indicator
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-06-08 21:16:53 +00:00
|
|
|
---@param args icon_indicator_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-06-16 15:19:32 +00:00
|
|
|
local function icon(args)
|
|
|
|
assert(type(args.label) == "string", "graphics.elements.indicators.icon: label is a required field")
|
|
|
|
assert(type(args.states) == "table", "graphics.elements.indicators.icon: states is a required field")
|
|
|
|
|
|
|
|
-- single line
|
|
|
|
args.height = 1
|
2022-06-11 21:06:32 +00:00
|
|
|
|
2022-06-08 21:16:53 +00:00
|
|
|
-- determine width
|
2022-06-11 16:21:14 +00:00
|
|
|
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 4
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2022-06-11 16:21:14 +00:00
|
|
|
-- write label and initial indicator light
|
2022-06-11 21:06:32 +00:00
|
|
|
e.window.setCursorPos(5, 1)
|
2022-06-11 16:21:14 +00:00
|
|
|
e.window.write(args.label)
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
-- on state change
|
|
|
|
---@param new_state integer indicator state
|
|
|
|
function e.on_update(new_state)
|
|
|
|
local blit_cmd = state_blit_cmds[new_state]
|
2022-09-12 16:59:28 +00:00
|
|
|
e.value = new_state
|
2022-06-08 21:16:53 +00:00
|
|
|
e.window.setCursorPos(1, 1)
|
|
|
|
e.window.blit(blit_cmd.text, blit_cmd.fgd, blit_cmd.bkg)
|
|
|
|
end
|
|
|
|
|
2022-09-12 19:58:43 +00:00
|
|
|
-- set indicator state
|
|
|
|
---@param val integer indicator state
|
2022-09-12 16:59:28 +00:00
|
|
|
function e.set_value(val) e.on_update(val) end
|
|
|
|
|
2022-06-08 21:16:53 +00:00
|
|
|
-- initial icon draw
|
2022-06-16 15:19:32 +00:00
|
|
|
e.on_update(args.value or 1)
|
2022-06-08 21:16:53 +00:00
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
return e.get()
|
2022-06-08 21:16:53 +00:00
|
|
|
end
|
|
|
|
|
2022-06-16 15:19:32 +00:00
|
|
|
return icon
|