Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
8.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "pch_serverbrowser.h"
  8. using namespace vgui;
  9. //-----------------------------------------------------------------------------
  10. // Purpose: Invisible panel that forwards up mouse movement
  11. //-----------------------------------------------------------------------------
  12. class CMouseMessageForwardingPanel : public vgui::Panel
  13. {
  14. DECLARE_CLASS_SIMPLE( CMouseMessageForwardingPanel, vgui::Panel );
  15. public:
  16. CMouseMessageForwardingPanel( Panel *parent, const char *name );
  17. virtual void PerformLayout( void );
  18. virtual void OnMousePressed( vgui::MouseCode code );
  19. virtual void OnMouseDoublePressed( vgui::MouseCode code );
  20. virtual void OnMouseWheeled(int delta);
  21. };
  22. CMouseMessageForwardingPanel::CMouseMessageForwardingPanel( Panel *parent, const char *name ) : BaseClass( parent, name )
  23. {
  24. // don't draw an
  25. SetPaintEnabled(false);
  26. SetPaintBackgroundEnabled(false);
  27. SetPaintBorderEnabled(false);
  28. }
  29. void CMouseMessageForwardingPanel::PerformLayout()
  30. {
  31. // fill out the whole area
  32. int w, t;
  33. GetParent()->GetSize(w, t);
  34. SetBounds(0, 0, w, t);
  35. }
  36. void CMouseMessageForwardingPanel::OnMousePressed( vgui::MouseCode code )
  37. {
  38. if ( GetParent() )
  39. {
  40. GetParent()->OnMousePressed( code );
  41. }
  42. }
  43. void CMouseMessageForwardingPanel::OnMouseDoublePressed( vgui::MouseCode code )
  44. {
  45. if ( GetParent() )
  46. {
  47. GetParent()->OnMouseDoublePressed( code );
  48. }
  49. }
  50. void CMouseMessageForwardingPanel::OnMouseWheeled(int delta)
  51. {
  52. if ( GetParent() )
  53. {
  54. GetParent()->OnMouseWheeled( delta );
  55. }
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose:
  59. //-----------------------------------------------------------------------------
  60. CQuickListPanel::CQuickListPanel( vgui::Panel* pParent, const char *pElementName ) : BaseClass( pParent, pElementName )
  61. {
  62. SetParent( pParent );
  63. m_pListPanelParent = pParent;
  64. CMouseMessageForwardingPanel *panel = new CMouseMessageForwardingPanel(this, NULL);
  65. panel->SetZPos(3);
  66. m_pLatencyImage = new ImagePanel( this, "latencyimage" );
  67. m_pPlayerCountLabel = new Label( this, "playercount", "" );
  68. m_pOtherServersLabel = new Label( this, "otherservercount", "" );
  69. m_pServerNameLabel = new Label( this, "servername", "" );
  70. m_pBGroundPanel = new Panel( this, "background" );
  71. m_pMapImage = new ImagePanel( this, "mapimage" );
  72. m_pGameTypeLabel = new Label( this, "gametype", "" );
  73. m_pMapNameLabel = new Label( this, "mapname", "" );
  74. m_pLatencyLabel = new Label( this, "latencytext", "" );
  75. m_pReplayImage = new ImagePanel( this, "replayimage" );
  76. const char *pPathID = "PLATFORM";
  77. if ( g_pFullFileSystem->FileExists( "servers/QuickListPanel.res", "MOD" ) )
  78. {
  79. pPathID = "MOD";
  80. }
  81. LoadControlSettings( "servers/QuickListPanel.res", pPathID );
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose:
  85. //-----------------------------------------------------------------------------
  86. void CQuickListPanel::ApplySchemeSettings(IScheme *pScheme)
  87. {
  88. BaseClass::ApplySchemeSettings(pScheme);
  89. if ( pScheme && m_pBGroundPanel )
  90. {
  91. m_pBGroundPanel->SetBgColor( pScheme->GetColor("QuickListBGDeselected", Color(255, 255, 255, 0 ) ) );
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose:
  96. //-----------------------------------------------------------------------------
  97. void CQuickListPanel::SetRefreshing( void )
  98. {
  99. if ( m_pServerNameLabel )
  100. {
  101. m_pServerNameLabel->SetText( g_pVGuiLocalize->Find("#ServerBrowser_QuickListRefreshing") );
  102. }
  103. if ( m_pPlayerCountLabel )
  104. {
  105. m_pPlayerCountLabel->SetVisible( false );
  106. }
  107. if ( m_pOtherServersLabel )
  108. {
  109. m_pOtherServersLabel->SetVisible( false );
  110. }
  111. if ( m_pLatencyImage )
  112. {
  113. m_pLatencyImage->SetVisible( false );
  114. }
  115. if ( m_pReplayImage )
  116. {
  117. m_pReplayImage->SetVisible( false );
  118. }
  119. if ( m_pLatencyLabel )
  120. {
  121. m_pLatencyLabel->SetVisible( false );
  122. }
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose:
  126. //-----------------------------------------------------------------------------
  127. void CQuickListPanel::SetMapName( const char *pMapName )
  128. {
  129. Q_strncpy( m_szMapName, pMapName, sizeof( m_szMapName ) );
  130. if ( m_pMapNameLabel )
  131. {
  132. m_pMapNameLabel->SetText( pMapName );
  133. m_pMapNameLabel->SizeToContents();
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Purpose:
  138. //-----------------------------------------------------------------------------
  139. void CQuickListPanel::SetGameType( const char *pGameType )
  140. {
  141. if ( strlen ( pGameType ) == 0 )
  142. {
  143. m_pGameTypeLabel->SetVisible( false );
  144. return;
  145. }
  146. char gametype[ 512 ];
  147. Q_snprintf( gametype, sizeof( gametype ), "(%s)", pGameType );
  148. m_pGameTypeLabel->SetText( gametype );
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Purpose:
  152. //-----------------------------------------------------------------------------
  153. void CQuickListPanel::SetServerInfo ( KeyValues *pKV, int iListID, int iTotalServers )
  154. {
  155. if ( pKV == NULL )
  156. return;
  157. m_iListID = iListID;
  158. m_pServerNameLabel->SetText( pKV->GetString( "name", " " ) );
  159. int iPing = pKV->GetInt( "ping", 0 );
  160. if ( iPing <= 100 )
  161. {
  162. m_pLatencyImage->SetImage( "../vgui/icon_con_high.vmt" );
  163. }
  164. else if ( iPing <= 150 )
  165. {
  166. m_pLatencyImage->SetImage( "../vgui/icon_con_medium.vmt" );
  167. }
  168. else
  169. {
  170. m_pLatencyImage->SetImage( "../vgui/icon_con_low.vmt" );
  171. }
  172. m_pLatencyImage->SetVisible( false );
  173. if ( GameSupportsReplay() )
  174. {
  175. if ( pKV->GetInt( "Replay", 0 ) > 0 )
  176. {
  177. m_pReplayImage->SetVisible( true );
  178. }
  179. }
  180. char ping[ 512 ];
  181. Q_snprintf( ping, sizeof( ping ), "%d ms", iPing );
  182. m_pLatencyLabel->SetText( ping );
  183. m_pLatencyLabel->SetVisible( true );
  184. wchar_t players[ 512 ];
  185. wchar_t playercount[16];
  186. wchar_t *pwszPlayers = g_pVGuiLocalize->Find("#ServerBrowser_Players");
  187. g_pVGuiLocalize->ConvertANSIToUnicode( pKV->GetString( "players", " " ), playercount, sizeof( playercount ) );
  188. _snwprintf( players, ARRAYSIZE( players ), L"%ls %ls", playercount, pwszPlayers );
  189. m_pPlayerCountLabel->SetText( players );
  190. m_pPlayerCountLabel->SetVisible( true );
  191. // Now setup the other server count
  192. if ( iTotalServers == 2 )
  193. {
  194. m_pOtherServersLabel->SetText( g_pVGuiLocalize->Find("#ServerBrowser_QuickListOtherServer") );
  195. m_pOtherServersLabel->SetVisible( true );
  196. }
  197. else if ( iTotalServers > 2 )
  198. {
  199. wchar_t *pwszServers = g_pVGuiLocalize->Find("#ServerBrowser_QuickListOtherServers");
  200. _snwprintf( playercount, Q_ARRAYSIZE(playercount), L"%d", (iTotalServers-1) );
  201. g_pVGuiLocalize->ConstructString( players, sizeof( players ), pwszServers, 1, playercount );
  202. m_pOtherServersLabel->SetText( players );
  203. m_pOtherServersLabel->SetVisible( true );
  204. }
  205. else
  206. {
  207. m_pOtherServersLabel->SetVisible( false );
  208. }
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Purpose:
  212. //-----------------------------------------------------------------------------
  213. void CQuickListPanel::SetImage( const char *pMapName )
  214. {
  215. char path[ 512 ];
  216. Q_snprintf( path, sizeof( path ), "materials/vgui/maps/menu_thumb_%s.vmt", pMapName );
  217. char map[ 512 ];
  218. Q_snprintf( map, sizeof( map ), "maps/%s.bsp", pMapName );
  219. if ( g_pFullFileSystem->FileExists( map, "MOD" ) == false )
  220. {
  221. pMapName = "default_download";
  222. }
  223. else
  224. {
  225. if ( g_pFullFileSystem->FileExists( path, "MOD" ) == false )
  226. {
  227. pMapName = "default";
  228. }
  229. }
  230. if ( m_pMapImage )
  231. {
  232. char imagename[ 512 ];
  233. Q_snprintf( imagename, sizeof( imagename ), "..\\vgui\\maps\\menu_thumb_%s", pMapName );
  234. m_pMapImage->SetImage ( imagename );
  235. m_pMapImage->SetMouseInputEnabled( false );
  236. }
  237. }
  238. void CQuickListPanel::OnMousePressed( vgui::MouseCode code )
  239. {
  240. if ( m_pListPanelParent )
  241. {
  242. vgui::PanelListPanel *pParent = dynamic_cast < vgui::PanelListPanel *> ( m_pListPanelParent );
  243. if ( pParent )
  244. {
  245. pParent->SetSelectedPanel( this );
  246. m_pListPanelParent->CallParentFunction( new KeyValues("ItemSelected", "itemID", -1 ) );
  247. }
  248. if ( code == MOUSE_RIGHT )
  249. {
  250. m_pListPanelParent->CallParentFunction( new KeyValues("OpenContextMenu", "itemID", -1 ) );
  251. }
  252. }
  253. }
  254. void CQuickListPanel::OnMouseDoublePressed( vgui::MouseCode code )
  255. {
  256. if ( code == MOUSE_RIGHT )
  257. return;
  258. // call the panel
  259. OnMousePressed( code );
  260. m_pListPanelParent->CallParentFunction( new KeyValues("ConnectToServer", "code", code) );
  261. }