KF2-Server-Extension/ServerExt/Classes/KFGUI_TextField.uc

338 lines
6.6 KiB
Ucode
Raw Normal View History

2017-10-20 02:00:49 +00:00
Class KFGUI_TextField extends KFGUI_MultiComponent;
struct FTextPart
{
var string S;
var color C;
var float X;
};
struct FTextLineInfo
{
var array<FTextPart> Text;
var float Y;
};
var KFGUI_ScrollBarV ScrollBar;
var() string LineSplitter;
var() protected string Text;
var() color TextColor;
var() Canvas.FontRenderInfo TextFontInfo;
var() byte FontScale;
var protected transient array<FTextLineInfo> Lines,OrgLines;
var transient float MaxHeight,ScrollWidth,OldSize[2],InitFontScale,TextHeight;
var transient Font InitFont;
var transient bool bShowScrollbar,bTextParsed;
2020-11-28 20:04:55 +00:00
final function SetText(string S)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (Text==S)
2017-10-20 02:00:49 +00:00
return;
Text = S;
OldSize[0] = -1; // Force to refresh.
Lines.Length = 0;
OrgLines.Length = 0;
bTextParsed = false;
}
2020-11-28 21:54:57 +00:00
2017-10-20 02:00:49 +00:00
final function string GetText()
{
return Text;
}
final function ParseTextLines()
{
local array<string> SA;
local int i,j,z;
local string S;
local color C;
ParseStringIntoArray(Text,SA,LineSplitter,false);
Lines.Length = SA.Length;
C.A = 0;
2020-11-28 20:12:58 +00:00
for (i=0; i<SA.Length; ++i)
2017-10-20 02:00:49 +00:00
{
Lines[i].Text.Length = 0;
S = SA[i];
2020-11-28 20:12:58 +00:00
if (S=="")
2017-10-20 02:00:49 +00:00
continue;
z = 0;
2020-11-28 20:12:58 +00:00
while (true)
2017-10-20 02:00:49 +00:00
{
j = InStr(S,"#{");
2020-11-28 20:12:58 +00:00
if (j>0)
2017-10-20 02:00:49 +00:00
{
Lines[i].Text.Length = z+1;
Lines[i].Text[z].S = Left(S,j);
Lines[i].Text[z].C = C;
++z;
}
2020-11-28 20:12:58 +00:00
else if (j==-1)
2017-10-20 02:00:49 +00:00
break;
S = Mid(S,j+2);
2020-11-28 20:12:58 +00:00
if (Left(S,4)=="DEF}")
2017-10-20 02:00:49 +00:00
{
C.A = 0;
S = Mid(S,4);
}
else
{
C.R = GrabHexValue(Mid(S,0,2));
C.G = GrabHexValue(Mid(S,2,2));
C.B = GrabHexValue(Mid(S,4,2));
S = Mid(S,7);
C.A = 255;
}
}
Lines[i].Text.Length = z+1;
Lines[i].Text[z].S = S;
Lines[i].Text[z].C = C;
}
OrgLines = Lines; // Create a backup.
}
2020-11-28 21:54:57 +00:00
2020-11-28 20:04:55 +00:00
final function byte GrabHexValue(string S)
2017-10-20 02:00:49 +00:00
{
local byte n;
n = (HexToInt(Asc(Left(S,1)))<<4) | HexToInt(Asc(Right(S,1)));
S = Mid(S,2);
return n;
}
2020-11-28 21:54:57 +00:00
2020-11-28 20:04:55 +00:00
final function byte HexToInt(byte n)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (n>=48 && n<=57) // '0' - '9'
2017-10-20 02:00:49 +00:00
return n-48;
2020-11-28 20:12:58 +00:00
if (n>=65 && n<=70) // 'A' - 'F'
2017-10-20 02:00:49 +00:00
return n-55; // 'A' - 10
2020-11-28 20:12:58 +00:00
if (n>=97 && n<=102) // 'a' - 'f'
2017-10-20 02:00:49 +00:00
return n-87; // 'a' - 10
return 0;
}
function InitSize()
{
local byte i;
local float XS;
OldSize[0] = CompPos[2];
OldSize[1] = CompPos[3];
2020-11-28 20:12:58 +00:00
if (!bTextParsed)
2017-10-20 02:00:49 +00:00
{
ParseTextLines();
bTextParsed = true;
}
else Lines = OrgLines;
InitFont = Owner.CurrentStyle.PickFont(FontScale+Owner.CurrentStyle.DefaultFontSize,InitFontScale);
// Compute Y-offsets of each line.
Canvas.Font = InitFont;
Canvas.TextSize("ABC",XS,TextHeight,InitFontScale,InitFontScale);
ParseLines(CompPos[2] / InitFontScale);
MaxHeight = (Lines.Length * TextHeight);
bShowScrollbar = (MaxHeight>=CompPos[3]);
bClickable = bShowScrollbar;
bCanFocus = bShowScrollbar;
2020-11-28 20:12:58 +00:00
if (bShowScrollbar)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (ScrollBar==None)
2017-10-20 02:00:49 +00:00
{
ScrollBar = new(Self) class'KFGUI_ScrollBarV';
ScrollBar.SetPosition(0.9,0.0,0.1,1.0);
ScrollBar.Owner = Owner;
ScrollBar.ParentComponent = Self;
ScrollBar.InitMenu();
}
// Compute scrollbar size and X-position.
2020-11-28 20:12:58 +00:00
for (i=0; i<4; ++i)
2017-10-20 02:00:49 +00:00
ScrollBar.InputPos[i] = CompPos[i];
ScrollWidth = ScrollBar.GetWidth();
ScrollBar.XPosition = 1.f - ScrollWidth;
ScrollWidth *= CompPos[2];
ScrollBar.ComputeCoords();
// Recompute line sizes because we now have a scrollbar.
Lines = OrgLines;
ParseLines((CompPos[2]-ScrollWidth) / InitFontScale);
2020-11-28 20:12:58 +00:00
if (Components.Find(ScrollBar)==-1)
2017-10-20 02:00:49 +00:00
Components.AddItem(ScrollBar);
MaxHeight = (Lines.Length * TextHeight);
ScrollBar.UpdateScrollSize(0,Max(((MaxHeight-CompPos[3])/TextHeight)+1,1),1,1);
}
2020-11-28 20:12:58 +00:00
else if (ScrollBar!=None)
2017-10-20 02:00:49 +00:00
Components.RemoveItem(ScrollBar);
}
// Parse textlines to see if they're too long.
2020-11-28 20:04:55 +00:00
final function ParseLines(float ClipX)
2017-10-20 02:00:49 +00:00
{
local float X,XS,YS;
local int i,j,z,n;
2020-11-28 20:12:58 +00:00
for (i=0; i<Lines.Length; ++i)
2017-10-20 02:00:49 +00:00
{
Lines[i].Y = i*TextHeight;
X = 0.f;
2020-11-28 20:12:58 +00:00
for (j=0; j<Lines[i].Text.Length; ++j)
2017-10-20 02:00:49 +00:00
{
Lines[i].Text[j].X = (X*InitFontScale);
Canvas.TextSize(Lines[i].Text[j].S,XS,YS);
2020-11-28 20:12:58 +00:00
if ((X+XS)>ClipX)
2017-10-20 02:00:49 +00:00
{
z = FindSplitPoint(Lines[i].Text[j].S,X,ClipX);
// Add new line.
Lines.Insert(i+1,1);
// Append the remaining lines there.
2020-11-28 20:12:58 +00:00
for (n=j; n<Lines[i].Text.Length; ++n)
2017-10-20 02:00:49 +00:00
Lines[i+1].Text.AddItem(Lines[i].Text[n]);
// Split the string at wrapping point.
Lines[i+1].Text[0].S = Mid(Lines[i].Text[j].S,z);
// Remove whitespaces in front of the string.
Lines[i+1].Text[0].S = StripWhiteSpaces(Lines[i+1].Text[0].S);
// If empty, clean it up.
2020-11-28 20:12:58 +00:00
if (Lines[i+1].Text[0].S=="")
2017-10-20 02:00:49 +00:00
Lines[i+1].Text.Remove(0,1);
// End the current line at wrapping point.
Lines[i].Text[j].S = Left(Lines[i].Text[j].S,z);
Lines[i].Text.Length = j+1;
break;
}
X+=XS;
}
}
}
// Slow, find wrapped splitting point in text.
2020-11-28 20:04:55 +00:00
final function int FindSplitPoint(string S, float X, float ClipX)
2017-10-20 02:00:49 +00:00
{
local int i,l,PrevWord;
local float XL,YL;
local bool bWasWhite,bStartedZero;
bStartedZero = (X==0.f);
Canvas.TextSize(Mid(S,0,1),XL,YL);
X += XL;
i = 1;
l = Len(S);
PrevWord = 0;
bWasWhite = true;
2020-11-28 20:12:58 +00:00
while (i<l)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (Mid(S,i,1)==" ")
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (!bWasWhite)
2017-10-20 02:00:49 +00:00
{
PrevWord = i;
bWasWhite = true;
}
}
else
{
bWasWhite = false;
}
Canvas.TextSize(Mid(S,i,1),XL,YL);
X+=XL;
2020-11-28 20:12:58 +00:00
if (X>ClipX) // Split here if possible.
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (PrevWord==0)
2017-10-20 02:00:49 +00:00
return (bStartedZero ? i : 0); // No wrap.
return PrevWord;
}
++i;
}
return l;
}
2020-11-28 21:54:57 +00:00
2020-11-28 20:04:55 +00:00
final function string StripWhiteSpaces(string S)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (Left(S,1)==" ")
2017-10-20 02:00:49 +00:00
S = Mid(S,1);
return S;
}
function byte GetCursorStyle()
{
return 0;
}
function DrawMenu()
{
local int i,j;
local float Y;
2020-11-28 20:12:58 +00:00
if (Text=="")
2017-10-20 02:00:49 +00:00
return;
// Need to figure out best fitting font.
2020-11-28 20:12:58 +00:00
if (OldSize[0]!=CompPos[2] || OldSize[1]!=CompPos[3])
2017-10-20 02:00:49 +00:00
InitSize();
Canvas.Font = InitFont;
2020-11-28 20:12:58 +00:00
if (bShowScrollbar)
2017-10-20 02:00:49 +00:00
{
Canvas.SetClip(CompPos[0]+(CompPos[2]-ScrollWidth),CompPos[1]+CompPos[3]);
i = ScrollBar.GetValue();
}
else i = 0;
2020-11-28 20:12:58 +00:00
if (i<Lines.Length)
2017-10-20 02:00:49 +00:00
{
Y = Lines[i].Y;
2020-11-28 20:12:58 +00:00
for (i=i; i<Lines.Length; ++i)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (Lines[i].Text.Length!=0)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if ((Lines[i].Y-Y+TextHeight)>=CompPos[3])
2017-10-20 02:00:49 +00:00
break;
2020-11-28 20:12:58 +00:00
for (j=0; j<Lines[i].Text.Length; ++j)
2017-10-20 02:00:49 +00:00
{
// Set text color.
Canvas.DrawColor = Lines[i].Text[j].C;
2020-11-28 20:12:58 +00:00
if (Canvas.DrawColor.A==0)
2017-10-20 02:00:49 +00:00
Canvas.DrawColor = TextColor;
Canvas.SetPos(Lines[i].Text[j].X,Lines[i].Y-Y);
Canvas.DrawText(Lines[i].Text[j].S,,InitFontScale,InitFontScale,TextFontInfo);
}
}
}
}
}
function bool CaptureMouse()
{
return (bShowScrollbar ? Super.CaptureMouse() : false); // Nope.
}
2020-11-28 20:04:55 +00:00
function ScrollMouseWheel(bool bUp)
2017-10-20 02:00:49 +00:00
{
2020-11-28 20:12:58 +00:00
if (bShowScrollbar)
2017-10-20 02:00:49 +00:00
ScrollBar.ScrollMouseWheel(bUp);
}
defaultproperties
{
LineSplitter="|"
FontScale=0
Text="TextField"
TextColor=(R=255,G=255,B=255,A=255)
TextFontInfo=(bClipText=true,bEnableShadow=true)
bCanFocus=false
bClickable=false
}