Page 1 of 1

[Solved] Centering all the lines in a text statement

Posted: Wed May 06, 2020 8:05 am
by Adrian_DVL
Hi guys!

So I have objects in a class of which name I want to display in a text statement inside a screen, depending on which object is displayed, like this:

Code: Select all

text "[item.name]" xpos xt+115 ypos yt+8 xanchor 0.5 size 15 font "BRLNSR.ttf" at girlsbuttons
And I get this. Each text next to the correspondent item is what the code above is showing:
Image

As you can see, the text is centered in each square. But now I want to make it so that the item.name's that takes two or more words are displayed in two lines. What I've done in the first place is add a \n to the attribute name for each item, like ("Studio\nmicrophone"). This is the outcome:
Image

As you can see, the names are still centered. The problem is that it seems the statement text inside the screen doesn't seem to make a difference between words and it just renders the name of the item in two lines being the block of text centered instead of centering each line.
So how can I do it so each line is centered in the square?

Re: Centering all the lines in a text statement

Posted: Wed May 06, 2020 9:04 am
by fullmontis
The reason they don't align is because renpy is treating them as a single text block. In order to have them center align you have to create a text element for each line, align them to center, and pack them in a vbox.

Something like this:

Code: Select all

screen lines(line_list):
    vbox:
        for line in line_list:
            text line xanchor 0.5
and then use it like this in place of your current text block:

Code: Select all

        use lines(["Reflex", "Camera"])

Re: Centering all the lines in a text statement

Posted: Wed May 06, 2020 9:11 am
by hell_oh_world
You can also do this...

Code: Select all

vbox:
    xfill True

    for name in item.name.split():
        text name xalign 0.5
or with the approach of using `use` statement...

Code: Select all

screen splitted_words(string, spacing=5):
	vbox:
   		xfill True
		spacing spacing
		
    		for _ in string.split():
        		text _ xalign 0.5

screen inventory:
	use splitted_words(item.name, 3)

Re: Centering all the lines in a text statement

Posted: Wed May 06, 2020 9:25 am
by Adrian_DVL
fullmontis wrote:
Wed May 06, 2020 9:04 am
The reason they don't align is because renpy is treating them as a single text block. In order to have them center align you have to create a text element for each line, align them to center, and pack them in a vbox.

Something like this:

Code: Select all

screen lines(line_list):
    vbox:
        for line in line_list:
            text line xanchor 0.5
and then use it like this in place of your current text block:

Code: Select all

        use lines(["Reflex", "Camera"])
hell_oh_world wrote:
Wed May 06, 2020 9:11 am
You can also do this...

Code: Select all

vbox:
    xfill True

    for name in item.name.split():
        text name xalign 0.5
Both ways work! I'm going for hell_oh_world's one, just because I have many of these screens, each one with their own xpos and ypos variables, so it's easier this way.
Just for the record, I've also added a yalign 0.5 to the vbox, so that name items with one only line are centered in y as well as in x.
Thanks both of you!