Aligned labels

Demonstrates all nine horizontal and vertical alignment combinations supported by natiny.render.text_box.


    
main.lua
local natiny = require("natiny")

natiny.backend.init()

local win = natiny.window.create("Aligned labels", 900, 600)
natiny.window.set_scale_mode(win, natiny.window.SCALE_MODE_STRETCH)

local font = natiny.font.create(natiny.resource.load("data/common/fonts/roboto.ttf"), 20)

local BG     = {  21 / 255,  30 / 255,  44 / 255 } -- #151E2C
local OBJECT = { 169 / 255, 179 / 255, 197 / 255 } -- #A9B3C5
local ACCENT = { 248 / 255, 249 / 255, 253 / 255 } -- #F8F9FD
local LIGHT_TEXT = { 243 / 255, 245 / 255, 250 / 255 } -- #F3F5FA
local DARK_TEXT = { 17 / 255, 24 / 255, 39 / 255 } -- #111827

local COLS = { natiny.render.ALIGN_LEFT, natiny.render.ALIGN_CENTER, natiny.render.ALIGN_RIGHT }
local ROWS = { natiny.render.ALIGN_TOP, natiny.render.ALIGN_MIDDLE, natiny.render.ALIGN_BOTTOM }
local NAMES = { { "top left", "top center", "top right" },
                { "middle left", "middle center", "middle right" },
                { "bottom left", "bottom center", "bottom right" } }

natiny.backend.loop(function()
    natiny.window.bind(win, function()
        natiny.render.clear(BG[1], BG[2], BG[3], 1)

        local title = "nine ways to sit in a box"
        local tw = natiny.font.measure_text(font, title)
        natiny.render.set_color(LIGHT_TEXT[1], LIGHT_TEXT[2], LIGHT_TEXT[3], 1)
        natiny.render.text(font, (900 - tw) / 2, 26, title)

        for row = 1, 3 do
            for col = 1, 3 do
                local x = 40 + (col - 1) * 280
                local y = 80 + (row - 1) * 165

                natiny.render.set_color(OBJECT[1], OBJECT[2], OBJECT[3], 1)
                natiny.render.rectangle(x, y, 260, 145)
                natiny.render.set_color(ACCENT[1], ACCENT[2], ACCENT[3], 1)
                natiny.render.rectangle_outline(x, y, 260, 145, 3)

                natiny.render.set_color(DARK_TEXT[1], DARK_TEXT[2], DARK_TEXT[3], 1)
                natiny.render.text_box(font, x + 12, y + 12, 236, 121,
                           NAMES[row][col], ROWS[row] | COLS[col])
            end
        end
    end)
end)

natiny.backend.shutdown()