2024-04-28 01:38:23 +00:00
|
|
|
local lg = love.graphics
|
2024-04-21 14:10:53 +00:00
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
local t = {
|
|
|
|
name = "",
|
|
|
|
tooltip = "button",
|
2024-04-29 02:44:18 +00:00
|
|
|
icon = false,
|
2024-04-28 01:38:23 +00:00
|
|
|
x = 8,
|
|
|
|
y = 250,
|
|
|
|
w = 176,
|
|
|
|
h = 24,
|
2024-04-29 01:21:32 +00:00
|
|
|
group = false,
|
2024-04-28 01:38:23 +00:00
|
|
|
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
|
2024-04-21 14:10:53 +00:00
|
|
|
end
|
|
|
|
|
2024-04-29 02:44:18 +00:00
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
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
|
2024-04-21 14:10:53 +00:00
|
|
|
end
|
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
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,
|
2024-05-02 00:32:07 +00:00
|
|
|
b.x + (b.icon and b.h or 0),
|
|
|
|
b.y + 0.5 * ( b.h - lg.getFont():getHeight() ),
|
|
|
|
b.w - (b.icon and b.h or 0),
|
2024-04-29 02:44:18 +00:00
|
|
|
"center" )
|
|
|
|
if b.icon then
|
|
|
|
local h = b.icon:getHeight()
|
|
|
|
lg.draw( b.icon,
|
|
|
|
b.x, b.y,
|
2024-04-28 01:38:23 +00:00
|
|
|
0,
|
2024-04-29 02:44:18 +00:00
|
|
|
b.h / h )
|
2024-04-28 01:38:23 +00:00
|
|
|
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 )
|
2024-04-21 14:10:53 +00:00
|
|
|
end
|
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
function t.select( b )
|
|
|
|
t.selected = b
|
2024-04-21 14:10:53 +00:00
|
|
|
end
|
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
function t.selectNext()
|
2024-04-29 02:44:18 +00:00
|
|
|
repeat t.selected = t.selected.next until (t.selected == t) or t.selected.visible
|
2024-04-28 01:38:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function t.selectPrev()
|
2024-04-29 02:44:18 +00:00
|
|
|
repeat t.selected = t.selected.prev until (t.selected == t) or t.selected.visible
|
2024-04-28 01:38:23 +00:00
|
|
|
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
|
|
|
|
|
2024-04-29 01:21:32 +00:00
|
|
|
function t.selectNextInGroup()
|
2024-04-29 02:44:18 +00:00
|
|
|
--make sure our group is visible, otherwise the loop doesn't end
|
|
|
|
local group = t.selected and t.selected.visible and t.selected.group
|
2024-04-29 01:21:32 +00:00
|
|
|
if not group then return t.selectNext() end
|
|
|
|
repeat t.selectNext() until group == t.selected.group
|
|
|
|
end
|
|
|
|
|
|
|
|
function t.selectPrevInGroup()
|
2024-04-29 02:44:18 +00:00
|
|
|
--make sure our group is visible, otherwise the loop doesn't end
|
|
|
|
local group = t.selected and t.selected.visible and t.selected.group
|
2024-04-29 01:21:32 +00:00
|
|
|
if not group then return t.selectPrev() end
|
|
|
|
repeat t.selectPrev() until group == t.selected.group
|
|
|
|
end
|
|
|
|
|
|
|
|
function t.displayGroup( group, show )
|
|
|
|
local b = t
|
|
|
|
repeat
|
|
|
|
b = b.next
|
|
|
|
b.visible = ( b.group == group )
|
|
|
|
until b == t
|
|
|
|
t.visible = true
|
|
|
|
end
|
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
function t.deselect( b )
|
|
|
|
t.selected = t
|
|
|
|
end
|
2024-04-21 14:10:53 +00:00
|
|
|
|
|
|
|
setmetatable( t, t )
|
2024-04-28 01:38:23 +00:00
|
|
|
t.__index = t
|
|
|
|
t.__call = t.new
|
|
|
|
|
2024-04-21 14:10:53 +00:00
|
|
|
return t
|