aboutsummaryrefslogtreecommitdiff
path: root/sp/src/game/client/hud_animationinfo.cpp
blob: 27949987d1dd30d62028784c797a237fbb8070b7 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "iclientmode.h"
#include <KeyValues.h>
#include <vgui/ISurface.h>
#include <vgui/IScheme.h>
#include <vgui/ILocalize.h>
#include <vgui_controls/AnimationController.h>
#include <vgui_controls/Panel.h>
#include <vgui/IPanel.h>
#include "vgui_int.h"

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

using namespace vgui;

#define ANIM_INFO_START_Y 50
#define ANIM_INFO_WIDTH XRES( 300 )

class CHudAnimationInfo : public CHudElement, public vgui::Panel
{
	DECLARE_CLASS_SIMPLE( CHudAnimationInfo, vgui::Panel );

public:

	CHudAnimationInfo( const char *pElementName );

	virtual bool ShouldDraw();

	// vgui::Panel overrides.
	virtual void Paint( void );

	virtual void ApplySchemeSettings( IScheme *scheme );

	void SetWatch( Panel *element )
	{
		m_pWatch = element;
	}

protected:

	void PaintMappingInfo( int& x, int& y, Panel *element, PanelAnimationMap *map );
	void PaintString( int& x, int &y, const char *sz, Color *pLegendColor );

	CPanelAnimationVar( vgui::HFont, m_LabelFont, "LabelFont", "DebugFixed" );
	CPanelAnimationVar( vgui::HFont, m_ItemFont, "ItemFont", "DebugFixedSmall" );

	CPanelAnimationVar( Color, m_LabelColor, "LabelColor", "DebugLabel" );
	CPanelAnimationVar( Color, m_ItemColor, "ItemColor", "DebugText");

	Panel *m_pWatch;
};

DECLARE_HUDELEMENT( CHudAnimationInfo );

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pElementName - 
//			*panelName - 
//-----------------------------------------------------------------------------
CHudAnimationInfo::CHudAnimationInfo( const char *pElementName )
 : CHudElement( pElementName ), BaseClass( NULL, "HudAnimationInfo" )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	SetActive( true );

	m_pWatch = NULL;

	// Make sure we render on top of other hud elements since we are debugging info for them.
	SetZPos( 100 );
}

void CHudAnimationInfo::ApplySchemeSettings( IScheme *scheme )
{
	BaseClass::ApplySchemeSettings( scheme );

	m_LabelFont = scheme->GetFont( "DebugFixed", true );
	m_ItemFont= scheme->GetFont( "DebugFixedSmall", true );

	m_LabelColor = scheme->GetColor( "DebugLabel", GetFgColor() );
	m_ItemColor= scheme->GetColor( "DebugText", GetFgColor() );

	SetPaintBackgroundEnabled( false );
}

bool CHudAnimationInfo::ShouldDraw()
{
	return ( m_pWatch && CHudElement::ShouldDraw() );
}

void CHudAnimationInfo::PaintString( int& x, int &y, const char *sz, Color *pLegendColor )
{
	surface()->DrawSetTextFont( m_ItemFont );
	surface()->DrawSetTextPos( x, y );

	wchar_t szconverted[ 512 ];

	g_pVGuiLocalize->ConvertANSIToUnicode( "O->", szconverted, sizeof(szconverted)  );
		
	if ( pLegendColor )
	{
		surface()->DrawSetTextColor( *pLegendColor );
	}
	else
	{
		surface()->DrawSetTextColor( Color( 0, 0, 0, 0 ) );
	}

	surface()->DrawPrintText( szconverted, wcslen( szconverted ) );

	g_pVGuiLocalize->ConvertANSIToUnicode( sz, szconverted, sizeof(szconverted)  );

	surface()->DrawSetTextColor( m_ItemColor );
	surface()->DrawPrintText( szconverted, wcslen( szconverted ) );

	int fontHeight = surface()->GetFontTall( m_ItemFont );

	y += fontHeight;

	if ( y + fontHeight >= ScreenHeight() )
	{
		y = ANIM_INFO_START_Y;
		x += ANIM_INFO_WIDTH;
	}
}

void CHudAnimationInfo::PaintMappingInfo( int& x, int& y, Panel *element, PanelAnimationMap *map )
{
	if ( !map )
		return;

	// Draw label
	surface()->DrawSetTextFont( m_LabelFont );
	surface()->DrawSetTextColor( m_LabelColor );
	surface()->DrawSetTextPos( x, y );

	const char *className = "";
	if ( map->pfnClassName )
	{
		className = (*map->pfnClassName)();
	}

	const char *p = className;
	while ( *p )
	{
		surface()->DrawUnicodeChar( *p );
		p++;
	}

	y += surface()->GetFontTall( m_LabelFont ) + 1;

	x += 10;


	int c = map->entries.Count();
	for ( int i = 0; i < c; i++ )
	{
		PanelAnimationMapEntry *e = &map->entries[ i ];

		char sz[ 512 ];
		char value[ 256 ];

		Color col( 0, 0, 0, 0 );
		Color  *pColor = NULL;
		KeyValues *kv = new KeyValues( e->name() );
		if ( element->RequestInfo( kv ) )
		{
			KeyValues *dat = kv->FindKey(e->name());
			if ( dat && dat->GetDataType() == KeyValues::TYPE_COLOR )
			{
				col = dat->GetColor();
				Q_snprintf( value, sizeof( value ), "%i, %i, %i, %i",
					col[0], col[1], col[2], col[3] );
				pColor = &col;
			}
			else
			{
				Q_snprintf( value, sizeof( value ), "%s",
					dat->GetString() );
			}
		}
		else
		{
			Q_strncpy( value, "???", sizeof( value ) );
		}

		Q_snprintf( sz, sizeof( sz ), "%-30s %-20s (%s)",
			e->name(), e->type(), value );

		kv->deleteThis();

		PaintString( x, y, sz, pColor );
	}

	x -= 10;

	if ( map->baseMap )
	{
		PaintMappingInfo( x, y, element, map->baseMap );
	}
}

void CHudAnimationInfo::Paint()
{
	vgui::Panel *panel = m_pWatch;
	if ( !panel )
		return;

	// See if it has any animation info registered
	PanelAnimationMap *map = panel->GetAnimMap();
	if ( !map )
		return;

	int x = 15;
	int y = ANIM_INFO_START_Y;

	PaintMappingInfo( x, y, panel, map );

	x += 10;

	// Paint panel bounds
	int bounds[4];
	panel->GetBounds( bounds[0], bounds[1], bounds[2], bounds[3] );
	char sz[ 256 ];
	Q_snprintf( sz, sizeof( sz ), "%-30s %-20s (%i %i)", "Position", "pos", bounds[0], bounds[1] );
	PaintString( x, y, sz, NULL );
	Q_snprintf( sz, sizeof( sz ), "%-30s %-20s (%i %i)", "Size", "size", bounds[2], bounds[3] );
	PaintString( x, y, sz, NULL );
}


static int HudElementCompletion( const char *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
{
	const char *cmdname = "cl_animationinfo";

	char *substring = (char *)partial;
	if ( Q_strstr( partial, cmdname ) )
	{
		substring = (char *)partial + strlen( cmdname ) + 1;
	}

	int current = 0;

	int c = gHUD.m_HudList.Count();
	int i;
	for ( i = 0; i < c; i++ )
	{
		CHudElement *e = gHUD.m_HudList[ i ];
		if ( !e )
			continue;

		bool add = false;

		// Insert into lookup
		if ( substring[0] )
		{
			if ( !Q_strncasecmp( e->GetName(), substring, strlen( substring ) ) )
			{
				add = true;
			}
		}
		else
		{
			add = true;
		}

		if ( add )
		{
			Q_snprintf( commands[ current ], sizeof( commands[ current ] ), "%s %s", cmdname, e->GetName() );
			current++;
		}
	}

	return current;
}

CON_COMMAND_F_COMPLETION( cl_animationinfo, "Hud element to examine.", 0, HudElementCompletion )
{
	CHudAnimationInfo *info = GET_HUDELEMENT( CHudAnimationInfo );
	Assert( info );
	if ( !info )
		return;

	if ( args.ArgC() != 2 )
	{
		info->SetWatch( NULL );
		return;
	}

	// Find it
	CHudElement *element = NULL;
	
	for ( int i = 0; i < gHUD.m_HudList.Size(); i++ )
	{
		if ( stricmp( gHUD.m_HudList[i]->GetName(), args[1]  ) == 0 )
		{
			element = gHUD.m_HudList[i];
			break;
		}
	}

	if ( element )
	{
		info->SetWatch( dynamic_cast< Panel * >( element ) );
	}
	else 
	{
		VPANEL root = VGui_GetClientDLLRootPanel();
		vgui::Panel *rootPanel = ipanel()->GetPanel( root, info->GetModuleName() );
		Panel *panel = NULL;
		if ( rootPanel )
		{
			panel = rootPanel->FindChildByName( args[1], true );
		}

		if ( panel )
		{
			info->SetWatch( panel );
		}
		else
		{
			Msg( "No such element %s\n", args[1] );
		}
	}
}