2022-06-08 21:16:53 +00:00
|
|
|
-- Indicator Light Graphics Element
|
|
|
|
|
|
|
|
local element = require("graphics.element")
|
|
|
|
|
|
|
|
---@class indicator_light_args
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field label string indicator label
|
2022-06-08 21:16:53 +00:00
|
|
|
---@field colors cpair on/off colors (a/b respectively)
|
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
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
-- new indicator light
|
|
|
|
---@param args indicator_light_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-06-08 21:16:53 +00:00
|
|
|
local function indicator_light(args)
|
2022-06-16 15:19:32 +00:00
|
|
|
assert(type(args.label) == "string", "graphics.elements.indicators.light: label is a required field")
|
|
|
|
assert(type(args.colors) == "table", "graphics.elements.indicators.light: colors 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-07-16 17:25:07 +00:00
|
|
|
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 2
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
|
|
|
-- on state change
|
|
|
|
---@param new_state boolean indicator state
|
|
|
|
function e.on_update(new_state)
|
|
|
|
e.window.setCursorPos(1, 1)
|
|
|
|
if new_state then
|
2022-07-16 17:25:07 +00:00
|
|
|
e.window.blit(" \x95", "0" .. args.colors.blit_a, args.colors.blit_a .. e.fg_bg.blit_bkg)
|
2022-06-08 21:16:53 +00:00
|
|
|
else
|
2022-07-16 17:25:07 +00:00
|
|
|
e.window.blit(" \x95", "0" .. args.colors.blit_b, args.colors.blit_b .. e.fg_bg.blit_bkg)
|
2022-06-08 21:16:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-16 17:25:07 +00:00
|
|
|
-- write label and initial indicator light
|
|
|
|
e.on_update(false)
|
|
|
|
e.window.write(args.label)
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
return e.get()
|
2022-06-08 21:16:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return indicator_light
|