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.

434 lines
11 KiB

  1. //========= Copyright 1996-2005, 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_ONE_OVER_FADE_TIME 4.0f;
  31. #define CHAT_HISTORY_FADE_TIME 0.25f
  32. #define CHAT_HISTORY_IDLE_TIME 15.0f
  33. #define CHAT_HISTORY_IDLE_FADE_TIME 2.5f
  34. #define CHAT_HISTORY_ALPHA 127
  35. extern Color g_ColorBlue;
  36. extern Color g_ColorRed;
  37. extern Color g_ColorGreen;
  38. extern Color g_ColorDarkGreen;
  39. extern Color g_ColorYellow;
  40. extern Color g_ColorGrey;
  41. extern ConVar cl_showtextmsg;
  42. enum ChatFilters
  43. {
  44. CHAT_FILTER_NONE = 0,
  45. CHAT_FILTER_JOINLEAVE = 0x000001,
  46. CHAT_FILTER_NAMECHANGE = 0x000002,
  47. CHAT_FILTER_PUBLICCHAT = 0x000004,
  48. CHAT_FILTER_SERVERMSG = 0x000008,
  49. CHAT_FILTER_TEAMCHANGE = 0x000010,
  50. CHAT_FILTER_ACHIEVEMENT = 0x000020,
  51. };
  52. //-----------------------------------------------------------------------------
  53. enum TextColor
  54. {
  55. COLOR_NORMAL = 1,
  56. COLOR_USEOLDCOLORS = 2,
  57. COLOR_PLAYERNAME = 3,
  58. COLOR_LOCATION = 4,
  59. COLOR_ACHIEVEMENT = 5,
  60. COLOR_AWARD = 6,
  61. COLOR_PENALTY = 7,
  62. COLOR_SILVER = 8,
  63. COLOR_GOLD = 9,
  64. COLOR_RARITY_FIRST = 10,
  65. COLOR_COMMON = COLOR_RARITY_FIRST,
  66. COLOR_UNCOMMON = 11,
  67. COLOR_RARE = 12,
  68. COLOR_MYTHICAL = 13,
  69. COLOR_LEGENDARY = 14,
  70. COLOR_ANCIENT = 15,
  71. COLOR_IMMORTAL = 16,
  72. COLOR_RARITY_LAST = COLOR_IMMORTAL,
  73. COLOR_MAX
  74. };
  75. //--------------------------------------------------------------------------------------------------------------
  76. struct TextRange
  77. {
  78. int start;
  79. int end;
  80. Color color;
  81. };
  82. void StripEndNewlineFromString( char *str );
  83. void StripEndNewlineFromString( wchar_t *str );
  84. char* ConvertCRtoNL( char *str );
  85. wchar_t* ConvertCRtoNL( wchar_t *str );
  86. wchar_t* ReadLocalizedString( const char *szString, OUT_Z_BYTECAP(outSizeInBytes) wchar_t *pOut, int outSizeInBytes, bool bStripNewline, char *originalString = NULL, int originalSize = 0 );
  87. wchar_t* ReadChatTextString( const char *szString, OUT_Z_BYTECAP(outSizeInBytes) wchar_t *pOut, int outSizeInBytes, bool stripBugData = false );
  88. char* RemoveColorMarkup( char *str );
  89. //--------------------------------------------------------------------------------------------------------
  90. /**
  91. * Simple utility function to allocate memory and duplicate a wide string
  92. */
  93. inline wchar_t *CloneWString( const wchar_t *str )
  94. {
  95. wchar_t *cloneStr = new wchar_t [ wcslen(str)+1 ];
  96. wcscpy( cloneStr, str );
  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. virtual void ApplySettings( KeyValues *inResourceData );
  145. virtual void Paint();
  146. };
  147. class CHudChatFilterButton : public vgui::Button
  148. {
  149. DECLARE_CLASS_SIMPLE( CHudChatFilterButton, vgui::Button );
  150. public:
  151. CHudChatFilterButton( vgui::Panel *pParent, const char *pName, const char *pText );
  152. virtual void DoClick( void );
  153. };
  154. class CHudChatFilterCheckButton : public vgui::CheckButton
  155. {
  156. DECLARE_CLASS_SIMPLE( CHudChatFilterCheckButton, vgui::CheckButton );
  157. public:
  158. CHudChatFilterCheckButton( vgui::Panel *pParent, const char *pName, const char *pText, int iFlag );
  159. int GetFilterFlag( void ) { return m_iFlag; }
  160. private:
  161. int m_iFlag;
  162. };
  163. //-----------------------------------------------------------------------------
  164. // Purpose:
  165. //-----------------------------------------------------------------------------
  166. class CBaseHudChat : public CHudElement, public vgui::EditablePanel
  167. {
  168. DECLARE_CLASS_SIMPLE( CBaseHudChat, vgui::EditablePanel );
  169. public:
  170. DECLARE_MULTIPLY_INHERITED();
  171. enum
  172. {
  173. CHAT_INTERFACE_LINES = 6,
  174. MAX_CHARS_PER_LINE = 128
  175. };
  176. explicit CBaseHudChat( const char *pElementName );
  177. ~CBaseHudChat();
  178. static CBaseHudChat *GetHudChat( void );
  179. virtual void CreateChatInputLine( void );
  180. virtual void CreateChatLines( void );
  181. virtual void Init( void );
  182. void LevelInit( const char *newmap );
  183. void LevelShutdown( void );
  184. void MsgFunc_TextMsg(const char *pszName, int iSize, void *pbuf);
  185. virtual void Printf( int iFilter, PRINTF_FORMAT_STRING const char *fmt, ... );
  186. virtual void ChatPrintf( int iPlayerIndex, int iFilter, PRINTF_FORMAT_STRING const char *fmt, ... ) FMTFUNCTION( 4, 5 );
  187. virtual void ChatPrintfW( int iPlayerIndex, int iFilter, const wchar_t *wszNotice );
  188. virtual void StartMessageMode( int iMessageModeType );
  189. virtual void StopMessageMode( bool bFade = true );
  190. void Send( void );
  191. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  192. virtual void Paint( void );
  193. virtual void OnTick( void );
  194. virtual void Reset();
  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 bool MsgFunc_SayText( const CCSUsrMsg_SayText &msg );
  205. virtual bool MsgFunc_SayText2( const CCSUsrMsg_SayText2 &msg );
  206. virtual bool MsgFunc_TextMsg( const CCSUsrMsg_TextMsg &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. virtual void SetChatPrompt( int iMessageModeType );
  213. //-----------------------------------------------------------------------------
  214. virtual Color GetDefaultTextColor( void );
  215. virtual Color GetTextColorForClient( TextColor colorNum, int clientIndex );
  216. virtual Color GetClientColor( int clientIndex );
  217. virtual int GetFilterForString( const char *pString );
  218. virtual const char *GetDisplayedSubtitlePlayerName( int clientIndex );
  219. bool IsVoiceSubtitle( void ) { return m_bEnteringVoice; }
  220. void SetVoiceSubtitleState( bool bState ) { m_bEnteringVoice = bState; }
  221. protected:
  222. CBaseHudChatLine *FindUnusedChatLine( void );
  223. CBaseHudChatInputLine *m_pChatInput;
  224. CBaseHudChatLine *m_ChatLine;
  225. int m_iFontHeight;
  226. CHudChatHistory *m_pChatHistory;
  227. CHudChatFilterButton *m_pFiltersButton;
  228. CHudChatFilterPanel *m_pFilterPanel;
  229. private:
  230. void Clear( void );
  231. int ComputeBreakChar( int width, const char *text, int textlen );
  232. int m_nMessageMode;
  233. vgui::HFont m_hChatFont;
  234. int m_iFilterFlags;
  235. bool m_bEnteringVoice;
  236. };
  237. class CBaseHudChatEntry : public vgui::TextEntry
  238. {
  239. typedef vgui::TextEntry BaseClass;
  240. public:
  241. CBaseHudChatEntry( vgui::Panel *parent, char const *panelName, CBaseHudChat *pChat )
  242. : BaseClass( parent, panelName )
  243. {
  244. SetCatchEnterKey( true );
  245. SetAllowNonAsciiCharacters( true );
  246. SetDrawLanguageIDAtLeft( true );
  247. m_pHudChat = pChat;
  248. }
  249. virtual void ApplySchemeSettings( vgui::IScheme *pScheme )
  250. {
  251. BaseClass::ApplySchemeSettings(pScheme);
  252. SetPaintBorderEnabled( false );
  253. }
  254. virtual void OnKeyCodeTyped(vgui::KeyCode code)
  255. {
  256. if ( code == KEY_ENTER || code == KEY_PAD_ENTER || code == KEY_ESCAPE )
  257. {
  258. if ( code != KEY_ESCAPE )
  259. {
  260. if ( m_pHudChat )
  261. {
  262. m_pHudChat->Send();
  263. }
  264. }
  265. // End message mode.
  266. if ( m_pHudChat )
  267. {
  268. m_pHudChat->StopMessageMode();
  269. }
  270. }
  271. else if ( code == KEY_TAB )
  272. {
  273. // Ignore tab, otherwise vgui will screw up the focus.
  274. return;
  275. }
  276. else
  277. {
  278. BaseClass::OnKeyCodeTyped( code );
  279. }
  280. }
  281. private:
  282. CBaseHudChat *m_pHudChat;
  283. };
  284. //-----------------------------------------------------------------------------
  285. // Purpose: The prompt and text entry area for chat messages
  286. //-----------------------------------------------------------------------------
  287. class CBaseHudChatInputLine : public vgui::Panel
  288. {
  289. typedef vgui::Panel BaseClass;
  290. public:
  291. CBaseHudChatInputLine( CBaseHudChat *parent, char const *panelName );
  292. void SetPrompt( const wchar_t *prompt );
  293. void ClearEntry( void );
  294. void SetEntry( const wchar_t *entry );
  295. void GetMessageText( OUT_Z_BYTECAP(buffersizebytes) wchar_t *buffer, int buffersizebytes );
  296. virtual void PerformLayout();
  297. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  298. vgui::Panel *GetInputPanel( void );
  299. virtual vgui::VPANEL GetCurrentKeyFocus() { return m_pInput->GetVPanel(); }
  300. virtual void Paint()
  301. {
  302. BaseClass::Paint();
  303. }
  304. vgui::Label *GetPrompt( void ) { return m_pPrompt; }
  305. protected:
  306. vgui::Label *m_pPrompt;
  307. CBaseHudChatEntry *m_pInput;
  308. };
  309. class CHudChatFilterPanel : public vgui::EditablePanel
  310. {
  311. DECLARE_CLASS_SIMPLE( CHudChatFilterPanel, vgui::EditablePanel );
  312. public:
  313. CHudChatFilterPanel( vgui::Panel *pParent, const char *pName );
  314. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  315. MESSAGE_FUNC_PTR( OnFilterButtonChecked, "CheckButtonChecked", panel );
  316. CBaseHudChat *GetChatParent( void ) { return dynamic_cast < CBaseHudChat * > ( GetParent() ); }
  317. virtual void SetVisible(bool state);
  318. private:
  319. };
  320. #endif // HUD_BASECHAT_H