[Solved] Centering all the lines in a text statement

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Adrian_DVL
Regular
Posts: 95
Joined: Fri Mar 15, 2019 8:46 am
Projects: Clockwork Poison
Contact:

[Solved] Centering all the lines in a text statement

#1 Post by Adrian_DVL » Wed May 06, 2020 8:05 am

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?
Last edited by Adrian_DVL on Wed May 06, 2020 9:27 am, edited 1 time in total.

User avatar
fullmontis
Regular
Posts: 129
Joined: Sun May 05, 2013 8:03 am
Deviantart: fullmontis
itch: fullmontis
Location: Italy
Contact:

Re: Centering all the lines in a text statement

#2 Post by fullmontis » 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"])

User avatar
hell_oh_world
Veteran
Posts: 467
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Centering all the lines in a text statement

#3 Post by hell_oh_world » 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
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)

Adrian_DVL
Regular
Posts: 95
Joined: Fri Mar 15, 2019 8:46 am
Projects: Clockwork Poison
Contact:

Re: Centering all the lines in a text statement

#4 Post by Adrian_DVL » Wed May 06, 2020 9:25 am

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!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Mumeishi