Looking to center text

I picked up a script that works perfectly, but I was toying around with making adjustments in the body of the script. I'm not familiar with Ruby/RGSS but I've had a little play in C++ so I'm able to understand some of what is going on. Anyways, I came up on this little bit of code I want to rework a little, but I just can't seem to get it to work. Just for everyone's info, I'm not claiming this is my own script, I'm allowed to use and modify the script for my game, and I'm giving the original author full credit. Here's the snippet of code I'm working on:


def refresh
    contents.clear
    change_color(normal_color)
    unless $game_map.display_name.empty?
      # draw_text(0, 0, 100, 20, PAC::MM::Loc_a, 2) GS84  <--this is the line I'm adding in (GS84 = GreyStone84)
      draw_text(contents.rect, $game_map.display_name, 1)
    end
  end
end



My issue comes regarding the content.rect and centering. The contents.rect does a good job centering the map name in the middle of the rectangle, but it was designed for only one line of text. I extended the height of the map name window, no problem, but I also wanted to add in a line of text above the map name to simply say "Location", or "Location", which is the line of code I commented out. Looking around, I found out that I can place a number from 0-2 after the text and it is supposed to be the alignment, but it doesn't center it properly. It's almost like the 0-2 are referring to tabs instead of alignment. I tried to manually switch the x coordinate; window_width returns 160, so I assumed that since text_length = 100, I should adjust it so x=30 (word starts at 30, is 100 long, should leave 30 at the end to = 160) but that doesn't work, it started the word at 30, but "Map" isn't as long as "Location".


Basically I'm looking to center any text in the Map Name Window. Any help would be appreciated and I hope this is the right posting spot since I'm trying to learn how to do this properly.
Ok, so you seem like you have a good handle on the 'draw_text' method, but just so there's no confusion I'll lay it out. The 'draw_text' method is overloaded. The first form is 'draw_text(x, y, width, height, text, alignment)' and the second form is 'draw_text(rect, text, alignment)'. In both methods, 'text' is the text to draw and 'alignment' is an option from 0-2 as you mentioned with 0 being left aligned, 1 being centered, and 2 being right aligned. The difference is one uses x, y, width, and height to specify the rectangular space to draw and the other uses a Rect object which is a simple object with  x, y, width, and height properties. 


Ok, cool. So where does that leave us? Well for starters, the alignment is specific to the rectangle specified. So if your text is 100 pixels long, and you specify 100 as the width of the draw space, then the text can't move at all. With the window being 160 pixels, set the width to 160; this will set the draw space width to be 160 pixels and since the text is only 100 it should be able to center it.


One last note, 'contents.rect' is getting the Rect object for the window's contents (or bitmap). So, if you've switched the window from one line to two lines, just realize that contents.rect is going to return a rect that's twice as tall as it used to be.
With the window being 160 pixels, set the width to 160; this will set the draw space width to be 160 pixels and since the text is only 100 it should be able to center it.

I thought in the  (x, y, width, height, text, align) that the "width" was the text width. If I lower it, the text shrinks. Or is it the rectangle that shrinks thereby shrinking the text? I tried changing it to (0, 0, 160, 20, text, 1) but it did not center it properly as in the picture.

Spoiler
Fv6kuVv.jpg

The "Location" is the commented out part of the code I provided. The "Maps" is the part of the code with the contents.rect. As you can see, the "Maps" is centered perfectly within the Window. Location with the alignment of 1, or center, looks like the start of the word begins near the center, but isn't centered by the middle of the text. Once I saw this though, I realised that it isn't the centering of "Location" that is the problem because I can actually adjust this with the (x) coordinate. That line of text will always stay the same. The problem is with the "Maps" text in the contents.rect because I can't adjust the height and its too close to the "Location". If I switch it to the other method, I can't get it centered and that one changes depending on what map I am currently on, so if I center one map name, it won't be centered for a longer or shorter map name.
I thought in the  (x, y, width, height, text, align) that the "width" was the text width. If I lower it, the text shrinks. Or is it the rectangle that shrinks thereby shrinking the text? I tried changing it to (0, 0, 160, 20, text, 1) but it did not center it properly as in the picture.

So looking at the picture, I realized that the 'window' width is probably 160 which means the 'contents' width is smaller than that because there's some pixel padding (the padding defaults to 12 pixels but is adjustable. So try this:


draw_text(0, 0, contents.width, contents.height / 2, PAC::MM::Loc_a, 1)
draw_text(0, contents.height / 2, contents.width, contents.height / 2, $game_map.display_name, 1)


That will set both to take up the entire width of the contents (from 0 to contents.width).


It will set the first line to take up the top half of the height (from 0 to contents.height / 2).


It will set the second line to take up the bottom half of the height (from contents.height / 2 plus another contents.height / 2 to reach  contents.height)
draw_text(0, 0, contents.width, contents.height / 2, PAC::MM::Loc_a, 1)
draw_text(0, contents.height / 2, contents.width, contents.height / 2, $game_map.display_name, 1)

Great! This works perfectly, thanks for the help.

Original thread: https://forums.rpgmakerweb.com/threads/looking-to-center-text.60068/