UBrowser
Class UBrowserServerGrid

source: e:\games\UnrealTournament\UBrowser\Classes\UBrowserServerGrid.uc
Core.Object
   |
   +--UWindow.UWindowBase
      |
      +--UWindow.UWindowWindow
         |
         +--UWindow.UWindowGrid
            |
            +--UBrowser.UBrowserServerGrid
Direct Known Subclasses:UTBrowserServerGrid

class UBrowserServerGrid
extends UWindow.UWindowGrid

//============================================================================= // UBrowserServerGrid - base class for server listings //=============================================================================
Variables
 int AutoPingInterval
 int Count
 UBrowserRightClickMenu Menu
 UBrowserServerList OldPingServer
 MapNameName, PlayersName
 UBrowserServerList SelectedServer
 Players, SortByColumn
 float TimePassed
 bool bSortDescending


Function Summary
 int ByMap(UBrowserServerList T, UBrowserServerList B)
 int ByName(UBrowserServerList T, UBrowserServerList B)
 int ByPing(UBrowserServerList T, UBrowserServerList B)
 int ByPlayers(UBrowserServerList T, UBrowserServerList B)
 void Close(optional bool)
 int Compare(UBrowserServerList T, UBrowserServerList B)
 void CreateColumns()
 void Created()
 void DoubleClickRow(int Row)
 void DrawCell(Canvas C, float X, float Y, UWindowGridColumn Column, UBrowserServerList List)
 int GetSelectedRow()
 UBrowserServerList GetServerUnderRow(int Row)
 void JoinServer(UBrowserServerList Server)
 void KeyDown(int Key, float X, float Y)
 void MouseLeaveColumn(UWindowGridColumn Column)
 void PaintColumn(Canvas C, UWindowGridColumn Column, float MouseX, float MouseY)
 void RePing()
 void Refresh()
 void RefreshServer()
 void RightClickRow(int Row, float X, float Y)
 void SelectRow(int Row)
 void ShowInfo(UBrowserServerList List)
 void SortColumn(UWindowGridColumn Column)
 void Tick(float DeltaTime)



Source Code


00001	//=============================================================================
00002	// UBrowserServerGrid - base class for server listings
00003	//=============================================================================
00004	class UBrowserServerGrid extends UWindowGrid;
00005	
00006	#exec TEXTURE IMPORT NAME=Highlight FILE=Textures\Highlight.bmp GROUP="Icons" FLAGS=2 MIPS=OFF
00007	
00008	var UBrowserRightClickMenu Menu;
00009	var UWindowGridColumn Server, Ping, MapName, Players, SortByColumn;
00010	var bool bSortDescending;
00011	var localized string ServerName, PingName, MapNameName, PlayersName;
00012	
00013	var UBrowserServerList SelectedServer;
00014	var int Count;
00015	
00016	var float TimePassed;
00017	var int AutoPingInterval;
00018	var UBrowserServerList OldPingServer;
00019	
00020	function Created()
00021	{
00022		Super.Created();
00023	
00024		RowHeight = 12;
00025	
00026		CreateColumns();
00027	
00028		Menu = UBrowserRightClickMenu(Root.CreateWindow(UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).RightClickMenuClass, 0, 0, 100, 100, Self));
00029		Menu.HideWindow();
00030	}
00031	
00032	function Close(optional bool bByParent)
00033	{
00034		Super.Close(bByParent);
00035		if(Menu != None && Menu.bWindowVisible)
00036			Menu.CloseUp();
00037	}
00038	
00039	function CreateColumns()
00040	{
00041		Server	= AddColumn(ServerName, 300);
00042		Ping	= AddColumn(PingName, 30);
00043		MapName	= AddColumn(MapNameName, 100);
00044		Players	= AddColumn(PlayersName, 50);
00045	
00046		SortByColumn = Ping;
00047	}
00048	
00049	function DrawCell(Canvas C, float X, float Y, UWindowGridColumn Column, UBrowserServerList List)
00050	{
00051		switch(Column)
00052		{
00053		case Server:
00054			Column.ClipText( C, X, Y, List.HostName );
00055			break;
00056		case Ping:
00057			Column.ClipText( C, X, Y, Int(List.Ping) );
00058			break;
00059		case MapName:
00060			Column.ClipText( C, X, Y, List.MapDisplayName );
00061			break;
00062		case Players:
00063			Column.ClipText( C, X, Y, List.NumPlayers$"/"$List.MaxPlayers );
00064			break;
00065		}
00066	}
00067	
00068	function PaintColumn(Canvas C, UWindowGridColumn Column, float MouseX, float MouseY) 
00069	{
00070		local UBrowserServerList List;
00071		local float Y;
00072		local int Visible;
00073		local int Skipped;
00074		local int TopMargin;
00075		local int BottomMargin;
00076	
00077		C.Font = Root.Fonts[F_Normal];
00078	
00079	
00080		List = UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).PingedList;
00081	
00082		if(List == None)
00083			Count = 0;
00084		else
00085			Count = List.Count();
00086	
00087		if(bShowHorizSB)
00088			BottomMargin = LookAndFeel.Size_ScrollbarWidth;
00089		else
00090			BottomMargin = 0;
00091	
00092		TopMargin = LookAndFeel.ColumnHeadingHeight;
00093	
00094		Visible = int((WinHeight - (TopMargin + BottomMargin))/RowHeight);
00095		
00096		VertSB.SetRange(0, Count+1, Visible);
00097		TopRow = VertSB.Pos;
00098	
00099		Skipped = 0;
00100	
00101		List = UBrowserServerList(List.Next);
00102	
00103		if(List != None) 
00104		{
00105			Y = 1;
00106	
00107			while((Y < RowHeight + WinHeight - RowHeight - (TopMargin + BottomMargin)) && (List != None))
00108			{
00109				// FIXME: make more efficient - cache top server in list if TopRow doesn't change
00110				if(Skipped >= VertSB.Pos)
00111				{
00112					// Draw highlight
00113					if(List == SelectedServer)
00114						Column.DrawStretchedTexture( C, 0, Y-1 + TopMargin, Column.WinWidth, RowHeight + 1, Texture'Highlight');
00115	
00116					DrawCell(C, 2, Y + TopMargin, Column, List);
00117					Y = Y + RowHeight;			
00118				} 
00119				Skipped ++;
00120	
00121				List = UBrowserServerList(List.Next);
00122			}
00123		}
00124	}
00125	
00126	function SortColumn(UWindowGridColumn Column)
00127	{
00128		if(SortByColumn == Column)
00129			bSortDescending = !bSortDescending;
00130		else
00131			bSortDescending = False;
00132	
00133		SortByColumn = Column;
00134	
00135		UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).PingedList.Sort();	
00136	}
00137	
00138	function Tick(float DeltaTime)
00139	{
00140		local UBrowserServerListWindow W;
00141	
00142		W = UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow'));
00143	
00144		if(W.PingState == PS_Done && SelectedServer == None)
00145		{
00146			SelectedServer = UBrowserServerList(W.PingedList.Next);
00147			if(SelectedServer == None || SelectedServer.bPinging)
00148				SelectedServer = None;
00149			else
00150				UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).AutoInfo(SelectedServer);
00151		}
00152	
00153		if(W.PingState == PS_Done)
00154		{
00155			if(TimePassed >= AutoPingInterval)
00156			{
00157				TimePassed = 0;
00158	
00159				if(SelectedServer != OldPingServer)
00160				{
00161					if(OldPingServer != None)
00162						OldPingServer.CancelPing();
00163					OldPingServer = SelectedServer;
00164				}
00165	
00166				if(SelectedServer != None && !SelectedServer.bPinging)
00167					SelectedServer.PingServer(False, True, True);
00168			}
00169			TimePassed = TimePassed + DeltaTime;
00170		}
00171	}
00172	
00173	function SelectRow(int Row)
00174	{
00175		local UBrowserServerList S;
00176	
00177		S = GetServerUnderRow(Row);
00178	
00179		if(SelectedServer != S)
00180		{
00181			if(S != None)
00182				UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).AutoInfo(S);
00183			TimePassed = 0;
00184		}
00185	
00186		if(S != None)
00187			SelectedServer = S;
00188	}
00189	
00190	function RightClickRow(int Row, float X, float Y)
00191	{
00192		local float MenuX, MenuY;
00193	
00194		WindowToGlobal(X, Y, MenuX, MenuY);
00195		Menu.WinLeft = MenuX;
00196		Menu.WinTop = MenuY;
00197		Menu.List = GetServerUnderRow(Row);
00198		Menu.Grid = Self;
00199		Menu.ShowWindow();
00200	}
00201	
00202	function UBrowserServerList GetServerUnderRow(int Row)
00203	{
00204		local int i;
00205		local UBrowserServerList List;
00206	
00207		List = UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).PingedList;
00208		if(List != None)
00209		{
00210			i = 0;
00211			List = UBrowserServerList(List.Next);
00212			while(List != None)
00213			{
00214				if(i == Row)
00215					return List;
00216	
00217				List = UBrowserServerList(List.Next);
00218				i++;
00219			}
00220		}
00221		return None;
00222	}
00223	
00224	function int GetSelectedRow()
00225	{
00226		local int i;
00227		local UBrowserServerList List;
00228	
00229		List = UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).PingedList;
00230		if(List != None)
00231		{
00232			i = 0;
00233			List = UBrowserServerList(List.Next);
00234			while(List != None)
00235			{
00236				if(List == SelectedServer)
00237					return i;
00238	
00239				List = UBrowserServerList(List.Next);
00240				i++;
00241			}
00242		}
00243		return -1;
00244	}
00245	
00246	function JoinServer(UBrowserServerList Server)
00247	{
00248		if(Server != None && Server.GamePort != 0) 
00249		{
00250			GetPlayerOwner().ClientTravel("unreal://"$Server.IP$":"$Server.GamePort$UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).URLAppend, TRAVEL_Absolute, false);
00251			GetParent(class'UWindowFramedWindow').Close();
00252			Root.Console.CloseUWindow();
00253		}
00254	}
00255	
00256	function DoubleClickRow(int Row)
00257	{
00258		local UBrowserServerList Server;
00259	
00260		Server = GetServerUnderRow(Row);
00261		if(SelectedServer != Server) return; 
00262		JoinServer(Server);
00263	}
00264	
00265	function MouseLeaveColumn(UWindowGridColumn Column)
00266	{
00267		ToolTip("");
00268	}
00269	
00270	function KeyDown(int Key, float X, float Y) 
00271	{
00272		switch(Key)
00273		{
00274		case 0x74: // IK_F5;
00275			Refresh();
00276			break;
00277		case 0x26: // IK_Up
00278			SelectRow(Clamp(GetSelectedRow() - 1, 0, Count - 1));
00279			VertSB.Show(GetSelectedRow());
00280			break;
00281		case 0x28: // IK_Down
00282			SelectRow(Clamp(GetSelectedRow() + 1, 0, Count - 1));
00283			VertSB.Show(GetSelectedRow());
00284			break;
00285		case 0x0D: // IK_Enter:
00286			DoubleClickRow(GetSelectedRow());
00287			break;
00288		default:
00289			Super.KeyDown(Key, X, Y);
00290			break;
00291		}
00292	}
00293	
00294	function int Compare(UBrowserServerList T, UBrowserServerList B)
00295	{
00296		switch(SortByColumn)
00297		{
00298		case Server:
00299			return ByName(T, B);
00300		case Ping:
00301			return ByPing(T, B);
00302		case MapName:
00303			return ByMap(T, B);
00304		case Players:
00305			return ByPlayers(T, B);
00306		default:
00307			return 0;
00308		}	
00309	}
00310	
00311	function int ByPing(UBrowserServerList T, UBrowserServerList B)
00312	{
00313		local int Result;
00314	
00315		if(B == None) return -1;
00316		
00317		if(T.Ping < B.Ping)
00318		{
00319			Result = -1;
00320		}
00321		else
00322		if (T.Ping > B.Ping)
00323		{
00324			Result = 1;
00325		}
00326		else
00327		{
00328	/*		if(T.HostName < B.HostName)
00329				Result = -1;
00330			else
00331			if(T.HostName > B.HostName)
00332				Result = 1;
00333			else
00334	*/
00335				Result = 0;
00336		}
00337	
00338		if(bSortDescending)
00339			Result = -Result;
00340	
00341		return Result;
00342	}
00343	
00344	function int ByName(UBrowserServerList T, UBrowserServerList B)
00345	{
00346		local int Result;
00347	
00348		if(B == None) return -1;
00349		if(T.Ping == 9999) return 1;
00350		if(B.Ping == 9999) return -1;
00351		
00352		if(T.HostName < B.HostName)
00353		{
00354			Result = -1;
00355		}
00356		else
00357		if (T.HostName > B.HostName)
00358		{
00359			Result = 1;
00360		}
00361		else
00362		{
00363			Result = 0;//T.Ping - B.Ping;
00364		}
00365	
00366		if(bSortDescending)
00367			Result = -Result;
00368	
00369		return Result;
00370	}
00371	
00372	function int ByMap(UBrowserServerList T, UBrowserServerList B)
00373	{
00374		local int Result;
00375	
00376		if(B == None) return -1;
00377		
00378		if(T.Ping == 9999) return 1;
00379		if(B.Ping == 9999) return -1;
00380	
00381		if(T.MapDisplayName < B.MapDisplayName)
00382		{
00383			Result = -1;
00384		}
00385		else 
00386		if (T.MapDisplayName > B.MapDisplayName)
00387		{
00388			Result = 1;
00389		}
00390		else
00391		{
00392			Result = T.Ping - B.Ping;
00393		}
00394	
00395		if(bSortDescending)
00396			Result = -Result;
00397		
00398		return Result;
00399	}
00400	
00401	function int ByPlayers(UBrowserServerList T, UBrowserServerList B)
00402	{
00403		local int Result;
00404	
00405		if(B == None) return -1;
00406		
00407		if(T.Ping == 9999) return 1;
00408		if(B.Ping == 9999) return -1;
00409	
00410		if(T.NumPlayers > B.NumPlayers)
00411		{
00412			Result = -1;
00413		}
00414		else
00415		if (T.NumPlayers < B.NumPlayers)
00416		{
00417			Result = 1;
00418		}
00419		else
00420		{
00421			if (T.MaxPlayers > B.MaxPlayers)
00422			{
00423				Result = -1;
00424			}
00425			else
00426			if(T.MaxPlayers < B.MaxPlayers)
00427			{
00428				Result = 1;
00429			}
00430			else
00431			{
00432				Result = T.Ping - B.Ping;
00433			}
00434		}
00435	
00436		if(bSortDescending)
00437			Result = -Result;
00438	
00439		return Result;
00440	}
00441	
00442	function ShowInfo(UBrowserServerList List)
00443	{
00444		UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).ShowInfo(List);
00445	}
00446	
00447	function Refresh()
00448	{
00449		UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).Refresh();
00450	}
00451	
00452	function RefreshServer()
00453	{
00454		TimePassed = AutoPingInterval;
00455	}
00456	
00457	function RePing()
00458	{
00459		UBrowserServerListWindow(GetParent(class'UBrowserServerListWindow')).RePing();
00460	}
00461	
00462	defaultproperties
00463	{
00464	     ServerName="Server"
00465	     PingName="Ping"
00466	     MapNameName="Map Name"
00467	     PlayersName="Players"
00468	     AutoPingInterval=5
00469	}

End Source Code