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

View File

@ -1,6 +1,7 @@
-- Rectangle Graphics Element -- Rectangle Graphics Element
local element = require("graphics.element") local element = require("graphics.element")
local util = require("scada-common.util")
---@class rectangle_args ---@class rectangle_args
---@field border? graphics_border ---@field border? graphics_border
@ -15,24 +16,25 @@ local element = require("graphics.element")
-- new rectangle -- new rectangle
---@param args rectangle_args ---@param args rectangle_args
local function rectangle(args) local function rectangle(args)
-- create new graphics element rectangle object -- create new graphics element base object
local e = element.new(args) local e = element.new(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
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) 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 border_blit = colors.toBlit(args.border.color)
local spaces = "" local spaces = ""
local blit_fg = "" local blit_fg = ""
local blit_bg_top_bot = "" local blit_bg_top_bot = ""
local blit_bg_sides = "" 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 -- form the basic and top/bottom blit strings
for _ = 1, e.frame.w do for _ = 1, e.frame.w do
spaces = spaces .. " " spaces = spaces .. " "
@ -43,7 +45,7 @@ local function rectangle(args)
-- form the body blit strings (sides are border, inside is normal) -- form the body blit strings (sides are border, inside is normal)
for x = 1, e.frame.w do for x = 1, e.frame.w do
-- edges get border color, center gets normal -- 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 blit_bg_sides = blit_bg_sides .. border_blit
else else
blit_bg_sides = blit_bg_sides .. e.fg_bg.blit_bkg blit_bg_sides = blit_bg_sides .. e.fg_bg.blit_bkg
@ -53,7 +55,7 @@ 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(y, 1) 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) e.blit(spaces, blit_fg, blit_bg_top_bot)
else else
e.blit(spaces, blit_fg, blit_bg_sides) e.blit(spaces, blit_fg, blit_bg_sides)