light_world.lua/examples/lib/list.lua

44 lines
1.1 KiB
Lua
Raw Permalink Normal View History

2021-11-10 01:15:14 +00:00
local List = {}
function List:new(x, y, w, h)
o = {
x = x, y = y, w = w, h = h,
items = {},
}
setmetatable(o, self)
self.__index = self
return o
end
2021-11-20 01:14:08 +00:00
function List:add(label, cb)
2021-11-10 01:15:14 +00:00
local x, y = self.x+2, self.y+((self.h+1)*(#self.items-1)+1)
local w, h = self.w-3, self.h
2021-11-20 01:14:08 +00:00
table.insert(self.items, {label = label, cb = cb, x = x, y = y, w = w, h = h, hover = false})
2021-11-10 01:15:14 +00:00
end
function List:update(dt)
local mx, my = love.mouse.getPosition()
for i, item in ipairs(self.items) do
item.hover = mx >= item.x and mx <= (item.x+item.w) and my >= item.y and my <= (item.y+item.h)
end
end
function List:mousepressed(mx, my, b)
for i, item in ipairs(self.items) do
if item.hover then return item.cb() end
end
end
function List:draw()
love.graphics.setFont(self.font)
love.graphics.setColor(0.18, 0.61, 1)
for i, item in ipairs(self.items) do
love.graphics.setColor(0, 0, 0, item.hover and 0.49 or 0.24)
love.graphics.rectangle("fill", item.x+1, item.y+1, item.w-3, item.h)
love.graphics.setColor(1, 1, 1, item.hover and 1 or 0.49)
2021-11-20 01:14:08 +00:00
love.graphics.print(item.label, item.x+10, item.y+6)
2021-11-10 01:15:14 +00:00
end
end
return List