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.

210 lines
5.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "c_vguiscreen.h"
  10. #include "vgui_controls/Label.h"
  11. #include <vgui/IVGui.h>
  12. #include "c_plantedc4.h"
  13. #include "ienginevgui.h"
  14. using namespace vgui;
  15. //-----------------------------------------------------------------------------
  16. // Control screen
  17. //-----------------------------------------------------------------------------
  18. class CC4Panel : public CVGuiScreenPanel
  19. {
  20. DECLARE_CLASS( CC4Panel, CVGuiScreenPanel );
  21. public:
  22. CC4Panel( vgui::Panel *parent, const char *panelName );
  23. ~CC4Panel();
  24. virtual bool Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData );
  25. virtual void OnTick();
  26. virtual void ApplySchemeSettings( IScheme *pScheme );
  27. private:
  28. vgui::Label *m_pTimeLabel;
  29. float m_flNextDigitRandomizeTime; //next time to grab a new digit while scrolling random digits
  30. //in un-decoded digits
  31. int m_iLastRandomInt; //store the random digit between new rand calls
  32. bool m_bInitLabelColor;
  33. Color m_cArmed;
  34. Color m_cDefused;
  35. Color m_cInvisible;
  36. };
  37. DECLARE_VGUI_SCREEN_FACTORY( CC4Panel, "c4_panel" );
  38. //-----------------------------------------------------------------------------
  39. // Constructor:
  40. //-----------------------------------------------------------------------------
  41. CC4Panel::CC4Panel( vgui::Panel *parent, const char *panelName )
  42. : BaseClass( parent, "CC4Panel", vgui::scheme()->LoadSchemeFromFileEx( enginevgui->GetPanel( PANEL_CLIENTDLL ), "resource/C4Panel.res", "ClientScheme" ) )
  43. {
  44. SetSize( 10, 10 ); // Quiet "parent not sized yet" spew
  45. m_pTimeLabel = new vgui::Label( this, "TimerLabel", "" );
  46. m_flNextDigitRandomizeTime = 0;
  47. m_iLastRandomInt = 0;
  48. m_bInitLabelColor = true;
  49. }
  50. CC4Panel::~CC4Panel()
  51. {
  52. }
  53. void CC4Panel::ApplySchemeSettings( IScheme *pScheme )
  54. {
  55. assert( pScheme );
  56. m_cArmed = pScheme->GetColor( "C4Panel_Armed", GetFgColor() );
  57. m_cDefused = pScheme->GetColor( "C4Panel_Defused", GetFgColor() );
  58. m_cInvisible = Color( 0, 0, 0, 0 );
  59. if( m_bInitLabelColor )
  60. {
  61. m_pTimeLabel->SetFgColor( m_cArmed );
  62. m_bInitLabelColor = false;
  63. }
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Initialization
  67. //-----------------------------------------------------------------------------
  68. bool CC4Panel::Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData )
  69. {
  70. // Make sure we get ticked...
  71. vgui::ivgui()->AddTickSignal( GetVPanel() );
  72. if (!BaseClass::Init(pKeyValues, pInitData))
  73. return false;
  74. return true;
  75. }
  76. //how long to spend decoding each digit
  77. float flTransitionTimes[] = { 0.9, 0.8, 0.6, 0.45, 0.25, 0.15, 0.0 };
  78. //the defuse code, taken from the view model animation, v_c4.mdl
  79. char cDefuseCode[] = { '7', '3', '5', '5', '6', '0', '8', '\0' };
  80. char cArmedDisplay[] = { '*', '*', '*', '*', '*', '*', '*', '\0' };
  81. //convert an integer into the readable character version of that number
  82. #define INT_TO_CHAR(i) ( '0' + (i) )
  83. //-----------------------------------------------------------------------------
  84. // Update the display string
  85. //-----------------------------------------------------------------------------
  86. void CC4Panel::OnTick()
  87. {
  88. BaseClass::OnTick();
  89. SetVisible( true );
  90. float flProgress = 1.0;
  91. if ( g_PlantedC4s.Count() > 0 )
  92. {
  93. C_PlantedC4 *pC4 = g_PlantedC4s[0];
  94. if( pC4 )
  95. {
  96. flProgress = pC4->GetDefuseProgress();
  97. }
  98. else
  99. return;
  100. }
  101. m_pTimeLabel->SetFgColor( m_cArmed );
  102. // If flProgress is less than 0, the bomb has been defused
  103. if( flProgress < 0.0 )
  104. {
  105. //Flash when the bomb has been defused
  106. if( flProgress > -0.2 ) //flash for 2 seconds
  107. {
  108. int x = (int)( flProgress * 100 );
  109. if( x % 2 == 0 )
  110. m_pTimeLabel->SetFgColor( m_cInvisible );
  111. else
  112. m_pTimeLabel->SetFgColor( m_cDefused );
  113. }
  114. else
  115. m_pTimeLabel->SetFgColor( m_cDefused );
  116. //Show the full, decoded defuse code
  117. m_pTimeLabel->SetText( cDefuseCode );
  118. }
  119. else if( flProgress < 1.0 ) //defuse in progress
  120. {
  121. //Initial display
  122. char buf[8];
  123. Q_strncpy( buf, cArmedDisplay, MIN( sizeof(buf), sizeof(cArmedDisplay) ) );
  124. int iDigitPos = 0;
  125. while( flProgress < flTransitionTimes[iDigitPos] )
  126. {
  127. //Fill in the previously decoded digits
  128. buf[iDigitPos] = cDefuseCode[iDigitPos];
  129. iDigitPos++;
  130. }
  131. //Animate the character that we're decoding
  132. //Value drawn will be based on how long we've been
  133. //decoding this character
  134. float flTimeInThisChar = 1.0 - flTransitionTimes[0];
  135. if( iDigitPos > 0 )
  136. flTimeInThisChar = flTransitionTimes[iDigitPos-1] - flTransitionTimes[iDigitPos];
  137. assert( flTimeInThisChar > 0.0 );
  138. float flPercentDecoding = ( flProgress - flTransitionTimes[iDigitPos] ) / flTimeInThisChar;
  139. //Determine when to next change the digit that we're decoding
  140. if( m_flNextDigitRandomizeTime < gpGlobals->curtime )
  141. {
  142. //Get a new random int to draw
  143. m_iLastRandomInt = RandomInt( 0, 9 );
  144. if( flPercentDecoding > 0.7 )
  145. m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.05;
  146. else if( flPercentDecoding > 0.5 )
  147. m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.1;
  148. else if( flPercentDecoding > 0.3 )
  149. m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.15;
  150. else
  151. m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.3;
  152. }
  153. //Settle on the real value if we're close
  154. if( flPercentDecoding < 0.2 )
  155. buf[iDigitPos] = cDefuseCode[iDigitPos];
  156. else //else use a random digit
  157. buf[iDigitPos] = INT_TO_CHAR( m_iLastRandomInt );
  158. m_pTimeLabel->SetFgColor( m_cArmed );
  159. m_pTimeLabel->SetText( buf );
  160. }
  161. else
  162. {
  163. //Not being defused - draw the armed string
  164. m_pTimeLabel->SetFgColor( m_cArmed );
  165. m_pTimeLabel->SetText( cArmedDisplay );
  166. }
  167. }