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.

208 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "DemoPage.h"
  8. #include <VGUI/IVGui.h>
  9. #include <vgui_controls/Menu.h>
  10. #include <vgui_controls/MenuButton.h>
  11. #include <Keyvalues.h>
  12. #include <vgui_controls/Controls.h>
  13. using namespace vgui;
  14. class CascadingMenuDemo: public DemoPage
  15. {
  16. public:
  17. CascadingMenuDemo(Panel *parent, const char *name);
  18. ~CascadingMenuDemo();
  19. void InitMenus();
  20. // Functions that are executed in response to selecting ]
  21. // menu items.
  22. void OnMaggie();
  23. void OnHomer();
  24. void OnMarcia();
  25. void OnJohn();
  26. void OnRed();
  27. private:
  28. MenuButton *m_pOuterMenuButton;
  29. Menu *m_pOuterMenu;
  30. MenuButton *m_pInnerMenuButton;
  31. Menu *m_pInnerMenu;
  32. Menu *m_pInnerMenu2;
  33. Menu *m_pDeepestMenu;
  34. DECLARE_PANELMAP();
  35. };
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Constructor
  38. //-----------------------------------------------------------------------------
  39. CascadingMenuDemo::CascadingMenuDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  40. {
  41. InitMenus();
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Purpose: Destructor
  45. //-----------------------------------------------------------------------------
  46. CascadingMenuDemo::~CascadingMenuDemo()
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose: Create cascading menus.
  51. // We will create a Menu with 2 Menus inside it, and yet another Menu inside one of those!
  52. //-----------------------------------------------------------------------------
  53. void CascadingMenuDemo::InitMenus()
  54. {
  55. // A drop down menu button for the top most menu
  56. m_pOuterMenuButton = new MenuButton(this, "OuterMenu", "Click to open menu");
  57. // Size the Button so we can read its label.
  58. m_pOuterMenuButton->SizeToContents();
  59. int wide, tall;
  60. m_pOuterMenuButton->GetContentSize(wide, tall);
  61. m_pOuterMenuButton->SetSize(wide + Label::Content, tall + Label::Content);
  62. // Create the Menu to go with it.
  63. m_pOuterMenu = new Menu(m_pOuterMenuButton, "OuterMenu");
  64. // Add the menu items to this menu
  65. m_pOuterMenu->AddMenuItem("&Homer", new KeyValues ("Homer"), this);
  66. m_pOuterMenu->AddMenuItem("&Apu", new KeyValues ("Apu"), this);
  67. m_pOuterMenu->AddMenuItem("&Bart", new KeyValues ("Bart"), this);
  68. m_pOuterMenu->AddMenuItem("Lisa", new KeyValues ("Lisa"), this);
  69. m_pOuterMenu->AddMenuItem("&George", new KeyValues ("George"), this);
  70. m_pOuterMenu->AddMenuItem("&Marge", new KeyValues ("Marge"), this);
  71. m_pOuterMenu->AddMenuItem("Maggi&e", new KeyValues ("Maggie"), this);
  72. m_pOuterMenu->SetVisible(false);
  73. // Make the number of visible menu items 20
  74. m_pOuterMenu->SetNumberOfVisibleItems(20);
  75. // Attach this menu to the menu button
  76. m_pOuterMenuButton->SetMenu(m_pOuterMenu);
  77. // Position the menu button on screen.
  78. int x, y, dwide, dtall;
  79. GetBounds (x, y, dwide, dtall);
  80. m_pOuterMenuButton->SetPos(10, dtall/2);
  81. // Create cascading menu #1
  82. // Cascading menu's don't need menu buttons, as they are triggered
  83. // by selecting the menu item of the menu they are in.
  84. m_pInnerMenu = new Menu(m_pOuterMenu, "InnerMenu");
  85. // Add menu items to this menu.
  86. m_pInnerMenu->AddMenuItem("Marcia", new KeyValues ("Marcia"), this);
  87. m_pInnerMenu->AddMenuItem("Greg", new KeyValues ("Greg"), this);
  88. m_pInnerMenu->AddMenuItem("&Peter", new KeyValues ("Peter"), this);
  89. m_pInnerMenu->AddMenuItem("AliceWithALongName", new KeyValues ("Alice"), this);
  90. m_pInnerMenu->AddMenuItem("&Carol", new KeyValues ("Carol"), this);
  91. m_pInnerMenu->AddMenuItem("&Tiger", new KeyValues ("Tiger"), this);
  92. m_pInnerMenu->AddMenuItem("&Sam", new KeyValues ("Sam"), this);
  93. m_pInnerMenu->AddMenuItem("&Bobby", new KeyValues ("Bobby"), this);
  94. m_pInnerMenu->AddMenuItem("Cindy", new KeyValues ("Cindy"), this);
  95. m_pInnerMenu->SetVisible(false);
  96. // Now add the cascading menu to the top menu as a menu item!
  97. m_pOuterMenu->AddCascadingMenuItem("InnerMenu", this, m_pInnerMenu);
  98. // Create cascading menu #2
  99. m_pInnerMenu2 = new Menu(m_pOuterMenu, "InnerMenu2");
  100. m_pInnerMenu2->AddMenuItem("John", new KeyValues ("John"), this);
  101. m_pInnerMenu2->AddMenuItem("Paul", new KeyValues ("Paul"), this);
  102. m_pInnerMenu2->AddMenuItem("Ringo", new KeyValues ("Ringo"), this);
  103. m_pInnerMenu2->AddMenuItem("George", new KeyValues ("George"), this);
  104. m_pInnerMenu2->AddMenuItem("Magical", new KeyValues ("Magical"), this);
  105. m_pInnerMenu2->AddMenuItem("Mystery", new KeyValues ("Mystery"), this);
  106. m_pInnerMenu2->AddMenuItem("Tour", new KeyValues ("Tour"), this);
  107. m_pInnerMenu2->AddMenuItem("Yellow Sub", new KeyValues ("Yellow Sub"), this);
  108. m_pInnerMenu2->SetVisible(false);
  109. // Add this cascading menu to the top menu as a manu item.
  110. m_pOuterMenu->AddCascadingMenuItem("InnerMenu2", this, m_pInnerMenu2);
  111. // Finally, a cascading menu inside a cascading menu!
  112. m_pDeepestMenu = new Menu(m_pInnerMenu, "DeepestMenu");
  113. // Add menu items to this menu.
  114. m_pDeepestMenu->AddMenuItem("Red", new KeyValues ("Red"), this);
  115. m_pDeepestMenu->AddMenuItem("Orange", new KeyValues ("Orange"), this);
  116. m_pDeepestMenu->AddMenuItem("Yellow", new KeyValues ("Yellow"), this);
  117. m_pDeepestMenu->AddMenuItem("Green", new KeyValues ("Green"), this);
  118. m_pDeepestMenu->AddMenuItem("Blue", new KeyValues ("Blue"), this);
  119. m_pDeepestMenu->AddMenuItem("Indigo", new KeyValues ("Indigo"), this);
  120. m_pDeepestMenu->AddMenuItem("Purple", new KeyValues ("Purple"), this);
  121. m_pDeepestMenu->AddMenuItem("Yellow Sun", new KeyValues ("Yellow Sun"), this);
  122. m_pDeepestMenu->SetVisible(false);
  123. // Set the number of visible items in the menu to 4, this menu will have a scrollbar.
  124. m_pDeepestMenu->SetNumberOfVisibleItems(4);
  125. // Add this menu item to one of the other menus already in the top most menu above
  126. m_pInnerMenu->AddCascadingMenuItem("DeepestMenu", this, m_pDeepestMenu);
  127. }
  128. // Messages recieved from each of the menus, prints out when message is recieved
  129. void CascadingMenuDemo::OnMaggie()
  130. {
  131. ivgui()->DPrintf("Maggie selected.\n");
  132. }
  133. void CascadingMenuDemo::OnMarcia()
  134. {
  135. ivgui()->DPrintf("Marcia selected.\n");
  136. }
  137. void CascadingMenuDemo::OnJohn()
  138. {
  139. ivgui()->DPrintf("John selected.\n");
  140. }
  141. void CascadingMenuDemo::OnRed()
  142. {
  143. ivgui()->DPrintf("Red selected.\n");
  144. }
  145. void CascadingMenuDemo::OnHomer()
  146. {
  147. ivgui()->DPrintf("Homer selected.\n");
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Purpose: Message map
  151. //-----------------------------------------------------------------------------
  152. MessageMapItem_t CascadingMenuDemo::m_MessageMap[] =
  153. {
  154. MAP_MESSAGE( CascadingMenuDemo, "Maggie", OnMaggie ), // from outermenu
  155. MAP_MESSAGE( CascadingMenuDemo, "Homer", OnHomer ), // from outermenu
  156. MAP_MESSAGE( CascadingMenuDemo, "Marcia", OnMarcia ), // from innermenu2
  157. MAP_MESSAGE( CascadingMenuDemo, "John", OnJohn ), // from innermenu2
  158. MAP_MESSAGE( CascadingMenuDemo, "Red", OnRed ), // from deepest menu
  159. };
  160. IMPLEMENT_PANELMAP(CascadingMenuDemo, DemoPage);
  161. Panel* CascadingMenuDemo_Create(Panel *parent)
  162. {
  163. return new CascadingMenuDemo(parent, "CascadingMenuDemo");
  164. }