dcearth/button.lua

79 lines
1.7 KiB
Lua

local lg = love.graphics
local t = {
name = "",
tooltip = "button",
icon = lg.newImage( "icons/eye.bmp" ),
x = 8,
y = 250,
w = 176,
h = 24,
visible = true,
callback = function( self ) return print( "clicked button: ", self.name, self.x, self.y, self.w, self.h, self.visible ) end
}
t.selected, t.next, t.prev = t, t, t
function t.contains( button, x, y )
return x < button.x + button.w and x > button.x
and y < button.y + button.h and y > button.y
end
function t.new( b )
b = setmetatable( b or {}, t )
b.next = t
t.prev.next = b
b.prev = t.prev
t.prev = b
return b
end
local drawPassOngoing = false
function t.draw( b )
if b == t then
drawPassOngoing = not( drawPassOngoing )
if not drawPassOngoing then return end
elseif b.visible then
lg.rectangle( "line", b.x, b.y, b.w, b.h, 6 )
lg.printf( b.name,
b.x,
b.y + 0.5 * ( b.h- lg.getFont():getHeight() ),
b.w - 5,
"right" )
if b.icon then lg.draw( b.icon,
b.x, b.y + 0.5 * (b.h - b.icon:getHeight()),
0,
0.5, 0.5 )
end
if t.selected == b then
lg.rectangle( "fill", b.x, b.y, b.w, b.h, 6 )
end
end
return t.draw( b.next )
end
function t.select( b )
t.selected = b
end
function t.selectNext()
repeat t.selected = t.selected.next until t.selected.visible
end
function t.selectPrev()
repeat t.selected = t.selected.prev until t.selected.visible
end
function t.selectIn( x, y )
t.selected = t
repeat t.selected = t.selected.next until (t.selected == t) or (t.selected.visible and t.selected:contains( x, y ))
end
function t.deselect( b )
t.selected = t
end
setmetatable( t, t )
t.__index = t
t.__call = t.new
return t