DrawString (BaseStatusBar)

From ZDoom Wiki
Jump to navigation Jump to search

BaseStatusBar

void DrawString(HUDFont font, String string, Vector2 pos, int flags = 0, int translation = Font.CR_UNTRANSLATED, double Alpha = 1., int wrapwidth = -1, int linespacing = 4, Vector2 scale = (1, 1))

Usage

Can be used in a ZScript HUD to draw a text string on the screen.

Parameters

  • HUDFont font
A pointer to a previously created HUDFont.
  • String string
The text string to print. If you want to pass a reference from the LANGUAGE lump, pass StringTable.Localize("$LANGUAGECODE").
  • Vector2 pos
The position of the string on the screen. Note that the vertical alignment can't be modified in the function, and the text is always drawn downward from the specified pos.y.
  • int flags
Flags can be used to alter the starting position and the aligment of the string. Use | to combine multiple flags.
The DI_SCREEN* flags will change the origin point of the coordinates where the element is drawn (essentially, moving where the (0, 0) point is located.
  • DI_SCREEN_TOP - The coordinates begin at the top center of the screen
  • DI_SCREEN_BOTTOM - The coordinates begin at the bottom center of the screen
  • DI_SCREEN_LEFT_CENTER - The coordinates begin at the center left side of the screen
  • DI_SCREEN_RIGHT_CENTER - The coordinates begin at the center right side of the screen
  • DI_SCREEN_LEFT_TOP - The coordinates begin at the top left corner of the screen
  • DI_SCREEN_RIGHT_TOP - The coordinates begin at the top right corner of the screen
  • DI_SCREEN_LEFT_BOTTOM - The coordinates begin at the bottom left corner of the screen
  • DI_SCREEN_RIGHT_BOTTOM - The coordinates begin at the bottom right corner of the screen
  • DI_SCREEN_CENTER - The coordinates begin at the center of the screen
  • DI_SCREEN_CENTER_BOTTOM - The coordinates begin at the bottom center of the screen
Note, these flags do not change the orientation of coordinates. Regardless of where the element is drawn, positive X moves it to the right, positive Y moves it down.
More flags are defined in the StatusBarCore class, but they're mostly aliases of the above ones.
The DI_TEXT* flags change the alignment of the string relative to the starting position:
  • DI_TEXT_ALIGN_LEFT - Left alignment (default)
  • DI_TEXT_ALIGN_RIGHT - Center aligment
  • DI_TEXT_ALIGN_CENTER - Right alignment
  • int translation
Allows applying a color translation to the whole string. Colors are defined in the Font struct.
If translation is specified but the string also uses color escape codes (like \cW), the codes will take priority. However, the specified translation will still apply to the parts of the string that are not colorized with the code (note, the \c- instruction can be used to remove previously set color; the text following this instruction will use the translation as specified by this argument).
  • double Alpha
Opacity of the string.
  • int wrapwidth
The width in pixels at which the string should wrap to the next line.
  • int linespacing
Spacing between the lines of a multi-line text.
  • Vector2 scale
Scale of the text.

Examples

Prints the text "A quick brown fox" using SMALLFONT in gold at the center of the screen:

HUDFont hfnt = HUDFont.Create(smallfont); //smallfont is a constant which can be used directly
DrawString(hfnt, "A quick brown fox", (0, 0), DI_SCREEN_CENTER|DI_TEXT_ALIGN_CENTER, Font.CR_Gold);


Prints "A quick brown fox jumps over the lazy dog" using BigUpper font in green at the top left corner of the screen, with the maximum line width of 200 (which splits it into 3 lines using the standard 320x200 HUD resolution):

Font fnt = "BigUpper";
HUDFont hfnt = HUDFont.Create(fnt);
DrawString(hfnt, "A quick brown fox jumps over the lazy dog", (0, 0), DI_SCREEN_LEFT_TOP|DI_TEXT_ALIGN_LEFT, Font.CR_Green, wrapwidth: 200);


Prints the text "Health: AAA/BBB" in CONFONT centered exactly around the point 32 units below the center of the screen, where AAA is the current health value, and BBB is the maximum health value. The AAA part will be colorized in either green, yellow, orange or red depending on how much health the player has compared to their max health, while the rest of the string will be colored in green (thanks to the use of the \cC code):

Font fnt = "Confont";
HUDFont hfnt = HUDFont.Create(fnt);
int health = CPlayer.health;
int maxHealth = CPlayer.mo.GetMaxHealth(true);
// Modify color based on the ratio of health to maxhealth:
int col;
if (health >= maxhealth * 0.8)
	col = Font.CR_Green;
else if (health >= maxhealth * 0.5)
	col = Font.CR_Yellow;
else if (health >= maxhealth * 0.25)
	col = Font.CR_Orange;
else
	col = Font.CR_Red;
// Note that the first %d is escaled with \c- which allows the translation col to be applied to it,
// while the rest of the string is explicitly colorized with \cC:
DrawString(hfnt, String.Format("\cCHealth: \c-%d\cC/%d", health, maxhealth), (0, 32 - fnt.GetHeight() * 0.5), DI_SCREEN_CENTER|DI_TEXT_ALIGN_CENTER, col);

See also