Jump to content

Problem with scroll


ArrogantCoder

Recommended Posts

Hello ArrogantCoder,

in order to move the scroll bar with the mouse you have to implement attachment. A scrollbar that is attached to the mouse cursor is moving in the same direction as the cursor, limited by the scrollbar's own bounds. The math behind it is pretty simple. You remember the (x,y) position of the cursor and of the scrollbar (in pixels) on click. We define the pixel position of the scrollbar as the top-most (vertical) or left-most (horizontal) pixel of it. Then for each mouse movement you calculate the new position:

new scrollbar pixel-pos = old-scrollbar-pixel-pos-at-click + ( current-cursor-pos - old-cursor-pos-at-click )

You only have to calculate one dimension: y dimension of vertical scrollbar, x dimension of horizontal scrollbar (scrollbar-pixel-dim)

Then you need to transform the new pixel position to scrollbar-percentage.

scrollbar-perc = min( 1, max( 0, scrollbar-pixel-dim / ( scrollbar-area-size-dim - scrollbar-size-dim ) ) )

where scrollbar-area-size-dim is the width of the horizontal scrollbar containment area, scrollbar-size-dim is the width of the scrollbar control that you can click on, scrollbar-area-size-dim >= scrollbar-size-dim (analogous definition for vertical scrollbar).

Feel free to ask if you have any further questions. ?

Other relevant information:

Edited by The_GTA
  • Like 1
Link to comment
2 minutes ago, The_GTA said:

Hello ArrogantCoder,

in order to move the scroll bar with the mouse you have to implement attachment. A scrollbar that is attached to the mouse cursor is moving in the same direction as the cursor, limited by the scrollbar's own bounds. The math behind it is pretty simple. You remember the (x,y) position of the cursor and of the scrollbar (in pixels) on click. We define the pixel position of the scrollbar as the top-most (vertical) or left-most (horizontal) pixel of it. Then for each mouse movement you calculate the new position:

new scrollbar pixel-pos = old-scrollbar-pixel-pos-at-click + ( current-cursor-pos - old-cursor-pos-at-click )

You only have to calculate one dimension: y dimension of vertical scrollbar, x dimension of horizontal scrollbar (scrollbar-pixel-dim)

Then you need to transform the new pixel position to scrollbar-percentage.

scrollbar-perc = min( 1, max( 0, scrollbar-pixel-dim / ( scrollbar-area-size-dim - scrollbar-size-dim ) ) )

where scrollbar-area-size-dim is the width of the horizontal scrollbar containment area, scrollbar-size-dim is the width of the scrollbar control that you can click on, scrollbar-area-size-dim >= scrollbar-size-dim (analogous definition for vertical scrollbar).

Feel free to ask if you have any further questions. ?

Other relevant information:

thanks a lot, your explanation helped!

Link to comment
2 hours ago, ArrogantCoder said:

thanks a lot, your explanation helped!

You're welcome! I wish you a good experience when trying to implement it. By doing projects such as resedit I have a lot of experience when it comes to designing custom UI elements. Don't shy away to consult me again if the implementation is troubling you.

...

Regarding the private message you sent me, ...

How about you start with drawing code for the scrollbar? You need to draw a container for the rectangle, then inside of it a (clickable) rectangle. Maybe you add some arrows at the beginning and end.

I suggest you this because then it may be easier to understand by visuals. The description I gave you in the thread is complete so you may have an issue understanding it.

Edited by The_GTA
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...