box-of-eases/main.lua

253 lines
6.7 KiB
Lua
Raw Normal View History

2021-09-17 22:25:14 +00:00
local default_G = {}
for k, v in pairs(_G) do
table.insert(default_G, k)
end
2021-09-17 19:24:26 +00:00
local easelib = require 'easelib'
2021-09-17 22:25:14 +00:00
local dropdown = require 'dropdown'
2021-09-17 19:24:26 +00:00
2021-09-17 22:25:14 +00:00
-- utils
2021-09-17 19:24:26 +00:00
2021-09-17 22:25:14 +00:00
function mix(x, y, a)
2021-09-17 19:24:26 +00:00
return x * (1 - a) + y * a
end
-- eases
2021-09-17 22:25:14 +00:00
function mixEase(e1, e2, point)
2021-09-17 19:24:26 +00:00
if not point then point = 0.5 end
return function(a)
if a < point then
return e1(a / point) * point
else
return e2((a - point) / (1 - point)) * (1 - point) + point
end
end
end
2021-09-17 22:25:14 +00:00
eases = {}
2021-09-17 19:24:26 +00:00
for i,v in pairs(easelib) do
local min = 0
local q = 10
for i = 0, q do
local s = v[2](i / q)
if s < 0 then min = -1 end
end
eases[v[1]] = {
f = v[2],
max = 1,
min = min,
i = i
}
end
2021-09-17 22:25:14 +00:00
ease = nil
minEase = false
2021-09-17 19:24:26 +00:00
-- rendering constants
2021-09-17 22:25:14 +00:00
padding = 6
margin = 4
2021-09-17 19:24:26 +00:00
2021-09-17 22:25:14 +00:00
local quality = 256
2021-09-17 19:24:26 +00:00
local mixpoint = 0.5
local oldmixpoint = 0.5
local mixpointtimer = 0 -- easter egg thing
local mixpointspin = 0
-- graph
local graph = {}
-- dropdown bullshit
-- rendering
function love.load()
2021-09-17 22:25:14 +00:00
dropdown.createDropdowns()
2021-09-17 19:24:26 +00:00
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
mixpointtimer = mix(mixpointtimer + math.abs(mixpoint - oldmixpoint), 0, math.min(dt * 8))
oldmixpoint = mix(oldmixpoint, mixpoint, math.min(dt * 20, 1))
if mixpointtimer > 2 then
mixpointtimer = mixpointtimer - 2
mixpointspin = mixpointspin + 4
end
mixpointspin = mix(mixpointspin, 0, dt * 3)
2021-09-17 22:25:14 +00:00
dropdown.update(dt)
2021-09-17 19:24:26 +00:00
end
function love.draw()
2021-09-17 22:25:14 +00:00
local mode = dropdown.kget('mode').selected
2021-09-17 19:24:26 +00:00
local sw, sh = love.graphics.getDimensions()
local mx, my = love.mouse.getPosition()
love.graphics.setColor(0.09, 0.09, 0.12, 1)
love.graphics.rectangle('fill', 0, 0, sw, sh)
love.graphics.setColor(0.08, 0.08, 0.1, 1)
love.graphics.rectangle('line', 0, 0, sw, sh)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print('Box of Eases by oatmealine', padding, sh - love.graphics.getFont():getHeight() - padding)
-- sliders
-- yeah we do a lil' hardcoding
if mode == 2 then
local x, y, w, h = padding, padding * 2 + love.graphics.getFont():getHeight() + margin, 128, 32
love.graphics.setColor(0.7, 0.7, 0.7, 0.4)
love.graphics.line(x, y + h/2, x + w, y + h/2)
local sx, sy = x + w * oldmixpoint, y + h/2
local ssize = h * 0.5
love.graphics.push()
love.graphics.translate(sx, sy)
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
love.graphics.setColor(0.2, 0.2, 0.3, 1)
end
love.graphics.rectangle('fill', -ssize/2, -ssize/2, ssize, ssize)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('line', -ssize/2, -ssize/2, ssize, ssize)
love.graphics.rotate((mixpoint - oldmixpoint) * -2)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf(math.floor(mixpoint * 100)/100, -ssize * 6, ssize - 2, ssize * 12, 'center')
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
mixpoint = (mx - x) / w
createDropdowns()
end
end
-- dropdowns
2021-09-17 22:25:14 +00:00
dropdown.render()
2021-09-17 19:24:26 +00:00
-- graph
if mode == 1 or mode == 2 then
local csize = 10 -- preview point size
2021-09-17 22:25:14 +00:00
local size = math.min((sw - padding) - ((dropdown.kget('ease2') or dropdown.kget('ease1')).x + 128 + padding), sh - padding * 5 - csize)
2021-09-17 19:24:26 +00:00
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
function love.mousepressed(x, y, m)
2021-09-17 22:25:14 +00:00
if dropdown.mousepressed(x, y, m) then return end
2021-09-17 19:24:26 +00:00
end
function love.mousereleased(x, y, m)
2021-09-17 22:25:14 +00:00
if dropdown.mousereleased(x, y, m) then return end
2021-09-17 19:24:26 +00:00
end
function love.wheelmoved(x, y)
if y == 0 then return end
2021-09-17 22:25:14 +00:00
if dropdown.wheelmoved(x, y) then return end
end
function love.keypressed(key)
if key == 'f6' then -- print all globals
for k, v in pairs(_G) do
for _, g in ipairs(default_G) do
if g == k then goto continue end
2021-09-17 19:24:26 +00:00
end
2021-09-17 22:25:14 +00:00
print(k, v)
::continue::
2021-09-17 19:24:26 +00:00
end
end
2021-09-17 22:25:14 +00:00
end