yui/checkbox.lua

65 lines
1.8 KiB
Lua
Raw Normal View History

local BASE = (...):gsub('checkbox$', '')
2022-08-15 23:41:17 +02:00
2022-08-16 00:23:52 +02:00
local Widget = require(BASE..'widget')
local core = require(BASE..'core')
2022-08-15 23:41:17 +02:00
local shadowtext = require 'lib.gear.shadowtext'
local T = require('lib.moonspeak').translate
local Checkbox = setmetatable({}, Widget)
Checkbox.__index = Checkbox
function Checkbox.new(args)
local self = setmetatable(args, Checkbox)
self.text = self.text or ""
self.text = self.notranslate and self.text or T(self.text)
self.align = self.align or 'left'
self.valign = self.valign or 'center'
self.color = self.color or core.theme.color
self.cornerRadius = self.cornerRadius or core.theme.cornerRadius
self.checked = self.checked or false
return self
end
function Checkbox:onPointerInput(px,py, clicked)
self:grabFocus()
if clicked then
self.checked = not self.checked
self:onChange(self.checked)
end
end
function Checkbox:onActionInput(action)
if action.confirm then
self.checked = not self.checked
self:onChange(self.checked)
end
end
function Checkbox:draw()
local x,y,w,h = self.x,self.y,self.w,self.h
local c = self:colorForState()
local font = self.font or love.graphics.getFont()
-- Draw checkbox
core.drawBox(x+h/10,y+h/10,h*.8,h*.8, c, self.cornerRadius)
love.graphics.setColor(c.fg)
if self.checked then
love.graphics.setLineStyle('smooth')
love.graphics.setLineWidth(5)
love.graphics.setLineJoin('bevel')
love.graphics.line(x+h*.2,y+h*.55, x+h*.45,y+h*.75, x+h*.8,y+h*.2)
end
-- Most of the times checkboxes have no text, so test for performance
if self.text ~= "" then
love.graphics.setFont(font)
y = y + core.verticalOffsetForAlign(self.valign, font, self.h)
shadowtext.printf(self.text, x + h, y, w - h, self.align)
end
end
return Checkbox