mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#63 cleanup and assertions
This commit is contained in:
parent
4488a0594f
commit
89437b2be9
@ -13,11 +13,14 @@ local element = require("graphics.element")
|
|||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? integer 1 if omitted
|
---@field y? integer 1 if omitted
|
||||||
---@field height? integer parent height 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
|
-- new push button
|
||||||
---@param args push_button_args
|
---@param args push_button_args
|
||||||
local function 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)
|
local text_width = string.len(args.text)
|
||||||
args.width = math.max(text_width + 2, args.min_width)
|
args.width = math.max(text_width + 2, args.min_width)
|
||||||
|
|
||||||
|
@ -12,11 +12,15 @@ local element = require("graphics.element")
|
|||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? integer 1 if omitted
|
---@field y? integer 1 if omitted
|
||||||
---@field height? integer parent height 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)
|
-- new switch button (latch high/low)
|
||||||
---@param args switch_button_args
|
---@param args switch_button_args
|
||||||
local function 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)
|
-- button state (convert nil to false if missing)
|
||||||
local state = args.default or false
|
local state = args.default or false
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ local element = require("graphics.element")
|
|||||||
---@field width? integer parent width if omitted
|
---@field width? integer parent width if omitted
|
||||||
---@field height? integer parent height if omitted
|
---@field height? integer parent height if omitted
|
||||||
---@field gframe? graphics_frame frame instead of x/y/width/height
|
---@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
|
-- new horizontal bar
|
||||||
---@param args hbar_args
|
---@param args hbar_args
|
||||||
|
@ -9,16 +9,21 @@ local element = require("graphics.element")
|
|||||||
---@field unit? string indicator unit
|
---@field unit? string indicator unit
|
||||||
---@field format string data format (lua string format)
|
---@field format string data format (lua string format)
|
||||||
---@field label_unit_colors? cpair label foreground color (a), unit foreground color (b)
|
---@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 parent graphics_element
|
||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? integer 1 if omitted
|
---@field y? integer 1 if omitted
|
||||||
---@field width integer length
|
---@field width integer length
|
||||||
---@field fg_bg cpair foreground/background colors
|
---@field fg_bg? cpair foreground/background colors
|
||||||
|
|
||||||
-- new data indicator
|
-- new data indicator
|
||||||
---@param args data_indicator_args
|
---@param args data_indicator_args
|
||||||
local function 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
|
-- create new graphics element base object
|
||||||
local e = element.new(args)
|
local e = element.new(args)
|
||||||
|
|
||||||
@ -28,7 +33,7 @@ local function data_indicator(args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- write label
|
-- write label
|
||||||
e.setCursorPos(1, 1)
|
e.window.setCursorPos(1, 1)
|
||||||
e.window.write(args.label)
|
e.window.write(args.label)
|
||||||
|
|
||||||
local data_start = string.len(args.label) + 2
|
local data_start = string.len(args.label) + 2
|
||||||
@ -53,7 +58,7 @@ local function data_indicator(args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- initial value draw
|
-- initial value draw
|
||||||
e.on_update(args.default)
|
e.on_update(args.initial_value)
|
||||||
|
|
||||||
return e.get()
|
return e.get()
|
||||||
end
|
end
|
||||||
|
@ -16,11 +16,14 @@ local element = require("graphics.element")
|
|||||||
---@field parent graphics_element
|
---@field parent graphics_element
|
||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? 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
|
-- new icon indicator
|
||||||
---@param args icon_indicator_args
|
---@param args icon_indicator_args
|
||||||
local function 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
|
-- determine width
|
||||||
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 4
|
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
|
end
|
||||||
|
|
||||||
-- write label and initial indicator light
|
-- write label and initial indicator light
|
||||||
e.setCursorPos(5, 1)
|
e.window.setCursorPos(5, 1)
|
||||||
e.window.write(args.label)
|
e.window.write(args.label)
|
||||||
|
|
||||||
-- on state change
|
-- on state change
|
||||||
|
@ -11,11 +11,14 @@ local element = require("graphics.element")
|
|||||||
---@field parent graphics_element
|
---@field parent graphics_element
|
||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? 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
|
-- new indicator light
|
||||||
---@param args indicator_light_args
|
---@param args indicator_light_args
|
||||||
local function 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
|
-- determine width
|
||||||
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 3
|
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)
|
local off_blit = util.strrep(args.colors.blit_b, 2)
|
||||||
|
|
||||||
-- write label and initial indicator light
|
-- 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.blit(" ", "000", off_blit .. e.fg_bg.blit_bkg)
|
||||||
e.window.write(args.label)
|
e.window.write(args.label)
|
||||||
|
|
||||||
|
@ -16,11 +16,13 @@ local element = require("graphics.element")
|
|||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? integer 1 if omitted
|
---@field y? integer 1 if omitted
|
||||||
---@field height? integer 1 if omitted, must be an odd number
|
---@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
|
-- new state indicator
|
||||||
---@param args state_indicator_args
|
---@param args state_indicator_args
|
||||||
local function state_indicator(args)
|
local function state_indicator(args)
|
||||||
|
assert(type(args.states) == "table", "graphics.elements.indicator_state: states is a required field")
|
||||||
|
|
||||||
-- determine height
|
-- determine height
|
||||||
if util.is_int(args.height) then
|
if util.is_int(args.height) then
|
||||||
assert(args.height % 2 == 1, "graphics.elements.indicator_state: height should be an odd number")
|
assert(args.height % 2 == 1, "graphics.elements.indicator_state: height should be an odd number")
|
||||||
|
@ -12,7 +12,7 @@ local element = require("graphics.element")
|
|||||||
---@field width? integer parent width if omitted
|
---@field width? integer parent width if omitted
|
||||||
---@field height? integer parent height if omitted
|
---@field height? integer parent height if omitted
|
||||||
---@field gframe? graphics_frame frame instead of x/y/width/height
|
---@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
|
-- new rectangle
|
||||||
---@param args rectangle_args
|
---@param args rectangle_args
|
||||||
@ -23,7 +23,7 @@ local function rectangle(args)
|
|||||||
-- draw bordered box if requested
|
-- draw bordered box if requested
|
||||||
-- element constructor will have drawn basic colored rectangle regardless
|
-- element constructor will have drawn basic colored rectangle regardless
|
||||||
if args.border ~= nil then
|
if args.border ~= nil then
|
||||||
e.setCursorPos(1, 1)
|
e.window.setCursorPos(1, 1)
|
||||||
|
|
||||||
local border_width_v = args.border.width
|
local border_width_v = args.border.width
|
||||||
local border_width_h = util.trinary(args.border.even, args.border.width * 2, 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
|
-- draw rectangle with borders
|
||||||
for y = 1, e.frame.h do
|
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
|
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
|
else
|
||||||
e.blit(spaces, blit_fg, blit_bg_sides)
|
e.window.blit(spaces, blit_fg, blit_bg_sides)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -11,7 +11,7 @@ local util = require("scada-common.util")
|
|||||||
---@field parent graphics_element
|
---@field parent graphics_element
|
||||||
---@field x? integer 1 if omitted
|
---@field x? integer 1 if omitted
|
||||||
---@field y? 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
|
-- new spinbox control
|
||||||
---@param args spinbox_args
|
---@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(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(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)
|
local initial_str = util.sprintf("%" .. wn_prec .. "." .. fr_prec .. "f", value)
|
||||||
|
|
||||||
---@diagnostic disable-next-line: discard-returns
|
---@diagnostic disable-next-line: discard-returns
|
||||||
@ -69,11 +71,10 @@ local function spinbox(args)
|
|||||||
-- update value
|
-- update value
|
||||||
value = 0
|
value = 0
|
||||||
for i = 1, #digits do
|
for i = 1, #digits do
|
||||||
|
local pow = math.abs(wn_prec - i)
|
||||||
if i <= wn_prec then
|
if i <= wn_prec then
|
||||||
local pow = wn_prec - i
|
|
||||||
value = value + (digits[i] * (10 ^ pow))
|
value = value + (digits[i] * (10 ^ pow))
|
||||||
else
|
else
|
||||||
local pow = i - wn_prec
|
|
||||||
value = value + (digits[i] * (10 ^ -pow))
|
value = value + (digits[i] * (10 ^ -pow))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -21,7 +21,7 @@ local TEXT_ALIGN = core.graphics.TEXT_ALIGN
|
|||||||
-- new text box
|
-- new text box
|
||||||
---@param args textbox_args
|
---@param args textbox_args
|
||||||
local function 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
|
-- create new graphics element base object
|
||||||
local e = element.new(args)
|
local e = element.new(args)
|
||||||
|
@ -14,11 +14,13 @@ local element = require("graphics.element")
|
|||||||
---@field width? integer parent width if omitted
|
---@field width? integer parent width if omitted
|
||||||
---@field height? integer parent height if omitted
|
---@field height? integer parent height if omitted
|
||||||
---@field gframe? graphics_frame frame instead of x/y/width/height
|
---@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
|
-- new tiling box
|
||||||
---@param args tiling_args
|
---@param args tiling_args
|
||||||
local function 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
|
-- create new graphics element base object
|
||||||
local e = element.new(args)
|
local e = element.new(args)
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ local element = require("graphics.element")
|
|||||||
---@field width? integer parent width if omitted
|
---@field width? integer parent width if omitted
|
||||||
---@field height? integer parent height if omitted
|
---@field height? integer parent height if omitted
|
||||||
---@field gframe? graphics_frame frame instead of x/y/width/height
|
---@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
|
-- new vertical bar
|
||||||
---@param args vbar_args
|
---@param args vbar_args
|
||||||
|
Loading…
x
Reference in New Issue
Block a user