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.

439 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef HUD_BASECHAT_H
  8. #define HUD_BASECHAT_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "hudelement.h"
  13. #include <vgui_controls/Panel.h>
  14. #include "vgui_basepanel.h"
  15. #include "vgui_controls/Frame.h"
  16. #include <vgui_controls/TextEntry.h>
  17. #include <vgui_controls/RichText.h>
  18. #include <vgui_controls/Button.h>
  19. #include <vgui_controls/CheckButton.h>
  20. class CBaseHudChatInputLine;
  21. class CBaseHudChatEntry;
  22. class CHudChatFilterPanel;
  23. namespace vgui
  24. {
  25. class IScheme;
  26. };
  27. #define CHATLINE_NUM_FLASHES 8.0f
  28. #define CHATLINE_FLASH_TIME 5.0f
  29. #define CHATLINE_FADE_TIME 1.0f
  30. #define CHAT_HISTORY_FADE_TIME 0.25f
  31. #define CHAT_HISTORY_IDLE_TIME 15.0f
  32. #define CHAT_HISTORY_IDLE_FADE_TIME 2.5f
  33. #define CHAT_HISTORY_ALPHA 127
  34. extern Color g_ColorBlue;
  35. extern Color g_ColorRed;
  36. extern Color g_ColorGreen;
  37. extern Color g_ColorDarkGreen;
  38. extern Color g_ColorYellow;
  39. extern Color g_ColorGrey;
  40. extern ConVar cl_showtextmsg;
  41. enum ChatFilters
  42. {
  43. CHAT_FILTER_NONE = 0,
  44. CHAT_FILTER_JOINLEAVE = 0x000001,
  45. CHAT_FILTER_NAMECHANGE = 0x000002,
  46. CHAT_FILTER_PUBLICCHAT = 0x000004,
  47. CHAT_FILTER_SERVERMSG = 0x000008,
  48. CHAT_FILTER_TEAMCHANGE = 0x000010,
  49. //=============================================================================
  50. // HPE_BEGIN:
  51. // [tj]Added a filter for achievement announce
  52. //=============================================================================
  53. CHAT_FILTER_ACHIEVEMENT = 0x000020,
  54. //=============================================================================
  55. // HPE_END
  56. //=============================================================================
  57. };
  58. //-----------------------------------------------------------------------------
  59. enum TextColor
  60. {
  61. COLOR_NORMAL = 1,
  62. COLOR_USEOLDCOLORS = 2,
  63. COLOR_PLAYERNAME = 3,
  64. COLOR_LOCATION = 4,
  65. COLOR_ACHIEVEMENT = 5,
  66. COLOR_CUSTOM = 6, // Will use the most recently SetCustomColor()
  67. COLOR_HEXCODE = 7, // Reads the color from the next six characters
  68. COLOR_HEXCODE_ALPHA = 8,// Reads the color and alpha from the next eight characters
  69. COLOR_MAX
  70. };
  71. //--------------------------------------------------------------------------------------------------------------
  72. struct TextRange
  73. {
  74. TextRange() { preserveAlpha = false; }
  75. int start;
  76. int end;
  77. Color color;
  78. bool preserveAlpha;
  79. };
  80. void StripEndNewlineFromString( char *str );
  81. void StripEndNewlineFromString( wchar_t *str );
  82. char* ConvertCRtoNL( char *str );
  83. wchar_t* ConvertCRtoNL( wchar_t *str );
  84. wchar_t* ReadLocalizedString( bf_read &msg, OUT_Z_BYTECAP(outSizeInBytes) wchar_t *pOut, int outSizeInBytes, bool bStripNewline, OUT_Z_CAP(originalSize) char *originalString = NULL, int originalSize = 0 );
  85. wchar_t* ReadChatTextString( bf_read &msg, OUT_Z_BYTECAP(outSizeInBytes) wchar_t *pOut, int outSizeInBytes );
  86. char* RemoveColorMarkup( char *str );
  87. //--------------------------------------------------------------------------------------------------------
  88. /**
  89. * Simple utility function to allocate memory and duplicate a wide string
  90. */
  91. inline wchar_t *CloneWString( const wchar_t *str )
  92. {
  93. const int nLen = V_wcslen(str)+1;
  94. wchar_t *cloneStr = new wchar_t [ nLen ];
  95. const int nSize = nLen * sizeof( wchar_t );
  96. V_wcsncpy( cloneStr, str, nSize );
  97. return cloneStr;
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose: An output/display line of the chat interface
  101. //-----------------------------------------------------------------------------
  102. class CBaseHudChatLine : public vgui::RichText
  103. {
  104. typedef vgui::RichText BaseClass;
  105. public:
  106. CBaseHudChatLine( vgui::Panel *parent, const char *panelName );
  107. ~CBaseHudChatLine();
  108. void SetExpireTime( void );
  109. bool IsReadyToExpire( void );
  110. void Expire( void );
  111. float GetStartTime( void );
  112. int GetCount( void );
  113. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  114. vgui::HFont GetFont() { return m_hFont; }
  115. Color GetTextColor( void ) { return m_clrText; }
  116. void SetNameLength( int iLength ) { m_iNameLength = iLength; }
  117. void SetNameColor( Color cColor ){ m_clrNameColor = cColor; }
  118. virtual void PerformFadeout( void );
  119. virtual void InsertAndColorizeText( wchar_t *buf, int clientIndex );
  120. virtual void Colorize( int alpha = 255 ); ///< Re-inserts the text in the appropriate colors at the given alpha
  121. void SetNameStart( int iStart ) { m_iNameStart = iStart; }
  122. protected:
  123. int m_iNameLength;
  124. vgui::HFont m_hFont;
  125. Color m_clrText;
  126. Color m_clrNameColor;
  127. float m_flExpireTime;
  128. CUtlVector< TextRange > m_textRanges;
  129. wchar_t *m_text;
  130. int m_iNameStart;
  131. private:
  132. float m_flStartTime;
  133. int m_nCount;
  134. vgui::HFont m_hFontMarlett;
  135. private:
  136. CBaseHudChatLine( const CBaseHudChatLine & ); // not defined, not accessible
  137. };
  138. class CHudChatHistory : public vgui::RichText
  139. {
  140. DECLARE_CLASS_SIMPLE( CHudChatHistory, vgui::RichText );
  141. public:
  142. CHudChatHistory( vgui::Panel *pParent, const char *panelName );
  143. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  144. };
  145. class CHudChatFilterButton : public vgui::Button
  146. {
  147. DECLARE_CLASS_SIMPLE( CHudChatFilterButton, vgui::Button );
  148. public:
  149. CHudChatFilterButton( vgui::Panel *pParent, const char *pName, const char *pText );
  150. virtual void DoClick( void );
  151. };
  152. class CHudChatFilterCheckButton : public vgui::CheckButton
  153. {
  154. DECLARE_CLASS_SIMPLE( CHudChatFilterCheckButton, vgui::CheckButton );
  155. public:
  156. CHudChatFilterCheckButton( vgui::Panel *pParent, const char *pName, const char *pText, int iFlag );
  157. int GetFilterFlag( void ) { return m_iFlag; }
  158. private:
  159. int m_iFlag;
  160. };
  161. //-----------------------------------------------------------------------------
  162. // Purpose:
  163. //-----------------------------------------------------------------------------
  164. class CBaseHudChat : public CHudElement, public vgui::EditablePanel
  165. {
  166. DECLARE_CLASS_SIMPLE( CBaseHudChat, vgui::EditablePanel );
  167. public:
  168. DECLARE_MULTIPLY_INHERITED();
  169. enum
  170. {
  171. CHAT_INTERFACE_LINES = 6,
  172. MAX_CHARS_PER_LINE = 128
  173. };
  174. CBaseHudChat( const char *pElementName );
  175. virtual void CreateChatInputLine( void );
  176. virtual void CreateChatLines( void );
  177. virtual void Init( void );
  178. void LevelInit( const char *newmap );
  179. void LevelShutdown( void );
  180. void MsgFunc_TextMsg(const char *pszName, int iSize, void *pbuf);
  181. virtual void Printf( int iFilter, PRINTF_FORMAT_STRING const char *fmt, ... );
  182. virtual void ChatPrintf( int iPlayerIndex, int iFilter, PRINTF_FORMAT_STRING const char *fmt, ... ) FMTFUNCTION( 4, 5 );
  183. virtual void StartMessageMode( int iMessageModeType );
  184. virtual void StopMessageMode( void );
  185. void Send( void );
  186. MESSAGE_FUNC( OnChatEntrySend, "ChatEntrySend" );
  187. MESSAGE_FUNC( OnChatEntryStopMessageMode, "ChatEntryStopMessageMode" );
  188. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  189. virtual void Paint( void );
  190. virtual void OnTick( void );
  191. virtual void Reset();
  192. #ifdef _XBOX
  193. virtual bool ShouldDraw();
  194. #endif
  195. vgui::Panel *GetInputPanel( void );
  196. static int m_nLineCounter;
  197. virtual int GetChatInputOffset( void );
  198. // IGameEventListener interface:
  199. virtual void FireGameEvent( IGameEvent *event);
  200. CHudChatHistory *GetChatHistory();
  201. void FadeChatHistory();
  202. float m_flHistoryFadeTime;
  203. float m_flHistoryIdleTime;
  204. virtual void MsgFunc_SayText( bf_read &msg );
  205. virtual void MsgFunc_SayText2( bf_read &msg );
  206. virtual void MsgFunc_TextMsg( bf_read &msg );
  207. virtual void MsgFunc_VoiceSubtitle( bf_read &msg );
  208. CBaseHudChatInputLine *GetChatInput( void ) { return m_pChatInput; }
  209. CHudChatFilterPanel *GetChatFilterPanel( void );
  210. virtual int GetFilterFlags( void ) { return m_iFilterFlags; }
  211. void SetFilterFlag( int iFilter );
  212. //-----------------------------------------------------------------------------
  213. virtual Color GetDefaultTextColor( void );
  214. virtual Color GetTextColorForClient( TextColor colorNum, int clientIndex );
  215. virtual Color GetClientColor( int clientIndex );
  216. virtual int GetFilterForString( const char *pString );
  217. virtual const char *GetDisplayedSubtitlePlayerName( int clientIndex );
  218. bool IsVoiceSubtitle( void ) { return m_bEnteringVoice; }
  219. void SetVoiceSubtitleState( bool bState ) { m_bEnteringVoice = bState; }
  220. int GetMessageMode( void ) { return m_nMessageMode; }
  221. void SetCustomColor( Color colNew ) { m_ColorCustom = colNew; }
  222. void SetCustomColor( const char *pszColorName );
  223. protected:
  224. CBaseHudChatLine *FindUnusedChatLine( void );
  225. CBaseHudChatInputLine *m_pChatInput;
  226. CBaseHudChatLine *m_ChatLine;
  227. int m_iFontHeight;
  228. CHudChatHistory *m_pChatHistory;
  229. CHudChatFilterButton *m_pFiltersButton;
  230. CHudChatFilterPanel *m_pFilterPanel;
  231. Color m_ColorCustom;
  232. private:
  233. void Clear( void );
  234. int ComputeBreakChar( int width, const char *text, int textlen );
  235. int m_nMessageMode;
  236. int m_nVisibleHeight;
  237. vgui::HFont m_hChatFont;
  238. int m_iFilterFlags;
  239. bool m_bEnteringVoice;
  240. };
  241. class CBaseHudChatEntry : public vgui::TextEntry
  242. {
  243. typedef vgui::TextEntry BaseClass;
  244. public:
  245. CBaseHudChatEntry( vgui::Panel *parent, char const *panelName, vgui::Panel *pChat )
  246. : BaseClass( parent, panelName )
  247. {
  248. SetCatchEnterKey( true );
  249. SetAllowNonAsciiCharacters( true );
  250. SetDrawLanguageIDAtLeft( true );
  251. m_pHudChat = pChat;
  252. }
  253. virtual void ApplySchemeSettings( vgui::IScheme *pScheme )
  254. {
  255. BaseClass::ApplySchemeSettings(pScheme);
  256. SetPaintBorderEnabled( false );
  257. }
  258. virtual void OnKeyCodeTyped(vgui::KeyCode code)
  259. {
  260. if ( code == KEY_ENTER || code == KEY_PAD_ENTER || code == KEY_ESCAPE )
  261. {
  262. if ( code != KEY_ESCAPE )
  263. {
  264. if ( m_pHudChat )
  265. {
  266. PostMessage( m_pHudChat, new KeyValues("ChatEntrySend") );
  267. }
  268. }
  269. // End message mode.
  270. if ( m_pHudChat )
  271. {
  272. PostMessage( m_pHudChat, new KeyValues("ChatEntryStopMessageMode") );
  273. }
  274. }
  275. else if ( code == KEY_TAB )
  276. {
  277. // Ignore tab, otherwise vgui will screw up the focus.
  278. return;
  279. }
  280. else
  281. {
  282. BaseClass::OnKeyCodeTyped( code );
  283. }
  284. }
  285. private:
  286. vgui::Panel *m_pHudChat;
  287. };
  288. //-----------------------------------------------------------------------------
  289. // Purpose: The prompt and text entry area for chat messages
  290. //-----------------------------------------------------------------------------
  291. class CBaseHudChatInputLine : public vgui::Panel
  292. {
  293. typedef vgui::Panel BaseClass;
  294. public:
  295. CBaseHudChatInputLine( vgui::Panel *parent, char const *panelName );
  296. void SetPrompt( const wchar_t *prompt );
  297. void ClearEntry( void );
  298. void SetEntry( const wchar_t *entry );
  299. void GetMessageText( OUT_Z_BYTECAP(buffersizebytes) wchar_t *buffer, int buffersizebytes );
  300. virtual void PerformLayout();
  301. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  302. vgui::Panel *GetInputPanel( void );
  303. virtual vgui::VPANEL GetCurrentKeyFocus() { return m_pInput->GetVPanel(); }
  304. virtual void Paint()
  305. {
  306. BaseClass::Paint();
  307. }
  308. vgui::Label *GetPrompt( void ) { return m_pPrompt; }
  309. protected:
  310. vgui::Label *m_pPrompt;
  311. CBaseHudChatEntry *m_pInput;
  312. };
  313. class CHudChatFilterPanel : public vgui::EditablePanel
  314. {
  315. DECLARE_CLASS_SIMPLE( CHudChatFilterPanel, vgui::EditablePanel );
  316. public:
  317. CHudChatFilterPanel( vgui::Panel *pParent, const char *pName );
  318. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  319. MESSAGE_FUNC_PTR( OnFilterButtonChecked, "CheckButtonChecked", panel );
  320. CBaseHudChat *GetChatParent( void ) { return dynamic_cast < CBaseHudChat * > ( GetParent() ); }
  321. virtual void SetVisible(bool state);
  322. private:
  323. };
  324. #endif // HUD_BASECHAT_H