box-of-eases/slider.lua

167 lines
4.7 KiB
Lua
Raw Normal View History

2021-09-18 00:05:13 +00:00
local self = {}
local sliders = {}
function self.get(index)
return sliders[index]
end
function self.value(index)
return sliders[index].value
end
function self.kget(key)
for _, v in ipairs(sliders) do
if v.name == key then
return v
end
end
end
function self.kvalue(key)
2021-09-18 00:48:03 +00:00
return self.kget(key) and self.kget(key).oldvalue
2021-09-18 00:05:13 +00:00
end
local sliderId
local function insertSlider(tab, f)
sliderId = sliderId + 1
f.value = (self.kget(f.name) or {value = f.default}).value
f.oldvalue = (self.kget(f.name) or {oldvalue = f.value}).oldvalue
f.spintimer = (self.kget(f.name) or {spintimer = 0}).spintimer
f.spin = (self.kget(f.name) or {spin = 0}).spin
return table.insert(tab, f)
end
function self.createSliders()
local s = {}
sliderId = 0
if mode == 2 then -- mix eases
insertSlider(s, {
x = outerpadding,
2021-09-18 00:48:03 +00:00
y = outerpadding + fontHeight * 2.5 + padding,
2021-09-18 00:05:13 +00:00
width = dropdownWidth,
min = 0,
max = 1,
default = 0.5,
name = 'mix',
displayname = 'Mix'
})
end
2021-09-18 00:48:03 +00:00
local ease1 = ease.eases[dropdown.kselected('ease1')]
local ease2 = ease.eases[dropdown.kselected('ease2')]
local param1 = ease1 and ease1.params
local param2 = ease2 and ease2.params
if param1 then
for i,v in ipairs(param1) do
insertSlider(s, {
x = outerpadding + dropdownWidth + padding,
y = outerpadding + (fontHeight * 2.5 + padding) * i,
width = dropdownWidth,
min = v.min,
max = v.max,
default = v.default,
name = ease1.name .. 'param1' .. i,
2021-09-18 00:48:03 +00:00
displayname = 'Parameter ' .. v.name
})
end
end
if param2 then
for i,v in ipairs(param2) do
insertSlider(s, {
x = outerpadding + dropdownWidth + padding + dropdownWidth + padding,
y = outerpadding + (fontHeight * 2.5 + padding) * i,
width = dropdownWidth,
min = v.min,
max = v.max,
default = v.default,
name = ease2.name .. 'param2' .. i,
2021-09-18 00:48:03 +00:00
displayname = 'Parameter ' .. v.name
})
end
end
2021-09-18 00:05:13 +00:00
sliders = s
end
2021-09-18 00:48:03 +00:00
local function normalize(a, min, max)
return (a - min) / (max - min)
end
2021-09-18 00:05:13 +00:00
function self.update(dt)
for i, v in ipairs(sliders) do
2021-09-18 00:48:03 +00:00
v.spintimer = mix(v.spintimer + math.abs(normalize(v.value, v.min, v.max) - normalize(v.oldvalue, v.min, v.max)), 0, math.min(dt * 8))
2021-09-18 00:05:13 +00:00
v.oldvalue = mix(v.oldvalue, v.value, math.min(dt * 20, 1))
if v.spintimer > 2 then
v.spintimer = v.spintimer - 2
v.spin = v.spin + 4
end
v.spin = mix(v.spin, 0, dt * 3)
end
end
function self.render()
local mx, my = love.mouse.getPosition()
for i, v in ipairs(sliders) do
local x, y, w, h = v.x, v.y, v.width, 32
love.graphics.setColor(0.7, 0.7, 0.7, 0.4)
love.graphics.line(x, y + h/2, x + w, y + h/2)
2021-09-18 00:48:03 +00:00
local normalvalue = normalize(v.value, v.min, v.max)
local normaloldvalue = normalize(v.oldvalue, v.min, v.max)
2021-09-18 01:20:46 +00:00
local normaldefault = normalize(v.default, v.min, v.max)
love.graphics.setColor(0.7, 0.7, 0.7, 0.3)
love.graphics.line(x + normaldefault * w, y, x + normaldefault * w, y + h)
2021-09-18 00:48:03 +00:00
local sx, sy = x + w * normaloldvalue, y + h/2
2021-09-18 00:05:13 +00:00
local ssize = h * 0.5
love.graphics.push()
love.graphics.translate(sx, sy)
2021-09-18 00:48:03 +00:00
love.graphics.rotate((normalvalue - normaloldvalue) * 4 + v.spin * math.pi * 2)
2021-09-18 00:05:13 +00:00
local hovering = mx > sx - ssize/2 and mx < sx + ssize/2 and my > sy - ssize/2 and my < sy + ssize/2 and dropdown.openDropdown == 0
local dragging = mx > x and mx < x + w and my > y and my < y + h and love.mouse.isDown(1) and dropdown.openDropdown == 0
love.graphics.setColor(0, 0, 0, 1)
if hovering or dragging 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)
2021-09-18 00:48:03 +00:00
love.graphics.rotate((normalvalue - normaloldvalue) * -2)
2021-09-18 00:05:13 +00:00
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf(math.floor(v.value * 100)/100, -ssize * 6, ssize - 2, ssize * 12, 'center')
love.graphics.pop()
2021-09-18 00:48:03 +00:00
love.graphics.printf(v.displayname, v.x + margin * 2 - ssize * 6, v.y - 8, ssize * 12, 'center')
2021-09-18 00:05:13 +00:00
if dragging then
2021-09-18 00:54:51 +00:00
v.value = ((mx - (x + 1)) / (w - 2)) * (v.max - v.min) + v.min
2021-09-18 00:48:03 +00:00
if v.name == 'mix' then graph.touchtimer = 1 end -- sorry !!!
2021-09-18 00:05:13 +00:00
createUI()
end
2021-09-18 01:20:46 +00:00
if mx > x and mx < x + w and my > y and my < y + h and love.mouse.isDown(2) and dropdown.openDropdown == 0 then
v.value = v.default
createUI()
end
if mx > x and mx < x + w and my > y and my < y + h and love.mouse.isDown(3) and dropdown.openDropdown == 0 then
v.value = v.min + math.random() * (v.max - v.min)
createUI()
end
2021-09-18 00:05:13 +00:00
end
end
return self