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.

126 lines
3.2 KiB

  1. //========= Copyright 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 "toolframework_client.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. // forward declarations
  18. void ToolFramework_RecordMaterialParams( IMaterial *pMaterial );
  19. EXPOSE_INTERFACE( CBaseToggleTextureProxy, IMaterialProxy, "ToggleTexture" IMATERIAL_PROXY_INTERFACE_VERSION );
  20. //-----------------------------------------------------------------------------
  21. // Constructor, destructor:
  22. //-----------------------------------------------------------------------------
  23. CBaseToggleTextureProxy::CBaseToggleTextureProxy()
  24. {
  25. Cleanup();
  26. }
  27. CBaseToggleTextureProxy::~CBaseToggleTextureProxy()
  28. {
  29. Cleanup();
  30. }
  31. C_BaseEntity *CBaseToggleTextureProxy::BindArgToEntity( void *pArg )
  32. {
  33. IClientRenderable *pRend = (IClientRenderable *)pArg;
  34. return pRend->GetIClientUnknown()->GetBaseEntity();
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Initialization, shutdown
  38. //-----------------------------------------------------------------------------
  39. bool CBaseToggleTextureProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  40. {
  41. char const* pTextureVarName = pKeyValues->GetString( "toggleTextureVar" );
  42. if( !pTextureVarName )
  43. return false;
  44. bool foundVar;
  45. m_TextureVar = pMaterial->FindVar( pTextureVarName, &foundVar, false );
  46. if( !foundVar )
  47. return false;
  48. char const* pTextureFrameNumVarName = pKeyValues->GetString( "toggleTextureFrameNumVar" );
  49. if( !pTextureFrameNumVarName )
  50. return false;
  51. m_TextureFrameNumVar = pMaterial->FindVar( pTextureFrameNumVarName, &foundVar, false );
  52. if( !foundVar )
  53. return false;
  54. m_WrapAnimation = !!pKeyValues->GetInt( "toggleShouldWrap", 1 );
  55. return true;
  56. }
  57. void CBaseToggleTextureProxy::Cleanup()
  58. {
  59. m_TextureVar = NULL;
  60. m_TextureFrameNumVar = NULL;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Does the dirty deed
  64. //-----------------------------------------------------------------------------
  65. void CBaseToggleTextureProxy::OnBind( void *pC_BaseEntity )
  66. {
  67. assert ( m_TextureVar );
  68. if (!pC_BaseEntity)
  69. return;
  70. if( m_TextureVar->GetType() != MATERIAL_VAR_TYPE_TEXTURE )
  71. {
  72. return;
  73. }
  74. ITexture *pTexture = NULL;
  75. pTexture = m_TextureVar->GetTextureValue();
  76. if ( pTexture == NULL )
  77. return;
  78. C_BaseEntity *pEntity = BindArgToEntity( pC_BaseEntity );
  79. if ( pEntity == NULL )
  80. return;
  81. int numFrames = pTexture->GetNumAnimationFrames();
  82. int frame = pEntity->GetTextureFrameIndex();
  83. int intFrame = ((int)frame) % numFrames;
  84. if ( m_WrapAnimation == false )
  85. {
  86. if ( frame > numFrames )
  87. intFrame = numFrames;
  88. }
  89. m_TextureFrameNumVar->SetIntValue( intFrame );
  90. if ( ToolsEnabled() )
  91. {
  92. ToolFramework_RecordMaterialParams( GetMaterial() );
  93. }
  94. }
  95. IMaterial *CBaseToggleTextureProxy::GetMaterial()
  96. {
  97. return m_TextureFrameNumVar->GetOwningMaterial();
  98. }