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.

88 lines
2.8 KiB

  1. //========= Copyright � Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Provide interface to custom materials for entities that use them
  4. //
  5. //=============================================================================//
  6. #pragma once
  7. #include "refcount.h"
  8. #include "materialsystem/icustommaterial.h"
  9. class IMaterial;
  10. class CCustomMaterialOwner;
  11. class CCompositeTexture;
  12. // classes that want to have a custom material should derive from this
  13. class CCustomMaterialOwner
  14. {
  15. public:
  16. virtual ~CCustomMaterialOwner() { ClearCustomMaterials( true ); }
  17. ICustomMaterial *GetCustomMaterial( int nIndex = 0 ) const; // returns NULL if the index is out of range
  18. virtual void SetCustomMaterial( ICustomMaterial* pCustomMaterial, int nIndex = 0 ); // either replaces and existing material (releasing the old one), or adds one to the vector
  19. bool IsCustomMaterialValid( int nIndex = 0 ) const;
  20. int GetCustomMaterialCount() const { return m_ppCustomMaterials.Count(); }
  21. void ClearCustomMaterials( bool bPurge = false );
  22. virtual void OnCustomMaterialsUpdated() {}
  23. virtual void DuplicateCustomMaterialsToOther( CCustomMaterialOwner *pOther ) const;
  24. private:
  25. // Pointers to custom materials owned by the mat system for this entity. Index
  26. // in this vector corresponds to the model material index to override with the custom material.
  27. CUtlVector< ICustomMaterial* > m_ppCustomMaterials;
  28. };
  29. inline ICustomMaterial *CCustomMaterialOwner::GetCustomMaterial( int nIndex ) const
  30. {
  31. return ( m_ppCustomMaterials.Count() > nIndex ) ? m_ppCustomMaterials[ nIndex ] : NULL;
  32. }
  33. inline void CCustomMaterialOwner::SetCustomMaterial( ICustomMaterial* pCustomMaterial, int nIndex )
  34. {
  35. while ( m_ppCustomMaterials.Count() <= nIndex )
  36. {
  37. m_ppCustomMaterials.AddToTail( NULL );
  38. }
  39. pCustomMaterial->AddRef();
  40. if ( m_ppCustomMaterials[ nIndex ] )
  41. m_ppCustomMaterials[ nIndex ]->Release();
  42. m_ppCustomMaterials[ nIndex ] = pCustomMaterial;
  43. }
  44. inline bool CCustomMaterialOwner::IsCustomMaterialValid( int nIndex ) const
  45. {
  46. return ( m_ppCustomMaterials.Count() > nIndex && m_ppCustomMaterials[ nIndex ] != NULL ) ? m_ppCustomMaterials[ nIndex ]->IsValid() : false;
  47. }
  48. inline void CCustomMaterialOwner::ClearCustomMaterials( bool bPurge )
  49. {
  50. for ( int i = 0; i < m_ppCustomMaterials.Count(); i++ )
  51. {
  52. if ( m_ppCustomMaterials[ i ] != NULL )
  53. {
  54. m_ppCustomMaterials[ i ]->Release();
  55. m_ppCustomMaterials[ i ] = NULL;
  56. }
  57. }
  58. if ( bPurge )
  59. {
  60. m_ppCustomMaterials.Purge();
  61. }
  62. else
  63. {
  64. m_ppCustomMaterials.RemoveAll();
  65. }
  66. }
  67. inline void CCustomMaterialOwner::DuplicateCustomMaterialsToOther( CCustomMaterialOwner *pOther ) const
  68. {
  69. pOther->ClearCustomMaterials();
  70. for ( int i = 0; i < CCustomMaterialOwner::m_ppCustomMaterials.Count(); i++ )
  71. {
  72. if ( m_ppCustomMaterials[ i ] == NULL )
  73. continue;
  74. pOther->SetCustomMaterial( m_ppCustomMaterials[ i ], i );
  75. }
  76. }