Something like this should work;
public class MyGame extends Game
{
private Stage stage;
private Texture myTexture;
private TextureRegion myTextureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private ImageButton button;
@Override
public void create()
{
myTexture = new Texture(Gdx.files.internal("myTexture.png"));
myTextureRegion = new TextureRegion(myTexture);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
button = new ImageButton(myTexRegionDrawable); //Set the button up
stage = new Stage(new ScreenViewport()); //Set up a stage for the ui
stage.addActor(button); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage); //Start taking input from the ui
}
@Override
public void render()
{
//Clear the screen, set the clear color, yada, yada
stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic
stage.draw(); //Draw the ui
}
}
ImageButton and other ui elements all extend Actor
. You can add an EventListener
to each actor to handle input. An example;
button.addListener(new EventListener()
{
@Override
public boolean handle(Event event)
{
//Handle the input event.
}
});