box-of-eases/dropdown.lua

234 lines
7.1 KiB
Lua
Raw Normal View History

2021-09-17 22:25:14 +00:00
local self = {}
local dropdowns = {}
local dropdownValueCache = {}
local maxDropdown = 16
self.openDropdown = 0
local dropdownScroll = 0
local dropdownScrollE = 0
local function skeys(t)
local k = {}
for n,v in pairs(t) do table.insert(k, {n, v}) end
table.sort(k, function(a, b) return a[2].i < b[2].i end)
local k2 = {}
for _,v in ipairs(k) do table.insert(k2, v[1]) end
return k2
end
function self.get(index)
return dropdowns[index]
end
function self.selected(index)
return dropdowns[index].options[dropdowns[index].selected]
end
function self.kget(key)
for _, v in ipairs(dropdowns) do
if v.name == key then
return v
end
end
end
function self.kselected(key)
for _, v in ipairs(dropdowns) do
if v.name == key then
return v.options[v.selected]
end
end
end
local dropdownId
local function insertDropdown(tab, f)
dropdownId = dropdownId + 1
f.selected = (self.kget(f.name) or dropdownValueCache[f.name] or {selected = 1}).selected
f.selected = (f.selected - 1) % #f.options + 1
return table.insert(tab, f)
end
function self.createDropdowns()
local d = {}
dropdownId = 0
insertDropdown(d, {
2021-09-17 22:51:42 +00:00
x = outerpadding,
y = outerpadding,
width = dropdownWidth,
2021-09-17 22:25:14 +00:00
options = {
'Preview Ease',
'Mix Eases',
'Create Ease'
},
name = 'mode'
})
if d[dropdownId].selected == 1 then -- preview ease
insertDropdown(d, {
2021-09-17 22:51:42 +00:00
x = outerpadding + dropdownWidth + padding,
y = outerpadding,
width = dropdownWidth,
2021-09-17 22:44:26 +00:00
options = skeys(ease.eases),
2021-09-17 22:25:14 +00:00
name = 'ease1'
})
2021-09-17 22:44:26 +00:00
ease.ease = ease.eases[d[dropdownId].options[d[dropdownId].selected]].f
2021-09-17 22:25:14 +00:00
elseif d[dropdownId].selected == 2 then -- mix eases
insertDropdown(d, {
2021-09-17 22:51:42 +00:00
x = outerpadding + dropdownWidth + padding,
y = outerpadding,
width = dropdownWidth,
2021-09-17 22:44:26 +00:00
options = skeys(ease.eases),
2021-09-17 22:25:14 +00:00
name = 'ease1'
})
insertDropdown(d, {
2021-09-17 22:51:42 +00:00
x = outerpadding + dropdownWidth + padding + dropdownWidth + padding,
y = outerpadding,
width = dropdownWidth,
2021-09-17 22:44:26 +00:00
options = skeys(ease.eases),
2021-09-17 22:25:14 +00:00
name = 'ease2'
})
2021-09-17 22:44:26 +00:00
ease.ease = ease.mixEase(ease.eases[d[dropdownId - 1].options[d[dropdownId - 1].selected]].f, ease.eases[d[dropdownId].options[d[dropdownId].selected]].f, mixpoint)
2021-09-17 22:25:14 +00:00
elseif d[dropdownId].selected == 3 then -- create eases
insertDropdown(d, {
2021-09-17 22:51:42 +00:00
x = outerpadding + dropdownWidth + padding,
y = outerpadding,
width = dropdownWidth,
2021-09-17 22:44:26 +00:00
options = skeys(ease.eases),
2021-09-17 22:25:14 +00:00
name = 'ease1'
})
end
dropdowns = d
2021-09-17 22:44:26 +00:00
minEase = (self.kselected('ease1') and ease.eases[self.kselected('ease1')].min == -1) or (self.kselected('ease2') and ease.eases[self.kselected('ease2')].min == -1)
2021-09-17 22:25:14 +00:00
end
function self.update(dt)
if self.openDropdown ~= 0 then
dropdownScroll = math.max(dropdownScroll, -(#self.get(self.openDropdown).options - maxDropdown + 1))
2021-09-17 22:25:14 +00:00
dropdownScroll = math.min(dropdownScroll, 0)
dropdownScrollE = mix(dropdownScrollE, dropdownScroll, dt * 10)
end
end
function self.render()
local mx, my = love.mouse.getPosition()
for i,v in ipairs(dropdowns) do
local x, y, w, h = v.x, v.y, v.width, love.graphics.getFont():getHeight() + margin
2021-09-17 22:38:04 +00:00
love.graphics.setColor(0.06, 0.06, 0.12, 0.3)
2021-09-17 22:25:14 +00:00
if love.mouse.getX() > x and love.mouse.getX() < x + w and love.mouse.getY() > y and love.mouse.getY() < y + h then
love.graphics.setColor(0.8, 0.8, 1, love.mouse.isDown(1) and 0.4 or 0.3)
end
love.graphics.rectangle('fill', x, y, w, h)
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
love.graphics.setColor(1, 1, 1, 1)
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
love.graphics.rectangle('line', x, y, w, h)
love.graphics.print(self.selected(i), x + margin/2, y + margin/2)
love.graphics.rectangle('line', x + w - h, y, h, h)
love.graphics.polygon('line', x + w - h/2 + 0.3 * h, y + h/2 - 0.3 * h, x + w - h/2 - 0.3 * h, y + h/2 - 0.3 * h, x + w - h/2, y + h/2 + 0.3 * h)
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
if self.openDropdown == i then
for i,o in ipairs(v.options) do
local x, y, w, h = x, y + i * h, w, h
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
y = y + dropdownScrollE * h
local gi = y / h
if gi > (maxDropdown + 1) or gi < 1 then
2021-09-17 22:25:14 +00:00
goto continue
end
2021-09-17 22:38:04 +00:00
-- help
local a = (1 - math.min(math.max((1 - (maxDropdown - gi)) * (1 - (math.abs(dropdownScrollE) / (#v.options - maxDropdown + 1))), 0), 1)) * math.max(math.min(gi - 1, 1), 0)
2021-09-17 22:38:04 +00:00
love.graphics.setColor(0.06, 0.06, 0.12, 0.3 * a)
2021-09-17 22:25:14 +00:00
if mx > x and mx < x + w and my > y and my < y + h then
love.graphics.setColor(0.8, 0.8, 1, (love.mouse.isDown(1) and 0.4 or 0.3) * a)
end
love.graphics.rectangle('fill', x, y, w, h)
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
love.graphics.setColor(1, 1, 1, 0.75 * a)
love.graphics.rectangle('line', x, y, w, h)
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
love.graphics.setColor(1, 1, 1, 1 * a)
love.graphics.print(v.options[i], x + 2, y + 2)
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
::continue::
end
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
-- scrollwheel
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
if #v.options > maxDropdown then
local displayed = maxDropdown / (#v.options)
local scroll = math.abs(dropdownScrollE) / (#v.options - maxDropdown + 1)
2021-09-17 22:25:14 +00:00
local size = margin
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
love.graphics.setColor(1, 1, 1, 0.9)
love.graphics.rectangle('fill', x + w - size, y + h + scroll * (1 - displayed) * (maxDropdown - 1) * h, size, displayed * (maxDropdown - 1) * h)
end
end
end
end
function self.mousepressed(x, y, m)
local clickedDropdown = false
for i,v in ipairs(dropdowns) do
local h = love.graphics.getFont():getHeight() + margin
if self.openDropdown == 0 then
if x > v.x and x < v.x + v.width and y > v.y and y < v.y + h + margin then
if m == 1 then
self.openDropdown = i
clickedDropdown = true
dropdownScroll = 0
dropdownScrollE = 0
elseif m == 3 then
dropdowns[i].selected = math.random(1, #dropdowns[i].options)
self.createDropdowns()
end
end
elseif self.openDropdown == i then
if x > v.x and x < v.x + v.width and y > v.y + h and y < v.y + h * (math.min(#v.options, maxDropdown) + 1) and m == 1 then
clickedDropdown = true
end
end
end
2021-09-17 22:38:04 +00:00
2021-09-17 22:25:14 +00:00
if not clickedDropdown and m == 1 then
self.openDropdown = 0
return true
end
end
function self.mousereleased(x, y, m)
for i,v in ipairs(dropdowns) do
local h = love.graphics.getFont():getHeight() + margin
if self.openDropdown == i then
if x > v.x and x < v.x + v.width and y > v.y + h and y < v.y + h * (math.min(#v.options, maxDropdown) + 1) and m == 1 then
v.selected = math.floor((y - v.y) / h - dropdownScrollE)
self.openDropdown = 0
dropdownValueCache[v.name] = {selected = v.selected}
self.createDropdowns()
end
end
end
end
function self.wheelmoved(x, y)
if self.openDropdown ~= 0 then
dropdownScroll = dropdownScroll + y
else
local mx, my = love.mouse.getPosition()
for i,v in ipairs(dropdowns) do
local h = love.graphics.getFont():getHeight() + margin
if mx > v.x and mx < v.x + v.width and my > v.y and my < v.y + h + margin then
dropdowns[i].selected = dropdowns[i].selected - math.floor(y)
dropdowns[i].selected = (dropdowns[i].selected - 1) % #dropdowns[i].options + 1
self.createDropdowns()
end
end
end
end
return self