Ed Leko
Ed Leko

Reputation: 39

ImageTextButton LibGDX

I am creating a game with a few friends, and I am currently working on the main menu. I figured out how to get regular old TextButtons, but ImageTextButtons are giving me a very hard time. I have two microphone images, one on and one off, and it should change when the user clicks it. I used a Texture Packing program, as part of this tutorial: https://www.youtube.com/watch?v=YPxd_xbnIpk

One of the numerous solutions I've been using is as follows: MenuState Class-

Private skin = new Skin;
TextureAtlas buttonAtlas = new TextureAtlas ("mutebuttons.pack");
skin.addRegions(buttonAtlas);
TextButtonStyle buttonStyle = new TextButtonStyle();
ImageTextButtonStyle buttonStyle2 = new ImageTextButtonStyle();
buttonStyle.up = skin.getDrawable("soundIconON");
buttonStyle.down = skin.getDrawable("soundIconOFF"); 

muteButtons.pack contains the on and off mic image already (soundIconON/OFF).

Let me know if you require any more info, and thanks ahead of time. Ed

Upvotes: 0

Views: 1865

Answers (1)

Madmenyo
Madmenyo

Reputation: 8584

Buttons check them selfs automatically. You don't need to do anything special and can always get the checked state by .isChecked. A button starts by default unchecked but you can change the button to checked when you create it by setChecked(true).

If you want to change the look of a button depending on it's checked state all you have to do is add the drawable. Internally it does something like If no checked state image just use default image.

Now for the type of button I think you will be fine with just a image button but any of the buttons can create a button with changeable looks. The ImageButton just allows you to add a image over the background of the button. These are the styles for the 3 different buttons:

    TextButton.TextButtonStyle textButtonStyle = 
            new TextButton.TextButtonStyle(
                    Drawable up, //Background for up state
                    Drawable down, //background for down state
                    Drawable checked, //background for checked == true
                    BitmapFont font); //font

    ImageButton.ImageButtonStyle imageButtonStyle = 
            new ImageButton.ImageButtonStyle(
                    Drawable up,
                    Drawable down,
                    Drawable checked,
                    Drawable imageUp, //Image for up state
                    Drawable imageDown, //Image for down state
                    Drawable imageChecked, //Image for checked == true
                    BitmapFont font);

    //Never actually used this one, you made me aware of it's excistance. I think this is a redundant class.
    ImageTextButton.ImageTextButtonStyle imageTextButtonStyle =
            new ImageTextButton.ImageTextButtonStyle(
                    Drawable up,
                    Drawable down,
                    Drawable checked,
                    BitmapFont font);

So all you need to do is set the checked drawable and start clicking the button.

You can set these in a skin files as well, just call the proper drawable names from json. This is how I create a simple toggle button using text button.

  com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle:
  {
    default: { up: my_up_button, checked: my_checked_button, font: default-font, fontColor: white }
  }

If I don't want text over it just hand a empty string. Alternatively you can alter the text on a click as well.

    final TextButton button = new TextButton("Ewwww! Touch me!", skin);
    button.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            if (button.isChecked())
            {
                button.setText("Now I am unchecked!");
            }
            else
            {
                button.setText("Now I am checked!");
            }
        }
    });

Upvotes: 1

Related Questions