3
\$\begingroup\$

In my libGDX application I am using the scene2d.ui classes to render my game's GUI. One of the things I render via these classes is a dialog window with a background image set. I have created the uiskin.json file accordingly, and the dialog is shown correctly with the background image.

There is one catch however: The size of the dialog window is exactly set to fit the text elements, but not more. If not much text is shown on the dialog, then the background image is rendered much smaller than it really is.

I can override or hard-code the window size by overriding getPrefWidth() and getPrefHeight() in my custom Dialog class. But that does not feel right.

My question: What is the correct way to set a scene2d.ui dialog with a background image to be as large as that background image, regardless of how much text is being shown?

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

It's easy, you just need to set the width during the insertion of the items on your table.

Example:

dialog.getButtonTable().add(btnSound).width(900);
\$\endgroup\$
2
\$\begingroup\$

Or you can also set directly the dimensions of the background image:

dialog.getBackground().setMinWidth(200);
dialog.getBackground().setMinHeight(200);

Actually I don't know if this solution has some side effect because it might be that other dialogs use the same background among you game. I think that it's a pity that the Dialog hasn't a inner method that allows to set it's dimensions.

\$\endgroup\$
2
\$\begingroup\$

Actually I've found another solution in these days that is the following: You could extend the Dialog class and create your own custom dialog then you override the methods that specify the preferred size of the dialog to the Layout.

public class MyDialog extends Dialog {

    @Override
    public float getPrefWidth() {
        return MY_WIDTH;
    }

    @Override
    public float getPrefHeight() {
        return MY_HEIGHT;
    }

}

This way you are sure the Dialog will be "forced" to have the dimension defined. A good solution is to create a constructor that accepts a width/height parameters.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .