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.

250 lines
6.6 KiB

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