It is currently Sun Jul 12, 2020 2:17 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Another scrollbar (math)
PostPosted: Sun Dec 01, 2019 4:41 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Jul 07, 2018 6:50 pm
Posts: 198
Need some minor custom scrollbar code adjustments because it doesn’t work as expected
Code:
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?


Last edited by Everything on Mon Dec 02, 2019 3:00 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Another scrollbar (math)
PostPosted: Mon Dec 02, 2019 2:59 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Jul 07, 2018 6:50 pm
Posts: 198
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).


Top
 Profile  
Reply with quote  
 Post subject: Re: Another scrollbar (math)
PostPosted: Mon Dec 02, 2019 8:11 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1241
Location: right here
Code:
StepPx.f         = 1.0 * (DataHeightPx - ViewPortHeight) / (ViewPortHeight - ThumbSize)

Shouldn't that be (BarHeight - ThumbSize) ?


Top
 Profile  
Reply with quote  
 Post subject: Re: Another scrollbar (math)
PostPosted: Mon Dec 02, 2019 9:21 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 687
Mby:

Code:
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


Last edited by Mijikai on Mon Dec 02, 2019 9:25 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Another scrollbar (math)
PostPosted: Mon Dec 02, 2019 9:24 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1241
Location: right here
@Everything
The ratio you are calculating might help for the thumbsize like you are doing:
Code:
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:
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Another scrollbar (math)
PostPosted: Mon Dec 02, 2019 6:58 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Jul 07, 2018 6:50 pm
Posts: 198
You are absolutely right! Thanks!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye