Centering text on the status line

How does one go about centering text on the status line? I’ve looked at the relevant routine for it (can’t remember if it’s Draw- or PrintStatusLine) in HUGOLIB.H but I can’t seem to add anything to it that will automatically put the text in the middle of the status line.

Any advice is appreciated. Thanks!

There are a few things you have to do. First, as you guessed, you have to change how “PrintStatusLine” works. Cut and paste the whole routine into the new game, and use “replace” instead of “routine” to begin.

Next, declare a string array big enough to hold the longest dictionary entry you’re going to be printing:

array tempbuf[100]

Inside the PrintStatusLine routine, you will convert the dictionary entry (it can be, something sent as a parameter, or whatever):

local a, l, x l = display.linelength len = string(tempbuf,,100)
Then, all that’s left to do is figure out how much is half of the remaining space, and print that at the appropriate spot in PrintStatusLine:

x = (l - a) / 2 print to x; StringPrint(tempbuf)
And that should work! If you need, I can write the changed version of PrintStatusLine. Let me know.

So that was it. :astonished: I had to declare a string array first, then do the calculations from there. Geez. I didn’t think declaring an array like that was needed.

Thanks for the tip, Merk. I’ll just post a reply here if I need the whole code, but what you said should work already. :wink:

Well, you really just need it to determine the length of the string. You could change StringPrint(tempbuf) to a normal print of the dictionary entry ( or whatever), and it would do the same thing.

If there is a direct way to get the length of a dictionary entry without using the string() conversion’s return value, I’m not sure what it is. Maybe I’ve overlooked something.

Hi again -

Here’s my PrintStatusLine routine, with the changes you had suggested (I filled some of the blanks in):

[code]array tempbuf[100]

replace PrintStatusline
local a, l, x, len
l = display.linelength
len = string(tempbuf,,100)
a = StringLength(len)
x = (l - a) / 2

if display.linelength < 80
	display.statusline_height = 2
	display.statusline_height = 1

window display.statusline_height
	locate 1, 1
	if not location
		print "\\_";
	elseif not light_source
		print "In the dark";
		! if FORMAT & DESCFORM_F:  print "\\_";
		print to x;

	!\\if display.statusline_height = 1
		print to 65;
		locate 1, 2
		if FORMAT & DESCFORM_F:  print "\\_";

		print number score; " / "; number counter;
	elseif STATUSTYPE = 2
		print HoursMinutes(counter);\\!


I used SAMPLE.HUG as my test file for this. When I tried it, the location name was placed way off-center (I don’t have the screenshot for this, unfortunately) and the text was in small letters.

I suspect the small-letters issue could be fixed by putting in “capital” but how about the other one? Is it something inherent in the Hugo display?

Thanks again!

The problem looks to be in this bit. You’re using a = StringLength(len), but len is already the length of the string. So yeah, it’d be way off center. You probably meant to do a = StringLength(tempbuf), but even that isn’t necessary since you get the length just by doing the string() conversion.

It’s my fault, because in my original example, I put len = string()… but then used “a” in the calculation. DOH

Try this:

local a, l, x l = display.linelength a = string(tempbuf,,100) x = (l - a) / 2
Or simplified/clarified like this:

local len, x len = string(tempbuf,,100) x = (display.linelength - len) / 2

Also, I guess you could simplify this:

as just this:

print to x;

You’re right - the variable name gave it away already but I didn’t see it. The “a” just surprised me, so I thought you had mean for that to get the length of the string.

That’s ok. Happens to the best of us :wink:

It’s working now. Thanks!

Happy to help! If you’re interested, check out “utils.hug” from the Trading Punches and/or Distress source code. I actually have a “PrintCenter” routine there, among other things.