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.

309 lines
8.0 KiB

  1. //========= Copyright 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. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. #ifdef TF_CLIENT_DLL
  22. static ConVar scr_centertime( "scr_centertime", "5" );
  23. #else
  24. static ConVar scr_centertime( "scr_centertime", "2" );
  25. #endif
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Implements Center String printing
  28. //-----------------------------------------------------------------------------
  29. class CCenterStringLabel : public vgui::Label
  30. {
  31. DECLARE_CLASS_SIMPLE( CCenterStringLabel, vgui::Label );
  32. public:
  33. CCenterStringLabel( vgui::VPANEL parent );
  34. virtual ~CCenterStringLabel( void );
  35. // vgui::Panel
  36. virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
  37. virtual void OnTick( void );
  38. virtual bool ShouldDraw( void );
  39. // CVGuiCenterPrint
  40. virtual void SetTextColor( int r, int g, int b, int a );
  41. virtual void Print( char *text );
  42. virtual void Print( wchar_t *text );
  43. virtual void ColorPrint( int r, int g, int b, int a, char *text );
  44. virtual void ColorPrint( int r, int g, int b, int a, wchar_t *text );
  45. virtual void Clear( void );
  46. protected:
  47. MESSAGE_FUNC_INT_INT( OnScreenSizeChanged, "OnScreenSizeChanged", oldwide, oldtall );
  48. private:
  49. void ComputeSize( void );
  50. vgui::HFont m_hFont;
  51. float m_flCentertimeOff;
  52. };
  53. //-----------------------------------------------------------------------------
  54. // Purpose:
  55. // Input : *parent -
  56. //-----------------------------------------------------------------------------
  57. CCenterStringLabel::CCenterStringLabel( vgui::VPANEL parent ) :
  58. BaseClass( NULL, "CCenterStringLabel", " " )
  59. {
  60. SetParent( parent );
  61. ComputeSize();
  62. SetVisible( false );
  63. SetCursor( null );
  64. SetKeyBoardInputEnabled( false );
  65. SetMouseInputEnabled( false );
  66. SetContentAlignment( vgui::Label::a_center );
  67. m_hFont = 0;
  68. SetFgColor( Color( 255, 255, 255, 255 ) );
  69. SetPaintBackgroundEnabled( false );
  70. m_flCentertimeOff = 0.0;
  71. vgui::ivgui()->AddTickSignal( GetVPanel(), 100 );
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose:
  75. //-----------------------------------------------------------------------------
  76. CCenterStringLabel::~CCenterStringLabel( void )
  77. {
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose: Updates panel to handle the new screen size
  81. //-----------------------------------------------------------------------------
  82. void CCenterStringLabel::OnScreenSizeChanged(int iOldWide, int iOldTall)
  83. {
  84. BaseClass::OnScreenSizeChanged(iOldWide, iOldTall);
  85. ComputeSize();
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose: Computes panel's desired size and position
  89. //-----------------------------------------------------------------------------
  90. void CCenterStringLabel::ComputeSize( void )
  91. {
  92. int w, h;
  93. w = ScreenWidth();
  94. h = ScreenHeight();
  95. int iHeight = (int)(h * 0.3);
  96. SetSize( w, iHeight );
  97. SetPos( 0, ( h * 0.35 ) - ( iHeight / 2 ) );
  98. }
  99. void CCenterStringLabel::ApplySchemeSettings(vgui::IScheme *pScheme)
  100. {
  101. BaseClass::ApplySchemeSettings(pScheme);
  102. // Use a large font
  103. m_hFont = pScheme->GetFont( "Trebuchet24" );
  104. assert( m_hFont );
  105. SetFont( m_hFont );
  106. int w, h;
  107. w = ScreenWidth();
  108. h = ScreenHeight();
  109. int iHeight = (int)(h * 0.3);
  110. SetSize( w, iHeight );
  111. SetPos( 0, ( h * 0.35 ) - ( iHeight / 2 ) );
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Purpose:
  115. // Input : r -
  116. // g -
  117. // b -
  118. // a -
  119. //-----------------------------------------------------------------------------
  120. void CCenterStringLabel::SetTextColor( int r, int g, int b, int a )
  121. {
  122. SetFgColor( Color( r, g, b, a ) );
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose:
  126. //-----------------------------------------------------------------------------
  127. void CCenterStringLabel::Print( char *text )
  128. {
  129. SetText( text );
  130. m_flCentertimeOff = scr_centertime.GetFloat() + gpGlobals->curtime;
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose:
  134. //-----------------------------------------------------------------------------
  135. void CCenterStringLabel::Print( wchar_t *text )
  136. {
  137. SetText( text );
  138. m_flCentertimeOff = scr_centertime.GetFloat() + gpGlobals->curtime;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. void CCenterStringLabel::ColorPrint( int r, int g, int b, int a, char *text )
  144. {
  145. SetTextColor( r, g, b, a );
  146. Print( text );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose:
  150. //-----------------------------------------------------------------------------
  151. void CCenterStringLabel::ColorPrint( int r, int g, int b, int a, wchar_t *text )
  152. {
  153. SetTextColor( r, g, b, a );
  154. Print( text );
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose:
  158. //-----------------------------------------------------------------------------
  159. void CCenterStringLabel::Clear( void )
  160. {
  161. m_flCentertimeOff = 0;
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Purpose:
  165. //-----------------------------------------------------------------------------
  166. void CCenterStringLabel::OnTick( void )
  167. {
  168. bool bVisible = ShouldDraw();
  169. if ( IsVisible() != bVisible )
  170. {
  171. SetVisible( bVisible );
  172. }
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose:
  176. // Output : Returns true on success, false on failure.
  177. // FIXME, this has dependencies on the engine that should go away
  178. //-----------------------------------------------------------------------------
  179. bool CCenterStringLabel::ShouldDraw( void )
  180. {
  181. if ( engine->IsDrawingLoadingImage() )
  182. {
  183. return false;
  184. }
  185. if ( m_flCentertimeOff <= gpGlobals->curtime )
  186. {
  187. // not time to turn off the message yet
  188. return false;
  189. }
  190. return true;
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Purpose:
  194. // Output :
  195. //-----------------------------------------------------------------------------
  196. CCenterPrint::CCenterPrint( void )
  197. {
  198. vguiCenterString = NULL;
  199. }
  200. void CCenterPrint::SetTextColor( int r, int g, int b, int a )
  201. {
  202. if ( vguiCenterString )
  203. {
  204. vguiCenterString->SetTextColor( r, g, b, a );
  205. }
  206. }
  207. void CCenterPrint::Print( char *text )
  208. {
  209. if ( vguiCenterString )
  210. {
  211. vguiCenterString->ColorPrint( 255, 255, 255, 255, text );
  212. }
  213. }
  214. void CCenterPrint::Print( wchar_t *text )
  215. {
  216. if ( vguiCenterString )
  217. {
  218. vguiCenterString->ColorPrint( 255, 255, 255, 255, text );
  219. }
  220. }
  221. void CCenterPrint::ColorPrint( int r, int g, int b, int a, char *text )
  222. {
  223. if ( vguiCenterString )
  224. {
  225. vguiCenterString->ColorPrint( r, g, b, a, text );
  226. }
  227. }
  228. void CCenterPrint::ColorPrint( int r, int g, int b, int a, wchar_t *text )
  229. {
  230. if ( vguiCenterString )
  231. {
  232. vguiCenterString->ColorPrint( r, g, b, a, text );
  233. }
  234. }
  235. void CCenterPrint::Clear( void )
  236. {
  237. if ( vguiCenterString )
  238. {
  239. vguiCenterString->Clear();
  240. }
  241. }
  242. void CCenterPrint::Create( vgui::VPANEL parent )
  243. {
  244. if ( vguiCenterString )
  245. {
  246. Destroy();
  247. }
  248. vguiCenterString = new CCenterStringLabel( parent );
  249. }
  250. void CCenterPrint::Destroy( void )
  251. {
  252. if ( vguiCenterString )
  253. {
  254. vguiCenterString->SetParent( (vgui::Panel *)NULL );
  255. delete vguiCenterString;
  256. vguiCenterString = NULL;
  257. }
  258. }
  259. static CCenterPrint g_CenterString;
  260. CCenterPrint *internalCenterPrint = &g_CenterString;
  261. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CCenterPrint, ICenterPrint, VCENTERPRINT_INTERFACE_VERSION, g_CenterString );