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.

106 lines
2.1 KiB

  1. //========= Copyright � 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Material proxy to stuff a convar into a material var.
  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 "convar.h"
  11. #include "materialsystem/imaterialproxy.h"
  12. #include "materialsystem/imaterialvar.h"
  13. #include "imaterialproxydict.h"
  14. // NOTE: This has to be the last file included!
  15. #include "tier0/memdbgon.h"
  16. class CConVarMaterialProxy: public IMaterialProxy
  17. {
  18. public:
  19. CConVarMaterialProxy()
  20. : m_pResult( NULL ),
  21. m_conVarRef( "", true )
  22. {
  23. }
  24. virtual ~CConVarMaterialProxy()
  25. {
  26. }
  27. virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  28. {
  29. const char *pResult = pKeyValues->GetString( "resultVar" );
  30. if ( !pResult )
  31. return false;
  32. bool found;
  33. m_pResult = pMaterial->FindVar( pResult, &found );
  34. if ( !found )
  35. {
  36. m_pResult = NULL;
  37. return false;
  38. }
  39. if ( !Q_stricmp( pResult, "$alpha" ) )
  40. {
  41. pMaterial->SetMaterialVarFlag( MATERIAL_VAR_ALPHA_MODIFIED_BY_PROXY, true );
  42. }
  43. pResult = pKeyValues->GetString( "convar" );
  44. if( !pResult )
  45. {
  46. return false;
  47. }
  48. m_conVarRef.Init( pResult, false );
  49. if ( !m_conVarRef.IsValid() )
  50. {
  51. return false;
  52. }
  53. return true;
  54. }
  55. virtual void OnBind( void* )
  56. {
  57. switch( m_pResult->GetType() )
  58. {
  59. case MATERIAL_VAR_TYPE_VECTOR:
  60. {
  61. float f = m_conVarRef.GetFloat();
  62. Vector4D vec( f, f, f, f );
  63. m_pResult->SetVecValue( vec.Base(), m_pResult->VectorSize() );
  64. }
  65. break;
  66. case MATERIAL_VAR_TYPE_INT:
  67. m_pResult->SetIntValue( m_conVarRef.GetInt() );
  68. break;
  69. case MATERIAL_VAR_TYPE_FLOAT:
  70. default:
  71. m_pResult->SetFloatValue( m_conVarRef.GetFloat() );
  72. break;
  73. }
  74. }
  75. virtual IMaterial *GetMaterial()
  76. {
  77. return m_pResult->GetOwningMaterial();
  78. }
  79. virtual void Release()
  80. {
  81. delete this;
  82. }
  83. protected:
  84. IMaterialVar *m_pResult;
  85. ConVarRef m_conVarRef;
  86. };
  87. EXPOSE_MATERIAL_PROXY( CConVarMaterialProxy, ConVar );