Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "DemoPage.h"
  9. #include <VGUI/IVGui.h>
  10. //#include <vgui_controls/Controls.h>
  11. #include <vgui_controls/WizardPanel.h>
  12. #include <vgui_controls/WizardSubPanel.h>
  13. #include <vgui_controls/PHandle.h>
  14. #include <vgui_controls/RadioButton.h>
  15. #include <vgui_controls/TextEntry.h>
  16. #include <vgui/ISurface.h>
  17. using namespace vgui;
  18. //-----------------------------------------------------------------------------
  19. // This is a demo of a Wizard.
  20. // A wizard is an interactive utility within an application that guides the user through
  21. // each step of a task.
  22. //
  23. // Wizards typically display a sequence of steps, the user fills in information
  24. // or makes selections and then clicks a "next" button to go to the next panel
  25. // in the sequence. After all panels have been completed, the user clicks "finish"
  26. // and the wizard exits.
  27. //
  28. // In VGUI, the Wizard class is the panel that holds the wizard navigation buttons
  29. // to move to the previous or next panel, and the finish and cancel buttons to
  30. // exit. It also creates the panels that display when the buttons are pressed, called
  31. // WizardSubPanels. These panels have thier own layout and functions that determine
  32. // when to enable/disable the Wizard's navigation buttons.
  33. //
  34. // In this demo we have a Wizard class, called CWonderfulWizard, that contains
  35. // two WizardSubPanel classes, called CSomeSelections and CMoreSelections.
  36. //
  37. //-----------------------------------------------------------------------------
  38. //-----------------------------------------------------------------------------
  39. //-----------------------------------------------------------------------------
  40. // CSomeSelections: First sub panel of the Wonderful wizard
  41. // Provide some user options that we load from a resource file.
  42. //-----------------------------------------------------------------------------
  43. class CSomeSelections : public WizardSubPanel
  44. {
  45. public:
  46. CSomeSelections(Panel *parent, const char *panelName);
  47. ~CSomeSelections(){};
  48. virtual WizardSubPanel *GetNextSubPanel();
  49. virtual void OnDisplayAsPrev();
  50. // Called when the wizard 'next' button is pressed.
  51. // Return true if the wizard should advance.
  52. virtual bool OnNextButton() { return true;}
  53. virtual void PerformLayout();
  54. private:
  55. TextEntry *m_pFirstNameEdit;
  56. TextEntry *m_pLastNameEdit;
  57. TextEntry *m_pUserNameEdit;
  58. TextEntry *m_pEmailEdit;
  59. };
  60. //-----------------------------------------------------------------------------
  61. // Purpose: Constructor
  62. //-----------------------------------------------------------------------------
  63. CSomeSelections::CSomeSelections(Panel *parent, const char *panelName) :
  64. WizardSubPanel(parent, panelName)
  65. {
  66. // create the controls
  67. m_pUserNameEdit = new TextEntry(this, "UserNameEdit");
  68. m_pUserNameEdit->SetPos(100,100);
  69. m_pFirstNameEdit = new TextEntry(this, "FirstNameEdit");
  70. m_pLastNameEdit = new TextEntry(this, "LastNameEdit");
  71. m_pEmailEdit = new TextEntry(this, "EmailEdit");
  72. // The layout of the controls is loaded from a resource file.
  73. LoadControlSettings("Demo/WizardPanelDemo.res");
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose: Return a pointer to the next subpanel that should be displayed
  77. // Output : WizardSubPanel *
  78. //-----------------------------------------------------------------------------
  79. WizardSubPanel *CSomeSelections::GetNextSubPanel()
  80. {
  81. // The next panel to be displayed is called 'CMoreSelections'
  82. return dynamic_cast<WizardSubPanel *>(GetWizardPanel()->FindChildByName("CMoreSelections"));
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Execute this code when a panel has had the 'prev' button pressed
  86. // and the panel to be displayed is this one.
  87. // Input :
  88. //-----------------------------------------------------------------------------
  89. void CSomeSelections::OnDisplayAsPrev()
  90. {
  91. // Enable the 'next' button
  92. GetWizardPanel()->SetNextButtonEnabled(true);
  93. // Buttons are disabled by default, so the prev button will be disabled,
  94. // which is correct since there are no panels before this one.
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose: Layout the window.
  98. //-----------------------------------------------------------------------------
  99. void CSomeSelections::PerformLayout()
  100. {
  101. // Set the title of the Wizard.
  102. GetWizardPanel()->SetTitle("Some Selections", false);
  103. // Make sure the 'finish' button is disabled, since we are not on the last panel.
  104. GetWizardPanel()->SetFinishButtonEnabled(false);
  105. }
  106. //-----------------------------------------------------------------------------
  107. //-----------------------------------------------------------------------------
  108. // CMoreSelections: Second and last sub panel of the Wonderful wizard
  109. // Just one radio button in here. If the button is selected
  110. // The 'finish' button becomes enabled.
  111. //-----------------------------------------------------------------------------
  112. class CMoreSelections : public WizardSubPanel
  113. {
  114. public:
  115. CMoreSelections(Panel *parent, const char *panelName);
  116. ~CMoreSelections(){};
  117. virtual WizardSubPanel *GetNextSubPanel();
  118. virtual void OnDisplayAsNext();
  119. virtual bool OnPrevButton() { return true;}
  120. virtual void PerformLayout();
  121. void OnRadioButtonChecked(Panel *panel);
  122. DECLARE_PANELMAP();
  123. private:
  124. RadioButton *m_pDoneRadio;
  125. };
  126. //-----------------------------------------------------------------------------
  127. // Purpose: Constructor
  128. //-----------------------------------------------------------------------------
  129. CMoreSelections::CMoreSelections(Panel *parent, const char *panelName) :
  130. WizardSubPanel(parent, panelName)
  131. {
  132. // create the controls
  133. // a radio button
  134. m_pDoneRadio = new RadioButton(this, "DoneRadio", "Are you done?");
  135. m_pDoneRadio->SizeToContents();
  136. m_pDoneRadio->SetPos(100,100);
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Purpose: The wizard tried to get the subpanel after this one.
  140. // There is no panel to be displayed after this one. So return NULL
  141. //-----------------------------------------------------------------------------
  142. WizardSubPanel *CMoreSelections::GetNextSubPanel()
  143. {
  144. return NULL;
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose: Called when the subpanel is displayed
  148. // All controls & data should be reinitialized at this time
  149. //-----------------------------------------------------------------------------
  150. void CMoreSelections::OnDisplayAsNext()
  151. {
  152. // There is no next panel so disable this button.
  153. GetWizardPanel()->SetNextButtonEnabled(false);
  154. // We want the finish button disabled until the radio button is set.
  155. GetWizardPanel()->SetFinishButtonEnabled(false);
  156. }
  157. //-----------------------------------------------------------------------------
  158. // Purpose: Layout the window and enable/disable buttons as appropriate.
  159. //-----------------------------------------------------------------------------
  160. void CMoreSelections::PerformLayout()
  161. {
  162. // Set the title of the Wizard.
  163. GetWizardPanel()->SetTitle("All finished?", false);
  164. // Check if the radio button is selected.
  165. if ( m_pDoneRadio->IsSelected())
  166. {
  167. // If it is, we will enable the 'finish' button.
  168. GetWizardPanel()->SetFinishButtonEnabled(true);
  169. }
  170. GetWizardPanel()->SetNextButtonEnabled(false);
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Purpose: Upon checking the radio button, enable the 'finish' button.
  174. //-----------------------------------------------------------------------------
  175. void CMoreSelections::OnRadioButtonChecked(Panel *panel)
  176. {
  177. if ( m_pDoneRadio->IsSelected())
  178. {
  179. GetWizardPanel()->SetFinishButtonEnabled(true);
  180. }
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Purpose: Message map
  184. //-----------------------------------------------------------------------------
  185. MessageMapItem_t CMoreSelections::m_MessageMap[] =
  186. {
  187. MAP_MESSAGE_PTR( CMoreSelections, "RadioButtonChecked", OnRadioButtonChecked, "panel" ), // custom message
  188. };
  189. IMPLEMENT_PANELMAP(CMoreSelections, Panel);
  190. //-----------------------------------------------------------------------------
  191. //-----------------------------------------------------------------------------
  192. // Purpose: A wizard panel containing two
  193. // wizard sub panels
  194. //-----------------------------------------------------------------------------
  195. class CWonderfulWizard : public WizardPanel
  196. {
  197. public:
  198. CWonderfulWizard();
  199. ~CWonderfulWizard(){};
  200. void Run(void);
  201. void Open();
  202. private:
  203. };
  204. //-----------------------------------------------------------------------------
  205. // Purpose: Constructor
  206. //-----------------------------------------------------------------------------
  207. CWonderfulWizard::CWonderfulWizard() : WizardPanel(NULL, "WonderfulWizard")
  208. {
  209. // The size of the Wizard.
  210. //SetBounds(0, 0, 480, 360);
  211. // The first panel to be displayed.
  212. WizardSubPanel *subPanel = new CSomeSelections(this, "CSomeSelections");
  213. subPanel->SetVisible(false);
  214. // The second panel to be displayed.
  215. subPanel = new CMoreSelections(this, "CMoreSelections");
  216. subPanel->SetVisible(false);
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Purpose: Start the wizard, starting with the startPanel
  220. //-----------------------------------------------------------------------------
  221. void CWonderfulWizard::Run( void )
  222. {
  223. SetVisible(true);
  224. // Call run, with the name of the first panel to be displayed.
  225. WizardPanel::Run(dynamic_cast<WizardSubPanel *>(FindChildByName("CSomeSelections")));
  226. SetTitle("A Wizard Panel ", true);
  227. }
  228. //-----------------------------------------------------------------------------
  229. // Purpose: Display the wizard.
  230. //-----------------------------------------------------------------------------
  231. void CWonderfulWizard::Open()
  232. {
  233. RequestFocus();
  234. MoveToFront();
  235. SetVisible(true);
  236. surface()->SetMinimized(this->GetVPanel(), false);
  237. }
  238. //-----------------------------------------------------------------------------
  239. //-----------------------------------------------------------------------------
  240. // Purpose: A demonstration of a wizard panel containing two
  241. // wizard sub panels
  242. //-----------------------------------------------------------------------------
  243. class WizardPanelDemo: public DemoPage
  244. {
  245. public:
  246. WizardPanelDemo(Panel *parent, const char *name);
  247. ~WizardPanelDemo(){};
  248. void SetVisible(bool status);
  249. private:
  250. // We use a handle because the window could be destroyed if someone
  251. // closed the wizard.
  252. DHANDLE<CWonderfulWizard> m_hWizardPanel;
  253. };
  254. //-----------------------------------------------------------------------------
  255. // Purpose: Constructor
  256. //-----------------------------------------------------------------------------
  257. WizardPanelDemo::WizardPanelDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  258. {
  259. }
  260. //-----------------------------------------------------------------------------
  261. // Purpose: When we make this this demo page visible we make the wizard visible.
  262. //-----------------------------------------------------------------------------
  263. void WizardPanelDemo::SetVisible(bool status)
  264. {
  265. if (status)
  266. {
  267. // Pop up the dialog
  268. if (m_hWizardPanel.Get())
  269. {
  270. m_hWizardPanel->Open();
  271. }
  272. else
  273. {
  274. CWonderfulWizard *pWizardPanel = new CWonderfulWizard();
  275. pWizardPanel->SetPos(100, 100);
  276. pWizardPanel->SetSize(480, 360);
  277. surface()->CreatePopup(pWizardPanel->GetVPanel(), false);
  278. m_hWizardPanel = pWizardPanel;
  279. m_hWizardPanel->Run();
  280. }
  281. }
  282. else
  283. {
  284. if (m_hWizardPanel.Get())
  285. {
  286. m_hWizardPanel->SetVisible(false);
  287. }
  288. }
  289. DemoPage::SetVisible(status);
  290. }
  291. Panel* WizardPanelDemo_Create(Panel *parent)
  292. {
  293. return new WizardPanelDemo(parent, "WizardPanelDemo");
  294. }