graphics library refactoring and bugfixes

This commit is contained in:
Mikayla Fischler
2022-06-16 11:19:32 -04:00
parent b628472d81
commit 971657c3d2
10 changed files with 85 additions and 49 deletions

View File

@@ -0,0 +1,65 @@
-- Button Graphics Element
local tcd = require("scada-common.tcallbackdsp")
local element = require("graphics.element")
---@class push_button_args
---@field text string button text
---@field callback function function to call on touch
---@field min_width? integer text length + 2 if omitted
---@field active_fg_bg? cpair foreground/background colors when pressed
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field height? integer parent height if omitted
---@field fg_bg? cpair foreground/background colors
-- new push button
---@param args push_button_args
local function push_button(args)
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")
-- single line
args.height = 1
local text_width = string.len(args.text)
args.width = math.max(text_width + 2, args.min_width)
-- create new graphics element base object
local e = element.new(args)
local h_pad = math.floor((e.frame.w - text_width) / 2)
local v_pad = math.floor(e.frame.h / 2) + 1
-- write the button text
e.window.setCursorPos(h_pad, v_pad)
e.window.write(args.text)
-- handle touch
---@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.window.setTextColor(args.active_fg_bg.fgd)
e.window.setBackgroundColor(args.active_fg_bg.bkg)
e.window.redraw()
-- show as unpressed in 0.5 seconds
tcd.dispatch(0.5, function ()
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.redraw()
end)
end
-- call the touch callback
args.callback()
end
return e.get()
end
return push_button

View File

@@ -0,0 +1,91 @@
-- Spinbox Numeric Graphics Element
local element = require("graphics.element")
local util = require("scada-common.util")
---@class spinbox_args
---@field default? number default value, defaults to 0.0
---@field whole_num_precision integer number of whole number digits
---@field fractional_precision integer number of fractional digits
---@field arrow_fg_bg cpair arrow foreground/background colors
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field fg_bg? cpair foreground/background colors
-- new spinbox control
---@param args spinbox_args
local function spinbox(args)
-- properties
local value = args.default or 0.0
local digits = {}
local wn_prec = args.whole_num_precision
local fr_prec = args.fractional_precision
local dec_point_x = args.whole_num_precision + 1
assert(util.is_int(wn_prec), "graphics.element.controls.spinbox_numeric: whole number precision must be an integer")
assert(util.is_int(fr_prec), "graphics.element.controls.spinbox_numeric: fractional precision must be an integer")
assert(type(args.arrow_fg_bg) == "table", "graphics.element.spinbox_numeric: arrow_fg_bg is a required field")
local initial_str = util.sprintf("%" .. wn_prec .. "." .. fr_prec .. "f", value)
---@diagnostic disable-next-line: discard-returns
initial_str:gsub("%d", function(char) table.insert(digits, char) end)
-- determine widths
args.width = wn_prec + fr_prec + util.trinary(fr_prec > 0, 1, 0)
args.height = 3
-- create new graphics element base object
local e = element.new(args)
-- draw the arrows
e.window.setBackgroundColor(args.arrow_fg_bg.bkg)
e.window.setTextColor(args.arrow_fg_bg.fgd)
e.window.setCursorPos(1, 1)
e.window.write(util.strrep("\x1e", wn_prec))
e.window.setCursorPos(1, 3)
e.window.write(util.strrep("\x1f", wn_prec))
if fr_prec > 0 then
e.window.setCursorPos(1, 1)
e.window.write(" " .. util.strrep("\x1e", fr_prec))
e.window.setCursorPos(1, 3)
e.window.write(" " .. util.strrep("\x1f", fr_prec))
end
-- handle touch
---@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
local idx = util.trinary(event.x > dec_point_x, event.x - 1, event.x)
if event.y == 1 then
-- increment
digits[idx] = digits[idx] + 1
elseif event.y == 3 then
-- decrement
digits[idx] = digits[idx] - 1
end
-- update value
value = 0
for i = 1, #digits do
local pow = math.abs(wn_prec - i)
if i <= wn_prec then
value = value + (digits[i] * (10 ^ pow))
else
value = value + (digits[i] * (10 ^ -pow))
end
end
end
end
-- get current value
---@return number|integer
function e.get_value() return value end
return e.get()
end
return spinbox

View File

@@ -0,0 +1,77 @@
-- Button Graphics Element
local element = require("graphics.element")
---@class switch_button_args
---@field text string button text
---@field callback function function to call on touch
---@field default? boolean default state, defaults to off (false)
---@field min_width? integer text length + 2 if omitted
---@field active_fg_bg cpair foreground/background colors when pressed
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field height? integer parent height if omitted
---@field fg_bg? cpair foreground/background colors
-- new switch button (latch high/low)
---@param args switch_button_args
local function switch_button(args)
assert(type(args.text) == "string", "graphics.elements.controls.switch_button: text is a required field")
assert(type(args.callback) == "function", "graphics.elements.controls.switch_button: callback is a required field")
assert(type(args.active_fg_bg) == "table", "graphics.elements.controls.switch_button: active_fg_bg is a required field")
-- single line
args.height = 1
-- button state (convert nil to false if missing)
local state = args.default or false
-- determine widths
local text_width = string.len(args.text)
args.width = math.max(text_width + 2, args.min_width)
-- create new graphics element base object
local e = element.new(args)
local h_pad = math.floor((e.frame.w - text_width) / 2)
local v_pad = math.floor(e.frame.h / 2) + 1
-- write the button text
e.window.setCursorPos(h_pad, v_pad)
e.window.write(args.text)
-- show the button state
local function draw_state()
if state then
-- show as pressed
e.window.setTextColor(args.active_fg_bg.fgd)
e.window.setBackgroundColor(args.active_fg_bg.bkg)
else
-- show as unpressed
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
end
e.window.redraw()
end
-- initial draw
draw_state()
-- handle touch
---@param event monitor_touch monitor touch event
---@diagnostic disable-next-line: unused-local
function e.handle_touch(event)
-- toggle state
state = not state
draw_state()
-- call the touch callback with state
args.callback(state)
end
return e.get()
end
return switch_button