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.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements visual effects entities: sprites, beams, bubbles, etc.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "shake.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. class CEnvFade : public CLogicalEntity
  12. {
  13. private:
  14. float m_Duration;
  15. float m_HoldTime;
  16. COutputEvent m_OnBeginFade;
  17. DECLARE_DATADESC();
  18. public:
  19. DECLARE_CLASS( CEnvFade, CLogicalEntity );
  20. virtual void Spawn( void );
  21. inline float Duration( void ) { return m_Duration; }
  22. inline float HoldTime( void ) { return m_HoldTime; }
  23. inline void SetDuration( float duration ) { m_Duration = duration; }
  24. inline void SetHoldTime( float hold ) { m_HoldTime = hold; }
  25. int DrawDebugTextOverlays(void);
  26. // Inputs
  27. void InputFade( inputdata_t &inputdata );
  28. };
  29. LINK_ENTITY_TO_CLASS( env_fade, CEnvFade );
  30. BEGIN_DATADESC( CEnvFade )
  31. DEFINE_KEYFIELD( m_Duration, FIELD_FLOAT, "duration" ),
  32. DEFINE_KEYFIELD( m_HoldTime, FIELD_FLOAT, "holdtime" ),
  33. DEFINE_INPUTFUNC( FIELD_VOID, "Fade", InputFade ),
  34. DEFINE_OUTPUT( m_OnBeginFade, "OnBeginFade"),
  35. END_DATADESC()
  36. #define SF_FADE_IN 0x0001 // Fade in, not out
  37. #define SF_FADE_MODULATE 0x0002 // Modulate, don't blend
  38. #define SF_FADE_ONLYONE 0x0004
  39. #define SF_FADE_STAYOUT 0x0008
  40. //-----------------------------------------------------------------------------
  41. // Purpose:
  42. //-----------------------------------------------------------------------------
  43. void CEnvFade::Spawn( void )
  44. {
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Input handler that does the screen fade.
  48. //-----------------------------------------------------------------------------
  49. void CEnvFade::InputFade( inputdata_t &inputdata )
  50. {
  51. int fadeFlags = 0;
  52. if ( m_spawnflags & SF_FADE_IN )
  53. {
  54. fadeFlags |= FFADE_IN;
  55. }
  56. else
  57. {
  58. fadeFlags |= FFADE_OUT;
  59. }
  60. if ( m_spawnflags & SF_FADE_MODULATE )
  61. {
  62. fadeFlags |= FFADE_MODULATE;
  63. }
  64. if ( m_spawnflags & SF_FADE_STAYOUT )
  65. {
  66. fadeFlags |= FFADE_STAYOUT;
  67. }
  68. if ( m_spawnflags & SF_FADE_ONLYONE )
  69. {
  70. if ( inputdata.pActivator && inputdata.pActivator->IsNetClient() )
  71. {
  72. UTIL_ScreenFade( inputdata.pActivator, m_clrRender, Duration(), HoldTime(), fadeFlags );
  73. }
  74. }
  75. else
  76. {
  77. UTIL_ScreenFadeAll( m_clrRender, Duration(), HoldTime(), fadeFlags|FFADE_PURGE );
  78. }
  79. m_OnBeginFade.FireOutput( inputdata.pActivator, this );
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose: Fetches the arguments from the command line for the fadein and fadeout
  83. // console commands.
  84. // Input : flTime - Returns the fade time in seconds (the time to fade in or out)
  85. // clrFade - Returns the color to fade to or from.
  86. //-----------------------------------------------------------------------------
  87. static void GetFadeParms( const CCommand &args, float &flTime, color32 &clrFade)
  88. {
  89. flTime = 2.0f;
  90. if ( args.ArgC() > 1 )
  91. {
  92. flTime = atof( args[1] );
  93. }
  94. clrFade.r = 0;
  95. clrFade.g = 0;
  96. clrFade.b = 0;
  97. clrFade.a = 255;
  98. if ( args.ArgC() > 4 )
  99. {
  100. clrFade.r = atoi( args[2] );
  101. clrFade.g = atoi( args[3] );
  102. clrFade.b = atoi( args[4] );
  103. if ( args.ArgC() == 5 )
  104. {
  105. clrFade.a = atoi( args[5] );
  106. }
  107. }
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose: Console command to fade out to a given color.
  111. //-----------------------------------------------------------------------------
  112. static void CC_FadeOut( const CCommand &args )
  113. {
  114. float flTime;
  115. color32 clrFade;
  116. GetFadeParms( args, flTime, clrFade );
  117. CBasePlayer *pPlayer = UTIL_GetCommandClient();
  118. UTIL_ScreenFade( pPlayer, clrFade, flTime, 0, FFADE_OUT | FFADE_PURGE | FFADE_STAYOUT );
  119. }
  120. static ConCommand fadeout("fadeout", CC_FadeOut, "fadeout {time r g b}: Fades the screen to black or to the specified color over the given number of seconds.", FCVAR_CHEAT );
  121. //-----------------------------------------------------------------------------
  122. // Purpose: Console command to fade in from a given color.
  123. //-----------------------------------------------------------------------------
  124. static void CC_FadeIn( const CCommand &args )
  125. {
  126. float flTime;
  127. color32 clrFade;
  128. GetFadeParms( args, flTime, clrFade );
  129. CBasePlayer *pPlayer = UTIL_GetCommandClient();
  130. UTIL_ScreenFade( pPlayer, clrFade, flTime, 0, FFADE_IN | FFADE_PURGE );
  131. }
  132. static ConCommand fadein("fadein", CC_FadeIn, "fadein {time r g b}: Fades the screen in from black or from the specified color over the given number of seconds.", FCVAR_CHEAT );
  133. //-----------------------------------------------------------------------------
  134. // Purpose: Draw any debug text overlays
  135. // Output : Current text offset from the top
  136. //-----------------------------------------------------------------------------
  137. int CEnvFade::DrawDebugTextOverlays( void )
  138. {
  139. int text_offset = BaseClass::DrawDebugTextOverlays();
  140. if (m_debugOverlays & OVERLAY_TEXT_BIT)
  141. {
  142. char tempstr[512];
  143. // print duration
  144. Q_snprintf(tempstr,sizeof(tempstr)," duration: %f", m_Duration);
  145. EntityText(text_offset,tempstr,0);
  146. text_offset++;
  147. // print hold time
  148. Q_snprintf(tempstr,sizeof(tempstr)," hold time: %f", m_HoldTime);
  149. EntityText(text_offset,tempstr,0);
  150. text_offset++;
  151. }
  152. return text_offset;
  153. }