make scrollbar clickable

This commit is contained in:
jill 2021-09-19 23:04:39 +03:00
parent 18a69b1ba5
commit 98cf070830

View file

@ -22,6 +22,8 @@ self.openDropdown = 0
local dropdownScroll = 0 local dropdownScroll = 0
local dropdownScrollE = 0 local dropdownScrollE = 0
local scrollbarSize = 4
local function skeys(t) local function skeys(t)
local k = {} local k = {}
for n,v in pairs(t) do table.insert(k, {n, v}) end for n,v in pairs(t) do table.insert(k, {n, v}) end
@ -211,6 +213,18 @@ function self.update(dt)
v.open = mix(v.open, 0, dt * 20) v.open = mix(v.open, 0, dt * 20)
end end
end end
if love.mouse.isDown(1) then
local x, y = love.mouse.getPosition()
for i,v in ipairs(dropdowns) do
local h = fontHeight + margin
if self.openDropdown == i then
if x > v.x and x > v.x + v.width - scrollbarSize and y > v.y + h and y < v.y + h * (math.min(#v.options, maxDropdown) + 1) and not (#v.options < maxDropdown) then
dropdownScroll = ((y - (v.y + h)) / (h * (math.min(#v.options, maxDropdown)))) * -(#v.options - maxDropdown + 1)
end
end
end
end
end end
function self.render() function self.render()
@ -295,9 +309,9 @@ function self.render()
if #v.options > maxDropdown then if #v.options > maxDropdown then
local displayed = maxDropdown / (#v.options) local displayed = maxDropdown / (#v.options)
local scroll = math.abs(dropdownScrollE) / (#v.options - maxDropdown + 1) local scroll = math.abs(dropdownScrollE) / (#v.options - maxDropdown + 1)
local size = 3 local size = scrollbarSize
love.graphics.setColor(1, 1, 1, 0.8 * v.open) love.graphics.setColor(1, 1, 1, v.open)
love.graphics.rectangle('fill', x + w - size, y + h + scroll * (1 - displayed) * (maxDropdown - 1) * h * v.open, size, displayed * (maxDropdown - 1) * h * v.open) love.graphics.rectangle('fill', x + w - size, y + h + scroll * (1 - displayed) * (maxDropdown - 1) * h * v.open, size, displayed * (maxDropdown - 1) * h * v.open)
end end
end end
@ -338,7 +352,7 @@ function self.mousereleased(x, y, m)
for i,v in ipairs(dropdowns) do for i,v in ipairs(dropdowns) do
local h = fontHeight + margin local h = fontHeight + margin
if self.openDropdown == i then 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 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 and (x < v.x + v.width - scrollbarSize or #v.options < maxDropdown) then
v.selected = math.floor((y - v.y) / h - dropdownScrollE) v.selected = math.floor((y - v.y) / h - dropdownScrollE)
self.openDropdown = 0 self.openDropdown = 0
dropdownValueCache[v.name] = {selected = v.selected} dropdownValueCache[v.name] = {selected = v.selected}