From 89437b2be939238578a41f27bd5a30f9d3b0a47d Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sat, 11 Jun 2022 17:06:32 -0400 Subject: [PATCH] #63 cleanup and assertions --- graphics/elements/button_push.lua | 5 ++++- graphics/elements/button_switch.lua | 6 +++++- graphics/elements/hbar.lua | 2 +- graphics/elements/indicator_data.lua | 13 +++++++++---- graphics/elements/indicator_icon.lua | 7 +++++-- graphics/elements/indicator_light.lua | 7 +++++-- graphics/elements/indicator_state.lua | 4 +++- graphics/elements/rectangle.lua | 10 +++++----- graphics/elements/spinbox_numeric.lua | 7 ++++--- graphics/elements/textbox.lua | 2 +- graphics/elements/tiling.lua | 4 +++- graphics/elements/vbar.lua | 2 +- 12 files changed, 46 insertions(+), 23 deletions(-) diff --git a/graphics/elements/button_push.lua b/graphics/elements/button_push.lua index a51e8f3..321e5a6 100644 --- a/graphics/elements/button_push.lua +++ b/graphics/elements/button_push.lua @@ -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) diff --git a/graphics/elements/button_switch.lua b/graphics/elements/button_switch.lua index c17e553..6a06348 100644 --- a/graphics/elements/button_switch.lua +++ b/graphics/elements/button_switch.lua @@ -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 diff --git a/graphics/elements/hbar.lua b/graphics/elements/hbar.lua index bba4eb6..fad6a50 100644 --- a/graphics/elements/hbar.lua +++ b/graphics/elements/hbar.lua @@ -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 diff --git a/graphics/elements/indicator_data.lua b/graphics/elements/indicator_data.lua index 4f0655a..97e2667 100644 --- a/graphics/elements/indicator_data.lua +++ b/graphics/elements/indicator_data.lua @@ -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 diff --git a/graphics/elements/indicator_icon.lua b/graphics/elements/indicator_icon.lua index fc06a29..6bcf326 100644 --- a/graphics/elements/indicator_icon.lua +++ b/graphics/elements/indicator_icon.lua @@ -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 diff --git a/graphics/elements/indicator_light.lua b/graphics/elements/indicator_light.lua index 9192099..8cf7d14 100644 --- a/graphics/elements/indicator_light.lua +++ b/graphics/elements/indicator_light.lua @@ -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) diff --git a/graphics/elements/indicator_state.lua b/graphics/elements/indicator_state.lua index 422259f..722f633 100644 --- a/graphics/elements/indicator_state.lua +++ b/graphics/elements/indicator_state.lua @@ -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") diff --git a/graphics/elements/rectangle.lua b/graphics/elements/rectangle.lua index a8c65b1..2ea84ea 100644 --- a/graphics/elements/rectangle.lua +++ b/graphics/elements/rectangle.lua @@ -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 diff --git a/graphics/elements/spinbox_numeric.lua b/graphics/elements/spinbox_numeric.lua index d77c3ea..841bf1f 100644 --- a/graphics/elements/spinbox_numeric.lua +++ b/graphics/elements/spinbox_numeric.lua @@ -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 diff --git a/graphics/elements/textbox.lua b/graphics/elements/textbox.lua index cd4a313..d5694a4 100644 --- a/graphics/elements/textbox.lua +++ b/graphics/elements/textbox.lua @@ -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) diff --git a/graphics/elements/tiling.lua b/graphics/elements/tiling.lua index 8395068..c046670 100644 --- a/graphics/elements/tiling.lua +++ b/graphics/elements/tiling.lua @@ -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) diff --git a/graphics/elements/vbar.lua b/graphics/elements/vbar.lua index c140c2e..ccd259a 100644 --- a/graphics/elements/vbar.lua +++ b/graphics/elements/vbar.lua @@ -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