libgdx API

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

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.ImageToggleButton
All Implemented Interfaces:
Layout

public class ImageToggleButton
extends Widget

A toggle button with an image on it.

Functionality

A toggle button can be either in a pressed or unpressed state. A ImageToggleButton.ClickListener can be registered with the ImageToggleButton which will be called in case the button changed its state.

Layout

The (preferred) width and height of an ImageToggleButton are derrived from the border patches in the background NinePatch as well as the width and height of the TextureRegion of the image displayed inside the ImageToggleButton. Use Widget.setPrefSize(int, int) to programmatically change the size to your liking. In case the width and height you set are to small for the contained image you will see artifacts.

Style

An ImageToggleButton is a Widget displaying a background NinePatch as well as image in form of a TextureRegion. The style is defined via an instance of ImageToggleButton.ImageToggleButtonStyle, which can be either done programmatically or via a Skin.

A Button's style definition in a skin XML file should look like this:
 <imagetogglebutton name="styleName" 
                    down="downNinePatch" 
                    up="upNinePatch" 
                    />
 
 
Note that the image's TextureRegion is defined at construction time or via setImageSize(float, float)

Author:
mzechner

Nested Class Summary
static interface ImageToggleButton.ClickListener
          Interface for listening to click events.
static class ImageToggleButton.ImageToggleButtonStyle
          Defines an image toggle button style, see ImageToggleButton
 
Field Summary
 
Fields inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
invalidated, prefHeight, prefWidth
 
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
ImageToggleButton(java.lang.String name, TextureRegion image, float imageWidth, float imageHeight, ImageToggleButton.ImageToggleButtonStyle style)
          Creates a new image toggle button.
ImageToggleButton(java.lang.String name, TextureRegion image, ImageToggleButton.ImageToggleButtonStyle style)
          Creates a new image toggle button.
ImageToggleButton(TextureRegion image, ImageToggleButton.ImageToggleButtonStyle style)
           
ImageToggleButton(TextureRegion image, Skin skin)
           
 
Method Summary
 void draw(SpriteBatch batch, float parentAlpha)
          Draws the Actor.
 TextureRegion getImage()
           
 boolean isPressed()
           
 void layout()
          Positions and sizes each child of this actor.
 ImageToggleButton setClickListener(ImageToggleButton.ClickListener listener)
          Sets the ImageToggleButton.ClickListener
 void setImage(TextureRegion image)
          Sets the image's TextureRegion.
 void setImageSize(float width, float height)
          Sets the image's size.
 void setPressed(boolean isPressed)
          Sets whether this ImageToggleButton is down.
 void setStyle(ImageToggleButton.ImageToggleButtonStyle style)
          Sets the style of this widget.
 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
getPrefHeight, getPrefWidth, hit, invalidate, invalidateHierarchy, setPrefSize
 
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

ImageToggleButton

public ImageToggleButton(TextureRegion image,
                         Skin skin)

ImageToggleButton

public ImageToggleButton(TextureRegion image,
                         ImageToggleButton.ImageToggleButtonStyle style)

ImageToggleButton

public ImageToggleButton(java.lang.String name,
                         TextureRegion image,
                         ImageToggleButton.ImageToggleButtonStyle style)
Creates a new image toggle button. The size is determined by the image's size as well as the style.

Parameters:
name - the name
image - the image' TextureRegion
style - the ImageToggleButton.ImageToggleButtonStyle

ImageToggleButton

public ImageToggleButton(java.lang.String name,
                         TextureRegion image,
                         float imageWidth,
                         float imageHeight,
                         ImageToggleButton.ImageToggleButtonStyle style)
Creates a new image toggle button. The size is determined by the image's size as well as the style.

Parameters:
name - the name
image - the image' TextureRegion
imageWidth - the image's width
imageHeight - the image's height
style - the ImageToggleButton.ImageToggleButtonStyle
Method Detail

setStyle

public void setStyle(ImageToggleButton.ImageToggleButtonStyle style)
Sets the style of this widget. Calls Widget.invalidateHierarchy() internally.

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

getImage

public TextureRegion getImage()
Returns:
the image's TextureRegion

setImage

public void setImage(TextureRegion image)
Sets the image's TextureRegion. Invalidates all parents. Keeps the previously set image size.

Parameters:
image - the image's TextureRegion

setImageSize

public void setImageSize(float width,
                         float height)
Sets the image's size. Invalidates all parents

Parameters:
width - the image's width
height - the image's height

setClickListener

public ImageToggleButton setClickListener(ImageToggleButton.ClickListener listener)
Sets the ImageToggleButton.ClickListener

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

isPressed

public boolean isPressed()
Returns:
whether this ImageToggleButton is down or not

setPressed

public void setPressed(boolean isPressed)
Sets whether this ImageToggleButton is down.

Parameters:
isPressed - whether this button is down

libgdx API

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