Code snippet: How to get the row height in a ListBox

Started by Monsieur OUXX, Tue 11/02/2014 08:35:31

Previous topic - Next topic

Monsieur OUXX

I hope there is no function in AGS that does that out-of-the-box or else I just totally ridiculed myself.

Code snippet: How to get the row height in a ListBox

Code: ags

[color=green]// AGS does not provide the height (in pixels) of a listbox row.
// The height depends purely on the font's height. 
// Yet, one cannot access that info either, using scripting only
// (even with GetTextHeight, because you'd need to know in advance the height
// of the tallest character in the entire font, I suppose).

// Hence, this function. It's a MASSIVE HACK
//
// IMPORTANT NOTE: this function can make the game crash because of a bug in AGS' ListBox
// coding. See http://www.adventuregamestudio.co.uk/forums/index.php?topic=48793.msg636466821#msg636466821
// To work around this, you must never call this function before the ListBox has been
// painted at least once.

// This function works with a struct similar to this :
// struct ExtendedListboxType {
//    ListBox* Box;
//    int rowHeight
// };
//
// ExtendedListboxType TextAreas[10];
//

[/color]

int TextAreaType::GetRowHeight()
{
  
  if (this.rowHeight!=0) //We do it only once, but it must be done after game_start
                         //or more generally after the box's first painting
    return this.rowHeight;
  else
  {   
    ListBox* b =  this.Box; //for convenience
    b.Clear();
    b.AddItem("0000000000000000000000000000000");
    b.AddItem("1111111111111111111111111111111");
    
    int i=0;
    
    int start_x=b.X+b.OwningGUI.X+1;
    int start_y=b.Y+b.OwningGUI.Y;

    int lastItem;
    lastItem=b.GetItemAtLocation(start_x, start_y+i);

    while (i<30) //we assume the characters are not higher than 30 pix high
    {
      int item = b.GetItemAtLocation(start_x, start_y+i);
      
      if (lastItem != item && item != -1)
      {
        b.Clear();
        this.rowHeight = i;
        return i;
      }  
      lastItem=item;
      i++;
    }

    Display("WARNING: could not find height of text area rows. The cursor might be off, or maybe your font is taller than 30 pixels?");
    b.Clear();
    this.rowHeight = 10; //arbitrary value
    return this.rowHeight; 
  }   
}
 

Crimson Wizard

#1
Using a knowledge from the engine code:

row height = font height + X, where X
  = 2 if game resolution is 320x200 or 320x240;
  = 4 if resolution is higher.

The font height is calculated as the height of "YpyjIHgMNWQ".

E: it is [ code = ags ], not [ code ]... the toolbar button must be fixed :(

Ghost

Quote from: Crimson Wizard on Tue 11/02/2014 09:07:40
row height = font height + X, where X
  = 2 if game resolution is 320x200 or 320x240;
  = 4 if resolution is higher.

Is this affected by TTF vs "game fonts"? I have noticed that the TTFs I use show up differently aligned even on the build-in GUIs (ALT-X quit et al)- they are always about 1 to 2 pixels "lower".

Monsieur OUXX

Quote from: Crimson Wizard on Tue 11/02/2014 09:07:40
The font height is calculated as the height of "YpyjIHgMNWQ".

That will come EXTREMELY HANDY. I'm about to release a module that can make the engine explode when used in the wrong conditions.
 

monkey0506

Quote from: Monsieur OUXX on Tue 11/02/2014 10:59:34I'm about to release a module that can make the engine explode when used...

Copycat. :cool:

Ghost

Quote from: Monsieur OUXX on Tue 11/02/2014 10:59:34
I'm about to release a module that can make the engine explode when used in the wrong conditions.

Can you make one where the engine explodes when used in the RIGHT conditions? I see potential there: Whole new genre- Point-and-Explode Adventures! Mr. Bay will be the patron saint and Mr. T can showcase the effect in a Knowledge Is Half The Battle segment (played tight after the explosion).
I need a coffee.

SMF spam blocked by CleanTalk