From 98cf070830967078bebe47a1da9589543d4da32d Mon Sep 17 00:00:00 2001 From: jill Date: Sun, 19 Sep 2021 23:04:39 +0300 Subject: [PATCH] make scrollbar clickable --- src/dropdown.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/dropdown.lua b/src/dropdown.lua index 8d9152f..768368b 100644 --- a/src/dropdown.lua +++ b/src/dropdown.lua @@ -22,6 +22,8 @@ self.openDropdown = 0 local dropdownScroll = 0 local dropdownScrollE = 0 +local scrollbarSize = 4 + local function skeys(t) local k = {} 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) 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 function self.render() @@ -295,9 +309,9 @@ function self.render() if #v.options > maxDropdown then local displayed = maxDropdown / (#v.options) 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) end end @@ -338,7 +352,7 @@ function self.mousereleased(x, y, m) 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 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) self.openDropdown = 0 dropdownValueCache[v.name] = {selected = v.selected}