From 83d23d8d72147a4a13c336ceb5148658407254ed Mon Sep 17 00:00:00 2001 From: jill Date: Sat, 18 Sep 2021 01:34:16 +0300 Subject: [PATCH] split graph into its own module --- graph.lua | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 103 ++++++------------------------------------------------ 2 files changed, 106 insertions(+), 92 deletions(-) create mode 100644 graph.lua diff --git a/graph.lua b/graph.lua new file mode 100644 index 0000000..fc90ada --- /dev/null +++ b/graph.lua @@ -0,0 +1,95 @@ +local self = {} + +local quality = 256 +local graph = {} + +function self.update(dt) + for i = 1, quality do + local a = (i - 1) / (quality - 1) + if not graph[i] then + graph[i] = ease(a) + end + end + for i,v in ipairs(graph) do + local a = (i - 1) / (quality - 1) + local b = ease(a) + if minEase then + b = b / 2 + 0.5 + end + graph[i] = mix(v, b, math.min(dt * 18, 1)) + end +end + +function self.render() + local sw, sh = love.graphics.getDimensions() + + if mode == 1 or mode == 2 then + local csize = 10 -- preview point size + local size = math.min((sw - padding) - ((dropdown.kget('ease2') or dropdown.kget('ease1')).x + 128 + padding), sh - padding * 5 - csize) + + local x, y, w, h = sw - padding - size, padding, size, size + love.graphics.setColor(1, 1, 1, 1) + love.graphics.rectangle('line', x, y, w, h) + + -- grid + love.graphics.setColor(0.2, 0.2, 0.4, 0.2) + 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 + + -- mixease point + if mode == 2 then + love.graphics.setColor(1, 1, 1, 0.8) + love.graphics.line(x + margin + mixpoint * w, y, x + margin + mixpoint * w, y + h) + end + + -- preview point + local t = love.timer.getTime() % 1 + love.graphics.setColor(0.4, 0.4, 1, 0.4) + love.graphics.line(x + margin + t * w, y, x + margin + t * w, y + h) + + -- 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) + + -- 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 + for gx = 1, quality - 1 do + local a = gx/quality + local b = graph[gx + 1] or 0 + 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 + love.graphics.line(ox, oy, px, py) + end + last = b + end + + -- preview + love.graphics.setColor(1, 1, 1, 0.2) + love.graphics.line(x + margin, y + h + padding * 2 + csize/2, x + w - margin, y + h + padding * 2 + csize/2) + + love.graphics.setColor(0.4, 0.4, 1, 1) + local a1 = ease(t) + local a2 = ease(math.max(math.min(t - 0.1, 1), 0)) + local da = a1 + if love.timer.getTime() % 2 < 1 and math.floor(ease(0) + 0.5) ~= math.floor(ease(1) + 0.5) then + da = 1 - da + end + if minEase then + da = da / 2 + 0.5 + end + + 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) + end +end + +return self \ No newline at end of file diff --git a/main.lua b/main.lua index 7c072c9..0d95453 100644 --- a/main.lua +++ b/main.lua @@ -5,7 +5,8 @@ end local easelib = require 'easelib' -local dropdown = require 'dropdown' +dropdown = require 'dropdown' +graph = require 'graph' -- utils @@ -53,16 +54,13 @@ minEase = false padding = 6 margin = 4 -local quality = 256 -local mixpoint = 0.5 -local oldmixpoint = 0.5 +mixpoint = 0.5 +oldmixpoint = 0.5 local mixpointtimer = 0 -- easter egg thing local mixpointspin = 0 -- graph -local graph = {} - -- dropdown bullshit @@ -73,21 +71,9 @@ function love.load() end function love.update(dt) - for i = 1, quality do - local a = (i - 1) / (quality - 1) - if not graph[i] then - graph[i] = ease(a) - end - end - for i,v in ipairs(graph) do - local a = (i - 1) / (quality - 1) - local b = ease(a) - if minEase then - b = b / 2 + 0.5 - end - graph[i] = mix(v, b, math.min(dt * 18, 1)) - end + graph.update(dt) + -- slider mixpointtimer = mix(mixpointtimer + math.abs(mixpoint - oldmixpoint), 0, math.min(dt * 8)) oldmixpoint = mix(oldmixpoint, mixpoint, math.min(dt * 20, 1)) @@ -102,7 +88,7 @@ function love.update(dt) end function love.draw() - local mode = dropdown.kget('mode').selected + mode = dropdown.kget('mode').selected local sw, sh = love.graphics.getDimensions() local mx, my = love.mouse.getPosition() @@ -132,7 +118,7 @@ function love.draw() love.graphics.rotate((mixpoint - oldmixpoint) * 4 + mixpointspin * math.pi * 2) love.graphics.setColor(0, 0, 0, 1) - if mx > sx - ssize/2 and mx < sx + ssize/2 and my > sy - ssize/2 and my < sy + ssize/2 and openDropdown == 0 then + if mx > sx - ssize/2 and mx < sx + ssize/2 and my > sy - ssize/2 and my < sy + ssize/2 and dropdown.openDropdown == 0 then love.graphics.setColor(0.2, 0.2, 0.3, 1) end love.graphics.rectangle('fill', -ssize/2, -ssize/2, ssize, ssize) @@ -146,9 +132,9 @@ function love.draw() love.graphics.pop() - if mx > x and mx < x + w and my > y and my < y + h and love.mouse.isDown(1) and openDropdown == 0 then + if mx > x and mx < x + w and my > y and my < y + h and love.mouse.isDown(1) and dropdown.openDropdown == 0 then mixpoint = (mx - x) / w - createDropdowns() + dropdown.createDropdowns() end end @@ -156,74 +142,7 @@ function love.draw() dropdown.render() -- graph - - if mode == 1 or mode == 2 then - local csize = 10 -- preview point size - local size = math.min((sw - padding) - ((dropdown.kget('ease2') or dropdown.kget('ease1')).x + 128 + padding), sh - padding * 5 - csize) - - local x, y, w, h = sw - padding - size, padding, size, size - love.graphics.setColor(1, 1, 1, 1) - love.graphics.rectangle('line', x, y, w, h) - - -- grid - love.graphics.setColor(0.2, 0.2, 0.4, 0.2) - 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 - - -- mixease point - if mode == 2 then - love.graphics.setColor(1, 1, 1, 0.8) - love.graphics.line(x + margin + mixpoint * w, y, x + margin + mixpoint * w, y + h) - end - - -- preview point - local t = love.timer.getTime() % 1 - love.graphics.setColor(0.4, 0.4, 1, 0.4) - love.graphics.line(x + margin + t * w, y, x + margin + t * w, y + h) - - -- 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) - - -- 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 - for gx = 1, quality - 1 do - local a = gx/quality - local b = graph[gx + 1] or 0 - 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 - love.graphics.line(ox, oy, px, py) - end - last = b - end - - -- preview - love.graphics.setColor(1, 1, 1, 0.2) - love.graphics.line(x + margin, y + h + padding * 2 + csize/2, x + w - margin, y + h + padding * 2 + csize/2) - - love.graphics.setColor(0.4, 0.4, 1, 1) - local a1 = ease(t) - local a2 = ease(math.max(math.min(t - 0.1, 1), 0)) - local da = a1 - if love.timer.getTime() % 2 < 1 and math.floor(ease(0) + 0.5) ~= math.floor(ease(1) + 0.5) then - da = 1 - da - end - if minEase then - da = da / 2 + 0.5 - end - - 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) - end + graph.render() end function love.mousepressed(x, y, m)