aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/toggletextureproxy.cpp
blob: 77eea576eb123b5c4b38e935d7edcb759333715e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "toggletextureproxy.h"
#include "materialsystem/imaterial.h"
#include "materialsystem/imaterialvar.h"
#include "materialsystem/itexture.h"
#include <KeyValues.h>
#include "functionproxy.h"
#include "toolframework_client.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

// forward declarations
void ToolFramework_RecordMaterialParams( IMaterial *pMaterial );

EXPOSE_INTERFACE( CBaseToggleTextureProxy, IMaterialProxy, "ToggleTexture" IMATERIAL_PROXY_INTERFACE_VERSION );

//-----------------------------------------------------------------------------
// Constructor, destructor: 
//-----------------------------------------------------------------------------

CBaseToggleTextureProxy::CBaseToggleTextureProxy()
{
	Cleanup();
}

CBaseToggleTextureProxy::~CBaseToggleTextureProxy()
{
	Cleanup();
}

C_BaseEntity *CBaseToggleTextureProxy::BindArgToEntity( void *pArg )
{
	IClientRenderable *pRend = (IClientRenderable *)pArg;
	return pRend->GetIClientUnknown()->GetBaseEntity();
}

//-----------------------------------------------------------------------------
// Initialization, shutdown
//-----------------------------------------------------------------------------
bool CBaseToggleTextureProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
	char const* pTextureVarName = pKeyValues->GetString( "toggleTextureVar" );
	if( !pTextureVarName )
		return false;

	bool foundVar;
	m_TextureVar = pMaterial->FindVar( pTextureVarName, &foundVar, false );
	if( !foundVar )
		return false;

	char const* pTextureFrameNumVarName = pKeyValues->GetString( "toggleTextureFrameNumVar" );
	if( !pTextureFrameNumVarName )
		return false;

	m_TextureFrameNumVar = pMaterial->FindVar( pTextureFrameNumVarName, &foundVar, false );
	if( !foundVar )
		return false;
	
	m_WrapAnimation = !!pKeyValues->GetInt( "toggleShouldWrap", 1 );
	return true;
}

void CBaseToggleTextureProxy::Cleanup()
{
	m_TextureVar = NULL;
	m_TextureFrameNumVar = NULL;
}


//-----------------------------------------------------------------------------
// Does the dirty deed
//-----------------------------------------------------------------------------
void CBaseToggleTextureProxy::OnBind( void *pC_BaseEntity )
{
	assert ( m_TextureVar );

	if (!pC_BaseEntity)
		return;

	if( m_TextureVar->GetType() != MATERIAL_VAR_TYPE_TEXTURE )
	{
		return;
	}

	ITexture *pTexture = NULL;

	pTexture = m_TextureVar->GetTextureValue();

	if ( pTexture == NULL )
		 return;

	C_BaseEntity *pEntity = BindArgToEntity( pC_BaseEntity );

	if ( pEntity == NULL )
		 return;
	
	int numFrames = pTexture->GetNumAnimationFrames();
	int frame = pEntity->GetTextureFrameIndex();

	int intFrame = ((int)frame) % numFrames; 

	if ( m_WrapAnimation == false )
	{
		if ( frame > numFrames )
			 intFrame = numFrames;
	}
		
	m_TextureFrameNumVar->SetIntValue( intFrame );

	if ( ToolsEnabled() )
	{
		ToolFramework_RecordMaterialParams( GetMaterial() );
	}
}

IMaterial *CBaseToggleTextureProxy::GetMaterial()
{
	return m_TextureFrameNumVar->GetOwningMaterial();
}