box-of-eases/src/graph.lua

172 lines
6.0 KiB
Lua
Raw Normal View History

2021-09-17 22:34:16 +00:00
local self = {}
local quality = 256
local graph = {}
2021-09-17 23:15:31 +00:00
self.touchtimer = 0
2021-09-19 16:18:32 +00:00
self.zoom = 1
local zoome = 1
2021-09-17 23:15:31 +00:00
2021-09-17 23:31:20 +00:00
local timer = 0
2021-09-17 22:34:16 +00:00
function self.update(dt)
for i = 1, quality do
local a = (i - 1) / (quality - 1)
if not graph[i] then
2021-09-17 22:44:26 +00:00
graph[i] = ease.ease(a)
2021-09-17 22:34:16 +00:00
end
end
for i,v in ipairs(graph) do
local a = (i - 1) / (quality - 1)
2021-09-17 22:44:26 +00:00
local b = ease.ease(a)
2021-09-17 22:34:16 +00:00
if minEase then
b = b / 2 + 0.5
end
graph[i] = mix(v, b, math.min(dt * 18, 1))
2021-09-18 00:48:03 +00:00
if graph[i] ~= graph[i] then -- is nan
graph[i] = b
end
2021-09-17 22:34:16 +00:00
end
2021-09-17 23:15:31 +00:00
self.touchtimer = mix(self.touchtimer, 0, dt * 2)
2021-09-17 23:31:20 +00:00
-- lots of math just for the timer
-- 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
local mx, my = getMousePosition()
2021-09-17 23:31:20 +00:00
if not (love.mouse.isDown(1) and mx > x and mx < x + w and my > y and my < y + h) then
2021-09-18 10:57:23 +00:00
timer = (timer + dt * ((slider.kvalue('bpm') or 120)/120)) % 2
2021-09-17 23:31:20 +00:00
else
timer = mix(timer, (mx - x) / w, dt * 14)
end
2021-09-19 16:18:32 +00:00
zoome = mix(zoome, self.zoom, dt * 6)
2021-09-17 22:34:16 +00:00
end
function self.render()
local sw, sh = love.graphics.getDimensions()
2021-09-19 10:16:27 +00:00
if mode == modes.preview or mode == modes.mix or mode == modes.multiply then
2021-09-17 22:34:16 +00:00
local csize = 10 -- preview point size
2021-09-17 22:51:42 +00:00
local size = math.min((sw - outerpadding) - ((dropdown.kget('ease2') or dropdown.kget('ease1')).x + dropdownWidth + padding), sh - outerpadding * 2 - padding * 3 - csize)
2021-09-17 22:38:04 +00:00
2021-09-19 18:20:22 +00:00
if screenshot then
size = screenshotsize - outerpadding * 2
end
2021-09-17 22:51:42 +00:00
local x, y, w, h = sw - outerpadding - size, outerpadding, size, size
2021-09-19 18:20:22 +00:00
if screenshot then
x, y = outerpadding, outerpadding
end
2021-09-17 22:34:16 +00:00
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('line', x, y, w, h)
2021-09-17 22:38:04 +00:00
2021-09-17 22:34:16 +00:00
-- grid
2021-09-19 19:14:06 +00:00
love.graphics.setColor(0.2, 0.2, 0.3, 0.2)
2021-09-17 22:34:16 +00:00
local gridsize = 64
for gx = 1, gridsize - 2 do
love.graphics.line(x + margin + gx * w/gridsize, y + margin, x + margin + gx * w/gridsize, y + h - margin)
end
for gy = 1, gridsize - 2 do
love.graphics.line(x + margin, y + margin + gy * h/gridsize, x + w - margin, y + margin + gy * h/gridsize)
end
2021-09-17 22:38:04 +00:00
2021-09-17 22:34:16 +00:00
-- mixease point
2021-09-18 22:11:44 +00:00
if mode == modes.mix and slider.kget('mix') then
2021-09-17 23:15:31 +00:00
love.graphics.setColor(1, 1, 1, 0.2 + self.touchtimer * 0.6)
love.graphics.line(x + slider.kvalue('mix') * w, y, x + slider.kvalue('mix') * w, y + h)
2021-09-17 22:34:16 +00:00
end
2021-09-17 22:38:04 +00:00
2021-09-17 22:34:16 +00:00
-- preview point
2021-09-17 23:31:20 +00:00
local t = timer % 1
2021-09-19 18:20:22 +00:00
if not screenshot then
love.graphics.setColor(0.4, 0.4, 1, 0.4)
love.graphics.line(x + t * w, y, x + margin + t * (w - margin * 2), y + h)
end
2021-09-17 22:38:04 +00:00
2021-09-17 22:34:16 +00:00
-- y = 0 point
-- todo: this will break with eases that dont have the first point at y0
local py = graph[1]
love.graphics.setColor(0.7, 0.7, 0.7, 0.4 * (1 - math.abs(py - 0.5) / 0.5))
love.graphics.line(x, y + h - py * h, x + w, y + py * h)
2021-09-17 22:38:04 +00:00
2021-09-17 22:34:16 +00:00
-- 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)
2021-09-20 06:33:51 +00:00
love.graphics.setScissor(x + margin, y, math.abs(w - (margin * 2)), math.abs(h))
2021-09-19 16:18:32 +00:00
local last = (((graph[1] or 0) - 0.5) * zoome) + 0.5
2021-09-17 22:34:16 +00:00
for gx = 1, quality - 1 do
local a = gx/quality
local b = graph[gx + 1] or 0
2021-09-19 16:18:32 +00:00
b = ((b - 0.5) * zoome) + 0.5
2021-09-19 18:34:14 +00:00
local px, py = x + margin + gx * ((w - margin * 2)/quality), y + h - margin - b * (h - margin * 2)
local ox, oy = x + margin + (gx - 1) * ((w - margin * 2)/quality), y + h - margin - last * (h - margin * 2)
2021-09-17 22:34:16 +00:00
if math.abs(b - last) < 1 then
love.graphics.line(ox, oy, px, py)
end
last = b
end
2021-09-19 18:34:14 +00:00
love.graphics.setScissor()
2021-09-17 22:38:04 +00:00
2021-09-19 16:18:32 +00:00
-- 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
2021-09-17 22:34:16 +00:00
-- preview
2021-09-19 18:20:22 +00:00
if not screenshot then
2021-09-19 19:47:01 +00:00
love.graphics.setScissor(x - margin, 0, math.abs(w + margin * 2), sh)
2021-09-19 18:20:22 +00:00
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 - 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
if minEase then
da = da / 2 + 0.5
end
2021-09-17 22:38:04 +00:00
2021-09-19 18:20:22 +00:00
love.graphics.ellipse('fill', x + margin + (w - margin * 2) * da, y + h + padding * 2 + csize/2, csize * (1 + math.min(math.abs(a1 - a2), 3) * 1.2), csize)
2021-09-19 18:34:14 +00:00
love.graphics.setScissor()
2021-09-19 18:20:22 +00:00
end
2021-09-17 22:34:16 +00:00
end
end
2021-09-19 16:18:32 +00:00
function self.wheelmoved(wx, wy)
local mx, my = getMousePosition()
2021-09-19 16:18:32 +00:00
-- 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
2021-09-17 22:34:16 +00:00
return self