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("common/fonts/roboto.ttf"), 20)

local BG = natiny.color.from_hex("#151E2C")
local OBJECT = natiny.color.from_hex("#A9B3C5")
local ACCENT = natiny.color.from_hex("#F8F9FD")
local LIGHT_TEXT = natiny.color.from_hex("#F3F5FA")
local DARK_TEXT = natiny.color.from_hex("#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(dt)
    natiny.window.bind(win, function()
        natiny.render.clear(BG)

        local title = "nine ways to sit in a box"
        local tw = natiny.font.measure_text(font, title)
        natiny.render.set_color(LIGHT_TEXT)
        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)
                natiny.render.rectangle(x, y, 260, 145)
                natiny.render.set_color(ACCENT)
                natiny.render.rectangle_outline(x, y, 260, 145, 3)

                natiny.render.set_color(DARK_TEXT)
                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()