I’m currently working on UI system for my game using Scene2d. I want buttons to respond right after I click them, so I override ClickListener touchDown method instead of using clicked method.
This button opens a table with more buttons, if the table is already open, it closes it:
// Save map button.
saveMap = new ImageButton(new ImageButtonStyle());
saveMap.getStyle().imageUp = uiSkin.getDrawable("save");
saveMap.getStyle().up = uiSkin.getDrawable("button_flat_up");
saveMap.getStyle().down = uiSkin.getDrawable("button_flat_down");
saveMap.addListener(new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (button != Buttons.LEFT) {
return false;
}
Game.resources.getSound("click_2").play();
if (saveBox.isVisible()) {
saveBox.setVisible(false);
return false;
}
saveBox.setVisible(true);
return true;
}
});
This is how the table looks like:
public class InputBox {
public static float OFFSET = 20;
private Table table;
private TextField inputField;
private TextButton okButton;
private TextButton closeButton;
private String saveText;
public InputBox(Skin skin, float x, float y, float width, float height, boolean visible) {
table = new Table();
inputField = new TextField("", skin);
okButton = new TextButton("Ok", skin);
okButton.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (button != Buttons.LEFT) {
return false;
}
Game.resources.getSound("click_2").play();
table.setVisible(false);
if (inputField.getText().equals("")) {
saveText = null;
return false;
}
saveText = inputField.getText();
return true;
}
});
closeButton = new TextButton("Close", skin);
closeButton.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (button != Buttons.LEFT) {
return false;
}
closeButton.setChecked(false);
Game.resources.getSound("click_2").play();
saveText = null;
table.setVisible(false);
return true;
}
});
table.setVisible(visible);
table.setX(x);
table.setY(y);
table.setWidth(width);
table.setHeight(height);
table.add(inputField).colspan(2).center().width(width - OFFSET).expand().padRight(2);
table.row();
table.add(okButton).width(width / 2 - OFFSET / 2).right();
table.add(closeButton).width(width / 2 - OFFSET / 2).left();
table.pad(5);
}
...
// This sets the visibility of the table
public void setVisible(boolean visible) {
table.setVisible(visible);
}
...
}
Now the issue that I’m having, is if I hide the table by clicking on one of its buttons and reopen it again, it displays itself with the last button being held down if I’m holding my left mouse button down on the reopen button (in this case the “saveMap” button).
I’ve recorded a video showing the issue: https://www.youtube.com/watch?v=Jf3WzU8G4dE