Width of String using @OVERHOTSPOT@ (SOLVED)

Started by Atelier, Thu 11/07/2013 21:54:47

Previous topic - Next topic

Atelier

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

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

monkey0506

You could do this:

Code: ags
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.

Crimson Wizard

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.

monkey0506

#3
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
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
gToolTip.Width = lbOverHSpot.GetRuntimeTextWidth();


Fixed my ignorant usage of GetTranslation.

Kitai

#4
Shouldn't this line:
Code: "ags"
buffer = buffer.Replace("@SCORETEXT@", String.Format(GetTranslation("Score: %d of %d"), game.score, game.total_score));
be something like the following:
Code: "ags"
buffer = buffer.Replace("@SCORETEXT@", GetTranslation(String.Format("%s: %d %s %d", GetTranslation("Score"), game.score, GetTranslation("of"), game.total_score)));
?

Crimson Wizard

#5
E: the SCORETEXT is replaced with just "%d of %d".

monkey0506

#6
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.

Atelier

Thanks monkey, your first snippet did the trick!

Crimson Wizard

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?

monkey0506

@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.

SMF spam blocked by CleanTalk