yui/label.lua
Lorenzo Cogotti 958206f1f7 [*] Rework theme customization.
This commit allows more flexible and consistent theme overriding
rules for each Widget. There are 3 levels of customization.

1. Global theme (yui.theme) this is the default theme for every Ui
2. Ui theme, this overrides the global theme and provides a default
   for every Widget of the Ui
3. Widget theme, this overrides the Ui theme for a single widget

This commit also allows themes to specify a font field.
This replaces the sparse color, font and cornerRadius field often
provided by each Widget.
Widgets and Uis may also partially override a theme by specifying
only a few fields.
2022-10-25 19:06:57 +02:00

54 lines
1.6 KiB
Lua

--- Implements a static label widget
--
-- @classmod yui.Label
-- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini
local BASE = (...):gsub('label$', '')
local Widget = require(BASE..'widget')
local core = require(BASE..'core')
local shadowtext = require 'lib.gear.shadowtext'
local T = require('lib.moonspeak').translate
-- Labels don't accept focus
local Label = setmetatable({
nofocus = true,
__call = function(cls, args) return cls:new(args) end
}, Widget)
Label.__index = Label
--- Attributes accepted by the @{Label} widget in addition to the standard @{yui.Widget.WidgetAttributes|attributes}.
--
-- @field text (string) text displayed inside the Label
-- @field[opt='center'] valign (string) vertical alignment 'top', 'bottom', 'center'
-- @field[opt='center'] align (string) horizontal alignment, 'left', 'center', 'right'
-- @field notranslate (boolean) don't translate text
-- @table LabelAttributes
--- Label constructor
-- @param args (@{LabelAttributes}) widget attributes
function Label:new(args)
self = setmetatable(args, self)
self.text = self.text or ""
self.text = self.notranslate and self.text or T(self.text)
self.align = self.align or 'center'
self.valign = self.valign or 'center'
return self
end
function Label:draw()
local x,y,w,h = self.x,self.y,self.w,self.h
local color, font, _ = core.themeForWidget(self)
y = y + core.verticalOffsetForAlign(self.valign, font, h)
love.graphics.setColor(color.normal.fg)
love.graphics.setFont(font)
shadowtext.printf(self.text, x+2, y, w-4, self.align)
end
return Label