From 40fa0de7a3ad2474c46ebd1d8fdb23145974d982 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 10 May 2023 10:56:56 -0400 Subject: [PATCH] #226 hazard and push buttons updated for new graphics mouse events --- graphics/elements/controls/hazard_button.lua | 40 +++++++------- graphics/elements/controls/push_button.lua | 56 +++++++++++++------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/graphics/elements/controls/hazard_button.lua b/graphics/elements/controls/hazard_button.lua index d8e2d6d..19047cb 100644 --- a/graphics/elements/controls/hazard_button.lua +++ b/graphics/elements/controls/hazard_button.lua @@ -6,6 +6,8 @@ local util = require("scada-common.util") local core = require("graphics.core") local element = require("graphics.element") +local CLICK_TYPE = core.events.CLICK_TYPE + ---@class hazard_button_args ---@field text string text to show on button ---@field accent color accent color for hazard border @@ -141,23 +143,26 @@ local function hazard_button(args) end -- handle mouse interaction - function e.handle_mouse(_) + ---@param event mouse_interaction + function e.handle_mouse(event) if e.enabled then - -- change text color to indicate clicked - e.window.setTextColor(args.accent) - e.window.setCursorPos(3, 2) - e.window.write(args.text) + if event.type == CLICK_TYPE.TAP or event.type == CLICK_TYPE.UP then + -- change text color to indicate clicked + e.window.setTextColor(args.accent) + e.window.setCursorPos(3, 2) + e.window.write(args.text) - -- abort any other callbacks - tcd.abort(on_timeout) - tcd.abort(on_success) - tcd.abort(on_failure) + -- abort any other callbacks + tcd.abort(on_timeout) + tcd.abort(on_success) + tcd.abort(on_failure) - -- 1.5 second timeout - tcd.dispatch(1.5, on_timeout) + -- 1.5 second timeout + tcd.dispatch(1.5, on_timeout) - -- call the touch callback - args.callback() + -- call the touch callback + args.callback() + end end end @@ -165,18 +170,13 @@ local function hazard_button(args) ---@param result boolean true for success, false for failure function e.response_callback(result) 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 -- set the value (true simulates pressing the button) ---@param val boolean new value 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 -- show the button as disabled diff --git a/graphics/elements/controls/push_button.lua b/graphics/elements/controls/push_button.lua index 79cde7f..a55a4e4 100644 --- a/graphics/elements/controls/push_button.lua +++ b/graphics/elements/controls/push_button.lua @@ -5,6 +5,8 @@ local tcd = require("scada-common.tcallbackdsp") local core = require("graphics.core") local element = require("graphics.element") +local CLICK_TYPE = core.events.CLICK_TYPE + ---@class push_button_args ---@field text string button text ---@field callback function function to call on touch @@ -47,36 +49,50 @@ local function push_button(args) e.window.write(args.text) 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 - function e.handle_mouse(_) + ---@param event mouse_interaction + function e.handle_mouse(event) 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() - + if event.type == CLICK_TYPE.TAP then + show_pressed() -- show as unpressed in 0.25 seconds - tcd.dispatch(0.25, function () - e.value = false - if e.enabled then - e.window.setTextColor(e.fg_bg.fgd) - e.window.setBackgroundColor(e.fg_bg.bkg) - end - draw() - end) + 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() end - - -- call the touch callback - args.callback() end end -- set the value (true simulates pressing the button) ---@param val boolean new value 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 -- show butten as enabled