aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/vgui_schemevisualizer.cpp
blob: b2a6f7741cd02490ba3356f3aa984a40fa7a1339 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//=======================================================================================//

#include "cbase.h"
#include "vgui_schemevisualizer.h"
#include "vgui/IBorder.h"
#include "vgui/ISurface.h"
#include "vgui/IVGui.h"
#include "vgui_controls/Label.h"
#include "vgui_controls/ComboBox.h"
#include "KeyValues.h"
#include "fmtstr.h"

//----------------------------------------------------------------------------------------

using namespace vgui;

//----------------------------------------------------------------------------------------

class CBorderVisualizerPanel : public Panel
{
	DECLARE_CLASS_SIMPLE( CBorderVisualizerPanel, Panel );
public:
	CBorderVisualizerPanel( Panel *pParent, const char *pName, IBorder *pBorder );

private:
	virtual void Paint();

	IBorder *m_pBorder;
};

//----------------------------------------------------------------------------------------

CBorderVisualizerPanel::CBorderVisualizerPanel( Panel *pParent, const char *pName, IBorder *pBorder )
:	Panel( pParent, pName ),
	m_pBorder( pBorder )
{
	SetBgColor( Color( 255, 0, 0, 255 ) );
}

void CBorderVisualizerPanel::Paint()
{
	BaseClass::Paint();

	surface()->PushMakeCurrent( GetVPanel(), false );
	m_pBorder->Paint( GetVPanel() );
	surface()->PopMakeCurrent( GetVPanel() );
}

//----------------------------------------------------------------------------------------

class CColorVisualizerPanel : public Panel
{
	DECLARE_CLASS_SIMPLE( CColorVisualizerPanel, Panel );
public:
	CColorVisualizerPanel( Panel *pParent, const char *pName, const Color &color );

private:
	virtual void Paint();

	Color m_Color;
};

//----------------------------------------------------------------------------------------

CColorVisualizerPanel::CColorVisualizerPanel( Panel *pParent, const char *pName, const Color &color )
:	Panel( pParent, pName ),
	m_Color( color )
{
}

void CColorVisualizerPanel::Paint()
{
	BaseClass::Paint();

	int nHorzBuffer = XRES( 2 );
	int nVertBuffer = YRES( 2 );

	surface()->DrawSetColor( m_Color );
	surface()->DrawFilledRect( nHorzBuffer, nVertBuffer, GetWide() - 2 * nHorzBuffer, GetTall() - 2 * nVertBuffer );
}

//----------------------------------------------------------------------------------------

CSchemeVisualizer::CSchemeVisualizer( vgui::Panel *pParent, IScheme *pViewScheme, const char *pSchemeName )
:	vgui::Frame( pParent, "SchemeVisualizer" ),
	m_pViewScheme( pViewScheme ),
	m_pList( NULL ),
	m_nListDataType( LISTDATATYPE_INVALID )
{
	CFmtStr fmtTitle( "Scheme Visualizer - scheme: \"%s\"", pSchemeName );
	SetTitle( fmtTitle.Access(), true );
	SetTitleBarVisible( true );
	SetMoveable( true );
	SetCloseButtonVisible( true );
	SetMinimizeButtonVisible( false );
	SetMaximizeButtonVisible( false );

	m_pListDataTypeCombo = new ComboBox( this, "DataTypeCombo", 3, false );
	m_aComboDataTypeToItemIDMap[ LISTDATATYPE_BORDERS ] = m_pListDataTypeCombo->AddItem( "Borders", NULL );
	m_aComboDataTypeToItemIDMap[ LISTDATATYPE_FONTS ] = m_pListDataTypeCombo->AddItem( "Fonts", NULL );
	m_aComboDataTypeToItemIDMap[ LISTDATATYPE_COLORS ] = m_pListDataTypeCombo->AddItem( "Colors", NULL );
	m_pListDataTypeCombo->SilentActivateItemByRow( 0 );
	m_pListDataTypeCombo->AddActionSignalTarget( this );

	m_nSelectedComboItem = m_aComboDataTypeToItemIDMap[ LISTDATATYPE_BORDERS ];

	UpdateList( LISTDATATYPE_BORDERS );

	ivgui()->AddTickSignal( GetVPanel(), 100 );
}

CSchemeVisualizer::~CSchemeVisualizer()
{
	ivgui()->RemoveTickSignal( GetVPanel() );
}

void CSchemeVisualizer::PerformLayout()
{
	BaseClass::PerformLayout();

	const int nComboWidth = XRES( 50 );
	m_pListDataTypeCombo->SetBounds(
		GetWide() - nComboWidth - XRES( 10 ),
		YRES( 2 ),
		nComboWidth,
		YRES( 8 )
	);

	const int nHorzBuffer = XRES( 2 );
	const int nVertBuffer = YRES( 10 );
	m_pList->SetBounds( nHorzBuffer, nVertBuffer, GetWide() - 2 * nHorzBuffer, GetTall() - 1.5f * nVertBuffer );
}

void CSchemeVisualizer::UpdateList( ListDataType_t nType )
{
	Assert( nType != m_nListDataType );

	// Cache off type
	m_nListDataType = nType;

	// Clear the list
	if ( m_pList )
	{
		m_pList->MarkForDeletion();
	}
	m_pList = new PanelListPanel( this, "ListPanel" );
	m_pList->SetBgColor( Color( 0, 255, 0, 255 ) );
	m_pList->SetPaintBackgroundEnabled( true );
	InvalidateLayout( true, false );

	// Set default column width - may be changed depending on type
	m_pList->SetFirstColumnWidth( XRES( 50 ) );

	switch( nType )
	{
	case LISTDATATYPE_BORDERS:	AddBordersToList(); break;
	case LISTDATATYPE_FONTS:	AddFontsToList(); break;
	case LISTDATATYPE_COLORS:	AddColorsToList(); break;
	}
}

void CSchemeVisualizer::AddBordersToList()
{
	const int nBorderCount = m_pViewScheme->GetBorderCount();
	for ( int i = 0; i < nBorderCount; ++i )
	{
		IBorder *pCurBorder = m_pViewScheme->GetBorderAtIndex( i );
		CFmtStr fmtName( "BorderPanel_%s", pCurBorder->GetName() );
		CBorderVisualizerPanel *pNewBorderPanel = new CBorderVisualizerPanel( m_pList, fmtName.Access(), pCurBorder );
		pNewBorderPanel->SetSize( m_pList->GetWide(), YRES( 45 ) );
		m_pList->AddItem( new Label( NULL, "Label", pCurBorder->GetName() ), pNewBorderPanel );
	}
}

void CSchemeVisualizer::AddFontsToList()
{
#ifdef POSIX
	const char strOAccent[] = { (char)0xc3, (char)0x93, 0x00 };	// UTF-8 for U+00D3 (LATIN CAPITAL LETTER O WITH ACUTE)
#else
	const uint8 strOAccent[] = { 0xd3, 0x00	};
#endif
	// Stick an intl character in here to test accents (O')
	CFmtStr fmtText( "ABCDEFGHIJKLMN%sPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789!@#$%%^&*()-_=+", strOAccent );

	const int nFontCount = m_pViewScheme->GetFontCount();

	for ( int i = 0; i < nFontCount; ++i )
	{
		HFont hCurFont = m_pViewScheme->GetFontAtIndex( i );
		const char *pCurFontName = m_pViewScheme->GetFontName( hCurFont );
		CFmtStr fmtName( "FontPanel_%s", pCurFontName );

		Label *pNewFontLabel = new Label( m_pList, fmtName.Access(), fmtText.Access() );
		pNewFontLabel->SetFont( hCurFont );
		pNewFontLabel->SizeToContents();
		pNewFontLabel->SetWide( m_pList->GetWide() );
		m_pList->AddItem( new Label( NULL, "Label", pCurFontName ), pNewFontLabel );
	}
}

void CSchemeVisualizer::AddColorsToList()
{
	KeyValues *pColorData = (KeyValues *)m_pViewScheme->GetColorData();
	FOR_EACH_SUBKEY( pColorData, pCurColor )
	{
		const char *pCurColorName = pCurColor->GetName();
		CFmtStr fmtName( "ColorPanel_%s", pCurColorName );

		int r = 0, g = 0, b = 0, a = 0;
		const char *pCurColorRGBA = pCurColor->GetString();
		if ( sscanf( pCurColorRGBA, "%d %d %d %d", &r, &g, &b, &a) < 3 )
		{
			Warning( "Skipping color \"%s\"\n", pCurColorRGBA );
			continue;
		}

		CColorVisualizerPanel *pNewColorPanel = new CColorVisualizerPanel( m_pList, fmtName.Access(), Color(r,g,b,a) );
		pNewColorPanel->SetSize( m_pList->GetWide(), YRES( 25 ) );
		m_pList->AddItem( new Label( NULL, "Label", pCurColorName ), pNewColorPanel );
	}
}

//----------------------------------------------------------------------------------------

static CSchemeVisualizer *g_pSchemeVisualizer = NULL;

//----------------------------------------------------------------------------------------

CON_COMMAND( showschemevisualizer, "Show borders, fonts and colors for a particular scheme.  The default is ClientScheme.res" )
{
	if ( g_pSchemeVisualizer )
	{
		g_pSchemeVisualizer->MarkForDeletion();
		g_pSchemeVisualizer = NULL;
	}

	// Load a scheme - defaults to "ClientScheme"
	const char *pSchemeName = "ClientScheme";
	if ( args.ArgC() == 2 )
	{
		pSchemeName = args.Arg( 1 );
	}
	IScheme *pScheme = scheme()->GetIScheme( scheme()->GetScheme( pSchemeName ) );

	Msg( "Using scheme %s...\n", pSchemeName );

	g_pSchemeVisualizer = vgui::SETUP_PANEL( new CSchemeVisualizer( NULL, pScheme, pSchemeName ) );
	g_pSchemeVisualizer->InvalidateLayout( false, true );

	engine->ClientCmd_Unrestricted( "gameui_activate" );

	const int nWidth = XRES( 300 );
	const int nHeight = YRES( 300 );

	g_pSchemeVisualizer->SetBounds(
		( ScreenWidth() - nWidth ) / 2,
		( ScreenHeight() - nHeight ) / 2,
		nWidth,
		nHeight
	);

	g_pSchemeVisualizer->Activate();
}

void CSchemeVisualizer::OnTick()
{
	const int nItemID = m_pListDataTypeCombo->GetActiveItem();
	if ( m_nSelectedComboItem == nItemID )
		return;

	// Cache
	m_nSelectedComboItem = nItemID;

	// Figure out which type was selected
	ListDataType_t nType = LISTDATATYPE_INVALID;
	for ( int i = 0; i < (int)NUM_TYPES; ++i )
	{
		if ( nItemID == m_aComboDataTypeToItemIDMap[ i ] )
		{
			nType = (ListDataType_t)i;
			break;
		}
	}

	AssertMsg( nType != LISTDATATYPE_INVALID, "Couldn't find item ID in list - this should never happen." );

	// Update the list now
	UpdateList( nType );
}