125 lines
2.9 KiB
Lua
125 lines
2.9 KiB
Lua
local lg = love.graphics
|
|
|
|
local t = {
|
|
name = "",
|
|
tooltip = "",
|
|
icon = false,
|
|
lit = false,
|
|
x = 8,
|
|
y = 250,
|
|
w = 13 * 28 - 4,
|
|
h = 24,
|
|
group = false,
|
|
visible = false,
|
|
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
|
|
|
|
function t.highlight( b )
|
|
lg.rectangle( "fill", b.x, b.y, b.w, b.h )
|
|
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 )
|
|
lg.printf( b.name,
|
|
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),
|
|
"center" )
|
|
if b.icon then
|
|
local h = b.icon:getHeight()
|
|
lg.draw( b.icon,
|
|
b.x, b.y,
|
|
0,
|
|
b.h / h )
|
|
end
|
|
if b.lit or t.selected == b then
|
|
b:highlight()
|
|
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 == t) or t.selected.visible
|
|
end
|
|
|
|
function t.selectPrev()
|
|
repeat t.selected = t.selected.prev until (t.selected == t) or 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.selectNextInGroup()
|
|
--make sure our group is visible, otherwise the loop doesn't end
|
|
local group = t.selected
|
|
group = group and t.selected.visible
|
|
group = group and t.selected.group
|
|
if not group then return t.selectNext() end
|
|
repeat t.selectNext() until group == t.selected.group
|
|
end
|
|
|
|
function t.selectPrevInGroup()
|
|
--make sure our group is visible, otherwise the loop doesn't end
|
|
local group = t.selected and t.selected.visible and t.selected.group
|
|
if not group then return t.selectPrev() end
|
|
repeat t.selectPrev() until group == t.selected.group
|
|
end
|
|
|
|
--show/hide all buttons in a group
|
|
--passing hide=true will hide the group, hide=false or nil will show the group
|
|
--solo=true will hide all buttons outside the group
|
|
function t.displayGroup( group, hide, solo )
|
|
local b = t
|
|
repeat
|
|
b = b.next
|
|
local inGroup = (group == b.group)
|
|
if solo or inGroup then
|
|
b.visible = not(hide) and inGroup
|
|
end
|
|
until b == t
|
|
t.visible = true
|
|
end
|
|
|
|
function t.mousepressed( x, y )
|
|
if t.selected and t.selected:contains( x, y ) then
|
|
return t.selected:callback()
|
|
end
|
|
end
|
|
|
|
function t.deselect( b )
|
|
t.selected = t
|
|
end
|
|
|
|
setmetatable( t, t )
|
|
t.__index = t
|
|
t.__call = t.new
|
|
|
|
return t |