#63 cleanup and assertions

This commit is contained in:
Mikayla Fischler 2022-06-11 17:06:32 -04:00
parent 4488a0594f
commit 89437b2be9
12 changed files with 46 additions and 23 deletions

View File

@ -13,11 +13,14 @@ local element = require("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
---@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.button_push: text is a required field")
assert(type(args.callback) == "function", "graphics.elements.button_push: callback is a required field")
local text_width = string.len(args.text)
args.width = math.max(text_width + 2, args.min_width)

View File

@ -12,11 +12,15 @@ local element = require("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
---@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.button_switch: text is a required field")
assert(type(args.callback) == "function", "graphics.elements.button_switch: callback is a required field")
assert(type(args.active_fg_bg) == "table", "graphics.elements.button_switch: active_fg_bg is a required field")
-- button state (convert nil to false if missing)
local state = args.default or false

View File

@ -13,7 +13,7 @@ local element = require("graphics.element")
---@field width? integer parent width if omitted
---@field height? integer parent height if omitted
---@field gframe? graphics_frame frame instead of x/y/width/height
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new horizontal bar
---@param args hbar_args

View File

@ -9,16 +9,21 @@ local element = require("graphics.element")
---@field unit? string indicator unit
---@field format string data format (lua string format)
---@field label_unit_colors? cpair label foreground color (a), unit foreground color (b)
---@field default any default value
---@field initial_value any default value
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field width integer length
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new data indicator
---@param args data_indicator_args
local function data_indicator(args)
assert(type(args.label) == "string", "graphics.elements.indicator_data: label is a required field")
assert(type(args.format) == "string", "graphics.elements.indicator_data: format is a required field")
assert(args.initial_value ~= nil, "graphics.elements.indicator_data: initial_value is a required field")
assert(util.is_int(args.width), "graphics.elements.indicator_data: width is a required field")
-- create new graphics element base object
local e = element.new(args)
@ -28,7 +33,7 @@ local function data_indicator(args)
end
-- write label
e.setCursorPos(1, 1)
e.window.setCursorPos(1, 1)
e.window.write(args.label)
local data_start = string.len(args.label) + 2
@ -53,7 +58,7 @@ local function data_indicator(args)
end
-- initial value draw
e.on_update(args.default)
e.on_update(args.initial_value)
return e.get()
end

View File

@ -16,11 +16,14 @@ local element = require("graphics.element")
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new icon indicator
---@param args icon_indicator_args
local function icon_indicator(args)
assert(type(args.label) == "string", "graphics.elements.indicator_icon: label is a required field")
assert(type(args.states) == "table", "graphics.elements.indicator_icon: states is a required field")
-- determine width
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 4
@ -40,7 +43,7 @@ local function icon_indicator(args)
end
-- write label and initial indicator light
e.setCursorPos(5, 1)
e.window.setCursorPos(5, 1)
e.window.write(args.label)
-- on state change

View File

@ -11,11 +11,14 @@ local element = require("graphics.element")
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new indicator light
---@param args indicator_light_args
local function indicator_light(args)
assert(type(args.label) == "string", "graphics.elements.indicator_light: label is a required field")
assert(type(args.colors) == "table", "graphics.elements.indicator_light: colors is a required field")
-- determine width
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 3
@ -27,7 +30,7 @@ local function indicator_light(args)
local off_blit = util.strrep(args.colors.blit_b, 2)
-- write label and initial indicator light
e.setCursorPos(1, 1)
e.window.setCursorPos(1, 1)
e.window.blit(" ", "000", off_blit .. e.fg_bg.blit_bkg)
e.window.write(args.label)

View File

@ -16,11 +16,13 @@ local element = require("graphics.element")
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field height? integer 1 if omitted, must be an odd number
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new state indicator
---@param args state_indicator_args
local function state_indicator(args)
assert(type(args.states) == "table", "graphics.elements.indicator_state: states is a required field")
-- determine height
if util.is_int(args.height) then
assert(args.height % 2 == 1, "graphics.elements.indicator_state: height should be an odd number")

View File

@ -12,7 +12,7 @@ local element = require("graphics.element")
---@field width? integer parent width if omitted
---@field height? integer parent height if omitted
---@field gframe? graphics_frame frame instead of x/y/width/height
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new rectangle
---@param args rectangle_args
@ -23,7 +23,7 @@ local function rectangle(args)
-- draw bordered box if requested
-- element constructor will have drawn basic colored rectangle regardless
if args.border ~= nil then
e.setCursorPos(1, 1)
e.window.setCursorPos(1, 1)
local border_width_v = args.border.width
local border_width_h = util.trinary(args.border.even, args.border.width * 2, args.border.width)
@ -54,11 +54,11 @@ local function rectangle(args)
-- draw rectangle with borders
for y = 1, e.frame.h do
e.setCursorPos(1, y)
e.window.setCursorPos(1, y)
if y <= border_width_v or y > (e.frame.h - border_width_v) then
e.blit(spaces, blit_fg, blit_bg_top_bot)
e.window.blit(spaces, blit_fg, blit_bg_top_bot)
else
e.blit(spaces, blit_fg, blit_bg_sides)
e.window.blit(spaces, blit_fg, blit_bg_sides)
end
end
end

View File

@ -11,7 +11,7 @@ local util = require("scada-common.util")
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new spinbox control
---@param args spinbox_args
@ -26,6 +26,8 @@ local function spinbox(args)
assert(util.is_int(wn_prec), "graphics.element.spinbox_numeric: whole number precision must be an integer")
assert(util.is_int(fr_prec), "graphics.element.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
@ -69,11 +71,10 @@ local function spinbox(args)
-- update value
value = 0
for i = 1, #digits do
local pow = math.abs(wn_prec - i)
if i <= wn_prec then
local pow = wn_prec - i
value = value + (digits[i] * (10 ^ pow))
else
local pow = i - wn_prec
value = value + (digits[i] * (10 ^ -pow))
end
end

View File

@ -21,7 +21,7 @@ local TEXT_ALIGN = core.graphics.TEXT_ALIGN
-- new text box
---@param args textbox_args
local function textbox(args)
assert(args.text ~= nil, "graphics.elements.textbox: empty text box")
assert(type(args.text) == "string", "graphics.elements.textbox: text is a required field")
-- create new graphics element base object
local e = element.new(args)

View File

@ -14,11 +14,13 @@ local element = require("graphics.element")
---@field width? integer parent width if omitted
---@field height? integer parent height if omitted
---@field gframe? graphics_frame frame instead of x/y/width/height
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new tiling box
---@param args tiling_args
local function tiling(args)
assert(type(args.fill_c) == "table", "graphics.elements.tiling: fill_c is a required field")
-- create new graphics element base object
local e = element.new(args)

View File

@ -11,7 +11,7 @@ local element = require("graphics.element")
---@field width? integer parent width if omitted
---@field height? integer parent height if omitted
---@field gframe? graphics_frame frame instead of x/y/width/height
---@field fg_bg cpair foreground/background colors
---@field fg_bg? cpair foreground/background colors
-- new vertical bar
---@param args vbar_args