//-----------------------------------------------------------------------------
// Purpose: Load any materials that reference this texture. Used so we can refresh a 
// material's preview image if a relevant .vtf changes.
//-----------------------------------------------------------------------------
void CTextureSystem::ReloadMaterialsUsingTexture( ITexture *pTestTexture )
{
	for ( int i=0; i < m_Textures.Count(); i++ )
	{
		IEditorTexture *pEditorTex = m_Textures[i];
		IMaterial *pMat = pEditorTex->GetMaterial( false );
		if ( !pMat )
			continue;
		
		IMaterialVar **pParams = pMat->GetShaderParams();
		int nParams = pMat->ShaderParamCount();
		for ( int iParam=0; iParam < nParams; iParam++ )
		{
			if ( pParams[iParam]->GetType() != MATERIAL_VAR_TYPE_TEXTURE )
				continue;
			
			ITexture *pTex = pParams[iParam]->GetTextureValue();
			if ( !pTex )
				continue;
			
			if ( pTex == pTestTexture )
			{
				pEditorTex->Reload( true );
			}
		}
	}
}
void CTextureSystem::OnFileChange( const char *pFilename, int context, CTextureSystem::EFileType eFileType )
{
	// It requires the forward slashes later...
	char fixedSlashes[MAX_PATH];
	V_StrSubst( pFilename, "\\", "/", fixedSlashes, sizeof( fixedSlashes ) );	

	// Get rid of the extension.
	if ( V_strlen( fixedSlashes ) < 5 )
	{
		Assert( false );
		return;
	}
	fixedSlashes[ V_strlen( fixedSlashes ) - 4 ] = 0;


	// Handle it based on what type of file we've got.
	if ( eFileType == k_eFileTypeVMT )
	{
		IEditorTexture *pTex = FindActiveTexture( fixedSlashes, NULL, FALSE );
		if ( pTex )
		{
			pTex->Reload( true );
		}
		else
		{
			EnumMaterial( fixedSlashes, context );
			IEditorTexture *pTex = FindActiveTexture( fixedSlashes, NULL, FALSE );
			if ( pTex )
			{
				GetMainWnd()->m_TextureBar.NotifyNewMaterial( pTex );
				GetMainWnd()->GetFaceEditSheet()->NotifyNewMaterial( pTex );
			}
		}
	}
	else if ( eFileType == k_eFileTypeVTF )
	{
		// Whether a VTF was added, removed, or modified, we do the same thing.. refresh it and any materials that reference it.
		ITexture *pTexture = materials->FindTexture( fixedSlashes, TEXTURE_GROUP_UNACCOUNTED, false );
		if ( pTexture )
		{
			pTexture->Download( NULL );
			ReloadMaterialsUsingTexture( pTexture );
		}
	}
}