Set size for checkbox image in libgdx UI
Asked Answered
V

2

7

I can't figure out how to manage checkbox images size. Of course, it is possible to create different size of image in my Texture atlas and take appropriate one, but I don't want to do that.

Here is my code:

AtlasRegion checkboxOn = AssetsHelper.textures.findRegion("checked");
AtlasRegion checkboxOff = AssetsHelper.textures.findRegion("unchecked");
CheckBoxStyle checkBoxStyle = new CheckBoxStyle();
checkBoxStyle.font = AssetsHelper.font66yellow;
checkBoxStyle.checkboxOff = checkboxOff;
checkBoxStyle.checkboxOn = checkboxOn;
CheckBox cbSound = new CheckBox(" Sound", checkBoxStyle);

cbSound object doesn't have such methods to rezise image of checkbox, but there is method getImage(), but seems it doesn't work too. This is not working:

cbSound.getImage().width = 120;
cbSound.getImage().height = 120;

FYI: for example, when I wanted to draw image I did like that:

batch.draw(textureRegion, 0, 0, widthIwant, heightIwant);

But in CheckBox class there is overrided only this (without setting width and height):

public void draw (SpriteBatch batch, float parentAlpha) {
    image.setRegion(isChecked ? style.checkboxOn : style.checkboxOff);
    super.draw(batch, parentAlpha);
}

Question: how can I change width and height of checkbox image?

Thanks in advance.

Vonnie answered 19/3, 2013 at 20:2 Comment(0)
H
8

The libgdx widgets are using drawables for drawing images. A drawable gets automatically scaled to fit the cell it is in. So in order to change the image size, change the cell size:

cbSound.getCells().get(0).size(widht, height);

For better results, you should use a nine patch for the drawable.

Hagar answered 20/3, 2013 at 18:56 Comment(1)
Thanks a lot, but also need to set height for actor cbSound.height = height, because if not than will be big clickable region (as real region height).Vonnie
E
1

You need to set the image scaling type. Also method getImageCell is more correct than method getCells().get(0). Default is none.

CheckBox soundCB = new CheckBox("Sound", uiskin);
soundCB.getImage().setScaling(Scaling.fill);
soundCB.getImageCell().size(GameConfig.WIDTH/6);
soundCB.left().pad(PAD);
soundCB.getLabelCell().pad(PAD);
//...
content.add(soundCB).width(GameConfig.WIDTH/1.5f).row(); //add to table
Ellette answered 23/7, 2019 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.