box-of-eases/button.lua

81 lines
No EOL
1.8 KiB
Lua

local self = {}
local buttons = {}
function self.get(index)
return buttons[index]
end
function self.kget(key)
for _, v in ipairs(buttons) do
if v.name == key then
return v
end
end
end
local buttonId
local function insertButton(tab, f)
buttonId = buttonId + 1
f.press = (self.kget(f.name) or {press = 1}).press
return table.insert(tab, f)
end
function self.createButtons()
local s = {}
buttonId = 0
insertButton(s, {
x = outerpadding,
y = love.graphics.getHeight() - outerpadding - fontHeight * 4 - padding * 2 - 32,
size = 32,
name = 'clipboard',
displayname = 'Copy to Clipboard',
tooltip = 'Copy to Clipboard'
})
buttons = s
end
function self.update(dt)
for i, v in ipairs(buttons) do
local mx, my = love.mouse.getPosition()
local targetsize = 1
if mx > v.x and mx < v.x + v.size and my > v.y and my < v.y + v.size and dropdown.openDropdown == 0 then
if love.mouse.isDown(1) then
targetsize = 0.8
else
targetsize = 0.95
end
end
v.press = mix(v.press, targetsize, dt * 12)
end
end
function self.render()
local mx, my = love.mouse.getPosition()
for i, v in ipairs(buttons) do
local x, y, w, h = v.x, v.y, v.size, v.size
w = w * v.press
h = h * v.press
x = x + (v.size - w) / 2
y = y + (v.size - h) / 2
local hovering = mx > x and mx < x + w and my > y and my < y + h and dropdown.openDropdown == 0
local clicking = hovering and love.mouse.isDown(1)
love.graphics.setColor(0, 0, 0, 1)
if hovering or dragging then
love.graphics.setColor(0.2, 0.2, 0.3, 1)
if v.tooltip then tooltips.show(v.tooltip) end
end
love.graphics.rectangle('fill', x, y, w, h)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('line', x, y, w, h)
end
end
return self