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
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.
-
Field Summary
Fields inherited from class GuiComponent
ICON_FONT -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSlider(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. -
Method Summary
Modifier and TypeMethodDescriptionGets the first arrow button (e.g. decrease).Gets the second arrow button (e.g. increase).Gets the spritesheet used to render the arrow buttons.Gets the list of registered change listeners.floatGets the currently selected value.floatGets the maximum selectable value.floatGets the minimum selectable value.protected abstract floatComputes a normalized value (typically in[0, 1]) corresponding to the current mouse location relative to the slider.abstract Point2DComputes the relative location of the slider thumb within the slider for the current value.Gets the slider thumb component.Gets the spritesheet used to render the slider thumb.intgetSteps()Computes the number of discrete steps betweengetMinValue()andgetMaxValue()given the current step size.floatGets the step size between adjacent selectable values.floatGets the relative size factor used when rendering the slider's tick marks.protected voidInfers the current value from the current mouse location and updates the slider accordingly.booleanReturns whether the slider is currently being dragged by the user.booleanReturns whether the slider is rendering tick marks.voidRegisters a callback that is invoked whenever the slider value changes.voidprepare()Prepare the GuiComponent and all its child Components (Makes the GuiComponent visible and adds mouse listeners.).voidrender(Graphics2D g) Note: If you override this and are modifying swing components, be sure you are in the AWT thread when you do so!protected abstract voidRenders the slider bar (track).protected abstract voidRenders the slider tick marks.protected voidsetButton1(ImageComponent button1) Sets the first arrow button (typically used to decrease the slider value).protected voidsetButton2(ImageComponent button2) Sets the second arrow button (typically used to increase the slider value).voidsetButtonSpritesheet(Spritesheet buttonSprite) Sets the spritesheet used to render the arrow buttons.voidsetCurrentValue(float newValue) Sets the currently selected value.voidsetMaxValue(float maxValue) Sets the maximum selectable value and refreshes the slider dimensions.voidsetMinValue(float minValue) Sets the minimum selectable value and refreshes the slider dimensions.voidsetShowTicks(boolean showTicks) Sets whether the slider should render tick marks.protected voidsetSliderComponent(ImageComponent slider) Sets the slider thumb component.voidsetSliderSpritesheet(Spritesheet sliderSprite) Sets the spritesheet used to render the slider thumb.voidsetStepSize(float stepSize) Sets the step size between adjacent selectable values and refreshes the slider dimensions.voidsetTickSize(float tickSize) Sets the relative size factor used when rendering the slider's tick marks.protected booleanshouldDecreaseOnKey(int keyCode) Determines whether the slider value should be decreased when the given key is typed.protected booleanshouldIncreaseOnKey(int keyCode) Determines whether the slider value should be increased when the given key is typed.protected abstract voidUpdates the geometry of the slider in response to a change in min/max value or step size.Methods inherited from class GuiComponent
addRenderedListener, addRenderListener, getAppearance, getAppearanceDisabled, getAppearanceHovered, getBoundingBox, getCenterX, getCenterY, getClickConsumer, getComponentId, getComponents, getCurrentAppearance, getFocusedComponent, getFont, getHeight, getHoverConsumer, getHoverSound, getLocation, getMouseDraggedConsumer, getMouseEnterConsumer, getMouseLeaveConsumer, getMouseMovedConsumer, getMousePressedConsumer, getMouseReleasedConsumer, getMouseWheelConsumer, getName, getShape, getTag, getText, getTextAlign, getTextAngle, getTextShadowColor, getTextShadowRadius, getTextToRender, getTextValign, getTextX, getTextY, getTweenValues, getWidth, getX, getY, hasAutomaticLineBreaks, hasInputFocus, hasTextAntialiasing, hasTextShadow, initializeComponents, isAutoScaling, isEnabled, isFocusable, isForwardMouseEvents, isHovered, isPressed, isSelected, isSuspended, isVisible, mouseClicked, mouseDragged, mouseEntered, mouseEventShouldBeForwarded, mouseExited, mouseMoved, mousePressed, mouseReleased, mouseWheelMoved, onClicked, onHovered, onMouseDragged, onMouseEnter, onMouseLeave, onMouseMoved, onMousePressed, onMouseReleased, onMouseWheelScrolled, onResolutionChanged, onTextChanged, removeListener, removeListener, setAutomaticLineBreaks, setAutoScaling, setDimension, setEnabled, setFocusable, setFont, setFontSize, setForwardMouseEvents, setHeight, setHovered, setHoverSound, setLocation, setLocation, setName, setSelected, setTag, setText, setTextAlign, setTextAngle, setTextAntialiasing, setTextShadow, setTextShadowColor, setTextShadowRadius, setTextValign, setTextX, setTextY, setTweenValues, setVisible, setWidth, setX, setY, suspend, toggleSelection, toggleSuspension
-
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 slidery- the y position of the sliderwidth- the width of the sliderheight- the height of the sliderminValue- the minimum selectable valuemaxValue- the maximum selectable valuestepSize- the step size between adjacent selectable values
-
-
Method Details
-
getButton1
Gets the first arrow button (e.g. decrease).- Returns:
- the first button component
-
setButton1
Sets the first arrow button (typically used to decrease the slider value).- Parameters:
button1- the button component to register
-
getButton2
Gets the second arrow button (e.g. increase).- Returns:
- the second button component
-
setButton2
Sets the second arrow button (typically used to increase the slider value).- Parameters:
button2- the button component to register
-
getButtonSpritesheet
Gets the spritesheet used to render the arrow buttons.- Returns:
- the button spritesheet
-
setButtonSpritesheet
Sets the spritesheet used to render the arrow buttons.- Parameters:
buttonSprite- the spritesheet to use
-
getChangeConsumer
-
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 betweengetMinValue()andgetMaxValue()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
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
Gets the slider thumb component.- Returns:
- the slider thumb component
-
setSliderComponent
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
Description copied from class:GuiComponentNote: If you override this and are modifying swing components, be sure you are in the AWT thread when you do so!- Specified by:
renderin interfaceIRenderable- Overrides:
renderin classGuiComponent- Parameters:
g- The current graphics object onto which this instance will render its visual contents.- See Also:
-
renderBar
Renders the slider bar (track).- Parameters:
g- the graphics context to draw to
-
renderTicks
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:
trueif 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:
trueif the value should be decreased
-
getSliderSpritesheet
Gets the spritesheet used to render the slider thumb.- Returns:
- the slider spritesheet
-
setSliderSpritesheet
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:
trueif the slider thumb is being dragged
-
isShowingTicks
public boolean isShowingTicks()Returns whether the slider is rendering tick marks.- Returns:
trueif tick marks are shown
-
setShowTicks
public void setShowTicks(boolean showTicks) Sets whether the slider should render tick marks.- Parameters:
showTicks-trueto show tick marks
-
onChange
-
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:GuiComponentPrepare 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:
preparein classGuiComponent
-