2022-10-07 14:19:37 +00:00
|
|
|
-- Hazard-bordered Button Graphics Element
|
2022-06-16 16:17:41 +00:00
|
|
|
|
|
|
|
local tcd = require("scada-common.tcallbackdsp")
|
|
|
|
|
|
|
|
local core = require("graphics.core")
|
|
|
|
local element = require("graphics.element")
|
|
|
|
|
2022-10-07 14:28:46 +00:00
|
|
|
---@class hazard_button_args
|
2022-10-07 14:19:37 +00:00
|
|
|
---@field text string text to show on button
|
|
|
|
---@field accent color accent color for hazard border
|
2022-06-16 16:17:41 +00:00
|
|
|
---@field callback function function to call on touch
|
|
|
|
---@field parent graphics_element
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field id? string element id
|
2022-06-16 16:17:41 +00:00
|
|
|
---@field x? integer 1 if omitted
|
|
|
|
---@field y? integer 1 if omitted
|
2022-07-16 17:25:07 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2022-06-16 16:17:41 +00:00
|
|
|
|
2022-10-07 14:28:46 +00:00
|
|
|
-- new hazard button
|
|
|
|
---@param args hazard_button_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-10-07 14:28:46 +00:00
|
|
|
local function hazard_button(args)
|
2022-10-07 14:19:37 +00:00
|
|
|
assert(type(args.text) == "string", "graphics.elements.controls.hazard_button: text is a required field")
|
|
|
|
assert(type(args.accent) == "number", "graphics.elements.controls.hazard_button: accent is a required field")
|
|
|
|
assert(type(args.callback) == "function", "graphics.elements.controls.hazard_button: callback is a required field")
|
2022-06-16 16:17:41 +00:00
|
|
|
|
|
|
|
-- static dimensions
|
|
|
|
args.height = 3
|
2022-10-07 14:19:37 +00:00
|
|
|
args.width = string.len(args.text) + 4
|
2022-06-16 16:17:41 +00:00
|
|
|
|
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
|
|
|
-- write the button text
|
|
|
|
e.window.setCursorPos(3, 2)
|
2022-10-07 14:19:37 +00:00
|
|
|
e.window.write(args.text)
|
2022-06-16 16:17:41 +00:00
|
|
|
|
|
|
|
-- draw border
|
2022-10-20 16:22:45 +00:00
|
|
|
---@param accent color accent color
|
|
|
|
local function draw_border(accent)
|
|
|
|
-- top
|
2022-10-20 17:53:39 +00:00
|
|
|
e.window.setTextColor(accent)
|
2022-10-20 16:22:45 +00:00
|
|
|
e.window.setBackgroundColor(args.fg_bg.bkg)
|
|
|
|
e.window.setCursorPos(1, 1)
|
|
|
|
e.window.write("\x99\x89\x89\x89\x89\x89\x89\x89\x99")
|
|
|
|
|
|
|
|
-- center left
|
|
|
|
e.window.setCursorPos(1, 2)
|
|
|
|
e.window.setTextColor(args.fg_bg.bkg)
|
2022-10-20 17:53:39 +00:00
|
|
|
e.window.setBackgroundColor(accent)
|
2022-10-20 16:22:45 +00:00
|
|
|
e.window.write("\x99")
|
|
|
|
|
|
|
|
-- center right
|
|
|
|
e.window.setTextColor(args.fg_bg.bkg)
|
2022-10-20 17:53:39 +00:00
|
|
|
e.window.setBackgroundColor(accent)
|
2022-10-20 16:22:45 +00:00
|
|
|
e.window.setCursorPos(9, 2)
|
|
|
|
e.window.write("\x99")
|
|
|
|
|
|
|
|
-- bottom
|
2022-10-20 17:53:39 +00:00
|
|
|
e.window.setTextColor(accent)
|
2022-10-20 16:22:45 +00:00
|
|
|
e.window.setBackgroundColor(args.fg_bg.bkg)
|
|
|
|
e.window.setCursorPos(1, 3)
|
|
|
|
e.window.write("\x99\x98\x98\x98\x98\x98\x98\x98\x99")
|
|
|
|
end
|
2022-06-16 16:17:41 +00:00
|
|
|
|
|
|
|
-- handle touch
|
|
|
|
---@param event monitor_touch monitor touch event
|
|
|
|
---@diagnostic disable-next-line: unused-local
|
|
|
|
function e.handle_touch(event)
|
2022-10-20 16:22:45 +00:00
|
|
|
if e.enabled then
|
|
|
|
-- call the touch callback
|
|
|
|
args.callback()
|
|
|
|
|
|
|
|
-- change text color to indicate clicked
|
|
|
|
e.window.setTextColor(args.accent)
|
|
|
|
e.window.setCursorPos(3, 2)
|
|
|
|
e.window.write(args.text)
|
|
|
|
|
|
|
|
-- restore text color after 1 second
|
|
|
|
tcd.dispatch(1, function ()
|
|
|
|
e.window.setTextColor(args.fg_bg.fgd)
|
|
|
|
e.window.setCursorPos(3, 2)
|
|
|
|
e.window.write(args.text)
|
|
|
|
end)
|
|
|
|
end
|
2022-06-16 16:17:41 +00:00
|
|
|
end
|
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
-- set the value
|
|
|
|
---@param val boolean new value
|
|
|
|
function e.set_value(val)
|
|
|
|
if val then e.handle_touch(core.events.touch("", 1, 1)) end
|
|
|
|
end
|
|
|
|
|
2022-10-20 17:53:39 +00:00
|
|
|
-- initial draw of border
|
|
|
|
---@todo disabling will change border
|
|
|
|
draw_border(args.accent)
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
return e.get()
|
2022-06-16 16:17:41 +00:00
|
|
|
end
|
|
|
|
|
2022-10-07 14:28:46 +00:00
|
|
|
return hazard_button
|