cc-mek-scada/graphics/elements/tiling.lua

96 lines
2.8 KiB
Lua
Raw Normal View History

2022-06-08 17:18:14 +00:00
-- "Basketweave" Tiling Graphics Element
local util = require("scada-common.util")
local element = require("graphics.element")
---@class tiling_args
---@field fill_c cpair colors to fill with
---@field even? boolean whether to account for rectangular pixels
2022-07-16 17:25:07 +00:00
---@field border_c? color optional frame color
2022-06-08 17:18:14 +00:00
---@field parent graphics_element
---@field id? string element id
2022-06-08 17:18:14 +00:00
---@field x? integer 1 if omitted
2023-07-10 03:42:44 +00:00
---@field y? integer auto incremented if omitted
2022-06-08 17:18:14 +00:00
---@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
2022-06-11 21:06:32 +00:00
---@field fg_bg? cpair foreground/background colors
---@field hidden? boolean true to hide on initial draw
2022-06-08 17:18:14 +00:00
-- new tiling box
---@param args tiling_args
---@return graphics_element element, element_id id
2022-06-08 17:18:14 +00:00
local function tiling(args)
2023-09-30 15:46:47 +00:00
element.assert(type(args.fill_c) == "table", "fill_c is a required field")
2022-06-11 21:06:32 +00:00
2022-06-08 17:18:14 +00:00
-- create new graphics element base object
local e = element.new(args)
local fill_a = args.fill_c.blit_a
local fill_b = args.fill_c.blit_b
local even = args.even == true
local start_x = 1
local start_y = 1
2022-07-16 17:25:07 +00:00
local inner_width = math.floor(e.frame.w / util.trinary(even, 2, 1))
local inner_height = e.frame.h
2022-06-08 17:18:14 +00:00
-- border
2022-07-16 17:25:07 +00:00
if args.border_c ~= nil then
start_x = 1 + util.trinary(even, 2, 1)
start_y = 2
2022-06-08 17:18:14 +00:00
2022-08-01 13:30:53 +00:00
inner_width = math.floor((e.frame.w - 2 * util.trinary(even, 2, 1)) / util.trinary(even, 2, 1))
2022-07-16 17:25:07 +00:00
inner_height = e.frame.h - 2
2022-06-08 17:18:14 +00:00
end
-- check dimensions
2023-09-30 15:46:47 +00:00
element.assert(inner_width > 0, "inner_width <= 0")
element.assert(inner_height > 0, "inner_height <= 0")
element.assert(start_x <= inner_width, "start_x > inner_width")
element.assert(start_y <= inner_height, "start_y > inner_height")
-- draw tiling box
function e.redraw()
local alternator = true
if args.border_c ~= nil then
e.w_set_bkg(args.border_c)
e.window.clear()
end
-- draw pattern
for y = start_y, inner_height + (start_y - 1) do
e.w_set_cur(start_x, y)
for _ = 1, inner_width do
if alternator then
if even then
e.w_blit(" ", "00", fill_a .. fill_a)
else
e.w_blit(" ", "0", fill_a)
end
2022-06-08 17:18:14 +00:00
else
if even then
e.w_blit(" ", "00", fill_b .. fill_b)
else
e.w_blit(" ", "0", fill_b)
end
2022-06-08 17:18:14 +00:00
end
alternator = not alternator
2022-06-08 17:18:14 +00:00
end
if inner_width % 2 == 0 then alternator = not alternator end
2022-06-08 17:18:14 +00:00
end
end
-- initial draw
e.redraw()
return e.complete()
2022-06-08 17:18:14 +00:00
end
return tiling