简体   繁体   中英

Libgdx scrollpane issue for a chatwindow class

I am having an issue with using a scrollpane in libgdx. It is going to be used for a chatwindow class. When you press enter the message will be added to the window and you will scroll to the latest posted message..However it doesn't. It misses one message and scrolls to the one before the latest message. Below I've posted the chatwindow class and the method that adds input to it. The textAreaholder is a table that holds everything. The chatField is where you input what you want to post to the chat. The chatarea is the textfield that then becomes added to the table. But as stated..it doesn't scroll properly, the error properly lies somewhere in the keyTyped method.

public ChatWindow(final Pipe<String> chatPipe) {
    this.chatPipe = chatPipe;
    messageFieldCounter = 0;

    white = new BitmapFont(Gdx.files.internal("fonts/ChatWindowText.fnt"), false);
    fontSize = white.getLineHeight();
    white.scale(TEXT_SCALE);
    final TextFilter filter = new TextFilter();

    /* Making a textfield style */
    textFieldStyle = new TextFieldStyle();
    textFieldStyle.fontColor = Color.WHITE;
    textFieldStyle.font = white;
    textFieldStyle.focusedFontColor = Color.CYAN;

    /*Area where all chat appears*/
    textAreaHolder = new Table();
    textAreaHolder.debug();

    /*Applies the scrollpane to the chat area*/
    scrollPane = new ScrollPane(textAreaHolder);
    scrollPane.setForceScroll(false, true);
    scrollPane.setFlickScroll(true);
    scrollPane.setOverscroll(false, false);

    /*Input chat*/
    chatField = new TextField("", textFieldStyle);
    chatField.setTextFieldFilter(filter);

    /*Tries to make the textField react on enter?*/
    chatField.setTextFieldListener(new TextFieldListener() {
        @Override
        public void keyTyped(final TextField textField, final char key) {
            if (key == '\n' || key == '\r') {
                if (messageFieldCounter <= 50) {
                    textAreaHolder.row();
                    StringBuilder message = new StringBuilder();   //Creates the message
                    message.append(chatField.getText());           //Appends the chatfield entry
                    TextArea chatArea = new TextArea(message.toString(), textFieldStyle); //Creates a chatArea with the message
                    chatArea.setHeight(fontSize + 1);
                    chatArea.setDisabled(true);
                    chatArea.setTextFieldFilter(filter);
                    textAreaHolder.add(chatArea).height(CHAT_INPUT_HEIGHT).width(CHAT_WIDTH);

                    scrollPane.scrollToCenter(0, 0, 0, 0);



                      //Scrolls to latest input


                chatField.setText("");
                //InputDecider.inputDecision(message.toString(), chatPipe); //TODO: Change the filter
                //chatPipe.put(message.toString()); //TODO: testing
                }
            }
        }
    });

Problems could occur, because you're using scrollPane.scrollToCenter(float x, float y, float width, float height) with zero parameters:

scrollPane.scrollToCenter(0, 0, 0, 0);

scrollToCenter method requires that parameters to be correctly supplied. So, try to supply message bounds.

The second reason could be because you call scrollToCenter before table do layout itself. So, try overwrite table's layout method and call scrollToCenter after:

@Override
public void layout()
{
    super.layout();
    if (new_messages_added)
    {
       scrollPane.scrollToCenter(...)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM