2
\$\begingroup\$

To my game I want to add a gui element that displays text and textButtons whenever something is happening or it wants you to make a decission (an answer while talking to a person for example). To keep track of your actions you shall be able to scroll it. Similar to text adverntures or dialog boxes in rpgs.

I use Scene2Dui for my GUI, so I put a scrollPane in my GUIs root table. I have methods for adding stuff to the contained table and tell the scrollPane to scroll to the bottom.

In general the idea works, but the thing doesn't scroll to the bottom but only to the bottom of the element I added before the current one. I'm puzzled.

Here's the code, leaving out the skins and styles part:

public class TextBox {

ScrollPane box;
Table table;
Label.LabelStyle lStyle;
Skin skin;

TextBox() {

    table.setFillParent(false);

    box = new ScrollPane(table, scrollStyle);

    box.setScrollingDisabled(true, false);
    box.setOverscroll(false, false);
    box.setFillParent(false);

    table.align(Align.topLeft);
    table.row();
}


void writeDecissions(GameEvent[] events) {

    Table tableD = new Table();
    tableD.setFillParent(false);

    if (events != null) {

        for (int i = 1; i < events.length; i++) {

            if (events[i] != null) {

                String s = Character.forDigit(i, 10) + " - "
                        + events[i].getDecision();

                TextButton button = new TextButton(s, style);
                button.getLabel().setWrap(true);
                button.getLabel().setAlignment(Align.left);

                tableD.add(button).width(box.getWidth() - 40).spaceBottom(20).spaceTop(20);

                tableD.row();

            }
        }
    }

    table.add(tableD).align(Align.left).pad(20);
    table.row();

    box.setScrollY(table.getHeight());
}

This is the way I test it:

public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act();
    stage.draw();

    if (Gdx.input.justTouched()) {
        gui.textBox.writeDecissions(events);
    }

I tried box.setScrollY(box.getMaxY) as well as setting overscroll to true (don't really know what that is tough). With the same result.

Finally a screenshot: enter image description here

Help appreciated.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

I found an answer on Stack Overflow.

The thing is I had to call layout() on the scrollPane manually.

The reason: Because the height of a wrapped label is unknown until layout() is automaticaly called while drawing. The maximum Y scroll position remains the same until this happens.

\$\endgroup\$
1
\$\begingroup\$

layout() should be called twice, for sure...

From label.setWrap documentation: In some cases the parent of the label will need to layout twice: once to set the width of the label and a second time to adjust to the label's new preferred height.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .