Counter Strike : Global Offensive Source Code
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.

210 lines
6.6 KiB

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