libgdx API

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

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

public class Button
extends Widget

A button with text on it.

Functionality

A button can be either in a pressed or unpressed state. A Button.ClickListener can be registered with the Button which will be called in case the button was clicked/touched.

Layout

The (preferred) width and height of a Button are derrived from the border patches in the background NinePatch as well as the bounding box around the multi-line text displayed on the Button. 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 text you will see artifacts.

Style

A Button is a Widget displaying a background NinePatch as well as multi-line text with a specific font and color. The style is defined via an instance of Button.ButtonStyle, which can be either done programmatically or via a Skin.

A Button's style definition in a skin XML file should look like this:
 <button name="styleName" 
         down="downNinePatch" 
         up="upNinePatch" 
         font="fontName" 
         fontColor="colorName"/>/>
 
 

Author:
mzechner

Nested Class Summary
static class Button.ButtonStyle
          Defines a button style, see Button
static interface Button.ClickListener
          Interface for listening to click events of a button.
 
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
Button(java.lang.String text, Button.ButtonStyle style)
           
Button(java.lang.String text, Skin skin)
           
Button(java.lang.String name, java.lang.String text, Button.ButtonStyle style)
           
 
Method Summary
 void draw(SpriteBatch batch, float parentAlpha)
          Draws the Actor.
 java.lang.String getText()
           
 void layout()
          Positions and sizes each child of this actor.
 Button setClickListener(Button.ClickListener listener)
          Sets the Button.ClickListener of this button
 void setStyle(Button.ButtonStyle style)
          Sets the style of this widget.
 void setText(java.lang.String text)
          Sets the multi-line label text of this button.
 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

Button

public Button(java.lang.String text,
              Skin skin)

Button

public Button(java.lang.String text,
              Button.ButtonStyle style)

Button

public Button(java.lang.String name,
              java.lang.String text,
              Button.ButtonStyle style)
Method Detail

setStyle

public void setStyle(Button.ButtonStyle 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

setText

public void setText(java.lang.String text)
Sets the multi-line label text of this button. Causes invalidation of all parents.

Parameters:
text -

getText

public java.lang.String getText()
Returns:
the label text of this button

setClickListener

public Button setClickListener(Button.ClickListener listener)
Sets the Button.ClickListener of this button

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

libgdx API

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