2022-12-13 20:18:29 +00:00
|
|
|
-- Radio Button Graphics Element
|
|
|
|
|
2023-09-17 20:37:11 +00:00
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2023-05-10 15:46:06 +00:00
|
|
|
local core = require("graphics.core")
|
2022-12-13 20:18:29 +00:00
|
|
|
local element = require("graphics.element")
|
|
|
|
|
2023-09-25 02:27:39 +00:00
|
|
|
local KEY_CLICK = core.events.KEY_CLICK
|
|
|
|
|
2022-12-13 20:18:29 +00:00
|
|
|
---@class radio_button_args
|
|
|
|
---@field options table button options
|
2023-09-17 20:37:11 +00:00
|
|
|
---@field radio_colors cpair radio button colors (inner & outer)
|
|
|
|
---@field select_color color color for radio button border when selected
|
2022-12-13 20:18:29 +00:00
|
|
|
---@field default? integer default state, defaults to options[1]
|
|
|
|
---@field min_width? integer text length + 2 if omitted
|
2023-09-25 02:27:39 +00:00
|
|
|
---@field callback? function function to call on touch
|
2022-12-13 20:18:29 +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
|
2022-12-13 20:18:29 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2023-05-25 21:40:16 +00:00
|
|
|
---@field hidden? boolean true to hide on initial draw
|
2022-12-13 20:18:29 +00:00
|
|
|
|
|
|
|
-- new radio button list (latch selection, exclusively one button at a time)
|
|
|
|
---@param args radio_button_args
|
|
|
|
---@return graphics_element element, element_id id
|
|
|
|
local function radio_button(args)
|
2023-09-30 15:46:47 +00:00
|
|
|
element.assert(type(args.options) == "table", "options is a required field")
|
|
|
|
element.assert(#args.options > 0, "at least one option is required")
|
|
|
|
element.assert(type(args.radio_colors) == "table", "radio_colors is a required field")
|
|
|
|
element.assert(type(args.select_color) == "number", "select_color is a required field")
|
|
|
|
element.assert(type(args.default) == "nil" or (type(args.default) == "number" and args.default > 0), "default must be nil or a number > 0")
|
|
|
|
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-12-13 20:18:29 +00:00
|
|
|
|
|
|
|
-- determine widths
|
|
|
|
local max_width = 1
|
|
|
|
for i = 1, #args.options do
|
|
|
|
local opt = args.options[i] ---@type string
|
|
|
|
if string.len(opt) > max_width then
|
|
|
|
max_width = string.len(opt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local button_text_width = math.max(max_width, args.min_width or 0)
|
|
|
|
|
2023-09-23 18:31:37 +00:00
|
|
|
-- set automatic args
|
|
|
|
args.can_focus = true
|
2022-12-13 20:18:29 +00:00
|
|
|
args.width = button_text_width + 2
|
2023-09-23 18:31:37 +00:00
|
|
|
args.height = #args.options -- one line per option
|
2022-12-13 20:18:29 +00:00
|
|
|
|
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
2023-09-23 18:31:37 +00:00
|
|
|
local focused_opt = 1
|
|
|
|
|
2022-12-13 20:18:29 +00:00
|
|
|
-- button state (convert nil to 1 if missing)
|
|
|
|
e.value = args.default or 1
|
|
|
|
|
|
|
|
-- show the button state
|
2023-09-29 23:34:10 +00:00
|
|
|
function e.redraw()
|
2022-12-13 20:18:29 +00:00
|
|
|
for i = 1, #args.options do
|
|
|
|
local opt = args.options[i] ---@type string
|
|
|
|
|
2023-09-17 20:37:11 +00:00
|
|
|
local inner_color = util.trinary(e.value == i, args.radio_colors.color_b, args.radio_colors.color_a)
|
|
|
|
local outer_color = util.trinary(e.value == i, args.select_color, args.radio_colors.color_b)
|
2022-12-13 20:18:29 +00:00
|
|
|
|
2023-09-17 20:37:11 +00:00
|
|
|
e.w_set_cur(1, i)
|
2022-12-13 20:18:29 +00:00
|
|
|
|
2023-09-17 20:37:11 +00:00
|
|
|
e.w_set_fgd(inner_color)
|
|
|
|
e.w_set_bkg(outer_color)
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_write("\x88")
|
2022-12-13 20:18:29 +00:00
|
|
|
|
2023-09-17 20:37:11 +00:00
|
|
|
e.w_set_fgd(outer_color)
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_bkg(e.fg_bg.bkg)
|
|
|
|
e.w_write("\x95")
|
2022-12-13 20:18:29 +00:00
|
|
|
|
|
|
|
-- write button text
|
2023-09-23 18:31:37 +00:00
|
|
|
if i == focused_opt and e.is_focused() and e.enabled then
|
|
|
|
e.w_set_fgd(e.fg_bg.bkg)
|
|
|
|
e.w_set_bkg(e.fg_bg.fgd)
|
|
|
|
else
|
|
|
|
e.w_set_fgd(e.fg_bg.fgd)
|
|
|
|
e.w_set_bkg(e.fg_bg.bkg)
|
|
|
|
end
|
|
|
|
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_write(opt)
|
2022-12-13 20:18:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-09 01:33:54 +00:00
|
|
|
-- handle mouse interaction
|
|
|
|
---@param event mouse_interaction mouse event
|
|
|
|
function e.handle_mouse(event)
|
2023-05-10 15:46:06 +00:00
|
|
|
if e.enabled and core.events.was_clicked(event.type) and (event.initial.y == event.current.y) then
|
|
|
|
-- determine what was pressed
|
|
|
|
if args.options[event.current.y] ~= nil then
|
|
|
|
e.value = event.current.y
|
2023-09-25 02:27:39 +00:00
|
|
|
focused_opt = e.value
|
2023-09-29 23:34:10 +00:00
|
|
|
e.redraw()
|
2023-09-25 02:27:39 +00:00
|
|
|
if type(args.callback) == "function" then args.callback(e.value) end
|
2022-12-13 20:18:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-23 18:31:37 +00:00
|
|
|
-- handle keyboard interaction
|
|
|
|
---@param event key_interaction key event
|
|
|
|
function e.handle_key(event)
|
2023-09-25 02:27:39 +00:00
|
|
|
if event.type == KEY_CLICK.DOWN or event.type == KEY_CLICK.HELD then
|
|
|
|
if event.type == KEY_CLICK.DOWN and (event.key == keys.space or event.key == keys.enter or event.key == keys.numPadEnter) then
|
2023-09-23 18:31:37 +00:00
|
|
|
e.value = focused_opt
|
2023-09-29 23:34:10 +00:00
|
|
|
e.redraw()
|
2023-09-25 02:27:39 +00:00
|
|
|
if type(args.callback) == "function" then args.callback(e.value) end
|
2023-09-23 18:31:37 +00:00
|
|
|
elseif event.key == keys.down then
|
|
|
|
if focused_opt < #args.options then
|
|
|
|
focused_opt = focused_opt + 1
|
2023-09-29 23:34:10 +00:00
|
|
|
e.redraw()
|
2023-09-23 18:31:37 +00:00
|
|
|
end
|
|
|
|
elseif event.key == keys.up then
|
|
|
|
if focused_opt > 1 then
|
|
|
|
focused_opt = focused_opt - 1
|
2023-09-29 23:34:10 +00:00
|
|
|
e.redraw()
|
2023-09-23 18:31:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-13 20:18:29 +00:00
|
|
|
-- set the value
|
|
|
|
---@param val integer new value
|
|
|
|
function e.set_value(val)
|
2023-09-25 02:27:39 +00:00
|
|
|
if val > 0 and val <= #args.options then
|
|
|
|
e.value = val
|
2023-09-29 23:34:10 +00:00
|
|
|
e.redraw()
|
2023-09-25 02:27:39 +00:00
|
|
|
end
|
2022-12-13 20:18:29 +00:00
|
|
|
end
|
|
|
|
|
2023-09-23 18:31:37 +00:00
|
|
|
-- handle focus
|
2023-09-29 23:34:10 +00:00
|
|
|
e.on_focused = e.redraw
|
|
|
|
e.on_unfocused = e.redraw
|
2023-09-23 18:31:37 +00:00
|
|
|
|
|
|
|
-- handle enable
|
2023-09-29 23:34:10 +00:00
|
|
|
e.on_enabled = e.redraw
|
|
|
|
e.on_disabled = e.redraw
|
2023-09-23 18:31:37 +00:00
|
|
|
|
2022-12-13 20:18:29 +00:00
|
|
|
-- initial draw
|
2023-09-29 23:34:10 +00:00
|
|
|
e.redraw()
|
2022-12-13 20:18:29 +00:00
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
return e.complete()
|
2022-12-13 20:18:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return radio_button
|