Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Atelier on Thu 11/07/2013 21:54:47

Title: Width of String using @OVERHOTSPOT@ (SOLVED)
Post by: Atelier on Thu 11/07/2013 21:54:47
Is there any way to find the width of a string, whilst also using the @OVERHOTSPOT@ tag on a label? The idea is to update the width of a tooltip GUI which displays the name of whatever the cursor is over.

Code (AGS) Select

gToolTip.Width = GetTextWidth(lbOverHSpot.Text, lbOverHSpot.Font);


(or the same with the label) doesn't seem to ever update the width.

If it's not possible I'll just have to write some code to explicitly get the name of whatever the player is pointing at - I just like the simplicity of the tag.

Thanks
Atelier
Title: Re: Width of String using @OVERHOTSPOT@
Post by: monkey0506 on Thu 11/07/2013 22:35:05
You could do this:

Code (ags) Select
String text = lbOverHSpot.Text.Replace("@OVERHOTSPOT@", Game.GetLocationName(mouse.x, mouse.y));
gToolTip.Width = GetTextWidth(text, lbOverHSpot.Font);


That way you don't have to manually call GetLocationName in rep_ex and constantly format the text yourself, but you can still get the right width.
Title: Re: Width of String using @OVERHOTSPOT@
Post by: Crimson Wizard on Thu 11/07/2013 22:55:22
Label's Text never gets changed, nor when it has tags (@overhotspot@, @score@), nor when translation is used. The final text is deduced only at the time of drawing and never stored anywhere.
Title: Re: Width of String using @OVERHOTSPOT@
Post by: monkey0506 on Thu 11/07/2013 23:04:28
CW, that's precisely the reason why just using GetTextWidth(label.Text, label.Font) doesn't work if it has a special tag. My code snippet relies on precisely this behavior to give the expected result though (otherwise you could just use GetTextWidth).

Edit:

Code (ags) Select
String GetRuntimeText(this Label*)
{
  String buffer = GetTranslation(this.Text);
  buffer = buffer.Replace("@GAMENAME@", GetTranslation(Game.Name));
  buffer = buffer.Replace("@OVERHOTSPOT@", GetTranslation(Game.GetLocationName(mouse.x, mouse.y)));
  buffer = buffer.Replace("@SCORE@", String.Format("%d", game.score));
  buffer = buffer.Replace("@SCORETEXT@", String.Format(GetTranslation("Score: %d of %d"), game.score, game.total_score));
  buffer = buffer.Replace("@TOTALSCORE@", String.Format("%d", game.total_score));
  return buffer;
}

int GetRuntimeTextWidth(this Label*)
{
  return GetTextWidth(this.GetRuntimeText(), this.Font);
}

int GetRuntimeTextHeight(this Label*, bool useLabelWidthAsMax)
{
  String text = this.GetRuntimeText();
  int width = GetTextWidth(text, this.Font);
  if ((useLabelWidthAsMax) && (width > this.Width)) width = this.Width;
  return GetTextHeight(text, this.Font, width);
}


Usage:

Code (ags) Select
gToolTip.Width = lbOverHSpot.GetRuntimeTextWidth();

Fixed my ignorant usage of GetTranslation.
Title: Re: Width of String using @OVERHOTSPOT@
Post by: Kitai on Fri 12/07/2013 07:46:48
Shouldn't this line:
Code ("ags") Select
buffer = buffer.Replace("@SCORETEXT@", String.Format(GetTranslation("Score: %d of %d"), game.score, game.total_score));be something like the following:
Code ("ags") Select
buffer = buffer.Replace("@SCORETEXT@", GetTranslation(String.Format("%s: %d %s %d", GetTranslation("Score"), game.score, GetTranslation("of"), game.total_score)));?
Title: Re: Width of String using @OVERHOTSPOT@
Post by: Crimson Wizard on Fri 12/07/2013 08:42:49
E: the SCORETEXT is replaced with just "%d of %d".
Title: Re: Width of String using @OVERHOTSPOT@
Post by: monkey0506 on Fri 12/07/2013 08:44:37
You could check the translation of "Score" and "of" independently, but it shouldn't be necessary. A translation of the format string should be sufficient. Otherwise translating the initial label text wouldn't really work either, but it does.

Edit: CW, maybe I read the manual wrong, but I swear it said it prints "Score: x of y" (CONFIRMED). If the actual behavior is anything different then it's a bug. Probably not a huge concern in practice, but it should be corrected.
Title: Re: Width of String using @OVERHOTSPOT@
Post by: Atelier on Sun 14/07/2013 20:51:16
Thanks monkey, your first snippet did the trick!
Title: Re: Width of String using @OVERHOTSPOT@ (SOLVED)
Post by: Crimson Wizard on Sun 14/07/2013 22:31:45
Quote from: monkey_05_06 on Fri 12/07/2013 08:44:37
Edit: CW, maybe I read the manual wrong, but I swear it said it prints "Score: x of y" (CONFIRMED). If the actual behavior is anything different then it's a bug. Probably not a huge concern in practice, but it should be corrected.
I looked into the engine code; also now tested in game. It prints "%d of %d". Although, is this a bug or mistake in the manual?
Title: Re: Width of String using @OVERHOTSPOT@ (SOLVED)
Post by: monkey0506 on Sun 14/07/2013 23:34:22
@Atelier: Great! Glad to hear it! :)

@CW: If you want to discuss it further we should take it elsewhere, but I will just drop my two cents here at the end of this post...

If the manual is out-of-date with the current scripting commands or features, then the manual should be updated (e.g., any pages that may reference old-style audio commands should be updated to reflect new-style audio [as well]/[as the preferred method]). In other cases of discrepancy between the manual and the engine, the manual should be given precedence.

That's all IMO of course, but in this particular instance I'd say that the fault lies in the engine (implementation) rather than the manual (documentation). The text "Score: X of XX" makes more sense logically for "@SCORETEXT@" than simply "X of XX". The player can still use @SCORE@, @TOTALSCORE@, String.Format, game.score, game.total_score, etc. however they wish to create a customized string, but even going back to the 2.72 manual the text for @SCORETEXT@ is documented the same way.

Ultimately not a big deal (*shrug*), but as I said, IMO the manual should take precedence in this type of discrepancy.