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.

118 lines
3.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "toggletextureproxy.h"
  9. #include "materialsystem/imaterial.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include "materialsystem/itexture.h"
  12. #include <keyvalues.h>
  13. #include "functionproxy.h"
  14. #include "imaterialproxydict.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. EXPOSE_MATERIAL_PROXY( CBaseToggleTextureProxy, ToggleTexture );
  18. //-----------------------------------------------------------------------------
  19. // Constructor, destructor:
  20. //-----------------------------------------------------------------------------
  21. CBaseToggleTextureProxy::CBaseToggleTextureProxy()
  22. {
  23. Cleanup();
  24. }
  25. CBaseToggleTextureProxy::~CBaseToggleTextureProxy()
  26. {
  27. Cleanup();
  28. }
  29. C_BaseEntity *CBaseToggleTextureProxy::BindArgToEntity( void *pArg )
  30. {
  31. IClientRenderable *pRend = (IClientRenderable *)pArg;
  32. return pRend->GetIClientUnknown()->GetBaseEntity();
  33. }
  34. //-----------------------------------------------------------------------------
  35. // Initialization, shutdown
  36. //-----------------------------------------------------------------------------
  37. bool CBaseToggleTextureProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  38. {
  39. char const* pTextureVarName = pKeyValues->GetString( "toggleTextureVar" );
  40. if( !pTextureVarName )
  41. return false;
  42. bool foundVar;
  43. m_TextureVar = pMaterial->FindVar( pTextureVarName, &foundVar, false );
  44. if( !foundVar )
  45. return false;
  46. char const* pTextureFrameNumVarName = pKeyValues->GetString( "toggleTextureFrameNumVar" );
  47. if( !pTextureFrameNumVarName )
  48. return false;
  49. m_TextureFrameNumVar = pMaterial->FindVar( pTextureFrameNumVarName, &foundVar, false );
  50. if( !foundVar )
  51. return false;
  52. m_WrapAnimation = !!pKeyValues->GetInt( "toggleShouldWrap", 1 );
  53. return true;
  54. }
  55. void CBaseToggleTextureProxy::Cleanup()
  56. {
  57. m_TextureVar = NULL;
  58. m_TextureFrameNumVar = NULL;
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Does the dirty deed
  62. //-----------------------------------------------------------------------------
  63. void CBaseToggleTextureProxy::OnBind( void *pC_BaseEntity )
  64. {
  65. assert ( m_TextureVar );
  66. if (!pC_BaseEntity)
  67. return;
  68. if( m_TextureVar->GetType() != MATERIAL_VAR_TYPE_TEXTURE )
  69. {
  70. return;
  71. }
  72. ITexture *pTexture = NULL;
  73. pTexture = m_TextureVar->GetTextureValue();
  74. if ( pTexture == NULL )
  75. return;
  76. C_BaseEntity *pEntity = BindArgToEntity( pC_BaseEntity );
  77. if ( pEntity == NULL )
  78. return;
  79. int numFrames = pTexture->GetNumAnimationFrames();
  80. int frame = pEntity->GetTextureFrameIndex();
  81. int intFrame = ((int)frame) % numFrames;
  82. if ( m_WrapAnimation == false )
  83. {
  84. if ( frame > numFrames )
  85. intFrame = numFrames;
  86. }
  87. m_TextureFrameNumVar->SetIntValue( intFrame );
  88. }
  89. IMaterial *CBaseToggleTextureProxy::GetMaterial()
  90. {
  91. return m_TextureFrameNumVar->GetOwningMaterial();
  92. }