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.

248 lines
6.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "playerlistdialog.h"
  8. #include <vgui/ILocalize.h>
  9. #include <vgui/ISurface.h>
  10. #include <vgui_controls/ListPanel.h>
  11. #include <keyvalues.h>
  12. #include <vgui_controls/Label.h>
  13. #include <vgui_controls/Button.h>
  14. #include <vgui_controls/MessageBox.h>
  15. #include "engineinterface.h"
  16. #include "game/client/IGameClientExports.h"
  17. #include "gameui_interface.h"
  18. #include "steam/steam_api.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. using namespace vgui;
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Constructor
  24. //-----------------------------------------------------------------------------
  25. CPlayerListDialog::CPlayerListDialog(vgui::Panel *parent) : BaseClass(parent, "PlayerListDialog")
  26. {
  27. SetSize(320, 240);
  28. SetTitle("#GameUI_CurrentPlayers", true);
  29. m_pMuteButton = new Button(this, "MuteButton", "");
  30. m_pPlayerList = new ListPanel(this, "PlayerList");
  31. m_pPlayerList->AddColumnHeader(0, "Name", "#GameUI_PlayerName", 180);
  32. m_pPlayerList->AddColumnHeader(1, "Properties", "#GameUI_Properties", 80);
  33. m_pPlayerList->SetEmptyListText("#GameUI_NoOtherPlayersInGame");
  34. LoadControlSettings("Resource/PlayerListDialog.res");
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Destructor
  38. //-----------------------------------------------------------------------------
  39. CPlayerListDialog::~CPlayerListDialog()
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. void CPlayerListDialog::Activate()
  46. {
  47. BaseClass::Activate();
  48. // refresh player list
  49. m_pPlayerList->DeleteAllItems();
  50. int maxClients = engine->GetMaxClients();
  51. for (int i = 1; i <= maxClients; i++)
  52. {
  53. // get the player info from the engine
  54. player_info_t pi;
  55. if ( !engine->GetPlayerInfo(i, &pi) )
  56. continue;
  57. char szPlayerIndex[32];
  58. Q_snprintf(szPlayerIndex, sizeof( szPlayerIndex ), "%d", i);
  59. // collate user data then add it to the table
  60. KeyValues *data = new KeyValues(szPlayerIndex);
  61. data->SetString("Name", pi.name );
  62. data->SetInt("index", i);
  63. // add to the list
  64. m_pPlayerList->AddItem(data, 0, false, false);
  65. }
  66. // refresh player properties info
  67. RefreshPlayerProperties();
  68. // select the first item by default
  69. m_pPlayerList->SetSingleSelectedItem( m_pPlayerList->GetItemIDFromRow(0) );
  70. // toggle button states
  71. OnItemSelected();
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: walks the players and sets their info display in the list
  75. //-----------------------------------------------------------------------------
  76. void CPlayerListDialog::RefreshPlayerProperties()
  77. {
  78. for (int i = 0; i <= m_pPlayerList->GetItemCount(); i++)
  79. {
  80. KeyValues *data = m_pPlayerList->GetItem(i);
  81. if (!data)
  82. continue;
  83. // assemble properties
  84. int playerIndex = data->GetInt("index");
  85. player_info_t pi;
  86. if ( !engine->GetPlayerInfo( playerIndex, &pi) )
  87. {
  88. // disconnected
  89. data->SetString("properties", "Disconnected");
  90. continue;
  91. }
  92. data->SetString( "name", pi.name );
  93. bool muted = false, friends = false, bot = false;
  94. if ( GameClientExports() && GameClientExports()->IsPlayerGameVoiceMuted(playerIndex) )
  95. {
  96. muted = true;
  97. }
  98. if ( pi.fakeplayer )
  99. {
  100. bot = true;
  101. }
  102. if (bot)
  103. {
  104. data->SetString("properties", "CPU Player");
  105. }
  106. else if (muted && friends)
  107. {
  108. data->SetString("properties", "Friend; Muted");
  109. }
  110. else if (muted)
  111. {
  112. data->SetString("properties", "Muted");
  113. }
  114. else if (friends)
  115. {
  116. data->SetString("properties", "Friend");
  117. }
  118. else
  119. {
  120. data->SetString("properties", "");
  121. }
  122. }
  123. m_pPlayerList->RereadAllItems();
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Purpose: Handles the AddFriend command
  127. //-----------------------------------------------------------------------------
  128. void CPlayerListDialog::OnCommand(const char *command)
  129. {
  130. if (!stricmp(command, "Mute"))
  131. {
  132. ToggleMuteStateOfSelectedUser();
  133. }
  134. else
  135. {
  136. BaseClass::OnCommand(command);
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose: toggles whether a user is muted or not
  141. //-----------------------------------------------------------------------------
  142. void CPlayerListDialog::ToggleMuteStateOfSelectedUser()
  143. {
  144. if (!GameClientExports())
  145. return;
  146. for ( int iSelectedItem = 0; iSelectedItem < m_pPlayerList->GetSelectedItemsCount(); iSelectedItem++ )
  147. {
  148. KeyValues *data = m_pPlayerList->GetItem( m_pPlayerList->GetSelectedItem( iSelectedItem ) );
  149. if (!data)
  150. return;
  151. int playerIndex = data->GetInt("index");
  152. Assert(playerIndex);
  153. if (GameClientExports()->IsPlayerGameVoiceMuted(playerIndex))
  154. {
  155. GameClientExports()->UnmutePlayerGameVoice(playerIndex);
  156. }
  157. else
  158. {
  159. GameClientExports()->MutePlayerGameVoice(playerIndex);
  160. }
  161. }
  162. RefreshPlayerProperties();
  163. OnItemSelected();
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose:
  167. //-----------------------------------------------------------------------------
  168. void CPlayerListDialog::OnItemSelected()
  169. {
  170. // make sure the data is up-to-date
  171. RefreshPlayerProperties();
  172. // set the button state based on the selected item
  173. bool bMuteButtonEnabled = false;
  174. if (m_pPlayerList->GetSelectedItemsCount() > 0)
  175. {
  176. KeyValues *data = m_pPlayerList->GetItem(m_pPlayerList->GetSelectedItem(0));
  177. player_info_t pi;
  178. int iLocalPlayer = engine->GetLocalPlayer();
  179. int iPlayerIndex = data->GetInt("index");
  180. bool isValidPlayer = engine->GetPlayerInfo( iPlayerIndex, &pi );
  181. // make sure the player is not a bot, or the user
  182. // Matt - changed this check to see if player indeces match, instead of using friends ID
  183. if ( pi.fakeplayer || iPlayerIndex == iLocalPlayer ) // || pi.friendsID == g_pFriendsUser->GetFriendsID() )
  184. {
  185. // invalid player,
  186. isValidPlayer = false;
  187. }
  188. if (data && isValidPlayer && GameClientExports() && GameClientExports()->IsPlayerGameVoiceMuted(data->GetInt("index")))
  189. {
  190. m_pMuteButton->SetText("#GameUI_UnmuteIngameVoice");
  191. }
  192. else
  193. {
  194. m_pMuteButton->SetText("#GameUI_MuteIngameVoice");
  195. }
  196. if (GameClientExports() && isValidPlayer)
  197. {
  198. bMuteButtonEnabled = true;
  199. }
  200. }
  201. else
  202. {
  203. m_pMuteButton->SetText("#GameUI_MuteIngameVoice");
  204. }
  205. m_pMuteButton->SetEnabled( bMuteButtonEnabled );
  206. }