Page 1 of 1

Another scrollbar (math)

Posted: Sun Dec 01, 2019 4:41 pm
by Everything
Need some minor custom scrollbar code adjustments because it doesn’t work as expected

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"
Image

ThumbPos result is wrong (as you can see on the picture thumb not in its max bottom position).
How to get the right one?

Re: Another scrollbar (math)

Posted: Mon Dec 02, 2019 2:59 am
by Everything
Strange, the question is pretty simple, but no one has answered yet...
Added more comments and image.

P.S.
My apologies. No working example (too much code for such a small clarification).

Re: Another scrollbar (math)

Posted: Mon Dec 02, 2019 8:11 am
by #NULL

Code: Select all

StepPx.f         = 1.0 * (DataHeightPx - ViewPortHeight) / (ViewPortHeight - ThumbSize)
Shouldn't that be (BarHeight - ThumbSize) ?

Re: Another scrollbar (math)

Posted: Mon Dec 02, 2019 9:21 am
by Mijikai
Mby:

Code: Select all

ThumbPos = (BarHeight - ThumbSize) + Bar_Y;<- ThumbPos = EndPos!
Debug ThumbPos
DataPos = (ThumbPos - Bar_Y) * (DataHeightLn / (BarHeight - ThumbSize));<- ThumbPos to DataPos / scaled against DataHeightLn (replace with the full data size if thats what u need)
Debug DataPos

Re: Another scrollbar (math)

Posted: Mon Dec 02, 2019 9:24 am
by #NULL
@Everything
The ratio you are calculating might help for the thumbsize like you are doing:

Code: Select all

Ratio.f          = (ViewPortHeight / DataHeightPx) ; 0.0500000007
[...]
ThumbSize.u      = (BarHeight * Ratio) ; 33px (thumb current height)
but to translate a position from editor scroll range to scrollbar scroll range you need another ratio.
I used Ratio2 instead of Ratio to calculate ThumbPos, and Ratio2 is the ratio between the possible scroll range of the editor and the possible scroll range of the scrollbar:

Code: Select all

DataPos.i  = (DataHeightLn - ViewPortLines)*LineHeight ; Set viewport data to end position (expecting ThumbPos = ThumbPosMax)
Ratio2.f         = (BarHeight - ThumbSize) / (DataHeightPx - ViewPortHeight)
ThumbPos.i = (DataPos*Ratio2) + Bar_Y                   ; Calculate thumb position inside scrollbar (in fact it's Y offset from the canvas gadget top)

Re: Another scrollbar (math)

Posted: Mon Dec 02, 2019 6:58 pm
by Everything
You are absolutely right! Thanks!