If I got multiple multiline Labels in Panel then they overlap each other.
It seems lemur cannot calculate height of multiline text block.
Any ideas how to do that?
If I got multiple multiline Labels in Panel then they overlap each other.
It seems lemur cannot calculate height of multiline text block.
Any ideas how to do that?
It can calculate the height but they had to have multiple lines in them when the layout was done. (It’s a bug that the Label doesn’t detect this case, I guess.)
If you know that you have multiple lines of text and want to relayout there are a couple of approaches… the easiest to try is invalidating the layout.
myLabel.getControl(GuiControl.class).invalidate();
…sorry that sort of thing is not better documented. I should probably add a pass-through on the Panel base class for convenience.
Edit: if that doesn’t work then I’ll need to know more about your hierarchy before I can offer better advice.
I created an issue here:
https://github.com/jMonkeyEngine-Contributions/Lemur/issues/30
…if you want to track it.
Can you tell me more about your panel setup to make sure it’s not related. I somehow have vague memories of something similar happening to someone else but many of my panels seem to resize appropriately.
val c = new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.Last, FillMode.Last))
guiNode.attachChild(c)
c.setLocalTranslation(500,500,200)
c.setPreferredSize(new Vector3f(400, 300, 0))
val label1 = new Label("kjshdf sdkhsd sdkfhsd fds sdf hdsf sdhgfjhgdsf ds ghfds fsdfgsdfhhb sdf jsdf dsfd sjhfds fsdj fhg")
c.addChild(label1)
val label2 = new Label("##############")
c.addChild(label2)
val label3 = new Label("kjshdf sdkhsd sdkfhsd fds sdf hdsf sdhgfjhgdsf ds ghfds fsdfgsdfhhb sdf jsdf dsfd sjhfds fsdj fhg")
c.addChild(label3)
c.getControl(classOf[GuiControl]) .invalidate()
label1.getControl(classOf[GuiControl]) .invalidate()
label2.getControl(classOf[GuiControl]) .invalidate()
label3.getControl(classOf[GuiControl]) .invalidate()
Invalidation change nothing.
I’m trying to make chat. And I need to display multiline text blocks.
I have no ideas at the moment how to move on.
It could be the FillMode.Last arguments screwing up the layout. I will take a look tonight and get back to you.
Ok. I’ve been able to duplicate the issue.
It’s because the text wraps because you’ve constrained the width. It’s definitely a bug though not entirely easy to fix because of how BitmapText works. I will see what I can do.
Note: for your particular use-case, you might ultimately be happier using a ListBox to display the text. You will still have a wrapping issue, I guess, though.
As a stop-gap solution you could try splitting your lines at a certain width.
I have come up with a solution for this problem and it has been checked into Lemur ‘head’ and will be in the next update: version 1.7.1 (maybe tonight).
Label gets a new stylable attribute that lets you set the maximum width of a label.
Lemur layout follow a simple two pass approach: 1) calculate preferred size, 2) layout the components based on actual size. This doesn’t give components a chance to change one size because another was constrained… of course, it also avoids the massive iteration that must happen to resolve those situations. It also avoids having to detect which constraints are unsolvable.
Setting the max width of a label is a way around the issue as it it’s a good indicator that the label should wrap at a certain width. Vertical sizing can then be properly calculated as part of preferred size calculation.
Here is an example of it in use:
import com.jme3.app.*;
import com.jme3.math.*;
import com.simsilica.lemur.*;
import com.simsilica.lemur.component.*;
public class MultilineText extends SimpleApplication {
public static void main( String... args ) {
MultilineText main = new MultilineText();
main.start();
}
public void simpleInitApp() {
GuiGlobals.initialize(this);
Container window = new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.Last, FillMode.Last));
guiNode.attachChild(window);
window.setLocalTranslation(500, 500, 200);
window.setPreferredSize(new Vector3f(400, 300, 0));
Label label1 = window.addChild(new Label("Line1 with lots of text that may wrap\nLine2 also with even more text that may wrap even further."));
label1.setMaxWidth(400);
Label label2 = window.addChild(new Label("#############"));
Label label3 = window.addChild(new Label("Line1 with lots of text that may wrap\nLine2 also with even more text that may wrap even further."));
}
}
This is the line that fixes the problem:
label1.setMaxWidth(400);
…without it, the text overlaps as in your example.
If you want this update immediately, you can build Lemur from source. It’s pretty straight forward (gradlew install). Else I will probably push 1.7.1 out tonight… tomorrow at the latest.