vision/src/client/ui/button.lua

58 lines
1.6 KiB
Lua

local lg = assert( love.graphics )
local button = {
h = 60,
y = 0,
x = 0,
w = 100,
space = 15,
}
local mt = { __index = button }
function button:new( t )
t = t or {}
if t.y then button.y = t.y end
if t.h then button.h = t.h end
if t.w then button.w = t.w end
if t.x then button.x = t.x end
if t.space then button.space = t.space end
t.x = t.x or button.x
t.y = t.y or button.y
t.w = t.w or button.w
t.h = t.h or button.h
t.text = t.text or lg.newText( lg.getFont(), "button" )
t.color = t.color or { 0.5, 0.5, 0.5, 0.5 }
t.callback = t.callback or function() print( "Clicked button:", t.text ) end
t.selected = t.selected or false
if t.active == nil then t.active = true end
button.y = button.y + t.h + button.space
return setmetatable( t, mt )
end
function button:contains( x, y )
local mx, my, Mx, My = self.x, self.y, self.x + self.w, self.y + self.h
return self.active and (x < Mx and x > mx and y > my and y < My)
end
function button:draw( )
if not self.active then return end
lg.setColor( self.color )
lg.rectangle( "fill", self.x, self.y, self.w, self.h, 10)
if self.selected then
lg.setColor( 1, 1, 1, 0.8 )
lg.rectangle( "fill", self.x + 3, self.y + 3, self.w - 6, self.h - 6, 10 )
end
lg.setColor( 0, 0, 0, 0.8 )
lg.draw( self.text, self.x + 15, self.y + self.h / 2 - self.text:getHeight() / 2 )
lg.setColor( 0, 0, 0, 0.2 )
lg.draw( self.text, self.x + 12, self.y + self.h / 2 - self.text:getHeight() / 2 )-- + self.h / 2 )
end
return setmetatable( button, { __call = button.new } )