box-of-eases/tooltips.lua

70 lines
1.7 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 * 18)
tooltipy = mix(tooltipy, my, dt * 18)
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
local function softlimit(x, f)
local sign = math.sign(x)
return sign * (f - (1.027 ^ (-10 * math.abs(x))) * f)
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)
local x, y, w, h = mx + 8, my + 8, tooltipwidth + 4 + margin, fontHeight + margin
local easiness = 3 -- hehe. magic numbers
local sx, sy = (w - softlimit(mx - tooltipx, w/easiness)/easiness) / w, (h - softlimit(my - tooltipy, h/easiness)/easiness) / h
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.scale(sx, sy)
love.graphics.setColor(0.2, 0.2, 0.3, a)
love.graphics.rectangle('fill', 0, 0, w, h)
love.graphics.setScissor(0, 0, math.max(mx + (tooltipwidth + 2 + margin/2 + 16) * sx, 0), 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