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.

171 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud_chat.h"
  8. #include "hud_macros.h"
  9. #include "text_message.h"
  10. #include "vguicenterprint.h"
  11. #include "hud_basechat.h"
  12. #include <vgui/ILocalize.h>
  13. DECLARE_HUDELEMENT( CHudChat );
  14. DECLARE_HUD_MESSAGE( CHudChat, SayText );
  15. DECLARE_HUD_MESSAGE( CHudChat, SayText2 );
  16. DECLARE_HUD_MESSAGE( CHudChat, TextMsg );
  17. //=====================
  18. //CHudChat
  19. //=====================
  20. CHudChat::CHudChat( const char *pElementName ) : BaseClass( pElementName )
  21. {
  22. }
  23. void CHudChat::Init( void )
  24. {
  25. BaseClass::Init();
  26. HOOK_HUD_MESSAGE( CHudChat, SayText );
  27. HOOK_HUD_MESSAGE( CHudChat, SayText2 );
  28. HOOK_HUD_MESSAGE( CHudChat, TextMsg );
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Purpose: Reads in a player's Chat text from the server
  32. //-----------------------------------------------------------------------------
  33. void CHudChat::MsgFunc_SayText2( bf_read &msg )
  34. {
  35. int client = msg.ReadByte();
  36. bool bWantsToChat = msg.ReadByte();
  37. wchar_t szBuf[6][256];
  38. char untranslated_msg_text[256];
  39. wchar_t *msg_text = ReadLocalizedString( msg, szBuf[0], sizeof( szBuf[0] ), false, untranslated_msg_text, sizeof( untranslated_msg_text ) );
  40. // keep reading strings and using C format strings for subsituting the strings into the localised text string
  41. ReadChatTextString ( msg, szBuf[1], sizeof( szBuf[1] ) ); // player name
  42. ReadChatTextString ( msg, szBuf[2], sizeof( szBuf[2] ) ); // chat text
  43. ReadLocalizedString( msg, szBuf[3], sizeof( szBuf[3] ), true );
  44. ReadLocalizedString( msg, szBuf[4], sizeof( szBuf[4] ), true );
  45. g_pVGuiLocalize->ConstructString( szBuf[5], sizeof( szBuf[5] ), msg_text, 4, szBuf[1], szBuf[2], szBuf[3], szBuf[4] );
  46. char ansiString[512];
  47. g_pVGuiLocalize->ConvertUnicodeToANSI( ConvertCRtoNL( szBuf[5] ), ansiString, sizeof( ansiString ) );
  48. if ( bWantsToChat )
  49. {
  50. // print raw chat text
  51. ChatPrintf( client, CHAT_FILTER_NONE, "%s", ansiString );
  52. Msg( "%s\n", RemoveColorMarkup(ansiString) );
  53. }
  54. else
  55. {
  56. // print raw chat text
  57. ChatPrintf( client, CHAT_FILTER_NONE, "%s", ansiString );
  58. }
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose:
  62. // Input : *pszName -
  63. // iSize -
  64. // *pbuf -
  65. //-----------------------------------------------------------------------------
  66. void CHudChat::MsgFunc_SayText( bf_read &msg )
  67. {
  68. char szString[256];
  69. msg.ReadByte(); // client ID
  70. msg.ReadString( szString, sizeof(szString) );
  71. Printf( CHAT_FILTER_NONE, "%s", szString );
  72. }
  73. // Message handler for text messages
  74. // displays a string, looking them up from the titles.txt file, which can be localised
  75. // parameters:
  76. // byte: message direction ( HUD_PRINTCONSOLE, HUD_PRINTNOTIFY, HUD_PRINTCENTER, HUD_PRINTTALK )
  77. // string: message
  78. // optional parameters:
  79. // string: message parameter 1
  80. // string: message parameter 2
  81. // string: message parameter 3
  82. // string: message parameter 4
  83. // any string that starts with the character '#' is a message name, and is used to look up the real message in titles.txt
  84. // the next (optional) one to four strings are parameters for that string (which can also be message names if they begin with '#')
  85. void CHudChat::MsgFunc_TextMsg( bf_read &msg )
  86. {
  87. char szString[2048];
  88. int msg_dest = msg.ReadByte();
  89. static char szBuf[6][256];
  90. msg.ReadString( szString, sizeof(szString) );
  91. char *msg_text = hudtextmessage->LookupString( szString, &msg_dest );
  92. Q_strncpy( szBuf[0], msg_text, sizeof( szBuf[0] ) );
  93. msg_text = szBuf[0];
  94. // keep reading strings and using C format strings for subsituting the strings into the localised text string
  95. msg.ReadString( szString, sizeof(szString) );
  96. char *sstr1 = hudtextmessage->LookupString( szString );
  97. Q_strncpy( szBuf[1], sstr1, sizeof( szBuf[1] ) );
  98. sstr1 = szBuf[1];
  99. StripEndNewlineFromString( sstr1 ); // these strings are meant for subsitution into the main strings, so cull the automatic end newlines
  100. msg.ReadString( szString, sizeof(szString) );
  101. char *sstr2 = hudtextmessage->LookupString( szString );
  102. Q_strncpy( szBuf[2], sstr2, sizeof( szBuf[2] ) );
  103. sstr2 = szBuf[2];
  104. StripEndNewlineFromString( sstr2 );
  105. msg.ReadString( szString, sizeof(szString) );
  106. char *sstr3 = hudtextmessage->LookupString( szString );
  107. Q_strncpy( szBuf[3], sstr3, sizeof( szBuf[3] ) );
  108. sstr3 = szBuf[3];
  109. StripEndNewlineFromString( sstr3 );
  110. msg.ReadString( szString, sizeof(szString) );
  111. char *sstr4 = hudtextmessage->LookupString( szString );
  112. Q_strncpy( szBuf[4], sstr4, sizeof( szBuf[4] ) );
  113. sstr4 = szBuf[4];
  114. StripEndNewlineFromString( sstr4 );
  115. char *psz = szBuf[5];
  116. if ( !cl_showtextmsg.GetInt() )
  117. return;
  118. switch ( msg_dest )
  119. {
  120. case HUD_PRINTCENTER:
  121. Q_snprintf( psz, sizeof( szBuf[5] ), msg_text, sstr1, sstr2, sstr3, sstr4 );
  122. internalCenterPrint->Print( ConvertCRtoNL( psz ) );
  123. break;
  124. case HUD_PRINTNOTIFY:
  125. psz[0] = 1; // mark this message to go into the notify buffer
  126. Q_snprintf( psz+1, sizeof( szBuf[5] ) - 1, msg_text, sstr1, sstr2, sstr3, sstr4 );
  127. Msg( "%s", ConvertCRtoNL( psz ) );
  128. break;
  129. case HUD_PRINTTALK:
  130. Q_snprintf( psz, sizeof( szBuf[5] ), msg_text, sstr1, sstr2, sstr3, sstr4 );
  131. Printf( CHAT_FILTER_NONE, "%s", ConvertCRtoNL( psz ) );
  132. break;
  133. case HUD_PRINTCONSOLE:
  134. Q_snprintf( psz, sizeof( szBuf[5] ), msg_text, sstr1, sstr2, sstr3, sstr4 );
  135. Msg( "%s", ConvertCRtoNL( psz ) );
  136. break;
  137. }
  138. }