Skip to main content
edited title
Link

Card hands Hand cards auto arrangement

Source Link

Card hands auto arrangement

I'm making a card game with libgdx scene2d and what I'm trying to achieve is auto arrange the cards in a way the all fit the screen. This is what it looks now when the hand is full: enter image description here

I have done that using a horizontalGroup and puting a -1/3 cardWidth space between the cards. I want the cards to spread evenly when some are removed. I think I have to change the space between the cards whenever a card is removed or added but I don't know how much.

This is my code:

    CardActor[] cards = new CardActor[14];
    HorizontalGroup hand = new HorizontalGroup();
    int width = Gdx.graphics.getWidth()/10;
    int height = width * 2;
    hand.setBounds(width/2, -height/4, stage.getWidth()-width, height);
    hand.space(width / -3f);
    hand.align(Align.bottom | Align.center);
    for (int i = 0; i < cards.length; i++) {
        cards[i] = new CardActor(new Card(MathUtils.random(3), MathUtils.random(3, 10)));
        hand.addActor(cards[i]);
    }
    stage.addActor(hand);`

And this is the CardActor:

public class CardActor extends ImageButton {

public CardActor(Card card) {
    super(new TextureRegionDrawable(CardTextures
            .getTexture(card.getType(), card.getRank())));

    int width = Gdx.graphics.getWidth()/10;
    int height = width*2;
    getImageCell().prefSize(width, height);
    setBounds(getX(), getY(), getWidth(), getHeight());
    addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            onSelected();
        }
    });
}