libGDX - 即使超出边界,按钮也会设置为向下

时间:2015-02-16 01:58:39

标签: java libgdx clicklistener

我正在尝试为我的用户界面正确设置按钮点击次数。我希望我的按钮在点击后立即响应,而不是使用ClickListeners touchDown方法释放。

我创建了一个创建并显示带有文本输入的表的类:

public class InputBox {

    public static float OFFSET = 20;

    private Table table;

    private TextField inputField;

    private TextButton okButton;
    private TextButton closeButton;

    private String saveText;

    private TextButtonStyle style;

    private Skin skin;

    public InputBox(Skin skin, float x, float y, float width, float height, boolean visible) {

        this.skin = skin;

        table = new Table();

        // Creating a new style as suggested.
        style = new TextButtonStyle();
        style.up = skin.getDrawable("button_normal_up");
        style.down = skin.getDrawable("button_normal_down");
        style.font = skin.getFont("default");

        inputField = new TextField("", skin);

        okButton = new TextButton("Ok", style);     
        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", style);
        closeButton.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();

                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);
    }

    public void setUnclicked() {


        style.down = skin.getDrawable("button_normal_up"); 

        // I think these two lines ar uneeded.
        okButton.setStyle(style);
        closeButton.setStyle(style);
    }

    public void setClicked() {

        style.down = skin.getDrawable("button_normal_down");

        // Same here.
        okButton.setStyle(style);
        closeButton.setStyle(style);
    }

    public void setText(String text) {

        inputField.setText(text);
    }

    public String getSaveTextOnce() {

        String temp = saveText;

        saveText = null;

        return temp;
    }

    public void setBackground(Drawable background) {

        table.setBackground(background);
    }

    public boolean isVisible() {

        return table.isVisible();
    }

    public void setVisible(boolean visible) {

        table.setVisible(visible);
    }

    public Table getTable() { 

        return table;
    }
}

单击其他按钮后,表格将显示为可见:

// 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);  
            saveBox.setUnclicked();
            return false;
        }
        saveBox.setVisible(true);
        saveBox.setClicked();

        return true;
    }       
});

saveMap按钮的整个类:http://pastebin.com/62ZGfcch

但是,如果我通过单击“确定”或“关闭”按钮关闭表格,然后在显示表格按钮上单击并按住鼠标,则表格会重新出现,并且最后点击的按钮会被按下。

此视频说明了最新情况: https://www.youtube.com/watch?v=Jf3WzU8G4dE&feature=youtu.be

如果我在按住鼠标的同时移动鼠标,则表格上最后一次单击的按钮会重置为正常状态。

2 个答案:

答案 0 :(得分:0)

首先,我建议您使用TextButtonStyle实例来设置按钮的样式而不是Skin实例。

你可以以某种方式获得TextButton对 saveMap.addListener 匿名内部类的引用。然后你可以手动设置Ok和Cancel TextButton的样式,如下所示:

public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

    Game.resources.getSound("click_2").play();

    if (saveBox.isVisible()) {

        saveBox.setVisible(false); 
        okButtonReference.setStyle(unclickedTextButtonStyle) // Not-clicked TextButtonStyle as parameter

        cancelButtonReference.setStyle(unclickedTextButtonStyle) // Not clicked TextButtonStyle as parameter.
        return false;
    }
    saveBox.setVisible(true);

    return true;
}

通过调用setStyle()强制TextButton更改它的TextButtonStyle,你可以确信该按钮会改变。

另外我注意到你没有在Table对象上调用debug()方法。这不是必要的,但建议:

    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);
    table.debug();

答案 1 :(得分:0)

经过更多的摆弄后,我发现我设置游戏InputProcessor的方式导致了这个问题。我没有正确处理touchUp事件。

Game.inputs.addProcessor(new InputProcessor() {

    ...

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {

        releasedButton = -1;

        if (UI.isOver()) {

            return false;
        }
        if (button == Buttons.LEFT) {

            releasedButton = Buttons.LEFT;
        }
        if (button == Buttons.RIGHT) {

            releasedButton = Buttons.RIGHT;
        }       
        return true; // <--- setting this to false fixes the issue
    }           
});