Here is my code
Code: Select all
self.update = function(dt)
local mx = love.mouse.getX();
local my = love.mouse.getY();
-- The ratio between the height of the viewport and the height of the content
local contentRatio = self.h / (50 * #content)
if(self.mouseDown) then
-- mpD is the difference between the current mouse position and the recorded mouse position when the grip was clicked
local mpD = my - self.mousePos
self.scrollbarY = self.scrollbarY + mpD * (#content * dt)
end
-- Calculate height of scrollbar
self.scrollbarH = (self.h * contentRatio) / scale
-- Limit Size of Grip
if(self.scrollbarH < 20) then
self.scrollbarH = 20 / scale
end
if(self.scrollbarH > self.h) then
self.scrollbarH = self.h / scale
end
if(game.state == self.state ) then
-- I suspect the issue is here
content.y = (self.h - self.scrollbarY) * ((#content * 50) / self.h ) / scale
for i,v in ipairs(content) do
v.y = (content.y + (i * 50)) * scale
v.x = self.x + self.w - content[i].w/2
end
-- Boundaries
if(self.scrollbarY < self.y) then
self.scrollbarY = self.y
elseif(self.scrollbarY + self.scrollbarH > self.y + self.h) then
self.scrollbarY = self.y - self.scrollbarH + self.h
end
end
end