hushaojie04 / libgdx

Automatically exported from code.google.com/p/libgdx
0 stars 0 forks source link

VerticalGroup and Label [wrap=true] fails to wrap #1117

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
A Label with wrap=true does not wrap at all inside a VerticalGroup, because a 
wrapped Label expects its parent to set its size to some meaningful value so it 
knows when to wrap; but VerticalGroup sets the label's width to the label's 
getPrefWidth() which is always zero when wrapping is true.

Therefore, the label is always set to a width of 0, which is PROBABLY 
interpreted as unlimited width, and never wraps.

This works fine when the label is inside a table for example, because the table 
just sets the label to whatever size the cell wants.

You can reproduce by:

uiStage = new Stage();
uiVerticalGrp = new VerticalGroup();
uiVerticalGrp.setSize(100, 100);
uiVerticalGrp.setPosition(300, 300);
Label label = new Label("put a long string here", uiSkin);
label.setWrap(true);
uiVerticalGrp.addActor(label);
stage.addActor(uiVerticalGrp);

You should see that the label just renders with no line breaks. This also 
happens if the VG is inside a table or a scrollpane; again, because the label's 
width is set to its preferred width, which is zero, so it can't wrap.

My fix is this, inside VerticalGroup.layout(), after

            if (child instanceof Layout) {
                Layout layout = (Layout)child;
                width = layout.getPrefWidth();
                height = layout.getPrefHeight();
            } else {
                width = child.getWidth();
                height = child.getHeight();
            }

after that, add this line:

if (width == 0 && child instanceof Label) { width = groupWidth; }

Here, groupWidth is actually VerticalGroup.getWidth(), which is set by its 
parent, or directly by the user with setSize.

You could further check the child label if wrapping is on, to make sure you 
don't interfere with anything else at all, except Label.wrap is a private 
member and cannot be accessed by VG.

Original issue reported on code.google.com by raicua...@gmail.com on 5 Nov 2012 at 1:57

GoogleCodeExporter commented 9 years ago
What I have is: table -> scrollpane -> vg

Maybe I can put something in between the scrollpane and the VG, like a table. 
I'll give that a try.

Original comment by raicua...@gmail.com on 7 Nov 2012 at 6:59

GoogleCodeExporter commented 9 years ago
I don't like the idea of special casing VerticalGroup to know about a Label 
because it is a generic container. VerticalGroup javadoc says, "The preferred 
width is the largest preferred width of any child." This seems reasonable. To 
handle your specific situation you can have:
table1 -> scrollpane -> table2 -> vg
Use table2 to set the desired width of the label.

Original comment by nathan.s...@gmail.com on 10 Nov 2012 at 7:50

GoogleCodeExporter commented 9 years ago
Makes sense, cheers.

Original comment by raicua...@gmail.com on 10 Nov 2012 at 8:02