Counter Strike : Global Offensive Source Code
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.

191 lines
5.6 KiB

  1. //========= Copyright � 1996-2005, 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 = GetClientMode()->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. if ( iSeconds < 10 )
  105. V_snwprintf( unicode, ARRAYSIZE(unicode), L"%d`0%d", iMinutes, iSeconds );
  106. else
  107. V_snwprintf( unicode, ARRAYSIZE(unicode), L"%d`%d", iMinutes, iSeconds );
  108. }
  109. // adjust the position to take into account 3 characters
  110. int charWidth = surface()->GetCharacterWidth(font, '0');
  111. if (value < 100 && m_bIndent)
  112. {
  113. xpos += charWidth;
  114. }
  115. if (value < 10 && m_bIndent)
  116. {
  117. xpos += charWidth;
  118. }
  119. surface()->DrawSetTextPos(xpos, ypos);
  120. surface()->DrawUnicodeString( unicode );
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: draws the text
  124. //-----------------------------------------------------------------------------
  125. void CHudNumericDisplay::PaintLabel( void )
  126. {
  127. surface()->DrawSetTextFont(m_hTextFont);
  128. surface()->DrawSetTextColor(GetFgColor());
  129. surface()->DrawSetTextPos(text_xpos, text_ypos);
  130. surface()->DrawUnicodeString( m_LabelText );
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose: renders the vgui panel
  134. //-----------------------------------------------------------------------------
  135. void CHudNumericDisplay::Paint()
  136. {
  137. if (m_bDisplayValue)
  138. {
  139. // draw our numbers
  140. surface()->DrawSetTextColor(GetFgColor());
  141. PaintNumbers(m_hNumberFont, digit_xpos, digit_ypos, m_iValue);
  142. // draw the overbright blur
  143. for (float fl = m_flBlur; fl > 0.0f; fl -= 1.0f)
  144. {
  145. if (fl >= 1.0f)
  146. {
  147. PaintNumbers(m_hNumberGlowFont, digit_xpos, digit_ypos, m_iValue);
  148. }
  149. else
  150. {
  151. // draw a percentage of the last one
  152. Color col = GetFgColor();
  153. col[3] *= fl;
  154. surface()->DrawSetTextColor(col);
  155. PaintNumbers(m_hNumberGlowFont, digit_xpos, digit_ypos, m_iValue);
  156. }
  157. }
  158. }
  159. // total ammo
  160. if (m_bDisplaySecondaryValue)
  161. {
  162. surface()->DrawSetTextColor(GetFgColor());
  163. PaintNumbers(m_hSmallNumberFont, digit2_xpos, digit2_ypos, m_iSecondaryValue);
  164. }
  165. PaintLabel();
  166. }