#226 hazard and push buttons updated for new graphics mouse events

This commit is contained in:
Mikayla Fischler 2023-05-10 10:56:56 -04:00
parent b8a8da1ac4
commit 40fa0de7a3
2 changed files with 56 additions and 40 deletions

View File

@ -6,6 +6,8 @@ local util = require("scada-common.util")
local core = require("graphics.core") local core = require("graphics.core")
local element = require("graphics.element") local element = require("graphics.element")
local CLICK_TYPE = core.events.CLICK_TYPE
---@class hazard_button_args ---@class hazard_button_args
---@field text string text to show on button ---@field text string text to show on button
---@field accent color accent color for hazard border ---@field accent color accent color for hazard border
@ -141,23 +143,26 @@ local function hazard_button(args)
end end
-- handle mouse interaction -- handle mouse interaction
function e.handle_mouse(_) ---@param event mouse_interaction
function e.handle_mouse(event)
if e.enabled then if e.enabled then
-- change text color to indicate clicked if event.type == CLICK_TYPE.TAP or event.type == CLICK_TYPE.UP then
e.window.setTextColor(args.accent) -- change text color to indicate clicked
e.window.setCursorPos(3, 2) e.window.setTextColor(args.accent)
e.window.write(args.text) e.window.setCursorPos(3, 2)
e.window.write(args.text)
-- abort any other callbacks -- abort any other callbacks
tcd.abort(on_timeout) tcd.abort(on_timeout)
tcd.abort(on_success) tcd.abort(on_success)
tcd.abort(on_failure) tcd.abort(on_failure)
-- 1.5 second timeout -- 1.5 second timeout
tcd.dispatch(1.5, on_timeout) tcd.dispatch(1.5, on_timeout)
-- call the touch callback -- call the touch callback
args.callback() args.callback()
end
end end
end end
@ -165,18 +170,13 @@ local function hazard_button(args)
---@param result boolean true for success, false for failure ---@param result boolean true for success, false for failure
function e.response_callback(result) function e.response_callback(result)
tcd.abort(on_timeout) tcd.abort(on_timeout)
if result then on_success() else on_failure(0) end
if result then
on_success()
else
on_failure(0)
end
end end
-- set the value (true simulates pressing the button) -- set the value (true simulates pressing the button)
---@param val boolean new value ---@param val boolean new value
function e.set_value(val) function e.set_value(val)
if val then e.handle_mouse(core.events.mouse_generic("", core.events.click_button.VIRTUAL, core.events.click_type.UP, 1, 1)) end if val then e.handle_mouse(core.events.mouse_generic(core.events.CLICK_TYPE.UP, 1, 1)) end
end end
-- show the button as disabled -- show the button as disabled

View File

@ -5,6 +5,8 @@ local tcd = require("scada-common.tcallbackdsp")
local core = require("graphics.core") local core = require("graphics.core")
local element = require("graphics.element") local element = require("graphics.element")
local CLICK_TYPE = core.events.CLICK_TYPE
---@class push_button_args ---@class push_button_args
---@field text string button text ---@field text string button text
---@field callback function function to call on touch ---@field callback function function to call on touch
@ -47,36 +49,50 @@ local function push_button(args)
e.window.write(args.text) e.window.write(args.text)
end end
-- 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
-- handle mouse interaction -- handle mouse interaction
function e.handle_mouse(_) ---@param event mouse_interaction
function e.handle_mouse(event)
if e.enabled then if e.enabled then
if args.active_fg_bg ~= nil then if event.type == CLICK_TYPE.TAP then
-- show as pressed show_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 -- show as unpressed in 0.25 seconds
tcd.dispatch(0.25, function () if args.active_fg_bg ~= nil then tcd.dispatch(0.25, show_unpressed) end
e.value = false args.callback()
if e.enabled then elseif event.type == CLICK_TYPE.DOWN then
e.window.setTextColor(e.fg_bg.fgd) show_pressed()
e.window.setBackgroundColor(e.fg_bg.bkg) elseif event.type == CLICK_TYPE.UP then
end show_unpressed()
draw() args.callback()
end) elseif event.type == CLICK_TYPE.EXITED then
show_unpressed()
end end
-- call the touch callback
args.callback()
end end
end end
-- set the value (true simulates pressing the button) -- set the value (true simulates pressing the button)
---@param val boolean new value ---@param val boolean new value
function e.set_value(val) function e.set_value(val)
if val then e.handle_mouse(core.events.mouse_generic("", core.events.click_button.VIRTUAL, core.events.click_type.UP, 1, 1)) end if val then e.handle_mouse(core.events.mouse_generic(core.events.CLICK_TYPE.UP, 1, 1)) end
end end
-- show butten as enabled -- show butten as enabled