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.

167 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud_bitmapnumericdisplay.h"
  8. #include "iclientmode.h"
  9. #include <Color.h>
  10. #include <KeyValues.h>
  11. #include <vgui/ISurface.h>
  12. #include <vgui/ISystem.h>
  13. #include <vgui/IVGui.h>
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. using namespace vgui;
  17. //-----------------------------------------------------------------------------
  18. // Purpose: constructor
  19. //-----------------------------------------------------------------------------
  20. CHudBitmapNumericDisplay::CHudBitmapNumericDisplay(vgui::Panel *parent, const char *name) : vgui::Panel(parent, name)
  21. {
  22. vgui::Panel *pParent = g_pClientMode->GetViewport();
  23. SetParent( pParent );
  24. m_iValue = 0;
  25. m_bDisplayValue = true;
  26. memset( m_pNumbers, 0, 10*sizeof(CHudTexture *) );
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Purpose: data accessor
  30. //-----------------------------------------------------------------------------
  31. void CHudBitmapNumericDisplay::SetDisplayValue(int value)
  32. {
  33. m_iValue = value;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose: renders the vgui panel
  37. //-----------------------------------------------------------------------------
  38. void CHudBitmapNumericDisplay::Paint()
  39. {
  40. float alpha = m_flAlphaOverride / 255;
  41. Color fgColor = GetFgColor();
  42. fgColor[3] *= alpha;
  43. SetFgColor( fgColor );
  44. if (m_bDisplayValue)
  45. {
  46. // draw our numbers
  47. // surface()->DrawSetTextColor(GetFgColor());
  48. PaintNumbers(digit_xpos, digit_ypos, m_iValue, GetFgColor());
  49. // draw the overbright blur
  50. for (float fl = m_flBlur; fl > 0.0f; fl -= 1.0f)
  51. {
  52. if (fl >= 1.0f)
  53. {
  54. PaintNumbers(digit_xpos, digit_ypos, m_iValue, GetFgColor());
  55. }
  56. else
  57. {
  58. // draw a percentage of the last one
  59. Color col = GetFgColor();
  60. col[3] *= fl;
  61. PaintNumbers(digit_xpos, digit_ypos, m_iValue, col);
  62. }
  63. }
  64. }
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: data accessor
  68. //-----------------------------------------------------------------------------
  69. void CHudBitmapNumericDisplay::SetShouldDisplayValue(bool state)
  70. {
  71. m_bDisplayValue = state;
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose:
  75. //-----------------------------------------------------------------------------
  76. void CHudBitmapNumericDisplay::PaintBackground( void )
  77. {
  78. int alpha = m_flAlphaOverride / 255;
  79. Color bgColor = GetBgColor();
  80. bgColor[3] *= alpha;
  81. SetBgColor( bgColor );
  82. BaseClass::PaintBackground();
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose:
  86. //-----------------------------------------------------------------------------
  87. void CHudBitmapNumericDisplay::PaintNumbers(int xpos, int ypos, int value, Color col, int numSigDigits )
  88. {
  89. if( !m_pNumbers[0] )
  90. {
  91. int i;
  92. char a[16];
  93. for( i=0;i<10;i++ )
  94. {
  95. sprintf( a, "number_%d", i );
  96. m_pNumbers[i] = gHUD.GetIcon( a );
  97. }
  98. if( !m_pNumbers[0] )
  99. return;
  100. }
  101. if( value > 100000 )
  102. {
  103. value = 99999;
  104. }
  105. int pos = 10000;
  106. float scale = ( digit_height / (float)m_pNumbers[0]->Height());
  107. int digit;
  108. Color color = GetFgColor();
  109. int width = m_pNumbers[0]->Width() * scale;
  110. int height = m_pNumbers[0]->Height() * scale;
  111. bool bStart = false;
  112. //right align to xpos
  113. int numdigits = 1;
  114. int x = pos;
  115. while( x >= 10 )
  116. {
  117. if( value >= x )
  118. numdigits++;
  119. x /= 10;
  120. }
  121. if( numdigits < numSigDigits )
  122. numdigits = numSigDigits;
  123. xpos -= numdigits * width;
  124. //draw the digits
  125. while( pos >= 1 )
  126. {
  127. digit = value / pos;
  128. value = value % pos;
  129. if( bStart || digit > 0 || pos <= pow(10.0f,numSigDigits-1) )
  130. {
  131. bStart = true;
  132. m_pNumbers[digit]->DrawSelf( xpos, ypos, width, height, col );
  133. xpos += width;
  134. }
  135. pos /= 10;
  136. }
  137. }