Skip to main content
"hadnle" to "handle", "controll" to "control" also changed "&" to "and" because of the least 6 characters.
Source Link

A scroll bar is like a 2d2D camera control that controllscontrols the scroll area's viewport's contents. I will assume you know how to render your gui in its entirety. I also strongly advice against making a gui that is complex unless it truly contributes to ux. This could lead to a mediocre gaming experience. I also strongly urge you to use a UI library.

In abstract high level pseudo code:

  1. Initialization:
  2. Define the scrollable area's width &and height.
  3. Define the viewport's width &and height.
  4. The "handle" is the same % of the scrollbar as the viewport is of the scrollable plain.
    • The "handle" is the rectangle that travels up &and down a vertical scrollbar.

Example: The scrollable area's height is 1000 pixels and the viewport's height is 200 pixels so the "handle" will be 20% of the scrollbar's height, if the scrollbar has the same height as the viewport than the handle will be 40 pixels high.

  1. Main loop:

  2. Render the entire scrollable area (if it's not gigantic).

    • Otherwise you need to smartly render only elements close the current position of the viewable area.
  3. The "handle" has an upper and lower bounds in % of the scrollbar.

  4. Blit the relevant rectangle with the same upper and lower % of the scrollable area.

  5. Place the result inside the viewport.

  6. Accepting input:

  7. When a user clicks in the scrollable area's viewport, consider the "handle's" upper bound.

  8. Adjust the click to a position % of the scrollable's area pixels lower based on upper bound.

  9. Dragging the scrollbar's "hadnle""handle":

  10. Detect a mouseDown event that occurs on the scrollbar.

    • If the event occured on the handle:
      1. Remember the position.
      2. Adjust the handle and viewport with the correct offset from that position until the mouseUp event occurs.
    • If the event occurs on the scrollbar but not on the "handle":
      1. Adjust the "handle" so it's centered on that position.

The explanation is based on a vertical scrollbar but the same applies to a horizontal bar.

A scroll bar is like a 2d camera control that controlls the scroll area's viewport's contents. I will assume you know how to render your gui in its entirety. I also strongly advice against making a gui that is complex unless it truly contributes to ux. This could lead to a mediocre gaming experience. I also strongly urge you to use a UI library.

In abstract high level pseudo code:

  1. Initialization:
  2. Define the scrollable area's width & height.
  3. Define the viewport's width & height.
  4. The "handle" is the same % of the scrollbar as the viewport is of the scrollable plain.
    • The "handle" is the rectangle that travels up & down a vertical scrollbar.

Example: The scrollable area's height is 1000 pixels and the viewport's height is 200 pixels so the "handle" will be 20% of the scrollbar's height, if the scrollbar has the same height as the viewport than the handle will be 40 pixels high.

  1. Main loop:

  2. Render the entire scrollable area (if it's not gigantic).

    • Otherwise you need to smartly render only elements close the current position of the viewable area.
  3. The "handle" has an upper and lower bounds in % of the scrollbar.

  4. Blit the relevant rectangle with the same upper and lower % of the scrollable area.

  5. Place the result inside the viewport.

  6. Accepting input:

  7. When a user clicks in the scrollable area's viewport, consider the "handle's" upper bound.

  8. Adjust the click to a position % of the scrollable's area pixels lower based on upper bound.

  9. Dragging the scrollbar's "hadnle":

  10. Detect a mouseDown event that occurs on the scrollbar.

    • If the event occured on the handle:
      1. Remember the position.
      2. Adjust the handle and viewport with the correct offset from that position until the mouseUp event occurs.
    • If the event occurs on the scrollbar but not on the "handle":
      1. Adjust the "handle" so it's centered on that position.

The explanation is based on a vertical scrollbar but the same applies to a horizontal bar.

A scroll bar is like a 2D camera control that controls the scroll area's viewport's contents. I will assume you know how to render your gui in its entirety. I also strongly advice against making a gui that is complex unless it truly contributes to ux. This could lead to a mediocre gaming experience. I also strongly urge you to use a UI library.

In abstract high level pseudo code:

  1. Initialization:
  2. Define the scrollable area's width and height.
  3. Define the viewport's width and height.
  4. The "handle" is the same % of the scrollbar as the viewport is of the scrollable plain.
    • The "handle" is the rectangle that travels up and down a vertical scrollbar.

Example: The scrollable area's height is 1000 pixels and the viewport's height is 200 pixels so the "handle" will be 20% of the scrollbar's height, if the scrollbar has the same height as the viewport than the handle will be 40 pixels high.

  1. Main loop:

  2. Render the entire scrollable area (if it's not gigantic).

    • Otherwise you need to smartly render only elements close the current position of the viewable area.
  3. The "handle" has an upper and lower bounds in % of the scrollbar.

  4. Blit the relevant rectangle with the same upper and lower % of the scrollable area.

  5. Place the result inside the viewport.

  6. Accepting input:

  7. When a user clicks in the scrollable area's viewport, consider the "handle's" upper bound.

  8. Adjust the click to a position % of the scrollable's area pixels lower based on upper bound.

  9. Dragging the scrollbar's "handle":

  10. Detect a mouseDown event that occurs on the scrollbar.

    • If the event occured on the handle:
      1. Remember the position.
      2. Adjust the handle and viewport with the correct offset from that position until the mouseUp event occurs.
    • If the event occurs on the scrollbar but not on the "handle":
      1. Adjust the "handle" so it's centered on that position.

The explanation is based on a vertical scrollbar but the same applies to a horizontal bar.

Source Link
AturSams
  • 10.6k
  • 1
  • 35
  • 60

A scroll bar is like a 2d camera control that controlls the scroll area's viewport's contents. I will assume you know how to render your gui in its entirety. I also strongly advice against making a gui that is complex unless it truly contributes to ux. This could lead to a mediocre gaming experience. I also strongly urge you to use a UI library.

In abstract high level pseudo code:

  1. Initialization:
  2. Define the scrollable area's width & height.
  3. Define the viewport's width & height.
  4. The "handle" is the same % of the scrollbar as the viewport is of the scrollable plain.
    • The "handle" is the rectangle that travels up & down a vertical scrollbar.

Example: The scrollable area's height is 1000 pixels and the viewport's height is 200 pixels so the "handle" will be 20% of the scrollbar's height, if the scrollbar has the same height as the viewport than the handle will be 40 pixels high.

  1. Main loop:

  2. Render the entire scrollable area (if it's not gigantic).

    • Otherwise you need to smartly render only elements close the current position of the viewable area.
  3. The "handle" has an upper and lower bounds in % of the scrollbar.

  4. Blit the relevant rectangle with the same upper and lower % of the scrollable area.

  5. Place the result inside the viewport.

  6. Accepting input:

  7. When a user clicks in the scrollable area's viewport, consider the "handle's" upper bound.

  8. Adjust the click to a position % of the scrollable's area pixels lower based on upper bound.

  9. Dragging the scrollbar's "hadnle":

  10. Detect a mouseDown event that occurs on the scrollbar.

    • If the event occured on the handle:
      1. Remember the position.
      2. Adjust the handle and viewport with the correct offset from that position until the mouseUp event occurs.
    • If the event occurs on the scrollbar but not on the "handle":
      1. Adjust the "handle" so it's centered on that position.

The explanation is based on a vertical scrollbar but the same applies to a horizontal bar.