Created
November 27, 2020 17:59
-
-
Save andr-ew/d6c3ef014743a8c315a33259a61560d2 to your computer and use it in GitHub Desktop.
norns boxed text with alignment & placement options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function txtbox(txt, x, y, x2, y2, xalign, yalign, font, size, lvl, border, fill, padding, font_headroom, font_leftroom) | |
font_headroom = font_headroom or 3/8 | |
font_leftroom = font_leftroom or 1/16 | |
screen.font_face(font) | |
screen.font_size(size) | |
local fixed = (x2 ~= nil) and (y2 ~= nil) | |
local width = screen.text_extents(txt) | |
local height = size * (1 - font_headroom) | |
local w, h, bx, by, tx, ty, tmode | |
if fixed then | |
w = x2 - x - 1 | |
h = y2 - y - 1 | |
bx = x | |
by = y | |
tx = bx + w/2 - 1 | |
ty = by + (h + height)/2 - 1 | |
tmode = 'center' | |
else | |
local px = (type(padding) == 'table' and padding[1] or padding) * 2 | |
local py = (type(padding) == 'table' and padding[2] or padding) * 2 | |
w = width + px - 1 | |
h = height + py - 1 | |
local bxalign = (xalign == 'center') and (((width + px) / 2) + 1) or (xalign == 'right') and (width + px) or 0 | |
local byalign = (yalign == 'center') and h/2 - 1 or (yalign == 'bottom') and h or 0 | |
local txalign = (xalign == 'center') and 0 or (xalign == 'right') and -1 or 1 | |
local tyalign = (yalign == 'center') and (height/2) or (yalign == 'bottom') and - (py/2) or height + (py/2) - 1 | |
bx = x - bxalign | |
by = y - byalign | |
tx = x - 1 + ((px / 2) * txalign) - (font == 1 and (size * font_leftroom) or 0) | |
ty = y + tyalign | |
tmode = xalign | |
end | |
if fill > 0 then | |
screen.level(fill) | |
screen.rect(bx - 1, by - 1, w + 1, h + 1) | |
screen.fill() | |
end | |
if border > 0 then | |
screen.level(border) | |
screen.rect(bx, by, w, h) | |
screen.stroke() | |
end | |
screen.level(lvl) | |
screen.move(tx, ty) | |
if tmode == 'right' then | |
screen.text_right(txt) | |
elseif tmode == 'center' then | |
screen.text_center(txt) | |
else | |
screen.text(txt) | |
end | |
end | |
function init() | |
screen.aa(0) | |
redraw() | |
end | |
function redraw() | |
screen.clear() | |
--top left aligned, solid border | |
txtbox('left', 4, 4, nil, nil, 'left', 'top', 1, 8, 15, 15, 0, 3) | |
--center aligned, solid fill | |
txtbox('in the center', 64, 32, nil, nil, 'center', 'center', 1, 8, 0, 0, 15, 3) | |
--fixed width | |
txtbox('fix', 100, 50, 124, 60, '', '', 1, 8, 12, 15, 1) | |
screen.update() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a lil pic !