CreateTextual width problem

Started by densming, Sun 05/10/2008 03:16:10

Previous topic - Next topic

densming

I'm having trouble with a textual Overlay, and GetTextWidth.

My function is fairly straightforward, I'm trying to create a textoverlay that's centered on the screen (both x and y):

Code: ags

Overlay *textOverlay;
function ShowTextCentered(String text) {
	int x, y, width, height;
	
	width = GetTextWidth(text, Game.NormalFont);
	height = GetTextHeight(text, Game.NormalFont, width);
	x = (System.ScreenWidth / 2) - (width / 2);
	y = (System.ScreenHeight / 2) - (height / 2);
	
	textOverlay = Overlay.CreateTextual(x, y, width, Game.NormalFont, 17238, text);
}


But whenever I run this code, it wraps the text on 2 lines, so it's not centered.  This seems to be the case no matter the length of the string.  For example, if I call the function using ShowTextCentered("Hello"); it will put a line break between the 'e' and the first 'l'.  This makes me think the width parameter of the CreateTextual function is not wide enough.  I know I can add 10 or so to width, but then the centering isn't right...  I know the GetTextWidth() function returns the right number (I've tested this by taking screen shots and measuring) so I'm thinking it's an issue with the Overlay.CreateTextual() function?

I'm trying to get this right for the video tutorial but I can't seem to figure out what's wrong.

monkey0506

#1
Hey densming, there is an outstanding request for aligned textual overlays, but in the mean-time you can render a graphical overlay instead.

Code: ags
import Overlay* CreateTextualOverlayAligned(int x, int y, int width, FontType font, int color, String text, Alignment=eAlignCentre);

Overlay *textOverlay;

Overlay* CreateTextualOverlayAligned(int x, int y, int width, FontType font, int color, String text, Alignment align) {
  int height = GetTextHeight(text, font, width);
  DynamicSprite *sprite = DynamicSprite.Create(width, height);
  DrawingSurface *surface = sprite.GetDrawingSurface();
  surface.DrawingColor = color;
  surface.DrawStringWrapped(0, 0, width, font, align, text);
  surface.Release();
  Overlay *newOverlay = Overlay.CreateGraphical(x, y, sprite.Graphic, true);
  sprite.Delete();
  return newOverlay;
}

function ShowTextCentered(String text) {
  int width = GetTextWidth(text, Game.NormalFont);
  int height = GetTextHeight(text, Game.NormalFont, width);
  int x = (System.ViewportWidth / 2) - (width / 2);
  int y = (System.ViewportHeight / 2) - (height / 2);
  textOverlay = CreateTextualOverlayAligned(x, y, width, Game.NormalFont, 17238, text);
}


Okay, actually I didn't fully read your post. It seems that the overlay isn't using a wide enough width? I guess the first thing I'd like to know is what output you are getting, as well as the values of x, y, width, and height.

Pumaman

Hmm, it seems that Overlay.CreateTextual automatically creates a 3-pixel border around the text (I think this was done to allow it to draw the text window border where appropriate), so it basically means you'd have to +6 to your width when creating it.

The problem now is that I don't want to risk fixing this in case it then breaks other games where people are relying on the current behaviour. Hmm.

monkey0506

Okay if that's the case then I think my graphical overlay workaround should still work properly, yes?

densming

Quote from: Pumaman
Hmm, it seems that Overlay.CreateTextual automatically creates a 3-pixel border around the text (I think this was done to allow it to draw the text window border where appropriate), so it basically means you'd have to +6 to your width when creating it.

Ahh, that makes sense.  I actually ended up adding 7 to the width (6 wasn't enough), so my CreateTextual statement looks like this:
Code: ags

textOverlay = Overlay.CreateTextual(x, y, width + 7, Game.NormalFont, 17238, text);


And that works great.  Thanks for the help.

Quote from: Pumaman
The problem now is that I don't want to risk fixing this in case it then breaks other games where people are relying on the current behaviour. Hmm.

I know the feeling.  I deal with this at work every day in supporting apps as well, so I understand.  Maybe add an optional bool as the last parameter (true by default) that indicates whether to add the border?

Quote from: monkey_05_06
Hey densming, there is an outstanding request for aligned textual overlays, but in the mean-time you can render a graphical overlay instead.

Thanks for the help, monkey_05_06.  I considered using a graphical overlay as well but I figured there had to be a simple solution with text overlays.  Arguably a graphical overlay might be better since it allows more creativity and control over what the overlay looks like, but I wanted something quick and simple for the tutorials.  Thanks!

GarageGothic

Quote from: densming on Mon 06/10/2008 03:37:30Maybe add an optional bool as the last parameter (true by default) that indicates whether to add the border?

Or add an optional int (1 by default) to specify border thickness?

SMF spam blocked by CleanTalk