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.

214 lines
6.8 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: Constructor
  11. //-----------------------------------------------------------------------------
  12. CFavoriteGames::CFavoriteGames(vgui::Panel *parent) :
  13. CBaseGamesPage(parent, "FavoriteGames", eFavoritesServer )
  14. {
  15. m_bRefreshOnListReload = false;
  16. }
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Destructor
  19. //-----------------------------------------------------------------------------
  20. CFavoriteGames::~CFavoriteGames()
  21. {
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Purpose: loads favorites list from disk
  25. //-----------------------------------------------------------------------------
  26. void CFavoriteGames::LoadFavoritesList()
  27. {
  28. if ( steamapicontext->SteamMatchmaking() && steamapicontext->SteamMatchmaking()->GetFavoriteGameCount() == 0 )
  29. {
  30. // set empty message
  31. m_pGameList->SetEmptyListText("#ServerBrowser_NoFavoriteServers");
  32. }
  33. else
  34. {
  35. m_pGameList->SetEmptyListText("#ServerBrowser_NoInternetGamesResponded");
  36. }
  37. if ( m_bRefreshOnListReload )
  38. {
  39. m_bRefreshOnListReload = false;
  40. StartRefresh();
  41. }
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Purpose: returns true if the game list supports the specified ui elements
  45. //-----------------------------------------------------------------------------
  46. bool CFavoriteGames::SupportsItem(InterfaceItem_e item)
  47. {
  48. switch (item)
  49. {
  50. case FILTERS:
  51. case ADDSERVER:
  52. return true;
  53. case ADDCURRENTSERVER:
  54. return !IsSteam() && BFiltersVisible();
  55. case GETNEWLIST:
  56. default:
  57. return false;
  58. }
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: called when the current refresh list is complete
  62. //-----------------------------------------------------------------------------
  63. void CFavoriteGames::RefreshComplete( HServerListRequest hReq, EMatchMakingServerResponse response )
  64. {
  65. SetRefreshing(false);
  66. if ( steamapicontext->SteamMatchmaking() && steamapicontext->SteamMatchmaking()->GetFavoriteGameCount() == 0 )
  67. {
  68. // set empty message
  69. m_pGameList->SetEmptyListText("#ServerBrowser_NoFavoriteServers");
  70. }
  71. else
  72. {
  73. m_pGameList->SetEmptyListText("#ServerBrowser_NoInternetGamesResponded");
  74. }
  75. m_pGameList->SortList();
  76. BaseClass::RefreshComplete( hReq, response );
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose: opens context menu (user right clicked on a server)
  80. //-----------------------------------------------------------------------------
  81. void CFavoriteGames::OnOpenContextMenu(int itemID)
  82. {
  83. CServerContextMenu *menu = ServerBrowserDialog().GetContextMenu(GetActiveList());
  84. // get the server
  85. int serverID = GetSelectedServerID();
  86. if ( serverID != -1 )
  87. {
  88. // Activate context menu
  89. menu->ShowMenu(this, serverID, true, true, true, false);
  90. menu->AddMenuItem("RemoveServer", "#ServerBrowser_RemoveServerFromFavorites", new KeyValues("RemoveFromFavorites"), this);
  91. }
  92. else
  93. {
  94. // no selected rows, so don't display default stuff in menu
  95. menu->ShowMenu( this,(uint32)-1, false, false, false, false );
  96. }
  97. menu->AddMenuItem("AddServerByName", "#ServerBrowser_AddServerByIP", new KeyValues("AddServerByName"), this);
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose: removes a server from the favorites
  101. //-----------------------------------------------------------------------------
  102. void CFavoriteGames::OnRemoveFromFavorites()
  103. {
  104. if ( !steamapicontext->SteamMatchmakingServers() || !steamapicontext->SteamMatchmaking() )
  105. return;
  106. // iterate the selection
  107. for ( int iGame = 0; iGame < m_pGameList->GetSelectedItemsCount(); iGame++ )
  108. {
  109. int itemID = m_pGameList->GetSelectedItem( iGame );
  110. int serverID = m_pGameList->GetItemData(itemID)->userData;
  111. gameserveritem_t *pServer = steamapicontext->SteamMatchmakingServers()->GetServerDetails( m_hRequest, serverID );
  112. if ( pServer )
  113. {
  114. steamapicontext->SteamMatchmaking()->RemoveFavoriteGame( pServer->m_nAppID, pServer->m_NetAdr.GetIP(), pServer->m_NetAdr.GetConnectionPort(), pServer->m_NetAdr.GetQueryPort(), k_unFavoriteFlagFavorite );
  115. }
  116. }
  117. UpdateStatus();
  118. InvalidateLayout();
  119. Repaint();
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose: Adds a server by IP address
  123. //-----------------------------------------------------------------------------
  124. void CFavoriteGames::OnAddServerByName()
  125. {
  126. // open the add server dialog
  127. CDialogAddServer *dlg = new CDialogAddServer( &ServerBrowserDialog(), this );
  128. dlg->MoveToCenterOfScreen();
  129. dlg->DoModal();
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Purpose: Adds the currently connected server to the list
  133. //-----------------------------------------------------------------------------
  134. void CFavoriteGames::OnAddCurrentServer()
  135. {
  136. gameserveritem_t *pConnected = ServerBrowserDialog().GetCurrentConnectedServer();
  137. if ( pConnected && steamapicontext->SteamMatchmaking() )
  138. {
  139. steamapicontext->SteamMatchmaking()->AddFavoriteGame( pConnected->m_nAppID, pConnected->m_NetAdr.GetIP(), pConnected->m_NetAdr.GetConnectionPort(), pConnected->m_NetAdr.GetQueryPort(), k_unFavoriteFlagFavorite, time( NULL ) );
  140. m_bRefreshOnListReload = true;
  141. if ( GameSupportsReplay() )
  142. {
  143. // send command to propagate to the client so the client can send it on to the GC
  144. char command[ 256 ];
  145. Q_snprintf( command, Q_ARRAYSIZE( command ), "rfgc %s\n", pConnected->m_NetAdr.GetConnectionAddressString() );
  146. g_pRunGameEngine->AddTextCommand( command );
  147. }
  148. }
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Purpose: Parse posted messages
  152. //
  153. //-----------------------------------------------------------------------------
  154. void CFavoriteGames::OnCommand(const char *command)
  155. {
  156. if (!Q_stricmp(command, "AddServerByName"))
  157. {
  158. OnAddServerByName();
  159. }
  160. else if (!Q_stricmp(command, "AddCurrentServer" ))
  161. {
  162. OnAddCurrentServer();
  163. }
  164. else
  165. {
  166. BaseClass::OnCommand(command);
  167. }
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Purpose: enables adding server
  171. //-----------------------------------------------------------------------------
  172. void CFavoriteGames::OnConnectToGame()
  173. {
  174. m_pAddCurrentServer->SetEnabled( true );
  175. }
  176. //-----------------------------------------------------------------------------
  177. // Purpose: disables adding current server
  178. //-----------------------------------------------------------------------------
  179. void CFavoriteGames::OnDisconnectFromGame( void )
  180. {
  181. m_pAddCurrentServer->SetEnabled( false );
  182. }