How can I add a button to the top right corner of the dialog in libgdx?
I want to add a close button to the top right corner of the dialog.
I've tried using setbounds with addactor and just add and set position with setize and addactor but nothing works. I know the dialog works with a table layout, it has a table for content and for buttons. I don't want to use this layout and put the button outside of this layout like on the border of the dialog.
How can i do this?
This is how it should be:
source to share
The easiest solution I could think of now is to use negative padding on your button to move it "outside" its cell.
Button closeButton = new TextButton("X", skin, "default");
getTitleTable().add(closeButton).size(60, 40).padRight(-30).padTop(-20);
With this cold-blooded hacker, you have the problem that the button will be outside of your dialog and by default Window will check the boundaries of your window when it does an evaluation Actor.hit(...)
.
We have to turn clipping off for this reason, but the rendering of the window depends on it. This is why we use another hack to enable it, just for rendering:
@Override
public void draw(Batch batch, float parentAlpha) {
setClip(true);
super.draw(batch, parentAlpha);
setClip(false);
}
source to share
Do it:
private Stage stage;
private Window window;
private Table table;
@Override
public void show() {
table = new Table();
table.setSize(Gdx.graphics.getWidth() / 2
, Gdx.graphics.getHeight() / 5);
window = new Window("", skin);
window.setSize(table.getWidth(), table.getHeight());
Button btnWindow = new Button(skin, "close");
btnWindow.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
window.setVisible(false);
}
});
window.addActor(btnWindow);
btnWindow.setSize(50, 50);
btnWindow.setPosition(window.getWidth() - btnWindow.getWidth()
, window.getHeight() - btnWindow.getHeight());
table.addActor(window);
window.setModal(true);
table.setPosition(Gdx.graphics.getWidth() / 2 - window.getWidth() / 2
, Gdx.graphics.getHeight() / 2 - window.getHeight() / 2 +
100);
window.addAction(Actions.sequence(Actions.alpha(0)
, Actions.fadeIn(.1f)
, Actions.moveTo(+50, +50, 1)));
stage.addActor(table);
}
source to share