Scene2d: Height of wrapped label

Anything libgdx related goes here!

Scene2d: Height of wrapped label

Postby butmoom » Wed Aug 14, 2019 4:47 pm

Hello,

I'm currently designing a dialog that pops up with a wrapped label. I would like the dialog to take the height of the label so, regardless the label's wrapped text, the dialog always "shrinks" itself to fit it's child (the label).

At the moment, the dialog is just a table (with a hardcoded height) that I add to the stage. I did try using a Window and then a Dialog container as well. I still can't get a reasonable "height" from the label immediately after setting it's text. I've tried getting the label's height with getHeight(), getPrefHeight(), getGlyphLayout().height after packing(), layout() and/or invalidating() it and still get unreasonable low or high values. I definitely don't know what I am doing.

I know it kind of goes against Scene2d way of "letting the parent define the size of it's children" but in this case I really need the parent to adopt the size of it's child. Is this wrong? What am I missing?

Thank you very much!
butmoom
 
Posts: 7
Joined: Wed Jul 31, 2019 4:25 am

Re: Scene2d: Height of wrapped label

Postby tomski » Wed Aug 14, 2019 5:20 pm

Can you share code as to what you are doing? And some previews, what you want the widget to look like, what it currently looks like?
tomski
 
Posts: 897
Joined: Wed Jun 26, 2013 6:06 pm

Re: Scene2d: Height of wrapped label

Postby shatterblast » Wed Aug 14, 2019 6:03 pm

butmoom wrote:At the moment, the dialog is just a table (with a hardcoded height) that I add to the stage.
...
I still can't get a reasonable "height" from the label immediately after setting it's text. I've tried getting the label's height with getHeight(), getPrefHeight(), getGlyphLayout().height after packing(), layout() and/or invalidating() it and still get unreasonable low or high values.

You might try replacing your Table with Container. After that, you could use something similar to:
Code: Select all
container.width( 200f );
container.height( 300f );


I think the following would work also :
Code: Select all
Container container = new Container( myLabel );
container.width( myLabel.getWidth() );
container.height( myLabel.getHeight() );


https://libgdx.badlogicgames.com/ci/nig ... dth-float-
https://libgdx.badlogicgames.com/ci/nig ... ght-float-

Table is better if you expect it to automatically adjust to a lot of stuff in its insides. You often, yet not always, need Container stuff inside a Table to achieve that purpose. Container is a more efficient version of Table meant for holding only one thing, like an Actor or Stack for example. I think some other things, like Window and Dialog, exist as specialized versions of Table, so they will still behave like Table.
shatterblast
 
Posts: 368
Joined: Sun Jul 06, 2014 1:14 pm

Re: Scene2d: Height of wrapped label

Postby butmoom » Wed Aug 14, 2019 7:17 pm

Thanks for the replies!

Here's a picture of what I'm talking about:
Image
(url in case cross origin doesn't work: https://imgur.com/RnYkGic)

Basically I'm trying to fit a container to it's child (or children, in the more complex case). The dark box is a table that contains a label. I would like the table to fit exactly the label, whose dimensions I don't know because they depend on the text length and it's wrapping.

This is my code:
Code: Select all
private void setup(Skin skin, float height){
        table = new Table(skin);
        table.align(Align.topLeft);
        table.setPosition((Gdx.graphics.getWidth() - WIDTH) / 2, 200);
        table.setSize(WIDTH, height);
        table.align(Align.topLeft);
        table.pad(PAD);

        label = new Label("", skin);
        label.setWrap(true);

        table.add(label).width(WIDTH - PAD).left();
        stage.addActor(table);
    }

Setup() is basically called on startup. It creates the dialog.

Code: Select all
public void setDialogText(String text){
        label.setText(text);
        table.setSize(label.getWidth(), label.getHeight());
}


Then comes setDialogText, that basically changes the label text and then tries to fit the table size to the label's size. I use this method to set the label's text to the one shown in the picture. You can see that the label in the picture has 4 lines of text, however, label.getWidth() returns an absurdly low value (15px) like if the label only had one line of text, which obviously doesn't match the reality. Is this a drawing delay?
butmoom
 
Posts: 7
Joined: Wed Jul 31, 2019 4:25 am

Re: Scene2d: Height of wrapped label

Postby tomski » Wed Aug 14, 2019 7:40 pm

setText does not force the layout. The pref height of a label in wrapping mode completely depends on the width of the widget. This typically happens after a layout has occured in its parent, potentially even twice. You'll most likely want to give a fixed width to the cell the label lives in, and dont set explicit size for the table. It will automatically size itself accordingly.
tomski
 
Posts: 897
Joined: Wed Jun 26, 2013 6:06 pm

Re: Scene2d: Height of wrapped label

Postby butmoom » Wed Aug 14, 2019 8:09 pm

Thank you very much for the reply.

I did as you said and removed the explicit setSize(). Also, I stopped using the label.setText() in favor of setting the label's text through it's constructor. With regard to the label's cell, it already had a fixed width. The table looks like it's adjusting itself to fit the label, however, the background of the table stayed the same?

https://imgur.com/nY123mN

That dark box on the top left corner is the background of the table. The red square should be the table debug. The code ended up like this:

Code: Select all
label = new Label("Hey! This is the label I'm talking about. As you can see, the label has the wrapped property set to true and I would like the container to fit exactly the size of the label", skin);
label.setWrap(true);

table = new Table(skin);
table.align(Align.topLeft);
table.add(label).width(WIDTH - PAD).left();


Sorry for being a pain in the ass!! This is driving me nuts.
butmoom
 
Posts: 7
Joined: Wed Jul 31, 2019 4:25 am

Re: Scene2d: Height of wrapped label

Postby tomski » Wed Aug 14, 2019 8:17 pm

Yup that looks normal, but your table behavior is strange, and thats not what it should be doiung. What are you adding the table to?
tomski
 
Posts: 897
Joined: Wed Jun 26, 2013 6:06 pm

Re: Scene2d: Height of wrapped label

Postby butmoom » Wed Aug 14, 2019 8:23 pm

I'm adding the table directly to the stage, as if it was a window. Maybe that's why the table doesn't know what size to take?
butmoom
 
Posts: 7
Joined: Wed Jul 31, 2019 4:25 am


Re: Scene2d: Height of wrapped label

Postby butmoom » Wed Aug 14, 2019 11:55 pm

You are my hero. Thank you so much.
butmoom
 
Posts: 7
Joined: Wed Jul 31, 2019 4:25 am


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot] and 1 guest