cc-mek-scada/graphics/elements/controls/switch_button.lua

82 lines
2.5 KiB
Lua
Raw Normal View History

2022-06-08 18:48:17 +00:00
-- Button Graphics Element
local core = require("graphics.core")
2022-06-08 18:48:17 +00:00
local element = require("graphics.element")
---@class switch_button_args
2022-06-08 18:48:17 +00:00
---@field text string button text
---@field callback function function to call on touch
---@field default? boolean default state, defaults to off (false)
2022-06-08 18:48:17 +00:00
---@field min_width? integer text length + 2 if omitted
---@field active_fg_bg cpair foreground/background colors when pressed
2022-06-08 18:48:17 +00:00
---@field parent graphics_element
---@field id? string element id
2022-06-08 18:48:17 +00:00
---@field x? integer 1 if omitted
2023-07-10 03:42:44 +00:00
---@field y? integer auto incremented if omitted
2022-06-08 18:48:17 +00:00
---@field height? integer parent height if omitted
2022-06-11 21:06:32 +00:00
---@field fg_bg? cpair foreground/background colors
---@field hidden? boolean true to hide on initial draw
2022-06-08 18:48:17 +00:00
-- new switch button (latch high/low)
---@param args switch_button_args
---@return graphics_element element, element_id id
local function switch_button(args)
2023-09-30 15:46:47 +00:00
element.assert(type(args.text) == "string", "text is a required field")
element.assert(type(args.callback) == "function", "callback is a required field")
element.assert(type(args.active_fg_bg) == "table", "active_fg_bg is a required field")
element.assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "min_width must be nil or a number > 0")
2022-06-08 18:48:17 +00:00
local text_width = string.len(args.text)
2023-05-11 23:55:02 +00:00
args.height = 1
args.min_width = args.min_width or 0
args.width = math.max(text_width, args.min_width)
2022-06-08 18:48:17 +00:00
-- create new graphics element base object
local e = element.new(args)
e.value = args.default or false
2022-12-04 18:59:10 +00:00
local h_pad = math.floor((e.frame.w - text_width) / 2) + 1
2022-06-08 18:48:17 +00:00
local v_pad = math.floor(e.frame.h / 2) + 1
-- show the button state
function e.redraw()
if e.value then
2023-08-31 01:11:57 +00:00
e.w_set_fgd(args.active_fg_bg.fgd)
e.w_set_bkg(args.active_fg_bg.bkg)
else
2023-08-31 01:11:57 +00:00
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
2022-06-08 18:48:17 +00:00
end
2022-12-04 18:59:10 +00:00
e.window.clear()
2023-08-31 01:11:57 +00:00
e.w_set_cur(h_pad, v_pad)
e.w_write(args.text)
end
-- handle mouse interaction
---@param event mouse_interaction mouse event
function e.handle_mouse(event)
if e.enabled and core.events.was_clicked(event.type) then
e.value = not e.value
e.redraw()
args.callback(e.value)
end
end
-- set the value
---@param val boolean new value
function e.set_value(val)
e.value = val
e.redraw()
2022-06-08 18:48:17 +00:00
end
-- initial draw
e.redraw()
return e.complete()
2022-06-08 18:48:17 +00:00
end
return switch_button