Multiline text output (text with line breaks)
Posted: Sun Nov 04, 2018 5:38 pm
This is a fairly obvious task, but I did not find information about it here.
For example, you need to send multiline text to the console, or draw it on the screen. DrawText and ClientMessage do not work correctly for line breaks.
Therefore, I have written several methods for this. You can use them in your code.
For example, you need to send multiline text to the console, or draw it on the screen. DrawText and ClientMessage do not work correctly for line breaks.
Therefore, I have written several methods for this. You can use them in your code.
Code: Select all
function DrawText(Canvas C, coerce string Text, float X, float Y, optional bool CR) {
local float _, DY;
local int Pos;
local string cCR, LF, Line;
cCR = chr(13);
LF = chr(10);
C.StrLen("X", _, DY);
while (true) {
Pos = InStr(Text, LF);
if (Pos == -1) break;
Line = Left(Text, Pos);
Text = Mid(Text, Pos + 1);
if (Pos > 0 && Right(Line, 1) == cCR) Line = Left(Line, Pos - 1);
C.SetPos(X, Y);
C.DrawText(Line, CR);
Y = Y + DY;
}
if (Text != "" && Right(Text, 1) == cCR) Text = Left(Text, Len(Text) - 1);
C.SetPos(X, Y);
C.DrawText(Text, CR);
}
function ClientMessage(Pawn Pawn, coerce string Text, optional name Type, optional bool bBeep) {
local int Pos;
local string CR, LF, Line;
CR = chr(13);
LF = chr(10);
while (true) {
Pos = InStr(Text, LF);
if (Pos == -1) break;
Line = Left(Text, Pos);
Text = Mid(Text, Pos + 1);
if (Pos > 0 && Right(Line, 1) == CR) Line = Left(Line, Pos - 1);
Pawn.ClientMessage(Line, Type);
}
if (Text != "" && Right(Text, 1) == CR) Text = Left(Text, Len(Text) - 1);
Pawn.ClientMessage(Text, Type, bBeep);
}