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

88 lines
2.7 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
---@field y? integer 1 if omitted
---@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
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)
2022-06-11 21:06:32 +00:00
assert(type(args.fill_c) == "table", "graphics.elements.tiling: fill_c is a required field")
2022-06-08 17:18:14 +00:00
-- create new graphics element base object
local e = element.new(args)
-- draw tiling box
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
local alternator = true
-- border
2022-07-16 17:25:07 +00:00
if args.border_c ~= nil then
e.window.setBackgroundColor(args.border_c)
2022-06-08 17:18:14 +00:00
e.window.clear()
2022-07-16 17:25:07 +00:00
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
2022-07-16 17:25:07 +00:00
assert(inner_width > 0, "graphics.elements.tiling: inner_width <= 0")
assert(inner_height > 0, "graphics.elements.tiling: inner_height <= 0")
assert(start_x <= inner_width, "graphics.elements.tiling: start_x > inner_width")
assert(start_y <= inner_height, "graphics.elements.tiling: start_y > inner_height")
2022-06-08 17:18:14 +00:00
-- create pattern
2022-07-16 17:25:07 +00:00
for y = start_y, inner_height + (start_y - 1) do
e.window.setCursorPos(start_x, y)
for x = 1, inner_width do
2022-06-08 17:18:14 +00:00
if alternator then
if even then
2022-08-01 13:30:53 +00:00
e.window.blit(" ", "00", fill_a .. fill_a)
2022-06-08 17:18:14 +00:00
else
2022-08-01 13:30:53 +00:00
e.window.blit(" ", "0", fill_a)
2022-06-08 17:18:14 +00:00
end
else
if even then
e.window.blit(" ", "00", fill_b .. fill_b)
else
e.window.blit(" ", "0", fill_b)
end
end
2022-08-01 13:30:53 +00:00
alternator = not alternator
2022-06-08 17:18:14 +00:00
end
2022-08-01 13:30:53 +00:00
if inner_width % 2 == 0 then alternator = not alternator end
2022-06-08 17:18:14 +00:00
end
return e.get()
2022-06-08 17:18:14 +00:00
end
return tiling