box-of-eases/tooltips.lua

58 lines
1.3 KiB
Lua

local self = {}
local tooltipwidth = 0
local tooltipx = 0
local tooltipy = 0
local tooltipopen = false
local tooltiptext = ''
local tooltiptargetwidth = 0
local tooltipframe = false
function self.update(dt)
local mx, my = love.mouse.getPosition()
tooltipx = mix(tooltipx, mx, dt * 14)
tooltipy = mix(tooltipy, my, dt * 14)
tooltipwidth = mix(tooltipwidth, tooltiptargetwidth, dt * 12)
end
function self.prerender()
tooltipframe = false
end
function self.show(text)
tooltipframe = true
tooltiptext = text
tooltiptargetwidth = love.graphics.newText(love.graphics.getFont(), text):getWidth()
end
function self.render()
local mx, my = love.mouse.getPosition()
if not tooltipframe then
tooltiptargetwidth = 0
end
if tooltipwidth > 1 then
local a = math.min((tooltipwidth - 1) / 6, 1)
love.graphics.push()
love.graphics.translate(mx + 16, my + 16)
love.graphics.setColor(0.2, 0.2, 0.3, a)
love.graphics.rectangle('fill', 0, 0, tooltipwidth + 4 + margin, fontHeight + margin)
love.graphics.setScissor(0, 0, mx + tooltipwidth + 2 + margin/2 + 16, love.graphics.getHeight())
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print(tooltiptext, 2 + margin/2, 2 + margin/2)
love.graphics.setScissor()
love.graphics.pop()
end
end
return self