libgdx API

com.badlogic.gdx.scenes.scene2d.ui
Class Slider

java.lang.Object
  extended by com.badlogic.gdx.scenes.scene2d.Actor
      extended by com.badlogic.gdx.scenes.scene2d.ui.Widget
          extended by com.badlogic.gdx.scenes.scene2d.ui.Slider
All Implemented Interfaces:
Layout

public class Slider
extends Widget

A value slider.

Functionality

A slider lets you select a value within a range (min, max), with stepping between each value the slider represents. To listen for changes of the slider value one can register a Slider.ValueChangedListener with the slider.

Layout

A slider's (preferred) width and height are determined by the parameter past to its constructor as well as the maximum height of the NinePatch and TextureRegion involved in the display of the slider. Use #setPrefSize(int, int) to programmatically change the size to your liking. In case the width and height you set are to small you will see artifacts.

The slider background will only be stretched in the x-axis. The slider handle will be centered on the background vertically.

Style

A slider is a Widget displaying a horizontal background NinePatch, stretched on the x-axis and using the total height of the NinePatch on the y-axis, as well as a TextureRegion for the slider handle. The style is defined via an instance of Slider.SliderStyle, which can be either done programmatically or via a Skin.

A Slider's style definition in a skin XML file should look like this:
 <slider name="styleName" 
         slider="sliderPatch" 
         knob="knobRegion"/>
 
 

Author:
mzechner

Nested Class Summary
static class Slider.SliderStyle
          Defines the style of a slider, see Slider.
static interface Slider.ValueChangedListener
          Interface to listen for changes of the value of the slider.
 
Field Summary
 
Fields inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
invalidated
 
Fields inherited from class com.badlogic.gdx.scenes.scene2d.Actor
actions, color, height, name, originX, originY, parent, rotation, scaleX, scaleY, touchable, visible, width, x, y
 
Constructor Summary
Slider(float min, float max, float steps, Skin skin)
           
Slider(float min, float max, float steps, Slider.SliderStyle style)
           
Slider(float min, float max, float steps, Slider.SliderStyle style, java.lang.String name)
          Creates a new slider.
 
Method Summary
 void draw(SpriteBatch batch, float parentAlpha)
          Draws the Actor.
 float getPrefHeight()
           
 float getPrefWidth()
           
 float getValue()
           
 Actor hit(float x, float y)
           
 void layout()
          Positions and sizes each child of this actor.
 void setRange(float min, float max)
          Sets the range of this slider.
 void setStyle(Slider.SliderStyle style)
          Sets the style of this widget.
 void setValue(float value)
          Sets the value of this slider
 Slider setValueChangedListener(Slider.ValueChangedListener listener)
          Sets the Slider.ValueChangedListener of this slider.
 boolean touchDown(float x, float y, int pointer)
           
 void touchDragged(float x, float y, int pointer)
           
 void touchUp(float x, float y, int pointer)
           
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, invalidate, invalidateHierarchy
 
Methods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
act, action, clearActions, isMarkedToRemove, keyDown, keyTyped, keyUp, markToRemove, remove, scrolled, toLocalCoordinates, toString, touchMoved
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Slider

public Slider(float min,
              float max,
              float steps,
              Skin skin)

Slider

public Slider(float min,
              float max,
              float steps,
              Slider.SliderStyle style)

Slider

public Slider(float min,
              float max,
              float steps,
              Slider.SliderStyle style,
              java.lang.String name)
Creates a new slider. It's width is determined by the given prefWidth parameter, its height is determined by the maximum of the height of either the slider NinePatch or slider handle TextureRegion. The min and max values determine the range the values of this slider can take on, the steps parameter specifies the distance between individual values. E.g. min could be 4, max could be 10 and steps could be 0.2, giving you a total of 30 values, 4.0 4.2, 4.4 and so on.

Parameters:
min - the minimum value
max - the maximum value
steps - the step size between values
style - the Slider.SliderStyle
prefWidth - the (preferred) width
name - the name
Method Detail

setStyle

public void setStyle(Slider.SliderStyle style)
Sets the style of this widget.

Parameters:
style -

layout

public void layout()
Description copied from interface: Layout
Positions and sizes each child of this actor. Subsequent calls will not have any affect unless Layout.invalidate() is called.


draw

public void draw(SpriteBatch batch,
                 float parentAlpha)
Description copied from class: Actor
Draws the Actor. The spriteBatch is configured so that the Actor can draw in its parents coordinate system. The parent's alpha is passed to the method in order for the Actor to multiply it with its own alpha. This will allow FadeIn and other Actions to have an effect even if they are only set on the parent of the Actor.

Specified by:
draw in class Actor
Parameters:
batch - the spritebatch to render with
parentAlpha - the parent's alpha value.

touchDown

public boolean touchDown(float x,
                         float y,
                         int pointer)
Specified by:
touchDown in class Actor

touchUp

public void touchUp(float x,
                    float y,
                    int pointer)
Specified by:
touchUp in class Actor

touchDragged

public void touchDragged(float x,
                         float y,
                         int pointer)
Specified by:
touchDragged in class Actor

hit

public Actor hit(float x,
                 float y)
Overrides:
hit in class Widget

setValueChangedListener

public Slider setValueChangedListener(Slider.ValueChangedListener listener)
Sets the Slider.ValueChangedListener of this slider.

Parameters:
listener - the listener or null
Returns:
this Slider for chaining

getValue

public float getValue()
Returns:
the current value of the slider

setValue

public void setValue(float value)
Sets the value of this slider

Parameters:
value - the value

setRange

public void setRange(float min,
                     float max)
Sets the range of this slider. The slider's current value is reset to min.

Parameters:
min - the minimum value
max - the maximum value

getPrefWidth

public float getPrefWidth()

getPrefHeight

public float getPrefHeight()

libgdx API

Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)