From 1b572c67a53cef1b86363e8c7fcbf35082d64fc6 Mon Sep 17 00:00:00 2001 From: jill Date: Sun, 19 Sep 2021 14:14:38 +0300 Subject: [PATCH] better looking tooltips --- button.lua | 5 +++-- tooltips.lua | 22 +++++++++++++++++----- util.lua | 6 ++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/button.lua b/button.lua index e154c88..1de64e4 100644 --- a/button.lua +++ b/button.lua @@ -29,7 +29,8 @@ function self.createButtons() y = love.graphics.getHeight() - outerpadding - fontHeight * 4 - padding * 2 - 32, size = 32, name = 'clipboard', - displayname = 'Copy to Clipboard' + displayname = 'Copy to Clipboard', + tooltip = 'Copy to Clipboard' }) buttons = s @@ -69,7 +70,7 @@ function self.render() love.graphics.setColor(0, 0, 0, 1) if hovering or dragging then 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 love.graphics.rectangle('fill', x, y, w, h) love.graphics.setColor(1, 1, 1, 1) diff --git a/tooltips.lua b/tooltips.lua index 09a5d72..c16eaea 100644 --- a/tooltips.lua +++ b/tooltips.lua @@ -12,8 +12,8 @@ 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) + tooltipx = mix(tooltipx, mx, dt * 18) + tooltipy = mix(tooltipy, my, dt * 18) tooltipwidth = mix(tooltipwidth, tooltiptargetwidth, dt * 12) end @@ -28,6 +28,12 @@ function self.show(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 @@ -37,14 +43,20 @@ function self.render() 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(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.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.print(tooltiptext, 2 + margin/2, 2 + margin/2) diff --git a/util.lua b/util.lua index b6e5ccf..2e6c9a2 100644 --- a/util.lua +++ b/util.lua @@ -1,3 +1,9 @@ function mix(x, 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 \ No newline at end of file