Libgdx button Textbutton ImageButton imagetextbutton buttons use

Source: Internet
Author: User
Tags gety libgdx

button is one of the most commonly used elements in the game interface. The button has a status of 3: Pressed, unpressed, checked. The checked state is triggered when the button is clicked. The same button is also a table, which can be used as a container for other actors.

The preferred size of the button is determined by the background picture and the loaded actors inside. The ChangeEvent event is triggered when the button is clicked, and the checked state is saved. Initialize function via button
Public Button (drawable up) {
This (new ButtonStyle (UP, NULL, NULL));
}
We know that we can not define buttonstyle or skins when initializing the button, and we can save a lot of time to practice. But in order to make the game more beautiful, we recommend the use of Uiskin.json, eliminating the time we define ButtonStyle.

    • Textbutton extends Button. A label is added to the button to display the text.

    • ImageButton extends Button. An image is added to the button to display the image.

    • Imagetextbutton extends Button. A label and an image are added to the button to display the text and the image.

The following direct display of code and pictures to more intuitive

Stage Stage;Skin Skin;Button BT;Textbutton TB;ImageButton IB;Imagetextbutton ITB;Texture Imgup, imgchecked;@Override public void Create () {stage = new stage ();Gdx. Input. Setinputprocessor(stage);Skin = new Skin (GDX. Files. Internal("Uiskin.json"));A simple button BT = new button (skin);IsChecked default is False for BT. SetPosition(GDX. Graphics. GetWidth() /2, -);Gdx. App. Log("button","Width="+ BT. GetWidth() +"Prewidth="+ BT. Getprefwidth());Gdx. App. Log("button","ischecked="+ BT. isChecked());Bt. AddListener(New Clicklistener () {@Override public void clicked (InputEvent event, floatx, floaty{//via log, you can see that the checked state switches GDX after clicking the button.. App. Log("button","ischecked="+ BT. isChecked());}        });Stage. Addactor(BT);Display text on the button TB = new Textbutton ("button", skin);Tb. SetPosition(BT. GetX(), BT. GetY() + BT. Getprefheight() +Ten);Tb. AddListener(New Clicklistener () {@Override public void clicked (InputEvent event, floatx, floaty) {if (TB. isChecked()) {TB. SetText("Clicked Me");Without resizing, the text will appear outside the button border TB. SetSize(TB. Getlabel(). Getprefwidth(), TB. Getlabel(). Getprefheight());} else {TB. SetText("button");}            }        });Stage. Addactor(TB);imgchecked = new Texture (GDX. Files. Internal("Badlogic.jpg"));Imgup = new Texture (GDX. Files. Internal("Badlogic.jpg"));Textureregion region = new Textureregion (imgchecked);Region. Flip(True, True);Textureregion Regiondown = new Textureregion (Imgup);Regiondown. Flip(True, False);After you have set the picture of Imageup Imagedown imagechecked, you can see that the image changes with it. IB = new ImageButton (New textureregiondrawable Egion (Imgup)), New Textureregiondrawable (Regiondown), new textureregiondrawable (region);Ib. SetSize( -, -);Ib. SetPosition(TB. GetX(), TB. GetY() + TB. GetHeight() + -);Ib. Debug();Stage. Addactor(IB);Imagetextbutton. ImagetextbuttonstyleItbs = new Imagetextbutton. Imagetextbuttonstyle();Get the font from the skin Itbs. Font= Skin. Get("Default-font", Bitmapfont. Class);Itbs. Imageup= new Textureregiondrawable (new Textureregion (Imgup));Itbs. Imagedown= new Textureregiondrawable (Regiondown);Itbs. imagechecked= new Textureregiondrawable (region);ITB = new Imagetextbutton ("Imagetext", Itbs);The length of the text and picture is altogether -, the default is the left side of the picture to the right text, you can re-write image ITB. SetSize( -, -);Itb. SetPosition(IB. GetX(), IB. GetY() + IB. GetHeight() + -);The default is fit, you can switch to this mode to see how the picture changes//ITB. GetImage(). setscaling(Scaling. Stretch);Itb. AddListener(New Clicklistener () {@Override public void clicked (InputEvent event, floatx, floaty) {if (ITB. isChecked()) {ITB. SetText("Click Me");} else {ITB. SetText("Imagetext");}            }        });Stage. Addactor(ITB);ITB = new Imagetextbutton ("Imagetest", style)} @Override public void render () {GDX. GL. Glclearcolor(0.39F0.58F0.92F1.0F;Gdx. GL. Glclear(GL20. GL_color_buffer_bit);Stage. Act();Stage. Draw();This event is triggered when the button is pressed if (BT. ispressed()) {GDX. App. Log("button","IsPressed");}//Triggers this event when the mouse hovers over this button if (BT. Isover()) {GDX. App. Log("button","Isover");}} @Override public void Dispose () {stage. Dispose();Skin. Dispose();Imgup. Dispose();Imgchecked. Dispose();}

Libgdx button Textbutton ImageButton imagetextbutton buttons use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.