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

121 lines
3.3 KiB
Lua
Raw Normal View History

2023-07-01 23:40:33 +00:00
-- Checkbox Graphics Element
local core = require("graphics.core")
local element = require("graphics.element")
---@class checkbox_args
---@field label string checkbox text
---@field box_fg_bg cpair colors for checkbox
2023-09-25 02:27:39 +00:00
---@field callback? function function to call on press
2023-07-01 23:40:33 +00:00
---@field parent graphics_element
---@field id? string element id
---@field x? integer 1 if omitted
2023-07-10 03:42:44 +00:00
---@field y? integer auto incremented if omitted
2023-07-01 23:40:33 +00:00
---@field fg_bg? cpair foreground/background colors
---@field hidden? boolean true to hide on initial draw
-- new checkbox control
---@param args checkbox_args
---@return graphics_element element, element_id id
local function checkbox(args)
2023-09-30 15:46:47 +00:00
element.assert(type(args.label) == "string", "label is a required field")
element.assert(type(args.box_fg_bg) == "table", "box_fg_bg is a required field")
2023-07-01 23:40:33 +00:00
args.can_focus = true
2023-07-01 23:40:33 +00:00
args.height = 1
args.width = 3 + string.len(args.label)
-- create new graphics element base object
local e = element.new(args)
e.value = false
-- show the button state
local function draw()
2023-08-31 01:11:57 +00:00
e.w_set_cur(1, 1)
2023-07-01 23:40:33 +00:00
if e.value then
-- show as selected
2023-08-31 01:11:57 +00:00
e.w_set_fgd(args.box_fg_bg.bkg)
e.w_set_bkg(args.box_fg_bg.fgd)
e.w_write("\x88")
e.w_set_fgd(args.box_fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write("\x95")
2023-07-01 23:40:33 +00:00
else
-- show as unselected
2023-08-31 01:11:57 +00:00
e.w_set_fgd(e.fg_bg.bkg)
e.w_set_bkg(args.box_fg_bg.bkg)
e.w_write("\x88")
e.w_set_fgd(args.box_fg_bg.bkg)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write("\x95")
2023-07-01 23:40:33 +00:00
end
end
-- write label text
local function draw_label()
if e.enabled and e.is_focused() then
e.w_set_cur(3, 1)
e.w_set_fgd(e.fg_bg.bkg)
e.w_set_bkg(e.fg_bg.fgd)
e.w_write(args.label)
else
e.w_set_cur(3, 1)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write(args.label)
end
end
2023-07-01 23:40:33 +00:00
-- handle mouse interaction
---@param event mouse_interaction mouse event
function e.handle_mouse(event)
2023-09-25 02:27:39 +00:00
if e.enabled and core.events.was_clicked(event.type) and e.in_frame_bounds(event.current.x, event.current.y) then
2023-07-01 23:40:33 +00:00
e.value = not e.value
draw()
2023-09-25 02:27:39 +00:00
if type(args.callback) == "function" then args.callback(e.value) end
2023-07-01 23:40:33 +00:00
end
end
-- handle keyboard interaction
---@param event key_interaction key event
function e.handle_key(event)
if event.type == core.events.KEY_CLICK.DOWN then
if event.key == keys.space or event.key == keys.enter or event.key == keys.numPadEnter then
e.value = not e.value
draw()
2023-09-25 02:27:39 +00:00
if type(args.callback) == "function" then args.callback(e.value) end
end
end
end
2023-07-01 23:40:33 +00:00
-- set the value
---@param val integer new value
function e.set_value(val)
e.value = val
draw()
end
-- handle focus
e.on_focused = draw_label
e.on_unfocused = draw_label
-- handle enable
e.on_enabled = draw_label
e.on_disabled = draw_label
2023-07-01 23:40:33 +00:00
-- element redraw
function e.redraw()
draw()
draw_label()
end
2023-07-01 23:40:33 +00:00
-- initial draw
e.redraw()
2023-07-01 23:40:33 +00:00
return e.complete()
end
return checkbox