I am using ImageButton in my game and I want to show it touched or pressed. How can I accomplish this, I am new to libgdx framework.
TextureRegion btLeft = new TextureRegion(new Texture("NUMBEROFF.png"));
Drawable drawableLeft = new TextureRegionDrawable(new TextureRegion(btLeft));
buttonLeft = new ImageButton(drawableLeft);
There are different constructors for ImageButton that allows you to add multiple Drawables for when the button is in different states (up, down, and checked). But I prefer to use the ImageButtonStyle to do this.
https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ImageButton.ImageButtonStyle.html
ImageButtonStyle style = new ImageButtonStyle();
style.imageUp = ...your drawable for up (normal) state
style.imageChecked = ...your drawable for checked state
style.imageDown = ...your drawable for down (pressed) state
ImageButton button = new ImageButton(style);
Related
In my game there is a menu, in this menu, there are 4 tabs and each tab have its own stage with tables scrollpanes and buttons (is it a good idea ?)
My problem is that every stage seems "frozen", buttons are not responding and scrollpanes don't scroll
My menu structure :
Menu class
-> render a tab (render selectedTab, selectedTab is a Tab object (custom class) that is asigned with a specific tab (Ex: sele shopTab (extend tab class))
// menu class
private Tab selectedTab;
private Tab RecipeTab, SellTab, UpgradeTab, ShopTab;
// menu constructor
RecipeTab = new RecipeTab(viewport, sb, itemsdata);
SellTab = new SellTab(viewport, sb, hud);
UpgradeTab = new UpgradeTab(viewport, sb, itemsdata);
ShopTab = new ShopTab(viewport, sb);
selectedTab = RecipeTab;
// render
selectedTab.render(sr, delta);
// on tab change
public void setSelectedTab(Tab newTab) {selectedTab = newTab;}
setSelectedTab(ShopTab);
-> menu contain a navbar to switch tabs (the selected tab is asigned with another tab object)
I don't know if the issue comes from the stages or the actors. even a simple textbutton doesn't work
// how my stages are made
// constructor
this.stage = new Stage(viewport, sb); // ExtendViewport (same everywhere), Spritebatch
Gdx.input.setInputProcessor(this.stage);
// render
this.stage.draw();
this.stage.act(delta);
I figured it out.
The answer was simple, there was a conflict with the inputProcessor
i was seting the inputProcessoron each tabs
so i added a setInputProcessor() method to the tabs a called it on tab change
I want to write score inside of a box image.
First I placed the box in the place then I added score string above it using label like this.
private void drawScoreBoxes() {
scoreImage = new Image(scoreTexture);
stage.addActor(scoreImage);
scoreImage.setPosition(UiConstants.BOX_POS_X,UiConstants.BOX_POS_Y2);
}
private void drawScore() {
Label.LabelStyle scoreStyle = new Label.LabelStyle();// style
scoreStyle.font = game.font2;
scoreLabel2 = new Label(scoreController.getScoreString(), scoreStyle);// label
scoreLabel2.setPosition(scoreImage.getX()+scoreImage.getWidth()/2,scoreImage.getY());
}
The score string should come exactly in the middle of the box image.In this case,when number of digit of score increases,string alignment is not proper.
How can I align it center always?
I found methods like getLabelAlign(),setAlignment(alignment) etc.
But I don't know how to use it properly.
You have two options:
1. Use TextButton:
Drawable scoreDrawable = new TextureRegionDrawable(new TextureRegion(scoreTexture));
TextButtonStyle textButtonStyle = new TextButtonStyle( scoreDrawable, scoreDrawable, scoreDrawable, font );
TextButton tb = new TextButton( scoreController.getScoreString() );
tb.setDisabled( true );
Or 2. Set Label bounds and align text:
LabelStyle labelStyle = new LabelStyle( font, Color.BLACK );
Label scoreLabel = new Label( scoreController.getScoreString(), labelStyle );
scoreLabel.setBounds( scoreImage.getX(), scoreImage.getY(), scoreImage.getWidth(), scoreImage.getHeight() );
scoreLabel.setAlignment( Align.center );
Use TextButton instead of Image + Label
TextButton is a Button that contains Label, you can pass Image drawable part to Button that show as background part of your Label.
button = new TextButton("Button1", textButtonStyle);
stage.addActor(button);
A TextButton takes a string to render and a ButtonStyle, in this case a TextButtonStyle, which is basically a class that contains all the information about the button (font, drawable to render while not pressed, drawable to render while pressed etc).
To make my question clearer , I created a simple test class as below.
`public void create () {
stage = new Stage();
// GP
Texture texture1 = new Texture(Gdx.files.internal("data/badlogic.jpg"));
TextureRegion tr = new TextureRegion(texture1);
Image image = new Image(tr);
Group gp = new Group();
ImageButton ib = new ImageButton(new Image(tr).getDrawable());
ib.setSize(90, 256);
gp.addActor(ib);
gp.setPosition(0, 0);
stage.addActor(gp);
gp.debugAll();
}`
The size of badlogic logo is 256*256. I set it to 90*256 in the test.But the outcome looks like this http://imgsrc.baidu.com/forum/w%3D580/sign=fb0e4402d21373f0f53f6f97940e4b8b/0958c32397dda1448779b0cbb1b7d0a20df486e2.jpg
If i set the image size before sending it to imagebutton, then it will rendered as 256*256 ,the full original size. This function is so simple. I can not figure out where is going wrong.
First of all, last update of Libgdx project is 1.4.1: http://libgdx.badlogicgames.com/download.html
The problem is the Drawable object that you are sending to the ImageButton, if you want to change the size of the Image or Texture before adding it to the ImageButton (so you will solve the problem asked), you will just need to use the .setBounds() method
I write down a little example:
1. Initialize Image object with your image file:
`Image image = new Image( new Texture ( Gdx.Files.internal("yourPathFile.png") ) );`
2. .setBounds() javadoc: (x coordinate, y coordinate, width of the image, height of the image)
image.setBounds(0f, 0f, 50f, 60f)
then you can send the image to the ImageButton object as Drawable.
`ImageButton myButton = new ImageButton(image.getDrawable());'
That's all.
Here you have some constructors of ImageButton object from the Libgdx official javadoc.
See also:
Release versions: http://libgdx.badlogicgames.com/releases/
ImageButton javadoc: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ImageButton.html
I am using libgdx + scene2d + box2d. Everything looked ok until I decided to replaced my TextButtons to ImageButton.
I can't get my ImageButton rezided, it always follows the image dimensions.
Here is my Stage:
stage = new Stage(new StretchViewport(camera.viewportWidth,camera.viewportHeight));
This how a create my Button:
ImageButtonStyle style = new ImageButtonStyle();
style.imageUp = new TextureRegionDrawable(textureButtonPlay);
ImageButton buttonPlay = new ImageButton(style);
buttonPlay.setSize(50, 20); //this does not work
This how a create my Window and add my Button to it:
WindowStyle windowStyle = new WindowStyle(new BitmapFont(), Color.WHITE, null);
windowTryAgain = new Window("", windowStyle);
windowTryAgain.setMovable(false);
//windowTryAgain.padTop(20);
windowTryAgain.setSize(150,150);
windowTryAgain.setPosition(camera.viewportWidth/2 - windowTryAgain.getWidth()/2,
camera.viewportHeight/2 - windowTryAgain.getHeight()/2);
windowTryAgain.row().fill().expandX();
windowTryAgain.add(buttonPlay).padTop(25).expandX().fillX();
windowTryAgain.center();
Is it possible to resize the Texture or TextureRegion? What would be the best approach?
My has issue has been resolved with following piece of code:
windowTryAgain.add(buttonMenu).width(100).height(100);
Then the image will resized accordiantly to the cell width and height.
Credits:
scene2d button scaling with libgdx
I am using libGDX 1.0 and not sure if it is a bug or not.
My ImageButton is displayed correctly on a LG OPtimus and HTC Desire using TextureAtlas. However, on a Galaxy Tab2 the it becomes completely back, but it is still clickable.
If I load its texture from the AssetsManger, everything works fine on any device.
Here is the relevant part of the code:
stage = new Stage(new StretchViewport(fontCamera.viewportWidth,fontCamera.viewportHeight));
ImageButtonStyle styleButtonPlay = new ImageButtonStyle();
//this works
//textureButtonPlay = new TextureRegion(game.manager.get("data/ui/play.png",Texture.class));
//styleButtonPlay.imageUp = new TextureRegionDrawable(textureButtonPlay);
//this don't on Samsung Tab2
atlas = new TextureAtlas("data/shot/atlas.pack");
styleButtonPlay.imageUp = new TextureRegionDrawable(atlas.findRegion("ui/play"));
buttonPlay = new ImageButton(styleButtonPlay);
buttonPlay.addListener(
new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
startGame();
return true;
}
});
buttonPlay.setSize(80, 60);
buttonPlay.setPosition(camera.viewportWidth/2 - buttonPlay.getWidth()/2,5);
stage.addActor(buttonPlay);
I now am concerned about packing all my sprites onto single imaged.
The problem has been resolved by creating a texturepack.png with the size smaller than 4096x4096.
For some reason it was ok only on some devices and no similar problem has been noticed on the desktop.
Hope this help someone...
see your code here:
atlas = new TextureAtlas("data/shot/atlas.pack");
style.imageUp = new TextureRegionDrawable(atlas.findRegion("ui/play"));
^^^^^
buttonPlay = new ImageButton(styleButtonPlay);
^^^^^^^^^^^^^^^
it looks like you assign the wrong style to your imageButton, try putting the drawable into the correct button style ("styleButtonPlay" instead "style"):
styleButtonPlay.imageUp = new TextureRegionDrawable(atlas.findRegion("ui/play"));