4

I'm trying to display a dialog in the middle of the screen. But I couldn't change the size of the dialog with setWidth() or setHeight(). I have the following code:

private void showDialog() {
    Window.WindowStyle dialogStyle = new Window.WindowStyle();
    dialogStyle.background = new TextureRegionDrawable(new TextureRegion(dialog_bg));
    dialogStyle.titleFont = gameFont;
    Dialog dialog = new Dialog("Test Dialog", dialogStyle);
    dialog.setWidth(200);   // will be ignored
    dialog.show(stage);
}

Any ideas?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    I'm not sure, but seems background determines dialog size, check it. – Metaphore Aug 18 '14 at 12:39
  • 1
    Yes, it has to do with the background size. When I use dialog.show(), the dialog will automatically packed. I solved the problem by using stage.addActor(dialog) instead of dialog.show() and then center the dialog manually with dialog.setPosition(). –  Aug 18 '14 at 14:37
  • Isn't there any other solution ? Becasue it'd be nice to continue to use the show method instead. – Luca Marzi Oct 05 '15 at 16:46
  • Take a look [here](http://gamedev.stackexchange.com/questions/73789/how-to-correctly-set-window-size-in-libgdx-scene2d-ui-dialog/109284#109284) – Luca Marzi Oct 05 '15 at 16:57

1 Answers1

2

Just call your changes to the size after you call dialog.show(). In this case, I am using a Stage with a FitViewport, so I scale down my actors so that they fit. When I adjust their size, I have to factor in the scale.

public void create(Dialog dialog, float scale){
    dialog.setScale(scale);
    dialog.setMovable(false);

    dialog.text("Are you sure you want to yada yada?");
    dialog.button("Yes", true); //sends "true" as the result
    dialog.button("No", false); //sends "false" as the result
}

public void show(Stage st) {
    dialog.show(st); //pack() is called internally
    dialog.setWidth(worldWidth/scale); //we override changes made by pack()
    dialog.setY(Math.round((st.getHeight() - dialog.getHeight()) / 2)); //we override changes made by pack()
}
AndreGraveler
  • 437
  • 6
  • 12