clocktoy/checkbox.lua

46 lines
1.3 KiB
Lua
Raw Normal View History

2024-04-21 22:42:22 +00:00
local Checkbox = {}
Checkbox.__index = Checkbox
function Checkbox:new(width, lineWidth, transform, checked)
local box = {
width = width or 20,
lineWidth = lineWidth or 2,
transform = transform or love.math.newTransform(),
checked = checked,
onChanged = nil
}
return setmetatable(box, self)
end
function Checkbox:draw()
local width = self.width
local lineWidth = self.lineWidth
local halfLine = lineWidth/2
local boxWidth = width - lineWidth
love.graphics.push("all")
love.graphics.applyTransform(self.transform)
love.graphics.setLineWidth(lineWidth)
love.graphics.rectangle("line", halfLine, halfLine, boxWidth, boxWidth)
if self.checked then
local crossEnd = width - halfLine
love.graphics.line(halfLine, halfLine, crossEnd, crossEnd)
love.graphics.line(halfLine, crossEnd, crossEnd, halfLine)
end
love.graphics.pop()
end
function Checkbox:getChecked()
return self.checked
end
function Checkbox:pressmouse(x, y, button)
if button == 1 then
local localX, localY = self.transform:inverseTransformPoint(x, y)
if localX >= 0 and localX < self.width and localY >= 0 and localY < self.width then
self.checked = not self.checked
if self.onChanged then self.onChanged(self.checked) end
end
end
end
return Checkbox