Class Slider

java.lang.Object
de.gurkenlabs.litiengine.gui.GuiComponent
de.gurkenlabs.litiengine.gui.Slider
All Implemented Interfaces:
IRenderable, Tweenable, MouseListener, MouseMotionListener, MouseWheelListener, EventListener
Direct Known Subclasses:
HorizontalSlider, VerticalSlider

public abstract class Slider extends GuiComponent

Abstract base class for GUI slider components that allow a user to pick a numerical value within a configurable range and step size.

Subclasses provide the concrete orientation (e.g. horizontal / vertical) and implement the slider geometry through renderBar(Graphics2D), renderTicks(Graphics2D) and the location/mouse mapping helpers.

  • Constructor Details

    • Slider

      protected Slider(double x, double y, double width, double height, float minValue, float maxValue, float stepSize)
      Constructs a new slider with the given bounds and value range.
      Parameters:
      x - the x position of the slider
      y - the y position of the slider
      width - the width of the slider
      height - the height of the slider
      minValue - the minimum selectable value
      maxValue - the maximum selectable value
      stepSize - the step size between adjacent selectable values
  • Method Details

    • getButton1

      public ImageComponent getButton1()
      Gets the first arrow button (e.g. decrease).
      Returns:
      the first button component
    • setButton1

      protected void setButton1(ImageComponent button1)
      Sets the first arrow button (typically used to decrease the slider value).
      Parameters:
      button1 - the button component to register
    • getButton2

      public ImageComponent getButton2()
      Gets the second arrow button (e.g. increase).
      Returns:
      the second button component
    • setButton2

      protected void setButton2(ImageComponent button2)
      Sets the second arrow button (typically used to increase the slider value).
      Parameters:
      button2 - the button component to register
    • getButtonSpritesheet

      public Spritesheet getButtonSpritesheet()
      Gets the spritesheet used to render the arrow buttons.
      Returns:
      the button spritesheet
    • setButtonSpritesheet

      public void setButtonSpritesheet(Spritesheet buttonSprite)
      Sets the spritesheet used to render the arrow buttons.
      Parameters:
      buttonSprite - the spritesheet to use
    • getChangeConsumer

      public List<Consumer<Float>> getChangeConsumer()
      Gets the list of registered change listeners.
      Returns:
      the change listener list
    • getCurrentValue

      public float getCurrentValue()
      Gets the currently selected value.
      Returns:
      the current value
    • setCurrentValue

      public void setCurrentValue(float newValue)
      Sets the currently selected value. The value is clamped to the configured min/max range and registered change listeners are notified.
      Parameters:
      newValue - the new value
    • getTickSize

      public float getTickSize()
      Gets the relative size factor used when rendering the slider's tick marks.
      Returns:
      the tick size factor
    • setTickSize

      public void setTickSize(float tickSize)
      Sets the relative size factor used when rendering the slider's tick marks.
      Parameters:
      tickSize - the tick size factor
    • getSteps

      public int getSteps()
      Computes the number of discrete steps between getMinValue() and getMaxValue() given the current step size.
      Returns:
      the number of selectable steps
    • getMaxValue

      public float getMaxValue()
      Gets the maximum selectable value.
      Returns:
      the maximum value
    • setMaxValue

      public void setMaxValue(float maxValue)
      Sets the maximum selectable value and refreshes the slider dimensions.
      Parameters:
      maxValue - the maximum value
    • getMinValue

      public float getMinValue()
      Gets the minimum selectable value.
      Returns:
      the minimum value
    • setMinValue

      public void setMinValue(float minValue)
      Sets the minimum selectable value and refreshes the slider dimensions.
      Parameters:
      minValue - the minimum value
    • getRelativeSliderLocation

      public abstract Point2D getRelativeSliderLocation()
      Computes the relative location of the slider thumb within the slider for the current value.
      Returns:
      the relative location of the slider thumb
    • getRelativeMouseValue

      protected abstract float getRelativeMouseValue()
      Computes a normalized value (typically in [0, 1]) corresponding to the current mouse location relative to the slider.
      Returns:
      the relative mouse value
    • getSliderComponent

      public ImageComponent getSliderComponent()
      Gets the slider thumb component.
      Returns:
      the slider thumb component
    • setSliderComponent

      protected void setSliderComponent(ImageComponent slider)
      Sets the slider thumb component.
      Parameters:
      slider - the slider thumb component to register
    • updateSliderDimensions

      protected abstract void updateSliderDimensions()
      Updates the geometry of the slider in response to a change in min/max value or step size.
    • render

      public void render(Graphics2D g)
      Description copied from class: GuiComponent
      Note: If you override this and are modifying swing components, be sure you are in the AWT thread when you do so!
      Specified by:
      render in interface IRenderable
      Overrides:
      render in class GuiComponent
      Parameters:
      g - The current graphics object onto which this instance will render its visual contents.
      See Also:
    • renderBar

      protected abstract void renderBar(Graphics2D g)
      Renders the slider bar (track).
      Parameters:
      g - the graphics context to draw to
    • renderTicks

      protected abstract void renderTicks(Graphics2D g)
      Renders the slider tick marks.
      Parameters:
      g - the graphics context to draw to
    • shouldIncreaseOnKey

      protected boolean shouldIncreaseOnKey(int keyCode)
      Determines whether the slider value should be increased when the given key is typed.
      Parameters:
      keyCode - the typed key code
      Returns:
      true if the value should be increased
    • shouldDecreaseOnKey

      protected boolean shouldDecreaseOnKey(int keyCode)
      Determines whether the slider value should be decreased when the given key is typed.
      Parameters:
      keyCode - the typed key code
      Returns:
      true if the value should be decreased
    • getSliderSpritesheet

      public Spritesheet getSliderSpritesheet()
      Gets the spritesheet used to render the slider thumb.
      Returns:
      the slider spritesheet
    • setSliderSpritesheet

      public void setSliderSpritesheet(Spritesheet sliderSprite)
      Sets the spritesheet used to render the slider thumb.
      Parameters:
      sliderSprite - the spritesheet to use
    • getStepSize

      public float getStepSize()
      Gets the step size between adjacent selectable values.
      Returns:
      the step size
    • setStepSize

      public void setStepSize(float stepSize)
      Sets the step size between adjacent selectable values and refreshes the slider dimensions.
      Parameters:
      stepSize - the step size
    • isDragging

      public boolean isDragging()
      Returns whether the slider is currently being dragged by the user.
      Returns:
      true if the slider thumb is being dragged
    • isShowingTicks

      public boolean isShowingTicks()
      Returns whether the slider is rendering tick marks.
      Returns:
      true if tick marks are shown
    • setShowTicks

      public void setShowTicks(boolean showTicks)
      Sets whether the slider should render tick marks.
      Parameters:
      showTicks - true to show tick marks
    • onChange

      public void onChange(Consumer<Float> c)
      Registers a callback that is invoked whenever the slider value changes.
      Parameters:
      c - the change consumer to register
    • inferValueFromMouseLocation

      protected void inferValueFromMouseLocation()
      Infers the current value from the current mouse location and updates the slider accordingly.
    • prepare

      public void prepare()
      Description copied from class: GuiComponent
      Prepare the GuiComponent and all its child Components (Makes the GuiComponent visible and adds mouse listeners.). This is, for example, done right before switching to a new screen.
      Overrides:
      prepare in class GuiComponent