#62 uneven border support because rectangular pixels

This commit is contained in:
Mikayla Fischler 2022-06-08 13:08:48 -04:00
parent 8002698dd0
commit 29c4c39d23
2 changed files with 37 additions and 18 deletions

View File

@ -7,6 +7,7 @@ local events = {}
---@field x integer
---@field y integer
-- create a new touch event definition
---@param monitor string
---@param x integer
---@param y integer
@ -33,14 +34,18 @@ graphics.TEXT_ALIGN = {
---@class graphics_border
---@field width integer
---@field color color
---@field even boolean
---@param width integer
---@param color color
-- create a new border definition
---@param width integer border width
---@param color color border color
---@param even boolean whether to pad width extra to account for rectangular pixels
---@return graphics_border
function graphics.border(width, color)
function graphics.border(width, color, even)
return {
width = width,
color = color
color = color,
even = even
}
end
@ -50,6 +55,7 @@ end
---@field w integer
---@field h integer
-- create a new graphics frame definition
---@param x integer
---@param y integer
---@param w integer
@ -65,20 +71,31 @@ function graphics.gframe(x, y, w, h)
end
---@class cpair
---@field color_a color
---@field color_b color
---@field blit_a string
---@field blit_b string
---@field fgd color
---@field bkg color
---@field blit_fgd string
---@field blit_bkg string
---@param foreground color
---@param background color
-- create a new color pair definition
---@param a color
---@param b color
---@return cpair
function graphics.cpair(foreground, background)
function graphics.cpair(a, b)
return {
fgd = foreground,
bkg = background,
blit_fgd = colors.toBlit(foreground),
blit_bkg = colors.toBlit(background)
-- color pairs
color_a = a,
color_b = b,
blit_a = a,
blit_b = b,
-- aliases
fgd = a,
bkg = b,
blit_fgd = colors.toBlit(a),
blit_bkg = colors.toBlit(b)
}
end

View File

@ -1,6 +1,7 @@
-- Rectangle Graphics Element
local element = require("graphics.element")
local util = require("scada-common.util")
---@class rectangle_args
---@field border? graphics_border
@ -15,24 +16,25 @@ local element = require("graphics.element")
-- new rectangle
---@param args rectangle_args
local function rectangle(args)
-- create new graphics element rectangle object
-- create new graphics element base object
local e = element.new(args)
-- draw bordered box if requested
-- element constructor will have drawn basic colored rectangle regardless
if args.border ~= nil then
assert(args.border.width * 2 <= e.frame.w, "graphics.elements.rectangle: border too thick for width")
assert(args.border.width * 2 <= e.frame.h, "graphics.elements.rectangle: border too thick for height")
e.setCursorPos(1, 1)
local border_width = 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_blit = colors.toBlit(args.border.color)
local spaces = ""
local blit_fg = ""
local blit_bg_top_bot = ""
local blit_bg_sides = ""
assert(border_width_v * 2 <= e.frame.w, "graphics.elements.rectangle: border too thick for width")
assert(border_width_h * 2 <= e.frame.h, "graphics.elements.rectangle: border too thick for height")
-- form the basic and top/bottom blit strings
for _ = 1, e.frame.w do
spaces = spaces .. " "
@ -43,7 +45,7 @@ local function rectangle(args)
-- 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
if x <= border_width or x > (e.frame.w - border_width) then
if x <= border_width_h or x > (e.frame.w - border_width_h) then
blit_bg_sides = blit_bg_sides .. border_blit
else
blit_bg_sides = blit_bg_sides .. e.fg_bg.blit_bkg
@ -53,7 +55,7 @@ local function rectangle(args)
-- draw rectangle with borders
for y = 1, e.frame.h do
e.setCursorPos(y, 1)
if y <= border_width or y > (e.frame.h - border_width) then
if y <= border_width_v or y > (e.frame.h - border_width_v) then
e.blit(spaces, blit_fg, blit_bg_top_bot)
else
e.blit(spaces, blit_fg, blit_bg_sides)