graphics element enable/disable, click indication on hazard buttons

This commit is contained in:
Mikayla Fischler 2022-10-20 12:22:45 -04:00
parent ab757e14a7
commit 6d5af98310
6 changed files with 88 additions and 47 deletions

View File

@ -60,6 +60,7 @@ function element.new(args)
---@class graphics_template
local protected = {
enabled = true,
value = nil, ---@type any
window = nil, ---@type table
fg_bg = core.graphics.cpair(colors.white, colors.black),
@ -157,6 +158,7 @@ function element.new(args)
end
-- handle data value changes
---@vararg any value(s)
function protected.on_update(...)
end
@ -168,7 +170,14 @@ function element.new(args)
-- set value
---@param value any value to set
function protected.set_value(value)
return nil
end
-- enable the control
function protected.enable()
end
-- disable the control
function protected.disable()
end
-- custom recolor command, varies by element if implemented
@ -301,6 +310,18 @@ function element.new(args)
protected.set_value(value)
end
-- enable the element
function public.enable()
protected.enabled = true
protected.enable()
end
-- disable the element
function public.disable()
protected.enabled = false
protected.disable()
end
-- resize attributes of the element value if supported
---@vararg number dimensions (element specific)
function public.resize(...)

View File

@ -35,37 +35,53 @@ local function hazard_button(args)
e.window.write(args.text)
-- draw border
---@param accent color accent color
local function draw_border(accent)
-- top
e.window.setTextColor(args.accent)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 1)
e.window.write("\x99\x89\x89\x89\x89\x89\x89\x89\x99")
-- top
e.window.setTextColor(args.accent)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 1)
e.window.write("\x99\x89\x89\x89\x89\x89\x89\x89\x99")
-- center left
e.window.setCursorPos(1, 2)
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(args.accent)
e.window.write("\x99")
-- center left
e.window.setCursorPos(1, 2)
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(args.accent)
e.window.write("\x99")
-- center right
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(args.accent)
e.window.setCursorPos(9, 2)
e.window.write("\x99")
-- center right
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(args.accent)
e.window.setCursorPos(9, 2)
e.window.write("\x99")
-- bottom
e.window.setTextColor(args.accent)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 3)
e.window.write("\x99\x98\x98\x98\x98\x98\x98\x98\x99")
-- bottom
e.window.setTextColor(args.accent)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 3)
e.window.write("\x99\x98\x98\x98\x98\x98\x98\x98\x99")
end
-- handle touch
---@param event monitor_touch monitor touch event
---@diagnostic disable-next-line: unused-local
function e.handle_touch(event)
-- call the touch callback
args.callback()
if e.enabled then
-- call the touch callback
args.callback()
-- change text color to indicate clicked
e.window.setTextColor(args.accent)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
-- restore text color after 1 second
tcd.dispatch(1, function ()
e.window.setTextColor(args.fg_bg.fgd)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
end)
end
end
-- set the value

View File

@ -91,7 +91,7 @@ local function multi_button(args)
---@param event monitor_touch monitor touch event
function e.handle_touch(event)
-- determine what was pressed
if event.y == 2 then
if e.enabled and event.y == 2 then
for i = 1, #args.options do
local opt = args.options[i] ---@type button_option

View File

@ -51,24 +51,26 @@ local function push_button(args)
---@param event monitor_touch monitor touch event
---@diagnostic disable-next-line: unused-local
function e.handle_touch(event)
if args.active_fg_bg ~= nil then
-- show as pressed
e.value = true
e.window.setTextColor(args.active_fg_bg.fgd)
e.window.setBackgroundColor(args.active_fg_bg.bkg)
draw()
-- show as unpressed in 0.25 seconds
tcd.dispatch(0.25, function ()
e.value = false
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
if e.enabled then
if args.active_fg_bg ~= nil then
-- show as pressed
e.value = true
e.window.setTextColor(args.active_fg_bg.fgd)
e.window.setBackgroundColor(args.active_fg_bg.bkg)
draw()
end)
end
-- call the touch callback
args.callback()
-- show as unpressed in 0.25 seconds
tcd.dispatch(0.25, function ()
e.value = false
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
draw()
end)
end
-- call the touch callback
args.callback()
end
end
-- set the value

View File

@ -108,7 +108,7 @@ local function spinbox(args)
---@param event monitor_touch monitor touch event
function e.handle_touch(event)
-- only handle if on an increment or decrement arrow
if event.x ~= dec_point_x then
if e.enabled and event.x ~= dec_point_x then
local idx = util.trinary(event.x > dec_point_x, event.x - 1, event.x)
if event.y == 1 then
-- increment

View File

@ -63,12 +63,14 @@ local function switch_button(args)
---@param event monitor_touch monitor touch event
---@diagnostic disable-next-line: unused-local
function e.handle_touch(event)
-- toggle state
e.value = not e.value
draw_state()
if e.enabled then
-- toggle state
e.value = not e.value
draw_state()
-- call the touch callback with state
args.callback(e.value)
-- call the touch callback with state
args.callback(e.value)
end
end
-- set the value