mirror of
https://codeberg.org/1414codeforge/yui.git
synced 2025-06-05 22:19:11 +02:00
[*] Initial commit.
This commit is contained in:
59
button.lua
Normal file
59
button.lua
Normal file
@ -0,0 +1,59 @@
|
||||
local BASE = (...):gsub('button', '')
|
||||
|
||||
local Widget = require BASE..'widget'
|
||||
local core = require BASE..'core'
|
||||
|
||||
local shadowtext = require 'lib.gear.shadowtext'
|
||||
local T = require('lib.moonspeak').translate
|
||||
|
||||
local Button = setmetatable({}, Widget)
|
||||
Button.__index = Button
|
||||
|
||||
|
||||
function Button.new(args)
|
||||
local self = setmetatable(args, Button)
|
||||
|
||||
self.text = self.text or ""
|
||||
self.align = self.align or 'center'
|
||||
self.valign = self.valign or 'center'
|
||||
self.color = self.color or core.theme.color
|
||||
self.cornerRadius = self.cornerRadius or core.theme.cornerRadius
|
||||
self.active = false
|
||||
if not self.notranslate then
|
||||
self.text = T(self.text)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
local function hit(button)
|
||||
if not button.active then
|
||||
button.active = true
|
||||
button:onHit()
|
||||
|
||||
button.ui.timer:after(0.15, function() button.active = false end)
|
||||
end
|
||||
end
|
||||
|
||||
function Button:onPointerInput(px,py, clicked)
|
||||
self:grabFocus()
|
||||
if clicked then hit(self) end
|
||||
end
|
||||
|
||||
function Button:onActionInput(action)
|
||||
if action.confirm then hit(self) end
|
||||
end
|
||||
|
||||
function Button:draw()
|
||||
local x,y,w,h = self.x,self.y,self.w,self.h
|
||||
local font = self.font or love.graphics.getFont()
|
||||
local c = self:colorForState()
|
||||
|
||||
core.drawBox(x,y,w,h, c, self.cornerRadius)
|
||||
love.graphics.setColor(c.fg)
|
||||
love.graphics.setFont(font)
|
||||
|
||||
y = y + core.verticalOffsetForAlign(self.valign, font, h)
|
||||
shadowtext.printf(self.text, x+2, y, w-4, self.align)
|
||||
end
|
||||
|
||||
return Button
|
Reference in New Issue
Block a user