2022-06-08 18:48:17 +00:00
|
|
|
-- Button Graphics Element
|
|
|
|
|
|
|
|
local element = require("graphics.element")
|
|
|
|
|
2022-06-08 20:21:49 +00:00
|
|
|
---@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
|
2022-06-08 20:21:49 +00:00
|
|
|
---@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
|
2022-06-08 20:21:49 +00:00
|
|
|
---@field active_fg_bg cpair foreground/background colors when pressed
|
2022-06-08 18:48:17 +00:00
|
|
|
---@field parent graphics_element
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field id? string element id
|
2022-06-08 18:48:17 +00:00
|
|
|
---@field x? integer 1 if omitted
|
|
|
|
---@field y? integer 1 if omitted
|
|
|
|
---@field height? integer parent height if omitted
|
2022-06-11 21:06:32 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2022-06-08 18:48:17 +00:00
|
|
|
|
2022-06-08 20:21:49 +00:00
|
|
|
-- new switch button (latch high/low)
|
|
|
|
---@param args switch_button_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-06-08 20:21:49 +00:00
|
|
|
local function switch_button(args)
|
2022-06-16 15:19:32 +00:00
|
|
|
assert(type(args.text) == "string", "graphics.elements.controls.switch_button: text is a required field")
|
|
|
|
assert(type(args.callback) == "function", "graphics.elements.controls.switch_button: callback is a required field")
|
|
|
|
assert(type(args.active_fg_bg) == "table", "graphics.elements.controls.switch_button: active_fg_bg is a required field")
|
|
|
|
|
|
|
|
-- single line
|
|
|
|
args.height = 1
|
2022-06-11 21:06:32 +00:00
|
|
|
|
2022-06-08 20:21:49 +00:00
|
|
|
-- determine widths
|
2022-06-08 18:48:17 +00:00
|
|
|
local text_width = string.len(args.text)
|
|
|
|
args.width = math.max(text_width + 2, args.min_width)
|
|
|
|
|
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
-- button state (convert nil to false if missing)
|
|
|
|
e.value = args.default or false
|
|
|
|
|
2022-06-08 18:48:17 +00:00
|
|
|
local h_pad = math.floor((e.frame.w - text_width) / 2)
|
|
|
|
local v_pad = math.floor(e.frame.h / 2) + 1
|
|
|
|
|
2022-06-08 20:21:49 +00:00
|
|
|
-- show the button state
|
|
|
|
local function draw_state()
|
2022-09-12 16:59:28 +00:00
|
|
|
if e.value then
|
2022-06-08 18:48:17 +00:00
|
|
|
-- show as pressed
|
|
|
|
e.window.setTextColor(args.active_fg_bg.fgd)
|
|
|
|
e.window.setBackgroundColor(args.active_fg_bg.bkg)
|
2022-06-08 20:21:49 +00:00
|
|
|
else
|
|
|
|
-- show as unpressed
|
|
|
|
e.window.setTextColor(e.fg_bg.fgd)
|
|
|
|
e.window.setBackgroundColor(e.fg_bg.bkg)
|
2022-06-08 18:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-08-16 15:22:58 +00:00
|
|
|
-- write the button text
|
|
|
|
e.window.setCursorPos(h_pad, v_pad)
|
|
|
|
e.window.write(args.text)
|
2022-06-08 20:21:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- initial draw
|
|
|
|
draw_state()
|
|
|
|
|
|
|
|
-- handle touch
|
2022-06-11 20:38:15 +00:00
|
|
|
---@param event monitor_touch monitor touch event
|
|
|
|
---@diagnostic disable-next-line: unused-local
|
2022-06-08 20:21:49 +00:00
|
|
|
function e.handle_touch(event)
|
2022-10-20 16:22:45 +00:00
|
|
|
if e.enabled then
|
|
|
|
-- toggle state
|
|
|
|
e.value = not e.value
|
|
|
|
draw_state()
|
2022-09-12 16:59:28 +00:00
|
|
|
|
2022-10-20 16:22:45 +00:00
|
|
|
-- call the touch callback with state
|
|
|
|
args.callback(e.value)
|
|
|
|
end
|
2022-09-12 16:59:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- set the value
|
|
|
|
---@param val boolean new value
|
|
|
|
function e.set_value(val)
|
|
|
|
-- set state
|
|
|
|
e.value = val
|
2022-06-08 20:21:49 +00:00
|
|
|
draw_state()
|
|
|
|
|
|
|
|
-- call the touch callback with state
|
2022-09-12 16:59:28 +00:00
|
|
|
args.callback(e.value)
|
2022-06-08 18:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
return e.get()
|
2022-06-08 18:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-06-08 20:21:49 +00:00
|
|
|
return switch_button
|