Classes:HUDFont

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.


HUDFont is an internal class in ZScript, for use in ZScript status bars.

This class is used in text-drawing functions as the font to use when drawing text. It also controls how the text is displayed, such as spacing and shadows.

Members

The following are members of the HUDFont. These must be accessed by using a . (period) between the HUDFont's name and the member.

Type Variables Use
Font mFont The Font object that the HUDFont is using for drawing text.

Functions

Return Type Function Arguments (first to last) Use/Arguments
HUDFont Create
  1. Font fnt,
  2. int spacing = 0,
  3. bool monospaced = false,
  4. int shadowx = 0,
  5. int shadowy = 0

Initializes the HUDFont.

  • fnt - The font to use.
  • spacing - The amount of spacing between letters that will be displayed. If monospaced is true, this controls the overall spacing (i.e a spacing of 0 would draw all the characters on top of each other), otherwise this controls the spacing added on to the width of the character.
  • monospaced - Controls whether the font is monospaced, or uses the width of each character as spacing. If this is true, you must specify a spacing value manually - this can be easily done by calling GetCharWidth("0") ("0" is used here, but other characters can be used too) on the Font object passed to the fnt argument.
  • shadowx - Controls how far to the right drawn texts' shadows are drawn, in pixels. Negative values will position the shadow to the left.
  • shadowy - Controls how far down the drawn texts' shadows are drawn, in pixels. Negative values will position the shadow upwards.

Note: this function is static and should be called off the class name, i.e. HUDFont.Create(...).

Example

In this example, 3 strings will be drawn to the screen, each in a different fashion.

version "2.5" // See Object scopes and versions

Class MyStatusBar : BaseStatusBar {
	HUDFont noMonospaceSmallfont;
	HUDFont monospaceSmallfont;
	HUDFont shadowSmallfont;
	
	override void Init() {
		Super.Init();
		SetSize(32, 320, 200);
		
		// smallfont is a built in Font object in ZScript - if you have your own font
		// and want to use that instead, you should initialize it like this:
		// Font myFont = "<FONT NAME>";
		
		// this font will not be monospaced when drawn
		noMonospaceSmallfont = HUDFont.Create(smallfont);
		// this font will be monospaced when drawn,
		// and each character will be spaced based on the width of the "0" character
		monospaceSmallfont = HUDFont.Create(smallfont, smallfont.GetCharWidth("0"), true);
		// this font will not be monospaced, but will cast a shadow 8 pixels to the right and 8 pixels to the left
		shadowSmallfont = HUDFont.Create(smallfont, 0, false, 8, 8);
	}
	
	override void Draw (int state, double TicFrac) {
		Super.Draw (state, TicFrac);
		
		if (state == HUD_StatusBar) {
			BeginStatusBar();
			DrawMainBar();
		}
		else if (state == HUD_Fullscreen) {
			BeginHUD();
			DrawFullScreenStuff();
		}
	}
	
	void DrawMainBar() {
		DrawSomeText();
	}
	
	void DrawFullScreenStuff() {
		DrawSomeText();
	}
	
	void DrawSomeText() {
		// get the height of the font (we could get the height of any of the 3 HUDFonts we defined,
		// but they all use the same font internally so it shouldn't matter)
		// this is used to position each string below the last one
		int fontHeight = noMonospaceSmallfont.mFont.GetHeight();
		
		// this string will have no monospacing - the "I" will be far skinnier than other characters.
		DrawString(noMonospaceSmallfont, "TEXT DISPLAY", (0, 0 * fontHeight));
		// this string will be monospaced - the "I" will be displayed very awkwardly
		DrawString(monospaceSmallfont, "TEXT DISPLAY", (0, 1 * fontHeight));
		// this string will have a shadow 8 pixels to the right and 8 pixels to the bottom
		DrawString(shadowSmallfont, "TEXT DISPLAY", (0, 2 * fontHeight));
	}
}