UWindow
Class UWindowEditBox

source: e:\games\UnrealTournament\UWindow\Classes\UWindowEditBox.uc
Core.Object
   |
   +--UWindow.UWindowBase
      |
      +--UWindow.UWindowWindow
         |
         +--UWindow.UWindowDialogControl
            |
            +--UWindow.UWindowEditBox
Direct Known Subclasses:NameEditBox

class UWindowEditBox
extends UWindow.UWindowDialogControl

// UWindowEditBox - simple edit box, for use in other controls such as // UWindowComboxBoxControl, UWindowEditBoxControl etc.
Variables
 int CaretOffset
 UWindowEditBoxHistory CurrentHistory
 UWindowEditBoxHistory HistoryList
 float LastDrawTime
 int MaxLength
 UWindowDialogControl NotifyOwner
 float Offset
 string Value
 string Value2
 bool bAllSelected
 bool bCanEdit
 bool bChangePending
 bool bControlDown
 bool bDelayedNotify
 bool bHistory
 bool bKeyDown
 bool bNumericFloat
 bool bNumericOnly
 bool bSelectOnFocus
 bool bShiftDown
 bool bShowCaret


Function Summary
 bool Backspace()
 void Clear()
 void Click(float X, float Y)
 void Close(optional bool)
 void Created()
 bool Delete()
 void DoubleClick(float X, float Y)
 void EditCopy()
 void EditCut()
 void EditPaste()
 void FocusOtherWindow(UWindowWindow W)
 string GetValue()
 string GetValue2()
 bool Insert(byte C)
     
// Inserts a character at the current caret position
 void InsertText(string Text)
 void KeyDown(int Key, float X, float Y)
 void KeyFocusEnter()
 void KeyFocusExit()
 void KeyType(int Key, float MouseX, float MouseY)
 void KeyUp(int Key, float X, float Y)
 void LMouseDown(float X, float Y)
 bool MoveEnd()
 bool MoveHome()
 bool MoveLeft()
 bool MoveRight()
 void Notify(byte E)
 void Paint(Canvas C, float X, float Y)
 void SelectAll()
 void SetEditable(bool bEditable)
 void SetHistory(bool bInHistory)
 void SetValue(string NewValue, optional string)
 bool WordLeft()
 bool WordRight()



Source Code


00001	// UWindowEditBox - simple edit box, for use in other controls such as 
00002	// UWindowComboxBoxControl, UWindowEditBoxControl etc.
00003	
00004	class UWindowEditBox extends UWindowDialogControl;
00005	
00006	var string		Value;
00007	var string		Value2;
00008	var int			CaretOffset;
00009	var int			MaxLength;
00010	var float		LastDrawTime;
00011	var bool		bShowCaret;
00012	var float		Offset;
00013	var UWindowDialogControl	NotifyOwner;
00014	var bool		bNumericOnly;
00015	var bool		bNumericFloat;
00016	var bool		bCanEdit;
00017	var bool		bAllSelected;
00018	var bool		bSelectOnFocus;
00019	var bool		bDelayedNotify;
00020	var bool		bChangePending;
00021	var bool		bControlDown;
00022	var bool		bShiftDown;
00023	var bool		bHistory;
00024	var bool		bKeyDown;
00025	var UWindowEditBoxHistory	HistoryList;
00026	var UWindowEditBoxHistory	CurrentHistory;
00027	
00028	function Created()
00029	{
00030		Super.Created();
00031		bCanEdit = True;
00032		bControlDown = False;
00033		bShiftDown = False;
00034	
00035		MaxLength = 255;
00036		CaretOffset = 0;
00037		Offset = 0;
00038		LastDrawTime = GetLevel().TimeSeconds;
00039	}
00040	
00041	function SetHistory(bool bInHistory)
00042	{
00043		bHistory = bInHistory;
00044	
00045		if(bHistory && HistoryList==None)
00046		{
00047			HistoryList = new(None) class'UWindowEditBoxHistory';
00048			HistoryList.SetupSentinel();
00049			CurrentHistory = None;
00050		}
00051		else
00052		if(!bHistory && HistoryList!=None)
00053		{
00054			HistoryList = None;
00055			CurrentHistory = None;
00056		}
00057	}
00058	
00059	function SetEditable(bool bEditable)
00060	{
00061		bCanEdit = bEditable;
00062	}
00063	
00064	function SetValue(string NewValue, optional string NewValue2)
00065	{
00066		Value = NewValue;
00067		Value2 = NewValue2;
00068	
00069		if(CaretOffset > Len(Value))
00070			CaretOffset = Len(Value);		
00071		Notify(DE_Change);
00072	}
00073	
00074	function Clear()
00075	{
00076		CaretOffset = 0;
00077		Value="";
00078		Value2="";
00079		bAllSelected = False;
00080		if(bDelayedNotify)
00081			bChangePending = True;
00082		else
00083			Notify(DE_Change);
00084	}
00085	
00086	function SelectAll()
00087	{
00088		if(bCanEdit && Value != "")
00089		{
00090			CaretOffset = Len(Value);
00091			bAllSelected = True;
00092		}
00093	}
00094	
00095	function string GetValue()
00096	{
00097		return Value;
00098	}
00099	
00100	function string GetValue2()
00101	{
00102		return Value2;
00103	}
00104	
00105	function Notify(byte E)
00106	{
00107		if(NotifyOwner != None)
00108		{
00109			NotifyOwner.Notify(E);
00110		} else {
00111			Super.Notify(E);
00112		}
00113	}
00114	
00115	function InsertText(string Text)
00116	{
00117		local int i;
00118	
00119		for(i=0;i<Len(Text);i++)
00120			Insert(Asc(Mid(Text,i,1)));
00121	}
00122	
00123	// Inserts a character at the current caret position
00124	function bool Insert(byte C)
00125	{
00126		local string	NewValue;
00127	
00128		NewValue = Left(Value, CaretOffset) $ Chr(C) $ Mid(Value, CaretOffset);
00129	
00130		if(Len(NewValue) > MaxLength) 
00131			return False;
00132	
00133		CaretOffset++;
00134	
00135		Value = NewValue;
00136		if(bDelayedNotify)
00137			bChangePending = True;
00138		else
00139			Notify(DE_Change);
00140		return True;
00141	}
00142	
00143	function bool Backspace()
00144	{
00145		local string	NewValue;
00146	
00147		if(CaretOffset == 0) return False;
00148	
00149		NewValue = Left(Value, CaretOffset - 1) $ Mid(Value, CaretOffset);
00150		CaretOffset--;
00151	
00152		Value = NewValue;
00153		if(bDelayedNotify)
00154			bChangePending = True;
00155		else
00156			Notify(DE_Change);
00157		return True;
00158	}
00159	
00160	function bool Delete()
00161	{
00162		local string	NewValue;
00163	
00164		if(CaretOffset == Len(Value)) return False;
00165	
00166		NewValue = Left(Value, CaretOffset) $ Mid(Value, CaretOffset + 1);
00167	
00168		Value = NewValue;
00169		Notify(DE_Change);
00170		return True;
00171	}
00172	
00173	function bool WordLeft()
00174	{
00175		while(CaretOffset > 0 && Mid(Value, CaretOffset - 1, 1) == " ")
00176			CaretOffset--;
00177		while(CaretOffset > 0 && Mid(Value, CaretOffset - 1, 1) != " ")
00178			CaretOffset--;
00179	
00180		LastDrawTime = GetLevel().TimeSeconds;
00181		bShowCaret = True;
00182	
00183		return True;	
00184	}
00185	
00186	function bool MoveLeft()
00187	{
00188		if(CaretOffset == 0) return False;
00189		CaretOffset--;
00190	
00191		LastDrawTime = GetLevel().TimeSeconds;
00192		bShowCaret = True;
00193	
00194		return True;	
00195	}
00196	
00197	function bool MoveRight()
00198	{
00199		if(CaretOffset == Len(Value)) return False;
00200		CaretOffset++;
00201	
00202		LastDrawTime = GetLevel().TimeSeconds;
00203		bShowCaret = True;
00204	
00205		return True;	
00206	}
00207	
00208	function bool WordRight()
00209	{
00210		while(CaretOffset < Len(Value) && Mid(Value, CaretOffset, 1) != " ")
00211			CaretOffset++;
00212		while(CaretOffset < Len(Value) && Mid(Value, CaretOffset, 1) == " ")
00213			CaretOffset++;
00214	
00215		LastDrawTime = GetLevel().TimeSeconds;
00216		bShowCaret = True;
00217	
00218		return True;	
00219	}
00220	
00221	function bool MoveHome()
00222	{
00223		CaretOffset = 0;
00224	
00225		LastDrawTime = GetLevel().TimeSeconds;
00226		bShowCaret = True;
00227	
00228		return True;	
00229	}
00230	
00231	function bool MoveEnd()
00232	{
00233		CaretOffset = Len(Value);
00234	
00235		LastDrawTime = GetLevel().TimeSeconds;
00236		bShowCaret = True;
00237	
00238		return True;	
00239	}
00240	
00241	function EditCopy()
00242	{
00243		if(bAllSelected || !bCanEdit)
00244			GetPlayerOwner().CopyToClipboard(Value);
00245	}
00246	
00247	function EditPaste()
00248	{
00249		if(bCanEdit)
00250		{
00251			if(bAllSelected)
00252				Clear();
00253			InsertText(GetPlayerOwner().PasteFromClipboard());
00254		}
00255	}
00256	
00257	function EditCut()
00258	{
00259		if(bCanEdit)
00260		{
00261			if(bAllSelected)
00262			{
00263				GetPlayerOwner().CopyToClipboard(Value);
00264				bAllSelected = False;
00265				Clear();
00266			}
00267		}
00268		else
00269			EditCopy();
00270	}
00271	
00272	function KeyType( int Key, float MouseX, float MouseY )
00273	{
00274		if(bCanEdit && bKeyDown)
00275		{
00276			if( !bControlDown )
00277			{
00278				if(bAllSelected)
00279					Clear();
00280	
00281				bAllSelected = False;
00282	
00283				if(bNumericOnly)
00284				{
00285					if( Key>=0x30 && Key<=0x39 )  
00286					{
00287						Insert(Key);
00288					}
00289				}
00290				else
00291				{
00292					if( Key>=0x20 && Key<0x80 )
00293					{
00294						Insert(Key);
00295					}
00296				}
00297			}
00298		}
00299	}
00300	
00301	function KeyUp(int Key, float X, float Y)
00302	{
00303		local PlayerPawn P;
00304		bKeyDown = False;
00305		P = GetPlayerOwner();
00306		switch (Key)
00307		{
00308		case P.EInputKey.IK_Ctrl:
00309			bControlDown = False;
00310			break;
00311		case P.EInputKey.IK_Shift:
00312			bShiftDown = False;
00313			break;
00314		}
00315	}
00316	
00317	function KeyDown(int Key, float X, float Y)
00318	{
00319		local PlayerPawn P;
00320	
00321		bKeyDown = True;
00322		P = GetPlayerOwner();
00323	
00324		switch (Key)
00325		{
00326		case P.EInputKey.IK_Ctrl:
00327			bControlDown = True;
00328			break;
00329		case P.EInputKey.IK_Shift:
00330			bShiftDown = True;
00331			break;
00332		case P.EInputKey.IK_Escape:
00333			break;
00334		case P.EInputKey.IK_Enter:
00335			if(bCanEdit)
00336			{
00337				if(bHistory)
00338				{
00339					if(Value != "")
00340					{
00341						CurrentHistory = UWindowEditBoxHistory(HistoryList.Insert(class'UWindowEditBoxHistory'));
00342						CurrentHistory.HistoryText = Value;
00343					}
00344					CurrentHistory = HistoryList;
00345				}
00346				Notify(DE_EnterPressed);
00347			}
00348			break;
00349		case P.EInputKey.IK_MouseWheelUp:
00350			if(bCanEdit)
00351				Notify(DE_WheelUpPressed);
00352			break;
00353		case P.EInputKey.IK_MouseWheelDown:
00354			if(bCanEdit)
00355				Notify(DE_WheelDownPressed);
00356			break;
00357	
00358		case P.EInputKey.IK_Right:
00359			if(bCanEdit) 
00360			{
00361				if(bControlDown)
00362					WordRight();
00363				else
00364					MoveRight();
00365			}
00366			bAllSelected = False;
00367			break;
00368		case P.EInputKey.IK_Left:
00369			if(bCanEdit)
00370			{
00371				if(bControlDown)
00372					WordLeft();
00373				else
00374					MoveLeft();
00375			}
00376			bAllSelected = False;
00377			break;
00378		case P.EInputKey.IK_Up:
00379			if(bCanEdit && bHistory)
00380			{
00381				bAllSelected = False;
00382				if(CurrentHistory != None && CurrentHistory.Next != None)
00383				{
00384					CurrentHistory = UWindowEditBoxHistory(CurrentHistory.Next);
00385					SetValue(CurrentHistory.HistoryText);
00386					MoveEnd();
00387				}
00388			}
00389			break;
00390		case P.EInputKey.IK_Down:
00391			if(bCanEdit && bHistory)
00392			{
00393				bAllSelected = False;
00394				if(CurrentHistory != None && CurrentHistory.Prev != None)
00395				{
00396					CurrentHistory = UWindowEditBoxHistory(CurrentHistory.Prev);
00397					SetValue(CurrentHistory.HistoryText);
00398					MoveEnd();
00399				}
00400			}
00401			break;
00402		case P.EInputKey.IK_Home:
00403			if(bCanEdit)
00404				MoveHome();
00405			bAllSelected = False;
00406			break;
00407		case P.EInputKey.IK_End:
00408			if(bCanEdit)
00409				MoveEnd();
00410			bAllSelected = False;
00411			break;
00412		case P.EInputKey.IK_Backspace:
00413			if(bCanEdit)
00414			{
00415				if(bAllSelected)
00416					Clear();
00417				else
00418					Backspace();
00419			}
00420			bAllSelected = False;
00421			break;
00422		case P.EInputKey.IK_Delete:
00423			if(bCanEdit)
00424			{
00425				if(bAllSelected)
00426					Clear();
00427				else
00428					Delete();
00429			}
00430			bAllSelected = False;
00431			break;
00432		case P.EInputKey.IK_Period:
00433		case P.EInputKey.IK_NumPadPeriod:
00434			if (bNumericFloat)
00435				Insert(Asc("."));
00436			break;
00437		default:
00438			if( bControlDown )
00439			{
00440				if( Key == Asc("c") || Key == Asc("C"))
00441					EditCopy();
00442	
00443				if( Key == Asc("v") || Key == Asc("V"))
00444					EditPaste();
00445	
00446				if( Key == Asc("x") || Key == Asc("X"))
00447					EditCut();
00448			}
00449			else
00450			{
00451				if(NotifyOwner != None)
00452					NotifyOwner.KeyDown(Key, X, Y);
00453				else
00454					Super.KeyDown(Key, X, Y);
00455			}
00456			break;
00457		}
00458	}
00459	
00460	function Click(float X, float Y)
00461	{
00462		Notify(DE_Click);
00463	}
00464	
00465	function LMouseDown(float X, float Y)
00466	{
00467		Super.LMouseDown(X, Y);
00468		Notify(DE_LMouseDown);
00469	}
00470	
00471	function Paint(Canvas C, float X, float Y)
00472	{
00473		local float W, H;
00474		local float TextY;
00475	
00476		C.Font = Root.Fonts[Font];
00477	
00478		TextSize(C, "A", W, H);
00479		TextY = (WinHeight - H) / 2;
00480	
00481		TextSize(C, Left(Value, CaretOffset), W, H);
00482	
00483		C.DrawColor.R = 255;
00484		C.DrawColor.G = 255;
00485		C.DrawColor.B = 255;
00486	
00487		if(W + Offset < 0)
00488			Offset = -W;
00489	
00490		if(W + Offset > (WinWidth - 2))
00491		{
00492			Offset = (WinWidth - 2) - W;
00493			if(Offset > 0) Offset = 0;
00494		}
00495	
00496		C.DrawColor = TextColor;
00497	
00498		if(bAllSelected)
00499		{
00500			DrawStretchedTexture(C, Offset + 1, TextY, W, H, Texture'UWindow.WhiteTexture');
00501	
00502			// Invert Colors
00503			C.DrawColor.R = 255 ^ C.DrawColor.R;
00504			C.DrawColor.G = 255 ^ C.DrawColor.G;
00505			C.DrawColor.B = 255 ^ C.DrawColor.B;
00506		}
00507	
00508		ClipText(C, Offset + 1, TextY,  Value);
00509	
00510		if((!bHasKeyboardFocus) || (!bCanEdit))
00511			bShowCaret = False;
00512		else
00513		{
00514			if((GetLevel().TimeSeconds > LastDrawTime + 0.3) || (GetLevel().TimeSeconds < LastDrawTime))
00515			{
00516				LastDrawTime = GetLevel().TimeSeconds;
00517				bShowCaret = !bShowCaret;
00518			}
00519		}
00520	
00521		if(bShowCaret)
00522			ClipText(C, Offset + W - 1, TextY, "|");
00523	}
00524	
00525	function Close(optional bool bByParent)
00526	{
00527		if(bChangePending)
00528		{
00529			bChangePending = False;
00530			Notify(DE_Change);
00531		}
00532		bKeyDown = False;
00533		Super.Close(bByParent);
00534	}
00535	
00536	function FocusOtherWindow(UWindowWindow W)
00537	{
00538		if(bChangePending)
00539		{
00540			bChangePending = False;
00541			Notify(DE_Change);
00542		}
00543	
00544		if(NotifyOwner != None)
00545			NotifyOwner.FocusOtherWindow(W);
00546		else
00547			Super.FocusOtherWindow(W);
00548	}
00549	
00550	function KeyFocusEnter()
00551	{
00552		if(bSelectOnFocus && !bHasKeyboardFocus)
00553			SelectAll();
00554	
00555		Super.KeyFocusEnter();
00556	}
00557	
00558	function DoubleClick(float X, float Y)
00559	{
00560		Super.DoubleClick(X, Y);
00561		SelectAll();
00562	}
00563	
00564	function KeyFocusExit()
00565	{
00566		bAllSelected = False;
00567		Super.KeyFocusExit();
00568	}
00569		
00570	
00571	defaultproperties
00572	{
00573	}

End Source Code