2022-06-08 18:48:17 +00:00
|
|
|
-- Button Graphics Element
|
|
|
|
|
|
|
|
local tcd = require("scada-common.tcallbackdsp")
|
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
local core = require("graphics.core")
|
2022-06-08 18:48:17 +00:00
|
|
|
local element = require("graphics.element")
|
|
|
|
|
2023-05-10 14:56:56 +00:00
|
|
|
local CLICK_TYPE = core.events.CLICK_TYPE
|
|
|
|
|
2022-06-08 20:21:49 +00:00
|
|
|
---@class push_button_args
|
2022-06-08 18:48:17 +00:00
|
|
|
---@field text string button text
|
|
|
|
---@field callback function function to call on touch
|
2023-04-09 01:33:54 +00:00
|
|
|
---@field min_width? integer text length if omitted
|
2022-06-08 18:48:17 +00:00
|
|
|
---@field active_fg_bg? cpair foreground/background colors when pressed
|
2023-02-02 02:55:02 +00:00
|
|
|
---@field dis_fg_bg? cpair foreground/background colors when disabled
|
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 push button
|
|
|
|
---@param args push_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 push_button(args)
|
2022-06-16 15:19:32 +00:00
|
|
|
assert(type(args.text) == "string", "graphics.elements.controls.push_button: text is a required field")
|
|
|
|
assert(type(args.callback) == "function", "graphics.elements.controls.push_button: callback is a required field")
|
2023-05-11 23:55:02 +00:00
|
|
|
assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0),
|
|
|
|
"graphics.elements.controls.push_button: min_width must be nil or a number > 0")
|
2022-06-16 15:19:32 +00:00
|
|
|
|
2022-11-24 19:20:11 +00:00
|
|
|
local text_width = string.len(args.text)
|
2022-06-11 21:06:32 +00:00
|
|
|
|
2022-11-24 19:20:11 +00:00
|
|
|
-- single line height, calculate width
|
|
|
|
args.height = 1
|
2022-07-16 17:25:07 +00:00
|
|
|
args.min_width = args.min_width or 0
|
2022-11-24 19:20:11 +00:00
|
|
|
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)
|
|
|
|
|
2022-07-16 17:25:07 +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
|
|
|
|
|
2022-07-28 15:42:22 +00:00
|
|
|
-- draw the button
|
|
|
|
local function draw()
|
|
|
|
e.window.clear()
|
|
|
|
|
|
|
|
-- write the button text
|
|
|
|
e.window.setCursorPos(h_pad, v_pad)
|
|
|
|
e.window.write(args.text)
|
|
|
|
end
|
2022-06-08 18:48:17 +00:00
|
|
|
|
2023-05-10 14:56:56 +00:00
|
|
|
-- draw the button as pressed (if active_fg_bg set)
|
|
|
|
local function show_pressed()
|
|
|
|
if e.enabled and args.active_fg_bg ~= nil then
|
|
|
|
e.value = true
|
|
|
|
e.window.setTextColor(args.active_fg_bg.fgd)
|
|
|
|
e.window.setBackgroundColor(args.active_fg_bg.bkg)
|
|
|
|
draw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- draw the button as unpressed (if active_fg_bg set)
|
|
|
|
local function show_unpressed()
|
|
|
|
if e.enabled and args.active_fg_bg ~= nil then
|
|
|
|
e.value = false
|
|
|
|
e.window.setTextColor(e.fg_bg.fgd)
|
|
|
|
e.window.setBackgroundColor(e.fg_bg.bkg)
|
|
|
|
draw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-09 01:33:54 +00:00
|
|
|
-- handle mouse interaction
|
2023-05-10 15:46:06 +00:00
|
|
|
---@param event mouse_interaction mouse event
|
2023-05-10 14:56:56 +00:00
|
|
|
function e.handle_mouse(event)
|
2022-10-20 16:22:45 +00:00
|
|
|
if e.enabled then
|
2023-05-10 14:56:56 +00:00
|
|
|
if event.type == CLICK_TYPE.TAP then
|
|
|
|
show_pressed()
|
2022-10-20 16:22:45 +00:00
|
|
|
-- show as unpressed in 0.25 seconds
|
2023-05-10 14:56:56 +00:00
|
|
|
if args.active_fg_bg ~= nil then tcd.dispatch(0.25, show_unpressed) end
|
|
|
|
args.callback()
|
|
|
|
elseif event.type == CLICK_TYPE.DOWN then
|
|
|
|
show_pressed()
|
|
|
|
elseif event.type == CLICK_TYPE.UP then
|
|
|
|
show_unpressed()
|
|
|
|
args.callback()
|
|
|
|
elseif event.type == CLICK_TYPE.EXITED then
|
|
|
|
show_unpressed()
|
2022-10-20 16:22:45 +00:00
|
|
|
end
|
|
|
|
end
|
2022-06-08 18:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-12-05 21:17:09 +00:00
|
|
|
-- set the value (true simulates pressing the button)
|
2022-09-12 16:59:28 +00:00
|
|
|
---@param val boolean new value
|
|
|
|
function e.set_value(val)
|
2023-05-10 14:56:56 +00:00
|
|
|
if val then e.handle_mouse(core.events.mouse_generic(core.events.CLICK_TYPE.UP, 1, 1)) end
|
2022-09-12 16:59:28 +00:00
|
|
|
end
|
|
|
|
|
2023-02-02 02:55:02 +00:00
|
|
|
-- show butten as enabled
|
|
|
|
function e.enable()
|
|
|
|
if args.dis_fg_bg ~= nil then
|
|
|
|
e.value = false
|
|
|
|
e.window.setTextColor(e.fg_bg.fgd)
|
|
|
|
e.window.setBackgroundColor(e.fg_bg.bkg)
|
|
|
|
draw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- show button as disabled
|
|
|
|
function e.disable()
|
|
|
|
if args.dis_fg_bg ~= nil then
|
|
|
|
e.value = false
|
|
|
|
e.window.setTextColor(args.dis_fg_bg.fgd)
|
|
|
|
e.window.setBackgroundColor(args.dis_fg_bg.bkg)
|
|
|
|
draw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 15:42:22 +00:00
|
|
|
-- initial draw
|
|
|
|
draw()
|
|
|
|
|
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 push_button
|