summaryrefslogtreecommitdiff
path: root/utils/vgui_panel_zoo/WizardPanelDemo.cpp
blob: 6455c43943f580686f8193805600970134b545a7 (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
340
341
342
343
344
345
346
347
348
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#include "DemoPage.h"

#include <VGUI/IVGui.h>
//#include <vgui_controls/Controls.h>

#include <vgui_controls/WizardPanel.h>
#include <vgui_controls/WizardSubPanel.h>
#include <vgui_controls/PHandle.h>

#include <vgui_controls/RadioButton.h>
#include <vgui_controls/TextEntry.h>
#include <vgui/ISurface.h>

using namespace vgui;

//-----------------------------------------------------------------------------
// This is a demo of a Wizard.
// A wizard is an interactive utility within an application that guides the user through 
// each step of a task.
//
// Wizards typically display a sequence of steps, the user fills in information
// or makes selections and then clicks a "next" button to go to the next panel
// in the sequence. After all panels have been completed, the user clicks "finish"
// and the wizard exits.
//
// In VGUI, the Wizard class is the panel that holds the wizard navigation buttons
// to move to the previous or next panel, and the finish and cancel buttons to 
// exit. It also creates the panels that display when the buttons are pressed, called
// WizardSubPanels. These panels have thier own layout and functions that determine
// when to enable/disable the Wizard's navigation buttons.
//
// In this demo we have a Wizard class, called CWonderfulWizard, that contains
// two WizardSubPanel classes, called CSomeSelections and CMoreSelections.
//
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// CSomeSelections: First sub panel of the Wonderful wizard
//			Provide some user options that we load from a resource file.
//-----------------------------------------------------------------------------
class CSomeSelections : public WizardSubPanel
{
public:
	CSomeSelections(Panel *parent, const char *panelName);
	~CSomeSelections(){};
	
	virtual WizardSubPanel *GetNextSubPanel();
	virtual void OnDisplayAsPrev();
	// Called when the wizard 'next' button is pressed.
	// Return true if the wizard should advance.
	virtual bool OnNextButton()	{ return true;}
	virtual void PerformLayout();
	
private:
	TextEntry *m_pFirstNameEdit;
	TextEntry *m_pLastNameEdit;
	TextEntry *m_pUserNameEdit;
	TextEntry *m_pEmailEdit;
	};

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSomeSelections::CSomeSelections(Panel *parent, const char *panelName) : 
WizardSubPanel(parent, panelName)
{
	// create the controls
	m_pUserNameEdit = new TextEntry(this, "UserNameEdit");
	m_pUserNameEdit->SetPos(100,100);
	m_pFirstNameEdit = new TextEntry(this, "FirstNameEdit");
	m_pLastNameEdit = new TextEntry(this, "LastNameEdit");
	m_pEmailEdit = new TextEntry(this, "EmailEdit");
		
	// The layout of the controls is loaded from a resource file.
	LoadControlSettings("Demo/WizardPanelDemo.res");
}


//-----------------------------------------------------------------------------
// Purpose: Return a pointer to the next subpanel that should be displayed
// Output : WizardSubPanel *
//-----------------------------------------------------------------------------
WizardSubPanel *CSomeSelections::GetNextSubPanel()
{
	// The next panel to be displayed is called 'CMoreSelections'
	return dynamic_cast<WizardSubPanel *>(GetWizardPanel()->FindChildByName("CMoreSelections"));
}

//-----------------------------------------------------------------------------
// Purpose: Execute this code when a panel has had the 'prev' button pressed
// and the panel to be displayed is this one.
// Input  :  
//-----------------------------------------------------------------------------
void CSomeSelections::OnDisplayAsPrev()
{
	// Enable the 'next' button
	GetWizardPanel()->SetNextButtonEnabled(true);
	// Buttons are disabled by default, so the prev button will be disabled,
	// which is correct since there are no panels before this one.
}

//-----------------------------------------------------------------------------
// Purpose: Layout the window.
//-----------------------------------------------------------------------------
void CSomeSelections::PerformLayout()
{
	// Set the title of the Wizard.
	GetWizardPanel()->SetTitle("Some Selections", false);
	// Make sure the 'finish' button is disabled, since we are not on the last panel.
	GetWizardPanel()->SetFinishButtonEnabled(false);
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// CMoreSelections: Second and last sub panel of the Wonderful wizard
//			Just one radio button in here. If the button is selected
//          The 'finish' button becomes enabled.
//-----------------------------------------------------------------------------
class CMoreSelections : public WizardSubPanel
{
public:
	CMoreSelections(Panel *parent, const char *panelName);
	~CMoreSelections(){};
	
	virtual WizardSubPanel *GetNextSubPanel();
	virtual void OnDisplayAsNext();
	virtual bool OnPrevButton() { return true;}
	virtual void PerformLayout();
	void OnRadioButtonChecked(Panel *panel);

	DECLARE_PANELMAP();
		
private:
	RadioButton *m_pDoneRadio;
};

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CMoreSelections::CMoreSelections(Panel *parent, const char *panelName) : 
WizardSubPanel(parent, panelName)
{
	// create the controls
	// a radio button
	m_pDoneRadio = new RadioButton(this, "DoneRadio", "Are you done?");
	m_pDoneRadio->SizeToContents();
	m_pDoneRadio->SetPos(100,100);
}

//-----------------------------------------------------------------------------
// Purpose: The wizard tried to get the subpanel after this one.
//  There is no panel to be displayed after this one. So return NULL
//-----------------------------------------------------------------------------
WizardSubPanel *CMoreSelections::GetNextSubPanel()
{
	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Called when the subpanel is displayed
// All controls & data should be reinitialized at this time
//-----------------------------------------------------------------------------
void CMoreSelections::OnDisplayAsNext()
{
	// There is no next panel so disable this button.
	GetWizardPanel()->SetNextButtonEnabled(false);
	// We want the finish button disabled until the radio button is set.
	GetWizardPanel()->SetFinishButtonEnabled(false);
}

//-----------------------------------------------------------------------------
// Purpose: Layout the window and enable/disable buttons as appropriate.
//-----------------------------------------------------------------------------
void CMoreSelections::PerformLayout()
{
	// Set the title of the Wizard.
	GetWizardPanel()->SetTitle("All finished?", false);

	// Check if the radio button is selected.
	if ( m_pDoneRadio->IsSelected())
	{
		// If it is, we will enable the 'finish' button.
		GetWizardPanel()->SetFinishButtonEnabled(true);

	}
	GetWizardPanel()->SetNextButtonEnabled(false);	
}

//-----------------------------------------------------------------------------
// Purpose: Upon checking the radio button, enable the 'finish' button.
//-----------------------------------------------------------------------------
void CMoreSelections::OnRadioButtonChecked(Panel *panel)
{
	if ( m_pDoneRadio->IsSelected())
	{
		GetWizardPanel()->SetFinishButtonEnabled(true);

	}
}

//-----------------------------------------------------------------------------
// Purpose: Message map
//-----------------------------------------------------------------------------
MessageMapItem_t CMoreSelections::m_MessageMap[] =
{
	MAP_MESSAGE_PTR( CMoreSelections, "RadioButtonChecked", OnRadioButtonChecked, "panel" ),	// custom message
};
IMPLEMENT_PANELMAP(CMoreSelections, Panel);



//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: A wizard panel containing two 
// wizard sub panels
//-----------------------------------------------------------------------------
class CWonderfulWizard : public WizardPanel
{
public:
	CWonderfulWizard();
	~CWonderfulWizard(){};
	
	void Run(void);
	void Open();
	
private:
};

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CWonderfulWizard::CWonderfulWizard() : WizardPanel(NULL, "WonderfulWizard")
{
	// The size of the Wizard.
	//SetBounds(0, 0, 480, 360);
	
	// The first panel to be displayed.
	WizardSubPanel *subPanel = new CSomeSelections(this, "CSomeSelections");
	subPanel->SetVisible(false);
	
	// The second panel to be displayed.
	subPanel = new CMoreSelections(this, "CMoreSelections");
	subPanel->SetVisible(false);
}

//-----------------------------------------------------------------------------
// Purpose: Start the wizard, starting with the startPanel
//-----------------------------------------------------------------------------
void CWonderfulWizard::Run( void )
{
	SetVisible(true);

	// Call run, with the name of the first panel to be displayed.
	WizardPanel::Run(dynamic_cast<WizardSubPanel *>(FindChildByName("CSomeSelections")));
	
	SetTitle("A Wizard Panel ", true);
}	 

//-----------------------------------------------------------------------------
// Purpose: Display the wizard.
//-----------------------------------------------------------------------------
void CWonderfulWizard::Open()
{
	RequestFocus();
	MoveToFront();
	SetVisible(true);
	surface()->SetMinimized(this->GetVPanel(), false);
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: A demonstration of a wizard panel containing two 
// wizard sub panels
//-----------------------------------------------------------------------------
class WizardPanelDemo: public DemoPage
{
public:
	WizardPanelDemo(Panel *parent, const char *name);
	~WizardPanelDemo(){};
	
	void SetVisible(bool status);
	
private:
	// We use a handle because the window could be destroyed if someone
	// closed the wizard. 
	DHANDLE<CWonderfulWizard> m_hWizardPanel;
	
};

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
WizardPanelDemo::WizardPanelDemo(Panel *parent, const char *name) : DemoPage(parent, name)
{
}

//-----------------------------------------------------------------------------
// Purpose: When we make this this demo page visible we make the wizard visible.
//-----------------------------------------------------------------------------
void WizardPanelDemo::SetVisible(bool status)
{
	if (status)
	{
		// Pop up the dialog
		if (m_hWizardPanel.Get())
		{
			m_hWizardPanel->Open();
		}
		else
		{
			CWonderfulWizard *pWizardPanel = new CWonderfulWizard();
			pWizardPanel->SetPos(100, 100);
			pWizardPanel->SetSize(480, 360);

			surface()->CreatePopup(pWizardPanel->GetVPanel(), false);
			m_hWizardPanel = pWizardPanel;
			m_hWizardPanel->Run();
		}
	}
	else
	{
		if (m_hWizardPanel.Get())
		{		
			m_hWizardPanel->SetVisible(false);
		}
	}

	DemoPage::SetVisible(status);	
}



Panel* WizardPanelDemo_Create(Panel *parent)
{
	return new WizardPanelDemo(parent, "WizardPanelDemo");
}