Classes:BrokenLines

From ZDoom Wiki
Jump to navigation Jump to search

BrokenLines is a ZScript class designed to split text strings into multiple separate strings.

This class has no fields exposed to ZScript, only methods.

Initialization

An instance of this class can only be created by calling fnt.BreakLines where fnt is a Font object (see here). BreakLines splits the string so each separate string does exceed a given width in pixels (which, naturally, can only be done for a specific font, since different fonts have different glyph sizes).

Methods

Non-static

  • native int Count()
Returns the total number of strings stored in the calling instance of BrokenLines.
  • native int StringWidth(int line)
Returns the width in pixels of the given string stored in the calling instance of BrokenLines.
  • int line — the index of the line.
  • native String StringAt(int line)
Returns the given string stored in the calling instance of BrokenLines.
  • int line — the index of the line.

Note that line indexes in this class begin at 0, so if a BrokenLines instance contains 3 strings (calling Count() on it will return 3), the indexes of those strings will be 0, 1 and 2.

Examples

This shows an example of using BrokenLines in an event handler: a long string is broken into multiple lines so the width of each line doesn't exceed 256:

override void RenderOverlay(RenderEvent e)
{
	String teststring = "Officia nostrum suscipit laborum voluptates explicabo suscipit. Amet illum odit asperiores aut mollitia eaque. Est et officiis rerum. At recusandae est eos.";
	Font fnt = Font.FindFont('BigUpper');

	// Create an instance of BrokenLines and break the lines
	// using the font initialized above:
	BrokenLines brlines = fnt.BreakLines(teststring, 256);
	double lineHeight = fnt.GetHeight(); //this will be used for vertical spacing
	// Start printing at the top left corner:
	Vector2 strPos = (0,0);
	// Iterate over each separate string and draw it:
	for (int i = 0; i < brlines.Count(); i++)
	{
		Screen.DrawText(fnt, Font.CR_UNTRANSLATED, strPos.x, strPos.y, brlines.StringAt(i));
		strPos.y += lineHeight; //move the position down according to the height of the font
	}
}

See also