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.

213 lines
6.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Menu responsible for allowing players to join a game
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "menumanager.h"
  9. #include "basemenu.h"
  10. #include "vgui_controls/listpanel.h"
  11. #include "vgui_controls/textentry.h"
  12. #include "vgui_controls/button.h"
  13. #include "networkmessages.h"
  14. #include "networkmanager.h"
  15. #include "tier1/KeyValues.h"
  16. //-----------------------------------------------------------------------------
  17. // Constructor, destructor
  18. //-----------------------------------------------------------------------------
  19. class CJoinGameMenu : public CBaseMenu, public INetworkMessageListener
  20. {
  21. DECLARE_CLASS_SIMPLE( CJoinGameMenu, CBaseMenu );
  22. public:
  23. CJoinGameMenu( vgui::Panel *pParent, const char *pPanelName );
  24. virtual ~CJoinGameMenu();
  25. // Called when a particular network message occurs
  26. virtual void OnNetworkMessage( NetworkMessageRoute_t route, INetworkMessage *pNetworkMessage );
  27. virtual void OnCommand( const char *pCommand );
  28. MESSAGE_FUNC( OnTextNewLine, "TextNewLine" );
  29. private:
  30. vgui::ListPanel *m_pPlayerList;
  31. vgui::TextEntry *m_pChatLog;
  32. vgui::TextEntry *m_pServerName;
  33. vgui::TextEntry *m_pServerPort;
  34. vgui::TextEntry *m_pChatEntry;
  35. vgui::TextEntry *m_pPlayerName;
  36. vgui::Button *m_pJoinGame;
  37. bool m_bJoiningGame;
  38. };
  39. //-----------------------------------------------------------------------------
  40. // Hooks the menu into the menu manager
  41. //-----------------------------------------------------------------------------
  42. REGISTER_MENU( "JoinGameMenu", CJoinGameMenu );
  43. //-----------------------------------------------------------------------------
  44. // Sort by player name
  45. //-----------------------------------------------------------------------------
  46. static int __cdecl PlayerNameSortFunc( vgui::ListPanel *pPanel, const vgui::ListPanelItem &item1, const vgui::ListPanelItem &item2 )
  47. {
  48. const char *string1 = item1.kv->GetString("player");
  49. const char *string2 = item2.kv->GetString("player");
  50. return stricmp( string1, string2 );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Constructor, destructor
  54. //-----------------------------------------------------------------------------
  55. CJoinGameMenu::CJoinGameMenu( vgui::Panel *pParent, const char *pPanelName ) :
  56. BaseClass( pParent, pPanelName )
  57. {
  58. m_pPlayerList = new vgui::ListPanel( this, "PlayerList" );
  59. m_pPlayerList->AddColumnHeader( 0, "color", "Color", 52, 0 );
  60. m_pPlayerList->AddColumnHeader( 1, "player", "Player Name", 128, 0 );
  61. m_pPlayerList->SetSelectIndividualCells( false );
  62. m_pPlayerList->SetEmptyListText( "No Players" );
  63. m_pPlayerList->SetDragEnabled( false );
  64. m_pPlayerList->AddActionSignalTarget( this );
  65. m_pPlayerList->SetSortFunc( 0, PlayerNameSortFunc );
  66. m_pPlayerList->SetSortFunc( 1, PlayerNameSortFunc );
  67. m_pPlayerList->SetSortColumn( 1 );
  68. m_pServerName = new vgui::TextEntry( this, "ServerName" );
  69. m_pServerPort = new vgui::TextEntry( this, "ServerPort" );
  70. char pInitialPort[16];
  71. Q_snprintf( pInitialPort, sizeof(pInitialPort), "%d", NETWORKSYSTEM_DEFAULT_SERVER_PORT );
  72. m_pServerPort->SetText( pInitialPort );
  73. m_pPlayerName = new vgui::TextEntry( this, "PlayerName" );
  74. m_pPlayerName->SetMultiline( false );
  75. m_pChatLog = new vgui::TextEntry( this, "ChatLog" );
  76. m_pChatLog->SetMultiline( true );
  77. m_pChatLog->SetVerticalScrollbar( true );
  78. m_pChatEntry = new vgui::TextEntry( this, "ChatEntry" );
  79. m_pChatEntry->AddActionSignalTarget( this );
  80. m_pChatEntry->SetMultiline( false );
  81. m_pChatEntry->SendNewLine( true );
  82. m_pJoinGame = new vgui::Button( this, "JoinGame", "Join Game", this );
  83. LoadControlSettings( "resource/joingamemenu.res", "GAME" );
  84. m_pPlayerName->SetText( "Unnamed" );
  85. m_pChatEntry->SetEnabled( false );
  86. if ( !g_pNetworkManager->StartClient() )
  87. {
  88. m_pJoinGame->SetEnabled( false );
  89. return;
  90. }
  91. g_pNetworkManager->AddListener( NETWORK_MESSAGE_SERVER_TO_CLIENT, LEGION_NETMESSAGE_GROUP, CHAT_MESSAGE, this );
  92. }
  93. CJoinGameMenu::~CJoinGameMenu()
  94. {
  95. g_pNetworkManager->RemoveListener( NETWORK_MESSAGE_SERVER_TO_CLIENT, LEGION_NETMESSAGE_GROUP, CHAT_MESSAGE, this );
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Called when a particular network message occurs
  99. //-----------------------------------------------------------------------------
  100. void CJoinGameMenu::OnNetworkMessage( NetworkMessageRoute_t route, INetworkMessage *pNetworkMessage )
  101. {
  102. CNetworkMessage_Chat *pChatMsg = static_cast<CNetworkMessage_Chat*>( pNetworkMessage );
  103. m_pChatLog->InsertString( pChatMsg->m_Message.Get() );
  104. m_pChatLog->InsertChar( '\n' );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Called when the enter key is hit in the chat entry window
  108. //-----------------------------------------------------------------------------
  109. void CJoinGameMenu::OnTextNewLine()
  110. {
  111. CNetworkMessage_Chat msg;
  112. int nLen = m_pChatEntry->GetTextLength();
  113. if ( nLen > 0 )
  114. {
  115. char *pText = (char*)_alloca( (nLen+1) * sizeof(char) );
  116. m_pChatEntry->GetText( pText, nLen+1 );
  117. m_pChatEntry->SetText( "" );
  118. int nLenName = m_pPlayerName->GetTextLength();
  119. char *pName = (char*)_alloca( (nLenName+8) * sizeof(char) );
  120. if ( nLenName == 0 )
  121. {
  122. nLenName = 7;
  123. Q_strcpy( pName, "unnamed" );
  124. }
  125. else
  126. {
  127. m_pPlayerName->GetText( pName, nLenName+1 );
  128. }
  129. int nTotalLen = nLen + nLenName;
  130. msg.m_Message.SetLength( nTotalLen + 3 );
  131. Q_snprintf( msg.m_Message.Get(), nTotalLen+3, "[%s] %s", pName, pText );
  132. g_pNetworkManager->PostClientToServerMessage( &msg );
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Called when the enter key is hit in the chat entry window
  137. //-----------------------------------------------------------------------------
  138. void CJoinGameMenu::OnCommand( const char *pCommand )
  139. {
  140. if ( !Q_stricmp( pCommand, "Cancel" ) )
  141. {
  142. g_pNetworkManager->ShutdownClient();
  143. g_pMenuManager->PopMenu();
  144. return;
  145. }
  146. if ( !Q_stricmp( pCommand, "JoinGame" ) )
  147. {
  148. if ( !m_bJoiningGame )
  149. {
  150. g_pNetworkManager->DisconnectClientFromServer();
  151. m_pChatEntry->SetEnabled( false );
  152. m_pChatEntry->SetText( "" );
  153. m_bJoiningGame = true;
  154. m_pJoinGame->SetText( "Join Game" );
  155. }
  156. else
  157. {
  158. int nLen = m_pServerName->GetTextLength();
  159. char *pServer = (char*)_alloca( (nLen+1) * sizeof(char) );
  160. m_pServerName->GetText( pServer, nLen+1 );
  161. char pPort[32];
  162. m_pServerPort->GetText( pPort, sizeof(pPort) );
  163. if ( g_pNetworkManager->ConnectClientToServer( pServer, atoi( pPort ) ) )
  164. {
  165. m_pChatEntry->SetEnabled( true );
  166. m_bJoiningGame = false;
  167. m_pJoinGame->SetText( "Leave Game" );
  168. }
  169. }
  170. return;
  171. }
  172. BaseClass::OnCommand( pCommand );
  173. }