Another scrollbar (math)
Posted: Sun Dec 01, 2019 4:41 pm
Need some minor custom scrollbar code adjustments because it doesn’t work as expected
ThumbPos result is wrong (as you can see on the picture thumb not in its max bottom position).
How to get the right one?
Code: Select all
LineHeight.u = 31 ; (px)
DataHeightLn.i = 420 ; (total data lines)
DataHeightPx.i = LineHeight*DataHeightLn ; 420*31 = 13020 (same in px)
ViewPortLines.u = 21 ; (visible lines)
ViewPortHeight.u = ViewPortLines*LineHeight ; 21*31 = 651 (same in px)
Ratio.f = (ViewPortHeight / DataHeightPx) ; 0.0500000007
BarHeight.u = 666 ; ScrollBar height (note: BarHeight > ViewPortHeight)
Bar_Y.u = 49 ; ScrollBar Y pos
ThumbMinSize.u = 25
ThumbMaxSize.u = BarHeight
ThumbSize.u = (BarHeight * Ratio) ; 33px (thumb current height)
ThumbPosMin.u = Bar_Y ; 49 (top position)
ThumbPosMax.u = (Bar_Y + BarHeight) - ThumbSize ; 682 (end position)
;If ThumbSize < ThumbMinSize : ThumbSize = ThumbMinSize : EndIf
;If ThumbSize > ThumbMaxSize : ThumbSize = ThumbMaxSize : ThumbVisible = #False : EndIf
StepPx.u = (DataHeightPx - ViewPortHeight) / (ViewPortHeight - ThumbSize) ; 20 (Scroll "step" in px)
StepLn.u = Round(StepPx / LineHeight, #PB_Round_Nearest) ; 1 (Scroll "step" in lines)
;If StepLn < 1 : StepLn = 1 : EndIf
DataPos.i = (DataHeightLn - ViewPortLines)*LineHeight ; Set viewport data to end position (expecting ThumbPos = ThumbPosMax)
ThumbPos.i = (DataPos*Ratio) + Bar_Y ; Calculate thumb position inside scrollbar (in fact it's Y offset from the canvas gadget top)
;If ThumbPos < ThumbPosMin : ThumbPos = ThumbPosMin : EndIf
;If ThumbPos > ThumbPosMax : ThumbPos = ThumbPosMax : EndIf
Debug "ThumbPos = " +Str(ThumbPos) +" must be 682"
ThumbPos result is wrong (as you can see on the picture thumb not in its max bottom position).
How to get the right one?