UBrowser
Class UBrowserServerGrid

source: c:\runehov\UBrowser\Classes\UBrowserServerGrid.uc
Core.Object
   |
   +--UWindow.UWindowBase
      |
      +--UWindow.UWindowWindow
         |
         +--UWindow.UWindowGrid
            |
            +--UBrowser.UBrowserServerGrid
Direct Known Subclasses:RBrowserServerGrid

class UBrowserServerGrid
extends UWindow.UWindowGrid

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


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

End Source Code