add tooltip class

This commit is contained in:
jill 2021-09-19 13:56:48 +03:00
parent 57ac1247fa
commit 94305e5477
3 changed files with 65 additions and 2 deletions

View file

@ -24,7 +24,6 @@ function self.createButtons()
local s = {} local s = {}
buttonId = 0 buttonId = 0
--[[
insertButton(s, { insertButton(s, {
x = outerpadding, x = outerpadding,
y = love.graphics.getHeight() - outerpadding - fontHeight * 4 - padding * 2 - 32, y = love.graphics.getHeight() - outerpadding - fontHeight * 4 - padding * 2 - 32,
@ -32,7 +31,6 @@ function self.createButtons()
name = 'clipboard', name = 'clipboard',
displayname = 'Copy to Clipboard' displayname = 'Copy to Clipboard'
}) })
]]
buttons = s buttons = s
end end
@ -71,6 +69,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')
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,6 +12,7 @@ slider = require 'slider'
dropdown = require 'dropdown' dropdown = require 'dropdown'
graph = require 'graph' graph = require 'graph'
button = require 'button' button = require 'button'
tooltips = require 'tooltips'
modes = { modes = {
preview = 1, preview = 1,
@ -52,6 +53,7 @@ function love.update(dt)
slider.update(dt) slider.update(dt)
dropdown.update(dt) dropdown.update(dt)
button.update(dt) button.update(dt)
tooltips.update(dt)
end end
function love.draw() function love.draw()
@ -68,6 +70,8 @@ function love.draw()
love.graphics.setColor(0.2, 0.2, 0.3, 1) love.graphics.setColor(0.2, 0.2, 0.3, 1)
love.graphics.print('Box of Eases by oatmealine', outerpadding, sh - fontHeight - outerpadding) love.graphics.print('Box of Eases by oatmealine', outerpadding, sh - fontHeight - outerpadding)
tooltips.prerender()
button.render() button.render()
slider.render() slider.render()
@ -75,6 +79,8 @@ function love.draw()
dropdown.render() dropdown.render()
graph.render() graph.render()
tooltips.render()
end end
function love.mousepressed(x, y, m) function love.mousepressed(x, y, m)

58
tooltips.lua Normal file
View file

@ -0,0 +1,58 @@
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