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, { x = padding, y = padding, width = 128, options = { 'Preview Ease', 'Mix Eases', 'Create Ease' }, name = 'mode' }) if d[dropdownId].selected == 1 then -- preview ease insertDropdown(d, { x = padding + 128 + padding, y = padding, width = 128, options = skeys(ease.eases), name = 'ease1' }) ease.ease = ease.eases[d[dropdownId].options[d[dropdownId].selected]].f elseif d[dropdownId].selected == 2 then -- mix eases insertDropdown(d, { x = padding + 128 + padding, y = padding, width = 128, options = skeys(ease.eases), name = 'ease1' }) insertDropdown(d, { x = padding + 128 + padding + 128 + padding, y = padding, width = 128, options = skeys(ease.eases), name = 'ease2' }) 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) elseif d[dropdownId].selected == 3 then -- create eases insertDropdown(d, { x = padding + 128 + padding, y = padding, width = 128, options = skeys(ease.eases), name = 'ease1' }) end dropdowns = d minEase = (self.kselected('ease1') and ease.eases[self.kselected('ease1')].min == -1) or (self.kselected('ease2') and ease.eases[self.kselected('ease2')].min == -1) end function self.update(dt) if self.openDropdown ~= 0 then dropdownScroll = math.max(dropdownScroll, -(#self.get(self.openDropdown).options - maxDropdown)) 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 love.graphics.setColor(0, 0, 0, 0.3) 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) love.graphics.setColor(1, 1, 1, 1) 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) if self.openDropdown == i then for i,o in ipairs(v.options) do local x, y, w, h = x, y + i * h, w, h y = y + dropdownScrollE * h local gi = y / h if gi > maxDropdown or gi < 1 then goto continue end local a = 1 - math.min(math.max((1 - (maxDropdown - gi)) * (1 - (math.abs(dropdownScrollE) /(#v.options - maxDropdown))), 0), 1) love.graphics.setColor(0, 0, 0, 0.3 * a) 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) love.graphics.setColor(1, 1, 1, 0.75 * a) love.graphics.rectangle('line', x, y, w, h) love.graphics.setColor(1, 1, 1, 1 * a) love.graphics.print(v.options[i], x + 2, y + 2) ::continue:: end -- scrollwheel if #v.options > maxDropdown then local displayed = maxDropdown / #v.options local scroll = math.abs(dropdownScrollE) / (#v.options - maxDropdown) local size = margin 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 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