KF2-YetAnotherScoreboard/YAS/Classes/KFGUI_EditBox.uc

428 lines
8.0 KiB
Ucode
Raw Normal View History

2022-09-03 17:26:40 +00:00
class KFGUI_EditBox extends KFGUI_Clickable;
2021-06-12 20:11:37 +00:00
2020-01-10 13:14:11 +00:00
var enum eTextCase
{
TXTC_None,
TXTC_Upper,
TXTC_Lower,
2020-01-10 13:14:11 +00:00
} TextCase;
var Color FontColor;
2021-06-13 02:54:35 +00:00
var string TextStr, AllowedCharSet;
var bool bDrawBackground, bNoClearOnEnter, bMaskText, bIntOnly, bFloatOnly, bIncludeSign, bConvertSpaces, bCtrl, bAllSelected, bForceShowCaret;
2020-01-10 13:14:11 +00:00
var int MaxWidth;
2021-06-13 02:54:35 +00:00
var bool bReadOnly, bAlwaysNotify;
var int CaretPos, FirstVis, LastSizeX, LastCaret, LastLength;
2020-01-10 13:14:11 +00:00
var float TextScale;
function InitMenu()
{
Super.InitMenu();
2020-01-10 13:14:11 +00:00
2021-06-13 02:53:33 +00:00
if (bIntOnly || bFloatOnly)
{
AllowedCharSet = "0123456789";
2020-01-10 13:14:11 +00:00
if (bFloatOnly)
AllowedCharSet $= ".";
}
2020-01-10 13:14:11 +00:00
bAllSelected = true;
2020-01-10 13:14:11 +00:00
}
function SetText(string NewText, optional bool bIgnoreDelegate)
{
local bool bChanged;
2020-01-10 13:14:11 +00:00
bChanged = bAlwaysNotify || TextStr != NewText;
TextStr = NewText;
CaretPos = Len(TextStr);
2020-01-10 13:14:11 +00:00
if (bChanged && !bIgnoreDelegate)
TextChanged();
2020-01-10 13:14:11 +00:00
bAllSelected=true;
2020-01-10 13:14:11 +00:00
}
function bool NotifyInputChar(int Key, string Unicode)
{
2021-06-13 02:54:35 +00:00
local string Temp, S;
if (bReadOnly)
return false;
2021-06-13 02:53:33 +00:00
if (UniCode != "")
S = Unicode;
else S = Chr(Key);
2021-06-13 02:53:33 +00:00
if (Asc(S) == 13 || Asc(S) == 8 || Asc(S) == 27)
return false;
2021-06-13 02:53:33 +00:00
if (bCtrl)
return false;
2021-06-13 02:53:33 +00:00
if (bAllSelected)
{
TextStr="";
CaretPos=0;
bAllSelected=false;
}
2021-06-13 02:54:35 +00:00
if ((AllowedCharSet == "") || ( (bIncludeSign) && ( (S == "-") || (S == "+") ) && (TextStr == "") ) || (InStr(AllowedCharSet, S) >= 0))
{
2021-06-13 02:53:33 +00:00
if ((MaxWidth == 0) || (Len(TextStr) < MaxWidth))
{
2021-06-13 02:53:33 +00:00
if ((bConvertSpaces) && ((S == " ") || (S == "?") || (S == "\\")))
S = "_";
2021-06-13 03:00:19 +00:00
if ((TextStr == "") || ( CaretPos == Len(TextStr)))
{
TextStr = TextStr$S;
CaretPos=Len(TextStr);
}
else
{
2021-06-13 02:54:35 +00:00
Temp = Left(TextStr, CaretPos)$S$Mid(TextStr, CaretPos);
TextStr = Temp;
CaretPos++;
}
TextChanged();
return true;
}
}
return false;
2020-01-10 13:14:11 +00:00
}
function SetInputText(string S)
{
2021-06-13 03:00:19 +00:00
switch (TextCase)
{
case TXTC_Upper:
S = Caps(S);
break;
case TXTC_Lower:
S = Locs(S);
break;
}
TextStr = S;
TextChanged();
2020-01-10 13:14:11 +00:00
}
function AppendInputText(string Text)
{
local int Character;
while (Len(Text) > 0)
{
Character = Asc(Left(Text, 1));
Text = Mid(Text, 1);
if (Character >= 0x20 && Character < 0x100)
{
SetInputText(Left(TextStr, CaretPos) $ Chr(Character) $ Right(TextStr, Len(TextStr) - CaretPos));
CaretPos += 1;
}
}
2020-01-10 13:14:11 +00:00
}
function bool ProcessControlKey(name Key, EInputEvent Event)
{
if (Key == 'LeftControl' || Key == 'RightControl')
{
if (Event == IE_Released)
{
bCtrl = false;
}
else if (Event == IE_Pressed)
{
bCtrl = true;
}
return true;
}
else if (bCtrl && Event == IE_Pressed && GetPlayer() != None)
{
if (Key == 'V')
{
// paste
AppendInputText(GetPlayer().PasteFromClipboard());
return true;
}
else if (Key == 'C')
{
// copy
GetPlayer().CopyToClipboard(TextStr);
return true;
}
else if (Key == 'X')
{
// cut
if (TextStr != "")
{
GetPlayer().CopyToClipboard(TextStr);
SetInputText("");
CaretPos = 0;
}
return true;
}
}
return false;
2020-01-10 13:14:11 +00:00
}
function bool NotifyInputKey(int ControllerId, name Key, EInputEvent Event, float AmountDepressed, bool bGamepad)
{
local string Temp;
2021-06-13 02:53:33 +00:00
if (bReadOnly)
{
return false;
}
if (ProcessControlKey(Key, Event))
{
return false;
}
2021-06-13 02:53:33 +00:00
else if (Key == 'Escape' && Event == IE_Pressed)
{
2021-06-13 02:53:33 +00:00
if (TextStr != "")
{
SetInputText("");
CaretPos = 0;
return true;
}
else
{
2021-06-13 02:53:33 +00:00
if (ParentComponent != None)
{
ParentComponent.UserPressedEsc();
return true;
}
}
}
2021-06-13 02:53:33 +00:00
else if (Key == 'Enter' && Event == IE_Released)
{
2021-06-13 02:53:33 +00:00
if (TextStr != "")
{
Temp = TextStr;
OnTextFinished(self, Temp);
2021-06-13 02:53:33 +00:00
if (!bNoClearOnEnter)
{
SetInputText("");
CaretPos = 0;
}
}
return true;
}
2021-06-13 02:53:33 +00:00
else if (Key == 'Home')
{
CaretPos = 0;
return true;
}
2021-06-13 02:53:33 +00:00
else if (Key == 'End')
{
CaretPos = Len(TextStr);
return true;
}
2021-06-13 02:53:33 +00:00
else if (Event == IE_Pressed || Event == IE_Repeat)
{
2021-06-13 02:53:33 +00:00
if (Key == 'Backspace' || Key == 'Delete')
{
2021-06-13 02:53:33 +00:00
if (bAllSelected)
{
SetInputText("");
CaretPos = 0;
}
2021-06-13 02:53:33 +00:00
else if (CaretPos > 0)
{
2021-06-13 02:54:35 +00:00
SetInputText(Left(TextStr, CaretPos-1) $ Right(TextStr, Len(TextStr) - CaretPos));
CaretPos -= 1;
}
return true;
}
2021-06-13 02:53:33 +00:00
else if (Key == 'Left')
{
CaretPos = Max(0, CaretPos - 1);
return true;
}
2021-06-13 02:53:33 +00:00
else if (Key == 'Right')
{
CaretPos = Min(Len(TextStr), CaretPos + 1);
return true;
}
}
return true;
2020-01-10 13:14:11 +00:00
}
function string ConvertIllegal(string InputStr)
{
local int i, Max;
local string Retval;
local string C;
i = 0;
Max = Len(InputStr);
2021-06-13 02:53:33 +00:00
while ( i < Max)
{
2021-06-13 02:54:35 +00:00
C = Mid(InputStr, i,1);
if (AllowedCharSet != "" && InStr(AllowedCharSet, C) < 0)
{
C = "";
}
2021-06-13 02:53:33 +00:00
if (bConvertSpaces &&
2021-06-13 03:00:19 +00:00
((C == " ") || (C =="?") || (C == "\\")))
{
C = "_";
}
Retval = Retval $ C;
i++;
}
if (MaxWidth > 0)
2021-06-13 02:54:35 +00:00
return Left(Retval, MaxWidth);
return Retval;
2020-01-10 13:14:11 +00:00
}
function string GetText()
{
return TextStr;
2020-01-10 13:14:11 +00:00
}
function TextChanged()
{
OnChange(Self);
2020-01-10 13:14:11 +00:00
}
function DrawMenu()
{
2021-06-13 02:54:35 +00:00
local string Storage, FinalDraw, TmpString;
local int MaskIndex, StorageLength;
local float XL, YL, BoxWidth, FontScale, CursorY, BorderSize;
local FontRenderInfo FRI;
Super.DrawMenu();
2021-06-13 02:53:33 +00:00
if (bDrawBackground)
{
2021-06-13 02:54:35 +00:00
Canvas.SetDrawColor(250, 250, 250, 255);
Canvas.SetPos(0.f, 0.f);
Canvas.DrawTileStretched(Owner.CurrentStyle.BorderTextures[`BOX_SMALL], CompPos[2], CompPos[3], 0,0, Owner.CurrentStyle.BorderTextures[`BOX_SMALL].GetSurfaceWidth(), Owner.CurrentStyle.BorderTextures[`BOX_SMALL].GetSurfaceHeight());
}
BorderSize = Owner.CurrentStyle.ScreenScale(4.f);
FRI.bClipText = true;
FRI.bEnableShadow = true;
Storage = TextStr;
2021-06-13 02:53:33 +00:00
if (bMaskText && Len(Storage) > 0)
{
StorageLength = Len(Storage);
Storage = "";
2021-06-13 02:53:33 +00:00
for (MaskIndex=1; MaskIndex <= StorageLength; MaskIndex++)
{
Storage $= "*";
}
}
Canvas.Font = Owner.CurrentStyle.PickFont(FontScale);
FontScale *= TextScale;
BoxWidth=CompPos[2]*0.9875;
2021-06-13 02:53:33 +00:00
if ((Len(Storage) != LastLength) || (CaretPos != LastCaret))
{
2021-06-13 02:53:33 +00:00
if (CaretPos <= FirstVis)
2021-06-13 02:54:35 +00:00
FirstVis = Max(0, CaretPos-1);
else
{
FinalDraw = Mid(Storage, FirstVis, CaretPos-FirstVis);
Canvas.TextSize(FinalDraw, XL, YL, FontScale, FontScale);
2021-06-13 02:53:33 +00:00
while ( (XL >= BoxWidth) && (FirstVis < Len(Storage)))
{
FirstVis++;
FinalDraw = Mid(Storage, FirstVis, CaretPos-FirstVis);
Canvas.TextSize(FinalDraw, XL, YL, FontScale, FontScale);
}
}
}
LastLength = Len(Storage);
if (bReadOnly)
{
FirstVis = 0;
}
FinalDraw = Mid(Storage, FirstVis, Len(Storage)-FirstVis);
if (!bReadOnly && (Owner.KeyboardFocus == self || bForceShowCaret))
{
2021-06-13 02:53:33 +00:00
if ((FirstVis == CaretPos) || (Len(FinalDraw) == 0))
{
Canvas.TextSize("W", XL, YL, FontScale, FontScale);
XL = BorderSize;
bAllSelected=false;
}
else
{
TmpString = Mid(FinalDraw, 0, CaretPos-FirstVis);
Canvas.TextSize(TmpString, XL, YL, FontScale, FontScale);
}
CursorY = (CompPos[3]/2) - ((YL-Owner.HUDOwner.ScaledBorderSize)/2);
2021-06-13 02:53:33 +00:00
if (bAllSelected)
{
2021-06-13 02:54:35 +00:00
Canvas.SetDrawColor(255, 255, 255, 195);
Canvas.SetPos(BorderSize, CursorY);
2021-06-13 03:00:19 +00:00
Canvas.DrawTile(Owner.DefaultPens[`PEN_WHITE], XL, YL-Owner.HUDOwner.ScaledBorderSize, 0, 0, Owner.DefaultPens[`PEN_WHITE].GetSurfaceWidth(), Owner.DefaultPens[`PEN_WHITE].GetSurfaceHeight());
}
else
{
2021-06-13 02:54:35 +00:00
Canvas.SetDrawColor(255, 255, 255, Owner.CursorFlash);
Canvas.SetPos(XL + (Len(FinalDraw) == 0 ? 0 : 3), CursorY);
2021-06-13 03:00:19 +00:00
Canvas.DrawTile(Owner.DefaultPens[`PEN_WHITE], 3, YL-Owner.HUDOwner.ScaledBorderSize, 0, 0, Owner.DefaultPens[`PEN_WHITE].GetSurfaceWidth(), Owner.DefaultPens[`PEN_WHITE].GetSurfaceHeight());
}
}
Canvas.DrawColor = FontColor;
Canvas.SetPos(BorderSize, (CompPos[3]/2) - (YL/2));
2021-06-13 02:54:35 +00:00
Canvas.DrawText(FinalDraw, ,FontScale, FontScale, FRI);
2020-01-10 13:14:11 +00:00
}
2021-06-13 03:00:19 +00:00
function HandleMouseClick(bool bRight)
2020-01-10 13:14:11 +00:00
{
2021-06-13 02:53:33 +00:00
if (Owner.KeyboardFocus != self)
{
GrabKeyFocus();
}
CaretPos = Len(TextStr);
bAllSelected = !bAllSelected;
2020-01-10 13:14:11 +00:00
}
Delegate OnChange(KFGUI_EditBox Sender);
Delegate OnTextFinished(KFGUI_EditBox Sender, string S);
defaultproperties
{
2021-06-13 02:54:35 +00:00
FontColor=(R=255, G=255, B=255, A=255)
MaxWidth=768
TextScale=1
TextCase=TXTC_None
LastCaret=-1
LastLength=-1
YSize=0.06
}