summaryrefslogtreecommitdiff
path: root/tools/commedit/dmecommentarynodeentity.cpp
blob: 06ae8591d6e7ab235710ba461dce7f65b3bf3465 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "dmecommentarynodeentity.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "toolframework/itoolentity.h"
#include "materialsystem/imesh.h"
#include "materialsystem/imaterial.h"
#include "materialsystem/imaterialsystem.h"
#include "engine/iclientleafsystem.h"
#include "toolutils/enginetools_int.h"
#include "commedittool.h"
#include "KeyValues.h"

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


#define SPHERE_RADIUS 16

//-----------------------------------------------------------------------------
// Expose this class to the scene database 
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeCommentaryNodeEntity, CDmeCommentaryNodeEntity );


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CDmeCommentaryNodeEntity::OnConstruction()
{
	m_ClassName.InitAndSet( this, "classname", false, FATTRIB_HAS_CALLBACK );
	m_TargetName.Init( this, "targetname" );
	m_bIsPlaceholder.InitAndSet( this, "_placeholder", false, FATTRIB_DONTSAVE );
	m_vecLocalOrigin.Init( this, "origin" );
	m_vecLocalAngles.Init( this, "angles" );

	// Used to make sure these aren't saved if they aren't changed
	m_TargetName.GetAttribute()->AddFlag( FATTRIB_DONTSAVE | FATTRIB_HAS_CALLBACK );
	m_vecLocalAngles.GetAttribute()->AddFlag( FATTRIB_DONTSAVE | FATTRIB_HAS_CALLBACK );

	m_bInfoTarget = false;
	m_bIsDirty = false;
	m_hEngineEntity = HTOOLHANDLE_INVALID;

	KeyValues *pVMTKeyValues = new KeyValues( "UnlitGeneric" );
	pVMTKeyValues->SetString( "$basetexture", "editor/info_target" );
	pVMTKeyValues->SetInt( "$nocull", 1 );
	pVMTKeyValues->SetInt( "$vertexcolor", 1 );
	pVMTKeyValues->SetInt( "$vertexalpha", 1 );
	pVMTKeyValues->SetInt( "$no_fullbright", 1 );
	pVMTKeyValues->SetInt( "$translucent", 1 );
	m_InfoTargetSprite.Init( "__commentary_info_target", pVMTKeyValues );

	pVMTKeyValues = new KeyValues( "UnlitGeneric" );
	pVMTKeyValues->SetInt( "$nocull", 1 );
	pVMTKeyValues->SetString( "$color", "{255 0 0}" );
	pVMTKeyValues->SetInt( "$vertexalpha", 1 );
	pVMTKeyValues->SetInt( "$no_fullbright", 1 );
	pVMTKeyValues->SetInt( "$additive", 1 );
	m_SelectedInfoTarget.Init( "__selected_commentary_info_target", pVMTKeyValues );
}

void CDmeCommentaryNodeEntity::OnDestruction()
{
	// Unhook it from the engine
	AttachToEngineEntity( HTOOLHANDLE_INVALID );
	m_SelectedInfoTarget.Shutdown();
	m_InfoTargetSprite.Shutdown();
}


//-----------------------------------------------------------------------------
// Called whem attributes change
//-----------------------------------------------------------------------------
void CDmeCommentaryNodeEntity::OnAttributeChanged( CDmAttribute *pAttribute )
{
	BaseClass::OnAttributeChanged( pAttribute );

	// Once these have changed, then save them out, and don't bother calling back
	if ( pAttribute == m_TargetName.GetAttribute() ||
		 pAttribute == m_vecLocalAngles.GetAttribute() )
	{
		pAttribute->RemoveFlag( FATTRIB_DONTSAVE | FATTRIB_HAS_CALLBACK );
		return;
	}
	 
	if ( pAttribute == m_ClassName.GetAttribute() )
	{
		m_bInfoTarget = !Q_strncmp( m_ClassName, "info_target", 11 );
		if ( !Q_stricmp( m_ClassName, "point_commentary_node" ) )
		{
			SetModelName( "models/extras/info_speech.mdl" );
			GetMDL()->m_flPlaybackRate = 0.0f;
		}
		else
		{
			SetModelName( NULL );
		}
		return;
	}
}

	
//-----------------------------------------------------------------------------
// Returns the entity ID
//-----------------------------------------------------------------------------
int CDmeCommentaryNodeEntity::GetEntityId() const
{
	return atoi( GetName() );
}


//-----------------------------------------------------------------------------
// Mark the entity as being dirty
//-----------------------------------------------------------------------------
void CDmeCommentaryNodeEntity::MarkDirty( bool bDirty )
{
	m_bIsDirty = bDirty;
}


//-----------------------------------------------------------------------------
// Is the renderable transparent?
//-----------------------------------------------------------------------------
bool CDmeCommentaryNodeEntity::IsTransparent( void )
{
	return m_bIsDirty || m_bInfoTarget || BaseClass::IsTransparent();
}

	
//-----------------------------------------------------------------------------
// Entity Key iteration
//-----------------------------------------------------------------------------
bool CDmeCommentaryNodeEntity::IsEntityKey( CDmAttribute *pEntityKey )
{
	return pEntityKey->IsFlagSet( FATTRIB_USERDEFINED );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CDmAttribute *CDmeCommentaryNodeEntity::FirstEntityKey()
{
	for ( CDmAttribute *pAttribute = FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
	{
		if ( IsEntityKey( pAttribute ) )
			return pAttribute;
	}
	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CDmAttribute *CDmeCommentaryNodeEntity::NextEntityKey( CDmAttribute *pEntityKey )
{
	if ( !pEntityKey )
		return NULL;

	for ( CDmAttribute *pAttribute = pEntityKey->NextAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
	{
		if ( IsEntityKey( pAttribute ) )
			return pAttribute;
	}
	return NULL;
}

//-----------------------------------------------------------------------------
// Attach/detach from an engine entity with the same editor index
//-----------------------------------------------------------------------------
void CDmeCommentaryNodeEntity::AttachToEngineEntity( HTOOLHANDLE hToolHandle )
{
	if ( m_hEngineEntity != HTOOLHANDLE_INVALID )
	{
		clienttools->SetEnabled( m_hEngineEntity, true );
	}
	m_hEngineEntity = hToolHandle;
	if ( m_hEngineEntity != HTOOLHANDLE_INVALID )
	{
		clienttools->SetEnabled( m_hEngineEntity, false );
	}
}


//-----------------------------------------------------------------------------
// Position and bounds for the model
//-----------------------------------------------------------------------------
const Vector &CDmeCommentaryNodeEntity::GetRenderOrigin( void )
{
	return m_vecLocalOrigin;
}

const QAngle &CDmeCommentaryNodeEntity::GetRenderAngles( void )
{
	return *(QAngle*)(&m_vecLocalAngles.Get());
}


//-----------------------------------------------------------------------------
// Draws the helper for the entity
//-----------------------------------------------------------------------------
void CDmeCommentaryNodeEntity::DrawSprite( IMaterial *pMaterial )
{
	float t = 0.5f * sin( Plat_FloatTime() * M_PI / 1.0f ) + 0.5f;

	CMatRenderContextPtr pRenderContext( materials );
	pRenderContext->Bind( pMaterial );
	IMesh* pMesh = pRenderContext->GetDynamicMesh();

	CMeshBuilder meshBuilder;
	meshBuilder.Begin( pMesh, MATERIAL_TRIANGLE_STRIP, 4, 4 );

	unsigned char nBaseR = 255;
	unsigned char nBaseG = 255;
	unsigned char nBaseB = 255;
	unsigned char nAlpha = m_bIsDirty ? (unsigned char)(255 * t) : 255;

	meshBuilder.Position3f( -SPHERE_RADIUS, -SPHERE_RADIUS, 0.0f );
	meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
	meshBuilder.TexCoord2f( 0, 0.0f, 1.0f );
	meshBuilder.AdvanceVertex();

	meshBuilder.Position3f( SPHERE_RADIUS, -SPHERE_RADIUS, 0.0f );
	meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
	meshBuilder.TexCoord2f( 0, 1.0f, 1.0f );
	meshBuilder.AdvanceVertex();

	meshBuilder.Position3f( SPHERE_RADIUS, SPHERE_RADIUS, 0.0f );
	meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
	meshBuilder.TexCoord2f( 0, 1.0f, 0.0f );
	meshBuilder.AdvanceVertex();

	meshBuilder.Position3f( -SPHERE_RADIUS, SPHERE_RADIUS, 0.0f );
	meshBuilder.Color4ub( nBaseR, nBaseG, nBaseB, nAlpha );
	meshBuilder.TexCoord2f( 0, 0.0f, 0.0f );
	meshBuilder.AdvanceVertex();

	meshBuilder.FastIndex( 0 );
	meshBuilder.FastIndex( 1 );
	meshBuilder.FastIndex( 3 );
	meshBuilder.FastIndex( 2 );

	meshBuilder.End();
	pMesh->Draw();
}


//-----------------------------------------------------------------------------
// Draws the helper for the entity
//-----------------------------------------------------------------------------
int CDmeCommentaryNodeEntity::DrawModel( int flags )
{  
	bool bSelected = ( g_pCommEditTool->GetCurrentEntity().Get() == this );
	if ( !m_bInfoTarget )
	{
		// If we have a visible engine entity, we don't need to draw it here
		// info targets always draw though, because they have no visible model.
		CDisableUndoScopeGuard guard;
		float t = 0.5f * sin( Plat_FloatTime() * M_PI / 1.0f ) + 0.5f;
		unsigned char nAlpha = m_bIsDirty ? (unsigned char)(255 * t) : 255;
		if ( bSelected )
		{
			GetMDL()->m_Color.SetColor( 255, 64, 64, nAlpha );
		}
		else
		{
			GetMDL()->m_Color.SetColor( 255, 255, 255, nAlpha );
		}
		return BaseClass::DrawModel( flags );
	}

	Assert( IsDrawingInEngine() );

	CMatRenderContextPtr pRenderContext( materials );
	matrix3x4_t mat;
	VMatrix worldToCamera, cameraToWorld;
	pRenderContext->GetMatrix( MATERIAL_VIEW, &worldToCamera );
	MatrixInverseTR( worldToCamera, cameraToWorld );
	MatrixCopy( cameraToWorld.As3x4(), mat );
	MatrixSetColumn( m_vecLocalOrigin, 3, mat );

	pRenderContext->MatrixMode( MATERIAL_MODEL );
	pRenderContext->PushMatrix();
	pRenderContext->LoadMatrix( mat );

	pRenderContext->FogMode( MATERIAL_FOG_NONE );
	pRenderContext->SetNumBoneWeights( 0 );
	pRenderContext->CullMode( MATERIAL_CULLMODE_CW );

	DrawSprite( m_InfoTargetSprite );
	if ( bSelected )
	{
		DrawSprite( m_SelectedInfoTarget );
	}

	pRenderContext->CullMode( MATERIAL_CULLMODE_CCW );
	pRenderContext->MatrixMode( MATERIAL_MODEL );
	pRenderContext->PopMatrix();

	return 1; 
}


//-----------------------------------------------------------------------------
// Position and bounds for the model
//-----------------------------------------------------------------------------
void CDmeCommentaryNodeEntity::GetRenderBounds( Vector& mins, Vector& maxs )
{ 
	if ( !m_bInfoTarget )
	{
		BaseClass::GetRenderBounds( mins, maxs );
		return;
	}

	mins.Init( -SPHERE_RADIUS, -SPHERE_RADIUS, -SPHERE_RADIUS );
	maxs.Init( SPHERE_RADIUS, SPHERE_RADIUS, SPHERE_RADIUS );
}


//-----------------------------------------------------------------------------
// Update renderable position
//-----------------------------------------------------------------------------
void CDmeCommentaryNodeEntity::SetRenderOrigin( const Vector &vecOrigin )
{
	m_vecLocalOrigin = vecOrigin;
	clienttools->MarkClientRenderableDirty( this );
}

void CDmeCommentaryNodeEntity::SetRenderAngles( const QAngle &angles )
{
	m_vecLocalAngles = *(Vector*)&angles;
	clienttools->MarkClientRenderableDirty( this );
}