Class GuiSlider

All Implemented Interfaces:
ElementEvents, GuiParent<GuiSlider>, TooltipHandler<GuiSlider>

public class GuiSlider extends GuiElement<GuiSlider>
This can be used as the base for anything that requires the linear movement of an element between two position. e.g. Scroll bars, Slide controls and slide indicators.

Implementation is simple, Simply install a "Slide Element", this will be the moving element, The movement of this element is confined to the bounds og the GuiSlider

The position of the slider is managed via the installed SliderState

Created by brandon3055 on 02/09/2023

  • Field Details

    • invertDragScroll

      public boolean invertDragScroll
      This should theoretically never be needed, But just in case...
  • Constructor Details

    • GuiSlider

      public GuiSlider(@NotNull @NotNull GuiParent<?> parent, Axis axis)
      Creates a basic gui slider that moves along the specified axis. This includes a default slider element the width of which is bound to the GuiSlider, And the length of which is controlled by SliderState.sliderRatio()
    • GuiSlider

      public GuiSlider(@NotNull @NotNull GuiParent<?> parent, Axis axis, GuiElement<?> slider)
  • Method Details

    • vanillaScrollBar

      public static GuiSlider.ScrollBar vanillaScrollBar(GuiElement<?> parent, Axis axis)
      Vanilla does not really seem to have a standard for its scroll bars, But this is something that should at least fit in to a typical vanilla gui.
    • setSliderState

      public GuiSlider setSliderState(SliderState state)
      Set the slider state used by this slider element. The slider state is used to get and set the slider position. It also controls scroll speed.
    • setScrollableElement

      public GuiSlider setScrollableElement(GuiElement<?> scrollableElement)
      For use cases where this slider is controlling something like a scroll element. This enables scrolling when the cursor is over the scrollable element. It can also enable scrolling via middle-click + drag.
    • setScrollableElement

      public GuiSlider setScrollableElement(GuiElement<?> scrollableElement, boolean middleClickScroll)
      For use cases where this slider is controlling something like a scroll element. This enables scrolling when the cursor is over the scrollable element. It can also enable scrolling via middle-click + drag.
    • installSlider

      public GuiSlider installSlider(GuiElement<?> slider)
      Install an element to be used as the sliding element. The sliders minimum position (meaning either LEFT or TOP) on the moving axis will be constrained the GuiSlider Attempting to override this constraint after installing the slider element will break the slider.

      The size constraints, and position constraint for the non-moving axis need to be set by the implementor.

      See Also:
    • bindSliderLength

      public GuiSlider bindSliderLength()
      Sets up constraints to automatically control the slider element length. The slider element length will be controlled by SliderState.sliderRatio() This is used for things like gui scroll bars where the bar length changes based on the ratio of content in view.
    • bindSliderWidth

      public GuiSlider bindSliderWidth()
      Binds the sliders position and size on the non-moving axis to the width and pos of the GuiSlider
    • getSlider

      public GuiElement<?> getSlider()
      Returns:
      the installed slider element.
    • isDragging

      public boolean isDragging()
      Returns:
      True if the slider is currently being dragged by the user.
    • setOutOfBoundsDist

      public GuiSlider setOutOfBoundsDist(double outOfBoundsDist)
      Set the out-of-bounds distance, If the cursor is dragged more than this distance from the slider bounds on the no-moving axis, the slider will snap back to its original position until the cursor moves back into bounds. Default is 50, -1 will disable the snap-back functionality.
    • setDragButton

      public GuiSlider setDragButton(int dragButton)
      Parameters:
      dragButton - The mouse button used to drag this slider (Default GuiButton.LEFT_CLICK)
    • setScrollDragButton

      public GuiSlider setScrollDragButton(int scrollDragButton)
      Parameters:
      scrollDragButton - The button used to scroll by clicking and dragging the defined scrollableElement (Default GuiButton.MIDDLE_CLICK)
      See Also:
    • mouseClicked

      public boolean mouseClicked(double mouseX, double mouseY, int button)
      Description copied from interface: ElementEvents
      Override this method to implement handling for the mouseClicked event. This event propagates through the entire gui element stack from top to bottom, If eny element consumes the event it will not propagate any further. For rare cases where you need to receive this even if it has been consumed, you can override ElementEvents.mouseClicked(double, double, int, boolean)

      Note: You do not need to call super when overriding this interface method.

      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      button - Mouse Button
      Returns:
      true to consume event.
    • mouseReleased

      public boolean mouseReleased(double mouseX, double mouseY, int button, boolean consumed)
      Description copied from interface: ElementEvents
      Root handler for mouseReleased event. This method will always be called for all elements even if the event has already been consumed. There are a few uses for this method, but the fast majority of mouseReleased handling should be implemented via ElementEvents.mouseReleased(double, double, int)

      Note: If overriding this method, do so with caution, You must either return true (if you wish to consume the event) or you must return the result of the super call.

      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      button - Mouse Button
      consumed - Will be true if this action has already been consumed.
      Returns:
      true if this event has been consumed.
    • mouseMoved

      public void mouseMoved(double mouseX, double mouseY)
      Description copied from interface: ElementEvents
      Called whenever the cursor position changes. Vanillas mouseDragged is not passed through because it is redundant. All mouse drag functionality can be archived using available events.
      Parameters:
      mouseX - new mouse X position
      mouseY - new mouse Y position
    • mouseScrolled

      public boolean mouseScrolled(double mouseX, double mouseY, double scroll)
      Description copied from interface: ElementEvents
      Override this method to implement handling for the mouseScrolled event. This event propagates through the entire gui element stack from top to bottom, If eny element consumes the event it will not propagate any further. For rare cases where you need to receive this even if it has been consumed, you can override ElementEvents.mouseScrolled(double, double, double, boolean)

      Note: You do not need to call super when overriding this interface method.

      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      scroll - Scroll direction and amount
      Returns:
      true to consume event.