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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//=======================================================================================//
#include "cbase.h"
#include "game_controls/navigationpanel.h"
#include "econ/econ_controls.h"
#include "vgui_controls/Frame.h"
#include "vgui/IInput.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
using namespace vgui;
//-----------------------------------------------------------------------------
DECLARE_BUILD_FACTORY( CNavigationPanel );
//-----------------------------------------------------------------------------
class CNavButton : public CExImageButton
{
DECLARE_CLASS_SIMPLE( CNavButton, CExImageButton );
public:
CNavButton( Panel *parent, const char *name, const char *text = "", Panel *pActionSignalTarget = NULL, const char *cmd = NULL )
: CExImageButton( parent, name, text, pActionSignalTarget, cmd ), m_iUserData( -1 ) {}
CNavButton( Panel *parent, const char *name, const wchar_t *wszText = L"", Panel *pActionSignalTarget = NULL, const char *cmd = NULL )
: CExImageButton( parent, name, wszText, pActionSignalTarget, cmd ) , m_iUserData( -1 ) {}
virtual void ApplySettings( KeyValues *pInResourceData )
{
BaseClass::ApplySettings( pInResourceData );
if ( m_iUserData < 0 )
{
m_iUserData = pInResourceData->GetInt( "userdata", -1 ); AssertMsg( m_iUserData != -1, "Any messages sent for this button will have invalid user data!" );
}
}
int m_iUserData;
};
//-----------------------------------------------------------------------------
CNavigationPanel::CNavigationPanel( Panel *pParent, const char *pName, bool bAddParentAsActionSignalTarget/*=true*/ )
: BaseClass( pParent, pName ),
m_bAutoLayout( false ),
m_bAutoScale( true ),
m_bDisplayVertical( false ),
m_iSelectedButton( 0 ),
// m_nAlignment( ALIGN_CENTER ),
m_pKVButtonSettings( NULL )
{
if ( bAddParentAsActionSignalTarget && pParent )
{
AddActionSignalTarget( pParent );
}
}
CNavigationPanel::~CNavigationPanel()
{
}
void CNavigationPanel::AddButton( int iUserData, const char *pTextToken )
{
const int i = m_vecButtons.Count();
CNavButton *pNewButton = new CNavButton( this, CFmtStr( "button_%i", i ).Access(), pTextToken );
pNewButton->m_iUserData = iUserData;
pNewButton->InvalidateLayout( true, false );
pNewButton->SetCommand( CFmtStr( "select_%i", i ).Access() );
m_vecButtons.AddToTail( pNewButton );
}
CExImageButton *CNavigationPanel::GetButton( int index )
{
return m_vecButtons[ index ];
}
void CNavigationPanel::ApplySettings( KeyValues *pInResourceData )
{
BaseClass::ApplySettings( pInResourceData );
/*
const char *pAlignment = pInResourceData->GetString( "align", NULL );
if ( pAlignment )
{
if ( !V_strnicmp( pAlignment, "west", 4 ) )
{
m_nAlignment = ALIGN_WEST;
}
else if ( !V_strnicmp( pAlignment, "center", 6 ) )
{
m_nAlignment = ALIGN_CENTER;
}
else if ( !V_strnicmp( pAlignment, "east", 4 ) )
{
AssertMsg( 0, "This type of alignment is not supported." );
}
}
*/
m_bAutoLayout = pInResourceData->GetBool( "auto_layout", false );
m_bAutoScale = pInResourceData->GetBool( "auto_scale", true );
m_bDisplayVertical = pInResourceData->GetBool( "display_vertically", false );
KeyValues *pKVButtonSettings = pInResourceData->FindKey( "ButtonSettings" ); AssertMsg( pKVButtonSettings, "This is required" );
if ( !pKVButtonSettings )
{
AssertMsg( 0, "No button settings specified. CNavigationPanel is useless without this data." );
return;
}
// Cache this off for later
if ( m_pKVButtonSettings )
{
m_pKVButtonSettings->deleteThis();
m_pKVButtonSettings = NULL;
}
m_pKVButtonSettings = pKVButtonSettings->MakeCopy();
// Get individual button data and apply now
KeyValues *pKVButtons = pInResourceData->FindKey( "Buttons" );
if ( !pKVButtons )
return;
// Go through each image description and create a button
if ( m_vecButtons.Count() )
return;
int i = 0;
FOR_EACH_SUBKEY( pKVButtons, pKVCurButton )
{
CNavButton *pNewButton = new CNavButton( this, pKVCurButton->GetString( "FieldName", pKVCurButton->GetName() ), L"" );
pNewButton->ApplySettings( pKVCurButton );
pNewButton->InvalidateLayout( true, false );
pNewButton->SetCommand( CFmtStr( "select_%i", i ).Access() );
m_vecButtons.AddToTail( pNewButton );
++i;
}
if ( m_vecButtons.IsValidIndex( m_iSelectedButtonDefault ) )
{
UpdateButtonSelectionStates( m_iSelectedButtonDefault );
m_vecButtons[ m_iSelectedButtonDefault ]->InvalidateLayout( true );
const int iUserData = m_vecButtons[ m_iSelectedButtonDefault ]->m_iUserData;
PostActionSignal( new KeyValues( "NavButtonSelected", "userdata", iUserData ) );
}
}
void CNavigationPanel::ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
}
void CNavigationPanel::PerformLayout()
{
if ( !m_vecButtons.Count() )
return;
if ( !m_pKVButtonSettings )
return;
BaseClass::PerformLayout();
// Get from settings
int nSettingWidth = m_pKVButtonSettings->GetInt( "wide" );
int nSettingHeight = m_pKVButtonSettings->GetInt( "tall" );
// Button dimensions for setting positions
int nButtonWidth = nSettingWidth;
int nButtonHeight = nSettingHeight;
if ( m_bAutoScale )
{
// Get image size
int nImageW, nImageH;
m_vecButtons[0]->GetImage()->GetSize( nImageW, nImageH );
int nWidth, nHeight;
if ( m_bDisplayVertical )
{
nWidth = GetWide();
nHeight = nSettingHeight * nWidth / ( nSettingWidth > 0 ? nSettingWidth : 1 );
}
else
{
nHeight = GetTall();
nWidth = nSettingWidth * nHeight / ( nSettingHeight > 0 ? nSettingHeight : 1 );
}
// Update button dimensions to scaled versions
nButtonWidth = nWidth;
nButtonHeight = nHeight;
}
FOR_EACH_VEC( m_vecButtons, i )
{
if ( m_vecButtons[i] && m_pKVButtonSettings )
{
// Apply generic settings
m_vecButtons[i]->ApplySettings( m_pKVButtonSettings );
}
if ( m_bAutoLayout )
{
// Display buttons vertically or horizontally?
if ( m_bDisplayVertical )
{
m_vecButtons[i]->SetPos( 0, i * ( nButtonHeight + m_nVerticalBuffer ) );
}
else
{
const int nStartX = 0;//0.5f * ( GetWide() - NumButtons() * ( nButtonWidth + m_nHorizontalBuffer ) );
m_vecButtons[i]->SetPos( nStartX + i * ( nButtonWidth + m_nHorizontalBuffer ), 0 );
}
}
if ( m_bAutoScale )
{
m_vecButtons[i]->SetSize( nButtonWidth, nButtonHeight );
m_vecButtons[i]->GetImage()->SetSize( nButtonWidth, nButtonHeight );
}
m_vecButtons[i]->SetVisible( true );
}
}
void CNavigationPanel::OnCommand( const char *pCommand )
{
if ( !V_strnicmp( pCommand, "select_", 7 ) )
{
const int iButton = atoi( pCommand + 7 ); AssertMsg( m_vecButtons.IsValidIndex( iButton ), "Button index out of range!" );
if ( !m_vecButtons.IsValidIndex( iButton ) )
return;
UpdateButtonSelectionStates( iButton );
const int iUserData = m_vecButtons[ iButton ]->m_iUserData;
PostActionSignal( new KeyValues( "NavButtonSelected", "userdata", iUserData ) );
}
else
{
BaseClass::OnCommand( pCommand );
}
}
void CNavigationPanel::OnThink()
{
BaseClass::OnThink();
// Make sure we only ever have one button in the selection state, since it's
// possible to do this if you select a button, then click and drag on another
// button, and release the mouse elsewhere.
if ( !vgui::input()->IsMouseDown( MOUSE_LEFT ) )
{
UpdateButtonSelectionStates( m_iSelectedButton );
}
}
void CNavigationPanel::UpdateButtonSelectionStates( int iButton )
{
if ( m_iSelectedButton != iButton )
{
m_iSelectedButton = iButton;
}
// Set the correct button as selected, all other buttons as not selected
for ( int i = 0; i < NumButtons(); ++i )
{
CNavButton *pCurButton = m_vecButtons[ i ];
if ( !pCurButton )
continue;
bool bShouldSelect = iButton == i;
pCurButton->SetSelected( bShouldSelect );
pCurButton->InvalidateLayout();
}
}
//-----------------------------------------------------------------------------
#ifdef _DEBUG
class CNavPanelTest : public Frame
{
DECLARE_CLASS_SIMPLE( CNavPanelTest, Frame );
public:
CNavPanelTest( vgui::Panel *pParent )
: Frame( pParent, "NavPanelTest" )
{
SetProportional( true );
LoadControlSettings( "Resource/UI/NavigationPanelTest.res" );
}
};
CON_COMMAND( open_navpanel_test, "" )
{
CNavPanelTest *pPanel = SETUP_PANEL( new CNavPanelTest( NULL ) );
pPanel->SetVisible( true );
pPanel->InvalidateLayout( false, true );
engine->ClientCmd_Unrestricted( "gameui_activate" );
const int nWidth = XRES( 300 );
const int nHeight = YRES( 300 );
pPanel->SetBounds(
( ScreenWidth() - nWidth ) / 2,
( ScreenHeight() - nHeight ) / 2,
nWidth,
nHeight
);
pPanel->Activate();
}
#endif // _DEBUG
|