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.

360 lines
9.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #include "cbase.h"
  10. #include <stdarg.h>
  11. #include "vguicenterprint.h"
  12. #include "ivrenderview.h"
  13. #include "vgui/IVGui.h"
  14. #include "VGuiMatSurface/IMatSystemSurface.h"
  15. #include "vgui_controls/Label.h"
  16. #include "vgui_controls/Controls.h"
  17. #include "vgui/ISurface.h"
  18. #include "vgui/IScheme.h"
  19. #include "vgui/IPanel.h"
  20. #if defined ( CSTRIKE15 )
  21. #include "Scaleform/HUD/sfhudinfopanel.h"
  22. #endif // CSTRIKE15
  23. // memdbgon must be the last include file in a .cpp file!!!
  24. #include "tier0/memdbgon.h"
  25. // [jason] Forward the message to the Scaleform info panel
  26. #if defined ( CSTRIKE15 )
  27. #define FORWARD_PRIORITY_MSG( x ) \
  28. { \
  29. CHudElement *pElement = GetHud().FindElement( "SFHudInfoPanel" ); \
  30. if ( pElement ) \
  31. { \
  32. ((SFHudInfoPanel *)pElement)->SetPriorityText( x ); \
  33. } \
  34. }
  35. #endif // CSTRIKE15
  36. #ifdef TF_CLIENT_DLL
  37. static ConVar scr_centertime( "scr_centertime", "5" );
  38. #else
  39. static ConVar scr_centertime( "scr_centertime", "4" );
  40. #endif
  41. //-----------------------------------------------------------------------------
  42. // Purpose: Implements Center String printing
  43. //-----------------------------------------------------------------------------
  44. class CCenterStringLabel : public vgui::Label
  45. {
  46. DECLARE_CLASS_SIMPLE( CCenterStringLabel, vgui::Label );
  47. public:
  48. explicit CCenterStringLabel( vgui::VPANEL parent );
  49. virtual ~CCenterStringLabel( void );
  50. // vgui::Panel
  51. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  52. virtual void OnTick( void );
  53. virtual bool ShouldDraw( void );
  54. // CVGuiCenterPrint
  55. virtual void SetTextColor( int r, int g, int b, int a );
  56. virtual void Print( char *text );
  57. virtual void Print( wchar_t *text );
  58. virtual void ColorPrint( int r, int g, int b, int a, char *text );
  59. virtual void ColorPrint( int r, int g, int b, int a, wchar_t *text );
  60. virtual void Clear( void );
  61. protected:
  62. MESSAGE_FUNC_INT_INT( OnScreenSizeChanged, "OnScreenSizeChanged", oldwide, oldtall );
  63. private:
  64. void ComputeSize( void );
  65. vgui::HFont m_hFont;
  66. float m_flCentertimeOff;
  67. };
  68. //-----------------------------------------------------------------------------
  69. // Purpose:
  70. // Input : *parent -
  71. //-----------------------------------------------------------------------------
  72. CCenterStringLabel::CCenterStringLabel( vgui::VPANEL parent ) :
  73. BaseClass( NULL, "CCenterStringLabel", " " )
  74. {
  75. SetParent( parent );
  76. ComputeSize();
  77. SetVisible( false );
  78. SetCursor( 0 );
  79. SetKeyBoardInputEnabled( false );
  80. SetMouseInputEnabled( false );
  81. SetContentAlignment( vgui::Label::a_center );
  82. SetScheme( "ClientScheme" );
  83. m_hFont = 0;
  84. SetFgColor( Color( 255, 255, 255, 255 ) );
  85. SetPaintBackgroundEnabled( false );
  86. m_flCentertimeOff = 0.0;
  87. vgui::ivgui()->AddTickSignal( GetVPanel(), 100 );
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose:
  91. //-----------------------------------------------------------------------------
  92. CCenterStringLabel::~CCenterStringLabel( void )
  93. {
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose: Updates panel to handle the new screen size
  97. //-----------------------------------------------------------------------------
  98. void CCenterStringLabel::OnScreenSizeChanged(int iOldWide, int iOldTall)
  99. {
  100. BaseClass::OnScreenSizeChanged(iOldWide, iOldTall);
  101. ComputeSize();
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose: Computes panel's desired size and position
  105. //-----------------------------------------------------------------------------
  106. void CCenterStringLabel::ComputeSize( void )
  107. {
  108. int w, h;
  109. if ( GetVParent() )
  110. {
  111. vgui::ipanel()->GetSize( GetVParent(), w, h );
  112. int iHeight = (int)(h * 0.3);
  113. SetSize( w, iHeight );
  114. SetPos( 0, ( h * 0.35 ) - ( iHeight / 2 ) );
  115. }
  116. }
  117. void CCenterStringLabel::ApplySchemeSettings(vgui::IScheme *pScheme)
  118. {
  119. BaseClass::ApplySchemeSettings(pScheme);
  120. // Use a large font
  121. m_hFont = pScheme->GetFont( "Default" );
  122. Assert( m_hFont );
  123. SetFont( m_hFont );
  124. ComputeSize();
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Purpose:
  128. // Input : r -
  129. // g -
  130. // b -
  131. // a -
  132. //-----------------------------------------------------------------------------
  133. void CCenterStringLabel::SetTextColor( int r, int g, int b, int a )
  134. {
  135. SetFgColor( Color( r, g, b, a ) );
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose:
  139. //-----------------------------------------------------------------------------
  140. void CCenterStringLabel::Print( char *text )
  141. {
  142. SetText( text );
  143. m_flCentertimeOff = scr_centertime.GetFloat() + gpGlobals->curtime;
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Purpose:
  147. //-----------------------------------------------------------------------------
  148. void CCenterStringLabel::Print( wchar_t *text )
  149. {
  150. SetText( text );
  151. m_flCentertimeOff = scr_centertime.GetFloat() + gpGlobals->curtime;
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose:
  155. //-----------------------------------------------------------------------------
  156. void CCenterStringLabel::ColorPrint( int r, int g, int b, int a, char *text )
  157. {
  158. SetTextColor( r, g, b, a );
  159. Print( text );
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Purpose:
  163. //-----------------------------------------------------------------------------
  164. void CCenterStringLabel::ColorPrint( int r, int g, int b, int a, wchar_t *text )
  165. {
  166. SetTextColor( r, g, b, a );
  167. Print( text );
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Purpose:
  171. //-----------------------------------------------------------------------------
  172. void CCenterStringLabel::Clear( void )
  173. {
  174. m_flCentertimeOff = 0;
  175. }
  176. //-----------------------------------------------------------------------------
  177. // Purpose:
  178. //-----------------------------------------------------------------------------
  179. void CCenterStringLabel::OnTick( void )
  180. {
  181. bool bVisible = ShouldDraw();
  182. if ( IsVisible() != bVisible )
  183. {
  184. SetVisible( bVisible );
  185. }
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose:
  189. // Output : Returns true on success, false on failure.
  190. // FIXME, this has dependencies on the engine that should go away
  191. //-----------------------------------------------------------------------------
  192. bool CCenterStringLabel::ShouldDraw( void )
  193. {
  194. // [jason] This element only exists to forward center print messages to the Scaleform InfoPanel
  195. #if defined ( CSTRIKE15 )
  196. return false;
  197. #endif
  198. if ( engine->IsDrawingLoadingImage() )
  199. {
  200. return false;
  201. }
  202. if ( m_flCentertimeOff <= gpGlobals->curtime )
  203. {
  204. // not time to turn off the message yet
  205. return false;
  206. }
  207. return true;
  208. }
  209. //-----------------------------------------------------------------------------
  210. // Purpose:
  211. // Output :
  212. //-----------------------------------------------------------------------------
  213. CCenterPrint::CCenterPrint( void )
  214. {
  215. vguiCenterString = NULL;
  216. }
  217. void CCenterPrint::SetTextColor( int r, int g, int b, int a )
  218. {
  219. if ( vguiCenterString )
  220. {
  221. vguiCenterString->SetTextColor( r, g, b, a );
  222. }
  223. }
  224. void CCenterPrint::Print( char *text )
  225. {
  226. // [jason] Forward the message to the Scaleform info panel
  227. #if defined ( CSTRIKE15 )
  228. FORWARD_PRIORITY_MSG( text );
  229. return;
  230. #endif
  231. if ( vguiCenterString )
  232. {
  233. vguiCenterString->ColorPrint( 255, 255, 255, 255, text );
  234. }
  235. }
  236. void CCenterPrint::Print( wchar_t *text )
  237. {
  238. // [jason] Forward the message to the Scaleform info panel
  239. #if defined ( CSTRIKE15 )
  240. FORWARD_PRIORITY_MSG( text );
  241. return;
  242. #endif
  243. if ( vguiCenterString )
  244. {
  245. vguiCenterString->ColorPrint( 255, 255, 255, 255, text );
  246. }
  247. }
  248. void CCenterPrint::ColorPrint( int r, int g, int b, int a, char *text )
  249. {
  250. // [jason] Forward the message to the Scaleform info panel
  251. #if defined ( CSTRIKE15 )
  252. FORWARD_PRIORITY_MSG( text );
  253. return;
  254. #endif
  255. if ( vguiCenterString )
  256. {
  257. vguiCenterString->ColorPrint( r, g, b, a, text );
  258. }
  259. }
  260. void CCenterPrint::ColorPrint( int r, int g, int b, int a, wchar_t *text )
  261. {
  262. // [jason] Forward the message to the Scaleform info panel
  263. #if defined ( CSTRIKE15 )
  264. FORWARD_PRIORITY_MSG( text );
  265. return;
  266. #endif
  267. if ( vguiCenterString )
  268. {
  269. vguiCenterString->ColorPrint( r, g, b, a, text );
  270. }
  271. }
  272. void CCenterPrint::Clear( void )
  273. {
  274. // [jason] Forward the message to the Scaleform info panel
  275. #if defined ( CSTRIKE15 )
  276. FORWARD_PRIORITY_MSG( static_cast<wchar_t*>(NULL) );
  277. return;
  278. #endif
  279. if ( vguiCenterString )
  280. {
  281. vguiCenterString->Clear();
  282. }
  283. }
  284. void CCenterPrint::Create( vgui::VPANEL parent )
  285. {
  286. if ( vguiCenterString )
  287. {
  288. Destroy();
  289. }
  290. vguiCenterString = new CCenterStringLabel( parent );
  291. }
  292. void CCenterPrint::Destroy( void )
  293. {
  294. if ( vguiCenterString )
  295. {
  296. vguiCenterString->SetParent( (vgui::Panel *)NULL );
  297. delete vguiCenterString;
  298. vguiCenterString = NULL;
  299. }
  300. }
  301. static CCenterPrint g_CenterString[ MAX_SPLITSCREEN_PLAYERS ];
  302. CCenterPrint *GetCenterPrint()
  303. {
  304. ASSERT_LOCAL_PLAYER_RESOLVABLE();
  305. return &g_CenterString[ GET_ACTIVE_SPLITSCREEN_SLOT() ];
  306. }