Class ScrollBar

All Implemented Interfaces:
Component, TextGUIElement

public class ScrollBar extends AbstractComponent<ScrollBar>
Classic scrollbar that can be used to display where inside a larger component a view is showing. This implementation is not interactable and needs to be driven externally, meaning you can't focus on the scrollbar itself, you have to update its state as part of another component being modified. ScrollBars are either horizontal or vertical, which affects the way they appear and how they are drawn.

This class works on two concepts, the min-position-max values and the view size. The minimum value is always 0 and cannot be changed. The maximum value is 100 and can be adjusted programmatically. Position value is whever along the axis of 0 to max the scrollbar's tracker currently is placed. The view size is an important concept, it determines how big the tracker should be and limits the position so that it can only reach maximum value - view size.

The regular way to use the ScrollBar class is to tie it to the model-view of another component and set the scrollbar's maximum to the total height (or width, if the scrollbar is horizontal) of the model-view. View size should then be assigned based on the current size of the view, meaning as the terminal and/or the GUI changes and the components visible space changes, the scrollbar's view size is updated along with it. Finally the position of the scrollbar should be equal to the scroll offset in the component.

  • Field Details

    • direction

      private final Direction direction
    • maximum

      private int maximum
    • position

      private int position
    • viewSize

      private int viewSize
  • Constructor Details

    • ScrollBar

      public ScrollBar(Direction direction)
      Creates a new ScrollBar with a specified direction
      Parameters:
      direction - Direction of the scrollbar
  • Method Details

    • getDirection

      public Direction getDirection()
      Returns the direction of this ScrollBar
      Returns:
      Direction of this ScrollBar
    • setScrollMaximum

      public ScrollBar setScrollMaximum(int maximum)
      Sets the maximum value the scrollbar's position (minus the view size) can have
      Parameters:
      maximum - Maximum value
      Returns:
      Itself
    • getScrollMaximum

      public int getScrollMaximum()
      Returns the maximum scroll value
      Returns:
      Maximum scroll value
    • setScrollPosition

      public ScrollBar setScrollPosition(int position)
      Sets the scrollbar's position, should be a value between 0 and maximum - view size
      Parameters:
      position - Scrollbar's tracker's position
      Returns:
      Itself
    • getScrollPosition

      public int getScrollPosition()
      Returns the position of the ScrollBar's tracker
      Returns:
      Position of the ScrollBar's tracker
    • setViewSize

      public ScrollBar setViewSize(int viewSize)
      Sets the view size of the scrollbar, determining how big the scrollbar's tracker should be and also affecting the maximum value of tracker's position
      Parameters:
      viewSize - View size of the scrollbar
      Returns:
      Itself
    • getViewSize

      public int getViewSize()
      Returns the view size of the scrollbar
      Returns:
      View size of the scrollbar
    • createDefaultRenderer

      protected ComponentRenderer<ScrollBar> createDefaultRenderer()
      Description copied from class: AbstractComponent
      When you create a custom component, you need to implement this method and return a Renderer which is responsible for taking care of sizing the component, rendering it and choosing where to place the cursor (if Interactable). This value is intended to be overridden by custom themes.
      Specified by:
      createDefaultRenderer in class AbstractComponent<ScrollBar>
      Returns:
      Renderer to use when sizing and drawing this component