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.

245 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "statuswindow.h"
  9. #include "drawhelper.h"
  10. CStatusWindow *g_pStatusWindow = NULL;
  11. #define STATUS_SCROLLBAR_SIZE 12
  12. #define STATUS_FONT_SIZE 10
  13. CStatusWindow::CStatusWindow(mxWindow *parent, int x, int y, int w, int h, const char *label /*= 0*/ )
  14. : mxWindow( parent, x, y, w, h, label ), m_pScrollbar(NULL)
  15. {
  16. for ( int i = 0; i < MAX_TEXT_LINES; i++ )
  17. {
  18. m_rgTextLines[ i ].m_szText[ 0 ] = 0;
  19. m_rgTextLines[ i ].r = CONSOLE_R;
  20. m_rgTextLines[ i ].g = CONSOLE_G;
  21. m_rgTextLines[ i ].b = CONSOLE_B;
  22. m_rgTextLines[ i ].curtime = 0;
  23. }
  24. m_nCurrentLine = 0;
  25. m_pScrollbar = new mxScrollbar( this, 0, 0, STATUS_SCROLLBAR_SIZE, 100, IDC_STATUS_SCROLL, mxScrollbar::Vertical );
  26. m_pScrollbar->setRange( 0, 1000 );
  27. m_pScrollbar->setPagesize( 100 );
  28. g_pStatusWindow = this;
  29. }
  30. CStatusWindow::~CStatusWindow()
  31. {
  32. g_pStatusWindow = NULL;
  33. }
  34. void CStatusWindow::redraw()
  35. {
  36. // if ( !ToolCanDraw() )
  37. // return;
  38. if ( !m_pScrollbar )
  39. return;
  40. CDrawHelper helper( this, RGB( 0, 0, 0 ) );
  41. RECT rc;
  42. helper.GetClientRect( rc );
  43. RECT rcText = rc;
  44. int lineheight = ( STATUS_FONT_SIZE + 2 );
  45. InflateRect( &rcText, -4, 0 );
  46. rcText.bottom = h2() - 4;
  47. rcText.top = rcText.bottom - lineheight;
  48. // int minval = m_pScrollbar->getMinValue();
  49. int maxval = m_pScrollbar->getMaxValue();
  50. int pagesize = m_pScrollbar->getPagesize();
  51. int curval = m_pScrollbar->getValue();
  52. int offset = ( maxval - pagesize ) - curval;
  53. offset = ( offset + lineheight - 1 ) / lineheight;
  54. offset = max( 0, offset );
  55. //offset = 0;
  56. //offset += 10;
  57. //offset = max( 0, offset );
  58. for ( int i = 0; i < MAX_TEXT_LINES - offset; i++ )
  59. {
  60. int rawline = m_nCurrentLine - i - 1;
  61. if ( rawline <= 0 )
  62. continue;
  63. if ( rcText.bottom < 0 )
  64. break;
  65. int line = ( rawline - offset ) & TEXT_LINE_MASK;
  66. COLORREF clr = RGB( m_rgTextLines[ line ].r, m_rgTextLines[ line ].g, m_rgTextLines[ line ].b );
  67. char *ptext = m_rgTextLines[ line ].m_szText;
  68. RECT rcTime = rcText;
  69. rcTime.right = rcTime.left + 50;
  70. char sz[ 32 ];
  71. sprintf( sz, "%.3f", m_rgTextLines[ line ].curtime );
  72. int len = helper.CalcTextWidth( "Arial", STATUS_FONT_SIZE, FW_NORMAL, sz );
  73. rcTime.left = rcTime.right - len - 5;
  74. helper.DrawColoredText( "Arial", STATUS_FONT_SIZE, FW_NORMAL, RGB( 255, 255, 150 ), rcTime, sz );
  75. rcTime = rcText;
  76. rcTime.left += 50;
  77. helper.DrawColoredText( "Arial", STATUS_FONT_SIZE, FW_NORMAL, clr, rcTime, ptext );
  78. OffsetRect( &rcText, 0, -lineheight );
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose:
  83. // Output : Returns true on success, false on failure.
  84. //-----------------------------------------------------------------------------
  85. bool CStatusWindow::PaintBackground( void )
  86. {
  87. redraw();
  88. return false;
  89. }
  90. void CStatusWindow::StatusPrint( int r, int g, int b, bool overwrite, const char *text )
  91. {
  92. float curtime = (float)Plat_FloatTime();
  93. char sz[32];
  94. sprintf( sz, "%.3f ", curtime );
  95. OutputDebugString( sz );
  96. OutputDebugString( text );
  97. char fixedtext[ 512 ];
  98. char *in, *out;
  99. in = (char *)text;
  100. out = fixedtext;
  101. int c = 0;
  102. while ( *in && c < 511 )
  103. {
  104. if ( *in == '\n' || *in == '\r' )
  105. {
  106. in++;
  107. }
  108. else
  109. {
  110. *out++ = *in++;
  111. c++;
  112. }
  113. }
  114. *out = 0;
  115. if ( overwrite )
  116. {
  117. m_nCurrentLine--;
  118. }
  119. int i = m_nCurrentLine & TEXT_LINE_MASK;
  120. strncpy( m_rgTextLines[ i ].m_szText, fixedtext, 511 );
  121. m_rgTextLines[ i ].m_szText[ 511 ] = 0;
  122. m_rgTextLines[ i ].r = r;
  123. m_rgTextLines[ i ].g = g;
  124. m_rgTextLines[ i ].b = b;
  125. m_rgTextLines[ i ].curtime = curtime;
  126. m_nCurrentLine++;
  127. if ( m_nCurrentLine <= MAX_TEXT_LINES )
  128. {
  129. PositionSliders( 0 );
  130. }
  131. m_pScrollbar->setValue( m_pScrollbar->getMaxValue() );
  132. redraw();
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose:
  136. // Input : sboffset -
  137. //-----------------------------------------------------------------------------
  138. void CStatusWindow::PositionSliders( int sboffset )
  139. {
  140. int lineheight = ( STATUS_FONT_SIZE + 2 );
  141. int linesused = min( (int)MAX_TEXT_LINES, m_nCurrentLine );
  142. linesused = max( linesused, 1 );
  143. int trueh = h2();
  144. int vpixelsneeded = max( linesused * lineheight, trueh );
  145. m_pScrollbar->setVisible( linesused * lineheight > trueh );
  146. m_pScrollbar->setPagesize( trueh );
  147. m_pScrollbar->setRange( 0, vpixelsneeded );
  148. redraw();
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Purpose:
  152. // Input : *event -
  153. // Output : int
  154. //-----------------------------------------------------------------------------
  155. int CStatusWindow::handleEvent( mxEvent *event )
  156. {
  157. int iret = 0;
  158. switch ( event->event )
  159. {
  160. default:
  161. break;
  162. case mxEvent::Size:
  163. {
  164. m_pScrollbar->setBounds( w2() - STATUS_SCROLLBAR_SIZE, 0, STATUS_SCROLLBAR_SIZE, h2() );
  165. PositionSliders( 0 );
  166. m_pScrollbar->setValue( m_pScrollbar->getMaxValue() );
  167. iret = 1;
  168. }
  169. break;
  170. case mxEvent::Action:
  171. {
  172. iret = 1;
  173. switch ( event->action )
  174. {
  175. default:
  176. iret = 0;
  177. break;
  178. case IDC_STATUS_SCROLL:
  179. {
  180. if ( event->event == mxEvent::Action &&
  181. event->modifiers == SB_THUMBTRACK)
  182. {
  183. int offset = event->height;
  184. m_pScrollbar->setValue( offset );
  185. PositionSliders( offset );
  186. }
  187. }
  188. break;
  189. }
  190. }
  191. break;
  192. }
  193. return iret;
  194. }