mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#226 hazard and push buttons updated for new graphics mouse events
This commit is contained in:
parent
b8a8da1ac4
commit
40fa0de7a3
@ -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,8 +143,10 @@ 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
|
||||||
|
if event.type == CLICK_TYPE.TAP or event.type == CLICK_TYPE.UP then
|
||||||
-- change text color to indicate clicked
|
-- change text color to indicate clicked
|
||||||
e.window.setTextColor(args.accent)
|
e.window.setTextColor(args.accent)
|
||||||
e.window.setCursorPos(3, 2)
|
e.window.setCursorPos(3, 2)
|
||||||
@ -160,23 +164,19 @@ local function hazard_button(args)
|
|||||||
args.callback()
|
args.callback()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- callback on request response
|
-- callback on request response
|
||||||
---@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
|
||||||
|
@ -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
|
||||||
|
|
||||||
-- handle mouse interaction
|
-- draw the button as pressed (if active_fg_bg set)
|
||||||
function e.handle_mouse(_)
|
local function show_pressed()
|
||||||
if e.enabled then
|
if e.enabled and args.active_fg_bg ~= nil then
|
||||||
if args.active_fg_bg ~= nil then
|
|
||||||
-- show as pressed
|
|
||||||
e.value = true
|
e.value = true
|
||||||
e.window.setTextColor(args.active_fg_bg.fgd)
|
e.window.setTextColor(args.active_fg_bg.fgd)
|
||||||
e.window.setBackgroundColor(args.active_fg_bg.bkg)
|
e.window.setBackgroundColor(args.active_fg_bg.bkg)
|
||||||
draw()
|
draw()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- show as unpressed in 0.25 seconds
|
-- draw the button as unpressed (if active_fg_bg set)
|
||||||
tcd.dispatch(0.25, function ()
|
local function show_unpressed()
|
||||||
|
if e.enabled and args.active_fg_bg ~= nil then
|
||||||
e.value = false
|
e.value = false
|
||||||
if e.enabled then
|
|
||||||
e.window.setTextColor(e.fg_bg.fgd)
|
e.window.setTextColor(e.fg_bg.fgd)
|
||||||
e.window.setBackgroundColor(e.fg_bg.bkg)
|
e.window.setBackgroundColor(e.fg_bg.bkg)
|
||||||
end
|
|
||||||
draw()
|
draw()
|
||||||
end)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- call the touch callback
|
-- handle mouse interaction
|
||||||
|
---@param event mouse_interaction
|
||||||
|
function e.handle_mouse(event)
|
||||||
|
if e.enabled then
|
||||||
|
if event.type == CLICK_TYPE.TAP then
|
||||||
|
show_pressed()
|
||||||
|
-- show as unpressed in 0.25 seconds
|
||||||
|
if args.active_fg_bg ~= nil then tcd.dispatch(0.25, show_unpressed) end
|
||||||
args.callback()
|
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
|
||||||
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user