Class WAbstractSlider

java.lang.Object
io.github.cottonmc.cotton.gui.widget.WWidget
io.github.cottonmc.cotton.gui.widget.WAbstractSlider
Direct Known Subclasses:
WLabeledSlider, WSlider

public abstract class WAbstractSlider extends WWidget
A base class for slider widgets that can be used to select int values.

You can set two listeners on a slider:

  • A value change listener that gets all value changes.
  • A dragging finished listener that gets called when the player stops dragging the slider or modifies the value with the keyboard. For example, this can be used for sending sync packets to the server when the player has selected a value.
  • Field Details

    • min

      protected int min
    • max

      protected int max
    • axis

      protected final Axis axis
    • direction

      protected WAbstractSlider.Direction direction
    • value

      protected int value
    • dragging

      protected boolean dragging
      True if the user is currently dragging the thumb. Used for visuals.
    • valueToCoordRatio

      protected float valueToCoordRatio
      A value:coordinate ratio. Used for converting user input into values.
      See Also:
    • coordToValueRatio

      protected float coordToValueRatio
      A coordinate:value ratio. Used for rendering the thumb.
      See Also:
  • Constructor Details

    • WAbstractSlider

      protected WAbstractSlider(int min, int max, Axis axis)
  • Method Details

    • getThumbWidth

      protected abstract int getThumbWidth()
      Returns the thumb size along the slider axis.
      Returns:
      the thumb size along the slider axis
    • isMouseInsideBounds

      protected abstract boolean isMouseInsideBounds(int x, int y)
      Checks if the mouse cursor is close enough to the slider that the user can start dragging.
      Parameters:
      x - the mouse x position
      y - the mouse y position
      Returns:
      if the cursor is inside dragging bounds
    • updateValueCoordRatios

      protected void updateValueCoordRatios()
      Updates coordToValueRatio and valueToCoordRatio. This method should be called whenever this widget resizes or changes it min/max value boundaries.
      Since:
      5.1.0
    • setSize

      public void setSize(int x, int y)
      Description copied from class: WWidget
      Sets the size of this widget.

      Overriding methods may restrict one of the dimensions to be a constant value, for example super.setSize(x, 20).

      Overrides:
      setSize in class WWidget
      Parameters:
      x - the new width
      y - the new height
    • canResize

      public boolean canResize()
      Description copied from class: WWidget
      Checks whether this widget can be resized using WWidget.setSize(int, int).
      Overrides:
      canResize in class WWidget
      Returns:
      true if this widget can be resized, false otherwise
    • canFocus

      public boolean canFocus()
      Description copied from class: WWidget
      Tests whether this widget can have the focus in the GUI.
      Overrides:
      canFocus in class WWidget
      Returns:
      true if this widget can be focused, false otherwise
    • onMouseDown

      @Environment(CLIENT) public InputResult onMouseDown(int x, int y, int button)
      Description copied from class: WWidget
      Notifies this widget that the mouse has been pressed while inside its bounds
      Overrides:
      onMouseDown in class WWidget
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onMouseDrag

      @Environment(CLIENT) public InputResult onMouseDrag(int x, int y, int button, double deltaX, double deltaY)
      Description copied from class: WWidget
      Notifies this widget that the mouse has been moved while pressed and inside its bounds.
      Overrides:
      onMouseDrag in class WWidget
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      deltaX - The amount of dragging on the X axis
      deltaY - The amount of dragging on the Y axis
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onClick

      @Environment(CLIENT) public InputResult onClick(int x, int y, int button)
      Description copied from class: WWidget
      Notifies this widget that the mouse has been pressed and released, both while inside its bounds.
      Overrides:
      onClick in class WWidget
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onMouseUp

      @Environment(CLIENT) public InputResult onMouseUp(int x, int y, int button)
      Description copied from class: WWidget
      Notifies this widget that the mouse has been released while inside its bounds
      Overrides:
      onMouseUp in class WWidget
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onMouseScroll

      @Environment(CLIENT) public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount)
      Description copied from class: WWidget
      Notifies this widget that the mouse has been scrolled inside its bounds.
      Overrides:
      onMouseScroll in class WWidget
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      horizontalAmount - The scrolled horizontal amount. Positive values are right and negative values are left.
      verticalAmount - The scrolled vertical amount. Positive values are up and negative values are down.
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • tick

      @Environment(CLIENT) public void tick()
      Description copied from class: WWidget
      Executes a client-side tick for this widget.
      Overrides:
      tick in class WWidget
    • getValue

      public int getValue()
    • setValue

      public void setValue(int value)
      Sets the slider value without calling listeners.
      Parameters:
      value - the new value
    • setValue

      public void setValue(int value, boolean callListeners)
      Sets the slider value.
      Parameters:
      value - the new value
      callListeners - if true, call all slider listeners
    • getValueChangeListener

      @Nullable public @Nullable IntConsumer getValueChangeListener()
    • setValueChangeListener

      public void setValueChangeListener(@Nullable @Nullable IntConsumer valueChangeListener)
    • getDraggingFinishedListener

      @Nullable public @Nullable IntConsumer getDraggingFinishedListener()
    • setDraggingFinishedListener

      public void setDraggingFinishedListener(@Nullable @Nullable IntConsumer draggingFinishedListener)
    • getMinValue

      public int getMinValue()
    • getMaxValue

      public int getMaxValue()
    • setMinValue

      public void setMinValue(int min)
    • setMaxValue

      public void setMaxValue(int max)
    • getAxis

      public Axis getAxis()
    • getDirection

      public WAbstractSlider.Direction getDirection()
      Gets the direction of this slider.
      Returns:
      the direction
      Since:
      2.0.0
    • setDirection

      public void setDirection(WAbstractSlider.Direction direction)
      Sets the direction of this slider.
      Parameters:
      direction - the new direction
      Throws:
      IllegalArgumentException - if the direction axis is not equal to axis.
      Since:
      2.0.0
    • onValueChanged

      protected void onValueChanged(int value)
    • onKeyPressed

      @Environment(CLIENT) public InputResult onKeyPressed(int ch, int key, int modifiers)
      Description copied from class: WWidget
      Notifies this widget that a key has been pressed.
      Overrides:
      onKeyPressed in class WWidget
      Parameters:
      key - the GLFW scancode of the key
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onKeyReleased

      @Environment(CLIENT) public InputResult onKeyReleased(int ch, int key, int modifiers)
      Description copied from class: WWidget
      Notifies this widget that a key has been released
      Overrides:
      onKeyReleased in class WWidget
      Parameters:
      key - the GLFW scancode of the key
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • isDragging

      public boolean isDragging()
      Tests whether the user is dragging this slider.
      Returns:
      true if this slider is being dragged, false otherwise
      Since:
      4.0.0
    • addNarrations

      @Environment(CLIENT) public void addNarrations(NarrationMessageBuilder builder)
      Description copied from class: WWidget
      Adds the narrations of this widget to a narration builder. Narrations will only apply if this widget is narratable.

      The widget needs to be focusable or hoverable, and also be focused/hovered for narrations to be added.

      Overrides:
      addNarrations in class WWidget
      Parameters:
      builder - the narration builder, cannot be null
    • isDecreasingKey

      public static boolean isDecreasingKey(int ch, WAbstractSlider.Direction direction)
      Tests if the key should decrease sliders with the specified direction.
      Parameters:
      ch - the key code
      direction - the direction
      Returns:
      true if the key should decrease sliders with the direction, false otherwise
      Since:
      2.0.0
    • isIncreasingKey

      public static boolean isIncreasingKey(int ch, WAbstractSlider.Direction direction)
      Tests if the key should increase sliders with the specified direction.
      Parameters:
      ch - the key code
      direction - the direction
      Returns:
      true if the key should increase sliders with the direction, false otherwise
      Since:
      2.0.0