add graph zoom

This commit is contained in:
jill 2021-09-19 19:18:32 +03:00
parent 261d8d6846
commit 7e303c86b7
2 changed files with 42 additions and 3 deletions

View File

@ -99,6 +99,7 @@ function love.wheelmoved(x, y)
if y == 0 then return end
if dropdown.wheelmoved(x, y) then return end
graph.wheelmoved(x, y)
end
function love.keypressed(key)

View File

@ -4,6 +4,9 @@ local quality = 256
local graph = {}
self.touchtimer = 0
self.zoom = 1
local zoome = 1
local timer = 0
@ -41,6 +44,8 @@ function self.update(dt)
else
timer = mix(timer, (mx - x) / w, dt * 14)
end
zoome = mix(zoome, self.zoom, dt * 6)
end
function self.render()
@ -84,10 +89,11 @@ function self.render()
-- polygone
-- this isnt done with a polygon because else itd waste a Bunch of ram and i kinda, dont want to do that?
love.graphics.setColor(1, 1, 1, 1)
local last = graph[1] or 0
local last = (((graph[1] or 0) - 0.5) * zoome) + 0.5
for gx = 1, quality - 1 do
local a = gx/quality
local b = graph[gx + 1] or 0
b = ((b - 0.5) * zoome) + 0.5
local px, py = x + margin + gx * ((w - margin)/quality), y + h - margin - b * (h - margin * 2)
local ox, oy = x + margin + (gx - 1) * ((w - margin)/quality), y + h - margin - last * (h - margin * 2)
if math.abs(b - last) < 1 then
@ -96,14 +102,29 @@ function self.render()
last = b
end
-- zoom lines
local a = math.min(math.abs(zoome - 1) * 10, 1)
if a > 0 then
love.graphics.setColor(1, 1, 1, a * 0.2)
local ytop = y + h/2 - h * zoome * 0.5
local ybot = y + h/2 + h * zoome * 0.5
love.graphics.line(x, ytop, x + w, ytop)
love.graphics.line(x, ybot, x + w, ybot)
love.graphics.setColor(1, 1, 1, a * 0.3)
love.graphics.print('y=1', x, ytop)
love.graphics.print('y=0', x, ybot)
end
-- preview
love.graphics.setColor(1, 1, 1, 0.2)
love.graphics.setColor(0.16, 0.16, 0.17, 1)
love.graphics.line(x + margin, y + h + padding * 2 + csize/2, x + w - margin, y + h + padding * 2 + csize/2)
love.graphics.setColor(0.3, 0.3, 0.31, 1)
love.graphics.line(x + margin + (w - margin * 2) * (1 - zoome) * 0.5, y + h + padding * 2 + csize/2, x + w - margin - (w - margin * 2) * (1 - zoome) * 0.5, y + h + padding * 2 + csize/2)
love.graphics.setColor(0.4, 0.4, 1, 1)
local a1 = ease.ease(t)
local a2 = ease.ease(math.max(math.min(t - 0.1, 1), 0))
local da = a1
local da = ((a1 - 0.5) * zoome) + 0.5
if timer % 2 > 1 and math.floor(ease.ease(0) + 0.5) ~= math.floor(ease.ease(1) + 0.5) then
da = 1 - da
end
@ -115,4 +136,21 @@ function self.render()
end
end
function self.wheelmoved(wx, wy)
local mx, my = love.mouse.getPosition()
-- may need to abstract this out, somehow
-- todo
local sw, sh = love.graphics.getDimensions()
local csize = 10 -- preview point size
local size = math.min((sw - outerpadding) - ((dropdown.kget('ease2') or dropdown.kget('ease1')).x + dropdownWidth + padding), sh - outerpadding * 2 - padding * 3 - csize)
local x, y, w, h = sw - outerpadding - size, outerpadding, size, size
if mx > x and mx < x + w and my > y and my < y + h then
self.zoom = self.zoom - wy * 0.1
self.zoom = math.min(self.zoom, 1)
self.zoom = math.max(self.zoom, 0.1)
end
end
return self