40 lines
985 B
Lua
40 lines
985 B
Lua
local button = {}
|
|
local lg = assert( love.graphics )
|
|
|
|
function button:new( t, x, y, w, h, text, color, callback )
|
|
--print( t.x, t.y, t.w, t.h, t.text, t.color, t.callback )
|
|
return setmetatable( t or
|
|
{
|
|
x = 0, y = 0,
|
|
w = 100, h = 100,
|
|
text = text, color = color,
|
|
callback = callback,
|
|
selected = false
|
|
},
|
|
{
|
|
__index = button,
|
|
__call = t.callback or callback
|
|
}
|
|
)
|
|
end
|
|
|
|
function button:contains( x, y )
|
|
local mx, my, Mx, My = self.x, self.y, self.x + self.w, self.y + self.h
|
|
return (x < Mx and x > mx and y > my and y < My)
|
|
end
|
|
|
|
function button:draw( )
|
|
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.7 )
|
|
lg.print( self.text, self.x + 15, self.y + 10 )
|
|
end
|
|
|
|
return setmetatable( button, { __call = button.new } ) |