better looking tooltips

This commit is contained in:
jill 2021-09-19 14:14:38 +03:00
parent 94305e5477
commit 1b572c67a5
3 changed files with 26 additions and 7 deletions

View File

@ -29,7 +29,8 @@ function self.createButtons()
y = love.graphics.getHeight() - outerpadding - fontHeight * 4 - padding * 2 - 32, y = love.graphics.getHeight() - outerpadding - fontHeight * 4 - padding * 2 - 32,
size = 32, size = 32,
name = 'clipboard', name = 'clipboard',
displayname = 'Copy to Clipboard' displayname = 'Copy to Clipboard',
tooltip = 'Copy to Clipboard'
}) })
buttons = s buttons = s
@ -69,7 +70,7 @@ function self.render()
love.graphics.setColor(0, 0, 0, 1) love.graphics.setColor(0, 0, 0, 1)
if hovering or dragging then if hovering or dragging then
love.graphics.setColor(0.2, 0.2, 0.3, 1) love.graphics.setColor(0.2, 0.2, 0.3, 1)
tooltips.show('Copy to Clipboard') if v.tooltip then tooltips.show(v.tooltip) end
end end
love.graphics.rectangle('fill', x, y, w, h) love.graphics.rectangle('fill', x, y, w, h)
love.graphics.setColor(1, 1, 1, 1) love.graphics.setColor(1, 1, 1, 1)

View File

@ -12,8 +12,8 @@ local tooltipframe = false
function self.update(dt) function self.update(dt)
local mx, my = love.mouse.getPosition() local mx, my = love.mouse.getPosition()
tooltipx = mix(tooltipx, mx, dt * 14) tooltipx = mix(tooltipx, mx, dt * 18)
tooltipy = mix(tooltipy, my, dt * 14) tooltipy = mix(tooltipy, my, dt * 18)
tooltipwidth = mix(tooltipwidth, tooltiptargetwidth, dt * 12) tooltipwidth = mix(tooltipwidth, tooltiptargetwidth, dt * 12)
end end
@ -28,6 +28,12 @@ function self.show(text)
tooltiptargetwidth = love.graphics.newText(love.graphics.getFont(), text):getWidth() tooltiptargetwidth = love.graphics.newText(love.graphics.getFont(), text):getWidth()
end 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() function self.render()
local mx, my = love.mouse.getPosition() local mx, my = love.mouse.getPosition()
if not tooltipframe then if not tooltipframe then
@ -37,14 +43,20 @@ function self.render()
if tooltipwidth > 1 then if tooltipwidth > 1 then
local a = math.min((tooltipwidth - 1) / 6, 1) 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.push()
love.graphics.translate(mx + 16, my + 16) love.graphics.translate(x, y)
love.graphics.scale(sx, sy)
love.graphics.setColor(0.2, 0.2, 0.3, a) love.graphics.setColor(0.2, 0.2, 0.3, a)
love.graphics.rectangle('fill', 0, 0, tooltipwidth + 4 + margin, fontHeight + margin) love.graphics.rectangle('fill', 0, 0, w, h)
love.graphics.setScissor(0, 0, mx + tooltipwidth + 2 + margin/2 + 16, love.graphics.getHeight()) 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.setColor(1, 1, 1, 1)
love.graphics.print(tooltiptext, 2 + margin/2, 2 + margin/2) love.graphics.print(tooltiptext, 2 + margin/2, 2 + margin/2)

View File

@ -1,3 +1,9 @@
function mix(x, y, a) function mix(x, y, a)
return x * (1 - a) + y * a return x * (1 - a) + y * a
end
function math.sign(x)
if x < 0 then return -1 end
if x > 0 then return 1 end
return 0
end end