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.

199 lines
5.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud_numericdisplay.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. CHudNumericDisplay::CHudNumericDisplay(vgui::Panel *parent, const char *name) : BaseClass(parent, name)
  21. {
  22. vgui::Panel *pParent = g_pClientMode->GetViewport();
  23. SetParent( pParent );
  24. m_iValue = 0;
  25. m_LabelText[0] = 0;
  26. m_iSecondaryValue = 0;
  27. m_bDisplayValue = true;
  28. m_bDisplaySecondaryValue = false;
  29. m_bIndent = false;
  30. m_bIsTime = false;
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Resets values on restore/new map
  34. //-----------------------------------------------------------------------------
  35. void CHudNumericDisplay::Reset()
  36. {
  37. m_flBlur = 0.0f;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Purpose: data accessor
  41. //-----------------------------------------------------------------------------
  42. void CHudNumericDisplay::SetDisplayValue(int value)
  43. {
  44. m_iValue = value;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: data accessor
  48. //-----------------------------------------------------------------------------
  49. void CHudNumericDisplay::SetSecondaryValue(int value)
  50. {
  51. m_iSecondaryValue = value;
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: data accessor
  55. //-----------------------------------------------------------------------------
  56. void CHudNumericDisplay::SetShouldDisplayValue(bool state)
  57. {
  58. m_bDisplayValue = state;
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: data accessor
  62. //-----------------------------------------------------------------------------
  63. void CHudNumericDisplay::SetShouldDisplaySecondaryValue(bool state)
  64. {
  65. m_bDisplaySecondaryValue = state;
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose: data accessor
  69. //-----------------------------------------------------------------------------
  70. void CHudNumericDisplay::SetLabelText(const wchar_t *text)
  71. {
  72. wcsncpy(m_LabelText, text, sizeof(m_LabelText) / sizeof(wchar_t));
  73. m_LabelText[(sizeof(m_LabelText) / sizeof(wchar_t)) - 1] = 0;
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose: data accessor
  77. //-----------------------------------------------------------------------------
  78. void CHudNumericDisplay::SetIndent(bool state)
  79. {
  80. m_bIndent = state;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: data accessor
  84. //-----------------------------------------------------------------------------
  85. void CHudNumericDisplay::SetIsTime(bool state)
  86. {
  87. m_bIsTime = state;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: paints a number at the specified position
  91. //-----------------------------------------------------------------------------
  92. void CHudNumericDisplay::PaintNumbers(HFont font, int xpos, int ypos, int value)
  93. {
  94. surface()->DrawSetTextFont(font);
  95. wchar_t unicode[6];
  96. if ( !m_bIsTime )
  97. {
  98. V_snwprintf(unicode, ARRAYSIZE(unicode), L"%d", value);
  99. }
  100. else
  101. {
  102. int iMinutes = value / 60;
  103. int iSeconds = value - iMinutes * 60;
  104. #ifdef PORTAL
  105. // portal uses a normal font for numbers so we need the seperate to be a renderable ':' char
  106. if ( iSeconds < 10 )
  107. V_snwprintf( unicode, ARRAYSIZE(unicode), L"%d:0%d", iMinutes, iSeconds );
  108. else
  109. V_snwprintf( unicode, ARRAYSIZE(unicode), L"%d:%d", iMinutes, iSeconds );
  110. #else
  111. if ( iSeconds < 10 )
  112. V_snwprintf( unicode, ARRAYSIZE(unicode), L"%d`0%d", iMinutes, iSeconds );
  113. else
  114. V_snwprintf( unicode, ARRAYSIZE(unicode), L"%d`%d", iMinutes, iSeconds );
  115. #endif
  116. }
  117. // adjust the position to take into account 3 characters
  118. int charWidth = surface()->GetCharacterWidth(font, '0');
  119. if (value < 100 && m_bIndent)
  120. {
  121. xpos += charWidth;
  122. }
  123. if (value < 10 && m_bIndent)
  124. {
  125. xpos += charWidth;
  126. }
  127. surface()->DrawSetTextPos(xpos, ypos);
  128. surface()->DrawUnicodeString( unicode );
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose: draws the text
  132. //-----------------------------------------------------------------------------
  133. void CHudNumericDisplay::PaintLabel( void )
  134. {
  135. surface()->DrawSetTextFont(m_hTextFont);
  136. surface()->DrawSetTextColor(GetFgColor());
  137. surface()->DrawSetTextPos(text_xpos, text_ypos);
  138. surface()->DrawUnicodeString( m_LabelText );
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: renders the vgui panel
  142. //-----------------------------------------------------------------------------
  143. void CHudNumericDisplay::Paint()
  144. {
  145. if (m_bDisplayValue)
  146. {
  147. // draw our numbers
  148. surface()->DrawSetTextColor(GetFgColor());
  149. PaintNumbers(m_hNumberFont, digit_xpos, digit_ypos, m_iValue);
  150. // draw the overbright blur
  151. for (float fl = m_flBlur; fl > 0.0f; fl -= 1.0f)
  152. {
  153. if (fl >= 1.0f)
  154. {
  155. PaintNumbers(m_hNumberGlowFont, digit_xpos, digit_ypos, m_iValue);
  156. }
  157. else
  158. {
  159. // draw a percentage of the last one
  160. Color col = GetFgColor();
  161. col[3] *= fl;
  162. surface()->DrawSetTextColor(col);
  163. PaintNumbers(m_hNumberGlowFont, digit_xpos, digit_ypos, m_iValue);
  164. }
  165. }
  166. }
  167. // total ammo
  168. if (m_bDisplaySecondaryValue)
  169. {
  170. surface()->DrawSetTextColor(GetFgColor());
  171. PaintNumbers(m_hSmallNumberFont, digit2_xpos, digit2_ypos, m_iSecondaryValue);
  172. }
  173. PaintLabel();
  174. }