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.

147 lines
3.1 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Material proxy to get the cycle from a CBaseAnimateing derived entity.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. // identifier was truncated to '255' characters in the debug information
  9. //#pragma warning(disable: 4786)
  10. #include "proxyentity.h"
  11. #include "materialsystem/imaterialvar.h"
  12. #include "imaterialproxydict.h"
  13. // NOTE: This has to be the last file included!
  14. #include "tier0/memdbgon.h"
  15. class CCycleMaterialProxy: public CEntityMaterialProxy
  16. {
  17. public:
  18. CCycleMaterialProxy()
  19. {
  20. m_pMaterial = NULL;
  21. m_pResult = NULL;
  22. m_bEaseIn = false;
  23. m_bEaseOut = false;
  24. m_fStart = 0.0f;
  25. m_fEnd = 1.0f;
  26. }
  27. virtual ~CCycleMaterialProxy()
  28. {
  29. }
  30. virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  31. {
  32. m_pMaterial = pMaterial;
  33. const char *pResult = pKeyValues->GetString( "resultVar", NULL );
  34. if ( !pResult )
  35. return false;
  36. bool found;
  37. m_pResult = m_pMaterial->FindVar( pResult, &found );
  38. if ( !found )
  39. {
  40. m_pResult = NULL;
  41. return false;
  42. }
  43. if ( !Q_stricmp( pResult, "$alpha" ) )
  44. {
  45. pMaterial->SetMaterialVarFlag( MATERIAL_VAR_ALPHA_MODIFIED_BY_PROXY, true );
  46. }
  47. pResult = pKeyValues->GetString( "easein", NULL );
  48. if( pResult && Q_atoi( pResult ) != 0 )
  49. {
  50. m_bEaseIn = true;
  51. }
  52. pResult = pKeyValues->GetString( "easeout", NULL );
  53. if( pResult && Q_atoi( pResult ) != 0 )
  54. {
  55. m_bEaseOut = true;
  56. }
  57. pResult = pKeyValues->GetString( "start", NULL );
  58. if( pResult )
  59. {
  60. m_fStart = Q_atof( pResult );
  61. }
  62. pResult = pKeyValues->GetString( "end", NULL );
  63. if( pResult )
  64. {
  65. m_fEnd = Q_atof( pResult );
  66. }
  67. return true;
  68. }
  69. virtual void OnBind( C_BaseEntity *pC_BaseEntity )
  70. {
  71. C_BaseAnimating *pBaseAnimating = pC_BaseEntity ? pC_BaseEntity->GetBaseAnimating() : NULL;
  72. if ( pBaseAnimating )
  73. {
  74. float fCycle = pBaseAnimating->GetCycle();
  75. float f = RemapValClamped( fCycle, m_fStart, m_fEnd, 0.0f, 1.0f );
  76. if ( m_bEaseIn && m_bEaseOut )
  77. {
  78. f = SimpleSpline( f );
  79. }
  80. else if ( m_bEaseIn )
  81. {
  82. f = sin( M_PI * f * 0.5f );
  83. }
  84. else if ( m_bEaseOut )
  85. {
  86. f = 1.0f - sin( M_PI * f * 0.5f + 0.5f * M_PI );
  87. }
  88. MaterialVarType_t resultType;
  89. int vecSize;
  90. ComputeResultType( resultType, vecSize );
  91. switch( resultType )
  92. {
  93. case MATERIAL_VAR_TYPE_VECTOR:
  94. {
  95. Vector4D vec( f, f, f, f );
  96. m_pResult->SetVecValue( vec.Base(), vecSize );
  97. }
  98. break;
  99. case MATERIAL_VAR_TYPE_FLOAT:
  100. case MATERIAL_VAR_TYPE_INT:
  101. default:
  102. m_pResult->SetFloatValue( f );
  103. break;
  104. }
  105. }
  106. }
  107. virtual IMaterial *GetMaterial()
  108. {
  109. return m_pMaterial;
  110. }
  111. void ComputeResultType( MaterialVarType_t& resultType, int& vecSize )
  112. {
  113. vecSize = 1;
  114. resultType = m_pResult->GetType();
  115. if (resultType == MATERIAL_VAR_TYPE_VECTOR)
  116. {
  117. vecSize = m_pResult->VectorSize();
  118. }
  119. }
  120. protected:
  121. IMaterial *m_pMaterial;
  122. IMaterialVar *m_pResult;
  123. bool m_bEaseIn;
  124. bool m_bEaseOut;
  125. float m_fStart;
  126. float m_fEnd;
  127. };
  128. EXPOSE_MATERIAL_PROXY( CCycleMaterialProxy, Cycle );