2022-06-08 16:27:28 +00:00
|
|
|
-- Rectangle Graphics Element
|
|
|
|
|
2022-06-08 17:08:48 +00:00
|
|
|
local util = require("scada-common.util")
|
2022-06-08 16:27:28 +00:00
|
|
|
|
2022-06-08 17:18:14 +00:00
|
|
|
local element = require("graphics.element")
|
|
|
|
|
2022-06-08 16:27:28 +00:00
|
|
|
---@class rectangle_args
|
|
|
|
---@field border? graphics_border
|
|
|
|
---@field parent graphics_element
|
|
|
|
---@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 16:27:28 +00:00
|
|
|
|
|
|
|
-- new rectangle
|
|
|
|
---@param args rectangle_args
|
|
|
|
local function rectangle(args)
|
2022-06-19 15:20:09 +00:00
|
|
|
-- offset children
|
|
|
|
if args.border ~= nil then
|
|
|
|
args.inner_x = args.border.width
|
|
|
|
args.inner_y = args.border.width
|
|
|
|
|
|
|
|
-- slightly different y offset if the border is set to even
|
|
|
|
if args.border.even then
|
|
|
|
local width_x2 = (2 * args.border.width)
|
|
|
|
args.inner_y = (width_x2 // 3) + util.trinary(width_x2 % 3 > 0, 1, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-08 17:08:48 +00:00
|
|
|
-- create new graphics element base object
|
2022-06-08 16:27:28 +00:00
|
|
|
local e = element.new(args)
|
|
|
|
|
|
|
|
-- draw bordered box if requested
|
|
|
|
-- element constructor will have drawn basic colored rectangle regardless
|
|
|
|
if args.border ~= nil then
|
2022-06-11 21:06:32 +00:00
|
|
|
e.window.setCursorPos(1, 1)
|
2022-06-08 16:27:28 +00:00
|
|
|
|
2022-06-18 05:33:45 +00:00
|
|
|
local border_width = args.border.width
|
2022-06-08 16:27:28 +00:00
|
|
|
local border_blit = colors.toBlit(args.border.color)
|
2022-06-18 05:33:45 +00:00
|
|
|
local width_x2 = border_width * 2
|
|
|
|
local inner_width = e.frame.w - width_x2
|
2022-06-08 16:27:28 +00:00
|
|
|
|
2022-06-08 17:18:14 +00:00
|
|
|
-- check dimensions
|
2022-06-18 05:33:45 +00:00
|
|
|
assert(width_x2 <= e.frame.w, "graphics.elements.rectangle: border too thick for width")
|
|
|
|
assert(width_x2 <= e.frame.h, "graphics.elements.rectangle: border too thick for height")
|
|
|
|
|
|
|
|
-- form the basic line strings and top/bottom blit strings
|
|
|
|
local spaces = util.spaces(e.frame.w)
|
|
|
|
local blit_fg = util.strrep(e.fg_bg.blit_fgd, e.frame.w)
|
|
|
|
local blit_bg_sides = ""
|
|
|
|
local blit_bg_top_bot = util.strrep(border_blit, e.frame.w)
|
2022-06-08 17:08:48 +00:00
|
|
|
|
2022-06-18 05:33:45 +00:00
|
|
|
-- partial bars
|
|
|
|
local p_a = util.spaces(border_width) .. util.strrep("\x8f", inner_width) .. util.spaces(border_width)
|
|
|
|
local p_b = util.spaces(border_width) .. util.strrep("\x83", inner_width) .. util.spaces(border_width)
|
|
|
|
local p_inv_fg = util.strrep(border_blit, border_width) .. util.strrep(e.fg_bg.blit_bkg, inner_width) ..
|
|
|
|
util.strrep(border_blit, border_width)
|
|
|
|
local p_inv_bg = util.strrep(e.fg_bg.blit_bkg, border_width) .. util.strrep(border_blit, inner_width) ..
|
|
|
|
util.strrep(e.fg_bg.blit_bkg, border_width)
|
2022-06-08 16:27:28 +00:00
|
|
|
|
|
|
|
-- form the body blit strings (sides are border, inside is normal)
|
|
|
|
for x = 1, e.frame.w do
|
|
|
|
-- edges get border color, center gets normal
|
2022-06-18 05:33:45 +00:00
|
|
|
if x <= border_width or x > (e.frame.w - border_width) then
|
2022-06-08 16:27:28 +00:00
|
|
|
blit_bg_sides = blit_bg_sides .. border_blit
|
|
|
|
else
|
|
|
|
blit_bg_sides = blit_bg_sides .. e.fg_bg.blit_bkg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- draw rectangle with borders
|
|
|
|
for y = 1, e.frame.h do
|
2022-06-11 21:06:32 +00:00
|
|
|
e.window.setCursorPos(1, y)
|
2022-06-18 05:33:45 +00:00
|
|
|
if y <= border_width then
|
|
|
|
-- partial pixel fill
|
|
|
|
if args.border.even and y == border_width then
|
|
|
|
if width_x2 % 3 == 1 then
|
|
|
|
e.window.blit(p_b, p_inv_bg, p_inv_fg)
|
|
|
|
elseif width_x2 % 3 == 2 then
|
|
|
|
e.window.blit(p_a, p_inv_bg, p_inv_fg)
|
|
|
|
else
|
|
|
|
-- skip line
|
|
|
|
e.window.blit(spaces, blit_fg, blit_bg_sides)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
e.window.blit(spaces, blit_fg, blit_bg_top_bot)
|
|
|
|
end
|
|
|
|
elseif y > (e.frame.h - border_width) then
|
|
|
|
-- partial pixel fill
|
|
|
|
if args.border.even and y == ((e.frame.h - border_width) + 1) then
|
|
|
|
if width_x2 % 3 == 1 then
|
|
|
|
e.window.blit(p_a, p_inv_fg, blit_bg_top_bot)
|
|
|
|
elseif width_x2 % 3 == 2 then
|
|
|
|
e.window.blit(p_b, p_inv_fg, blit_bg_top_bot)
|
|
|
|
else
|
|
|
|
-- skip line
|
|
|
|
e.window.blit(spaces, blit_fg, blit_bg_sides)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
e.window.blit(spaces, blit_fg, blit_bg_top_bot)
|
|
|
|
end
|
2022-06-08 16:27:28 +00:00
|
|
|
else
|
2022-06-11 21:06:32 +00:00
|
|
|
e.window.blit(spaces, blit_fg, blit_bg_sides)
|
2022-06-08 16:27:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return e.get()
|
|
|
|
end
|
|
|
|
|
|
|
|
return rectangle
|