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.

310 lines
7.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <mxtk/mx.h>
  8. #include "mxStatusWindow.h"
  9. #include "hlfaceposer.h"
  10. #include "choreowidgetdrawhelper.h"
  11. #include "MDLViewer.h"
  12. #include "faceposertoolwindow.h"
  13. extern double realtime;
  14. mxStatusWindow *g_pStatusWindow = NULL;
  15. #define STATUS_SCROLLBAR_SIZE 12
  16. #define STATUS_FONT_SIZE 9
  17. mxStatusWindow::mxStatusWindow(mxWindow *parent, int x, int y, int w, int h, const char *label /*= 0*/ )
  18. : mxWindow( parent, x, y, w, h, label ), IFacePoserToolWindow( "Status Window", "Output" ), m_pScrollbar(NULL)
  19. {
  20. for ( int i = 0; i < MAX_TEXT_LINES; i++ )
  21. {
  22. m_rgTextLines[ i ].m_szText[ 0 ] = 0;
  23. m_rgTextLines[ i ].rgb = CONSOLE_COLOR;
  24. m_rgTextLines[ i ].curtime = 0;
  25. }
  26. m_nCurrentLine = 0;
  27. m_pScrollbar = new mxScrollbar( this, 0, 0, STATUS_SCROLLBAR_SIZE, 100, IDC_STATUS_SCROLL, mxScrollbar::Vertical );
  28. m_pScrollbar->setRange( 0, 1000 );
  29. m_pScrollbar->setPagesize( 100 );
  30. }
  31. mxStatusWindow::~mxStatusWindow()
  32. {
  33. g_pStatusWindow = NULL;
  34. }
  35. void mxStatusWindow::redraw()
  36. {
  37. // if ( !ToolCanDraw() )
  38. // return;
  39. if ( !m_pScrollbar )
  40. return;
  41. CChoreoWidgetDrawHelper helper( this, RGB( 0, 0, 0 ) );
  42. HandleToolRedraw( helper );
  43. RECT rc;
  44. helper.GetClientRect( rc );
  45. RECT rcText = rc;
  46. int lineheight = ( STATUS_FONT_SIZE + 2 );
  47. InflateRect( &rcText, -4, 0 );
  48. rcText.bottom = h2() - 4;
  49. rcText.top = rcText.bottom - lineheight;
  50. //int minval = m_pScrollbar->getMinValue();
  51. int maxval = m_pScrollbar->getMaxValue();
  52. int pagesize = m_pScrollbar->getPagesize();
  53. int curval = m_pScrollbar->getValue();
  54. int offset = ( maxval - pagesize ) - curval;
  55. offset = ( offset + lineheight - 1 ) / lineheight;
  56. offset = max( 0, offset );
  57. //offset = 0;
  58. //offset += 10;
  59. //offset = max( 0, offset );
  60. for ( int i = 0; i < MAX_TEXT_LINES - offset; i++ )
  61. {
  62. int rawline = m_nCurrentLine - i - 1;
  63. if ( rawline <= 0 )
  64. continue;
  65. if ( rcText.bottom < 0 )
  66. break;
  67. int line = ( rawline - offset ) & TEXT_LINE_MASK;
  68. char *ptext = m_rgTextLines[ line ].m_szText;
  69. RECT rcTime = rcText;
  70. rcTime.right = rcTime.left + 50;
  71. char sz[ 32 ];
  72. sprintf( sz, "%.3f", m_rgTextLines[ line ].curtime );
  73. int len = helper.CalcTextWidth( "Arial", STATUS_FONT_SIZE, FW_NORMAL, sz );
  74. rcTime.left = rcTime.right - len - 5;
  75. helper.DrawColoredText( "Arial", STATUS_FONT_SIZE, FW_NORMAL, RGB( 255, 255, 150 ), rcTime, sz );
  76. rcTime = rcText;
  77. rcTime.left += 50;
  78. helper.DrawColoredText( "Arial", STATUS_FONT_SIZE, FW_NORMAL, m_rgTextLines[ line ].rgb, rcTime, ptext );
  79. OffsetRect( &rcText, 0, -lineheight );
  80. }
  81. DrawActiveTool();
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose:
  85. // Output : Returns true on success, false on failure.
  86. //-----------------------------------------------------------------------------
  87. bool mxStatusWindow::PaintBackground( void )
  88. {
  89. redraw();
  90. return false;
  91. }
  92. void mxStatusWindow::StatusPrint( COLORREF clr, bool overwrite, const char *text )
  93. {
  94. float curtime = (float)Plat_FloatTime();
  95. char sz[32];
  96. sprintf( sz, "%.3f ", curtime );
  97. OutputDebugString( sz );
  98. OutputDebugString( text );
  99. char fixedtext[ 512 ];
  100. char *in, *out;
  101. in = (char *)text;
  102. out = fixedtext;
  103. int c = 0;
  104. while ( *in && c < 511 )
  105. {
  106. if ( *in == '\n' || *in == '\r' )
  107. {
  108. in++;
  109. }
  110. else
  111. {
  112. *out++ = *in++;
  113. c++;
  114. }
  115. }
  116. *out = 0;
  117. if ( overwrite )
  118. {
  119. m_nCurrentLine--;
  120. }
  121. int i = m_nCurrentLine & TEXT_LINE_MASK;
  122. strncpy( m_rgTextLines[ i ].m_szText, fixedtext, 511 );
  123. m_rgTextLines[ i ].m_szText[ 511 ] = 0;
  124. m_rgTextLines[ i ].rgb = clr;
  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 mxStatusWindow::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() - GetCaptionHeight();
  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 mxStatusWindow::handleEvent( mxEvent *event )
  156. {
  157. int iret = 0;
  158. if ( HandleToolEvent( event ) )
  159. {
  160. return iret;
  161. }
  162. switch ( event->event )
  163. {
  164. default:
  165. break;
  166. case mxEvent::Size:
  167. {
  168. m_pScrollbar->setBounds( w2() - STATUS_SCROLLBAR_SIZE, GetCaptionHeight(), STATUS_SCROLLBAR_SIZE, h2()-GetCaptionHeight() );
  169. PositionSliders( 0 );
  170. m_pScrollbar->setValue( m_pScrollbar->getMaxValue() );
  171. iret = 1;
  172. }
  173. break;
  174. case mxEvent::Action:
  175. {
  176. iret = 1;
  177. switch ( event->action )
  178. {
  179. default:
  180. iret = 0;
  181. break;
  182. case IDC_STATUS_SCROLL:
  183. {
  184. if ( event->event == mxEvent::Action &&
  185. event->modifiers == SB_THUMBTRACK)
  186. {
  187. int offset = event->height;
  188. m_pScrollbar->setValue( offset );
  189. PositionSliders( offset );
  190. DrawActiveTool();
  191. }
  192. }
  193. break;
  194. }
  195. }
  196. break;
  197. }
  198. return iret;
  199. }
  200. #include "StudioModel.h"
  201. #include "faceposer_models.h"
  202. //-----------------------------------------------------------------------------
  203. // Purpose:
  204. //-----------------------------------------------------------------------------
  205. void mxStatusWindow::DrawActiveTool()
  206. {
  207. RECT rcTool;
  208. rcTool.left = 0;
  209. rcTool.top = GetCaptionHeight() + 2;
  210. rcTool.bottom = h2();
  211. rcTool.right = w2() - 16;
  212. rcTool.bottom = rcTool.top + 10;
  213. rcTool.left = rcTool.right - 500;
  214. char sz[ 256 ];
  215. IFacePoserToolWindow *activeTool = IFacePoserToolWindow::GetActiveTool();
  216. static float lastrealtime = 0.0f;
  217. float dt = (float)realtime - lastrealtime;
  218. dt = clamp( dt, 0.0f, 1.0f );
  219. float fps = 0.0f;
  220. if ( dt > 0.0001f )
  221. {
  222. fps = 1.0f / dt;
  223. }
  224. sprintf( sz, "%s (%i) at %.3f (%.2f fps) (soundcount %i)",
  225. activeTool ? activeTool->GetToolName() : "None",
  226. g_MDLViewer->GetCurrentFrame(),
  227. (float)realtime,
  228. fps,
  229. models->CountActiveSources() );
  230. lastrealtime = realtime;
  231. int len = CChoreoWidgetDrawHelper::CalcTextWidth( "Courier New", 10, FW_NORMAL, sz );
  232. CChoreoWidgetDrawHelper helper( this, rcTool, RGB( 32, 0, 0 ) );
  233. rcTool.left = rcTool.right - len - 15;
  234. helper.DrawColoredText( "Courier New", 10, FW_NORMAL, RGB( 255, 255, 200 ), rcTool, sz );
  235. }
  236. //-----------------------------------------------------------------------------
  237. // Purpose:
  238. // Input : dt -
  239. //-----------------------------------------------------------------------------
  240. void mxStatusWindow::Think( float dt )
  241. {
  242. DrawActiveTool();
  243. }