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.

116 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "MenuDemo.h"
  8. //-----------------------------------------------------------------------------
  9. // Purpose: Constructor
  10. //-----------------------------------------------------------------------------
  11. MenuDemo::MenuDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  12. {
  13. // It takes 3 parts to make a simple menu.
  14. // 1. A MenuButton that triggers the menu to open.
  15. // 2. A Menu that opens when the button is pressed.
  16. // 3. MenuItems that are listed in the menu and execute commands when selected.
  17. // First create a menu button.
  18. m_pMenuButton = new MenuButton(this, "AMenuButton", "Press Me To Open Menu!");
  19. // Size the Button to its label.
  20. int wide, tall;
  21. m_pMenuButton->GetContentSize(wide, tall);
  22. m_pMenuButton->SetSize(wide + Label::Content, tall + Label::Content);
  23. // Next create a menu, and link it to the menu button
  24. m_pMenu = new Menu(m_pMenuButton, "AMenu");
  25. // Now add menu items to the menu. These are the choices that will be seen.
  26. // In adding an item the first arg is the name of the item as it will
  27. // appear in the list, '&' chars are used to flag windows hotkeys that
  28. // will also work to select the item. They will have a little underline
  29. // underneath them.
  30. // The second arg is a KeyValues structure, that in this case holds a string name
  31. // of the command to be sent when the item is selected.
  32. // The third arg is the target of the command, we are sending all commands
  33. // back to this class.
  34. m_pMenu->AddMenuItem("&Homer", new KeyValues ("Homer"), this);
  35. m_pMenu->AddMenuItem("&Apu", new KeyValues ("Apu"), this);
  36. m_pMenu->AddMenuItem("&Bart", new KeyValues ("Bart"), this);
  37. // A Menu Item with no hotkey
  38. m_pMenu->AddMenuItem("Lisa", new KeyValues ("Lisa"), this);
  39. m_pMenu->AddMenuItem("&George", new KeyValues ("George"), this);
  40. m_pMenu->AddMenuItem("&Marge", new KeyValues ("Marge"), this);
  41. // The 'M' hotkey was already used in Marge, 'a' and 'g' are also taken.
  42. // Use another letter not taken for the hotkey.
  43. m_pMenu->AddMenuItem("Maggi&e", new KeyValues ("Maggie"), this);
  44. // Hide the menu to start. The button will make it visible.
  45. m_pMenu->SetVisible(false);
  46. // Tell the menu button which menu it should open.
  47. m_pMenuButton->SetMenu(m_pMenu);
  48. // Position the menu button in the window.
  49. int x, y, dwide, dtall;
  50. GetBounds (x, y, dwide, dtall);
  51. m_pMenuButton->SetPos(10, dtall/2);
  52. // By default the menu's width will be that of the largest item it contains.
  53. // You can also set a fixed width, which we will do so our menu doesn't look so
  54. // scrawny.
  55. // Get the size of the menu button
  56. m_pMenuButton->GetSize(wide, tall);
  57. // Set the width of the menu to be the same as the menu button.
  58. // Comment this line out if you want to see the menu in its original scrawny size
  59. m_pMenu->SetFixedWidth(wide);
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose: Destructor
  63. //-----------------------------------------------------------------------------
  64. MenuDemo::~MenuDemo()
  65. {
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose: This is the function that will be executed when the "Maggie"
  69. // messsage is recieved. Selecting the "Maggie" menu item will trigger
  70. // this message to be sent. This class has been selected as the target of the
  71. // button, and our Message Map translates this string message to a function
  72. // command.
  73. //-----------------------------------------------------------------------------
  74. void MenuDemo::OnMaggie()
  75. {
  76. // Put a breakpoint here and watch the code break
  77. // when you select "Maggie"
  78. ivgui()->DPrintf("Maggie selected.\n");
  79. }
  80. // explain message passing
  81. MessageMapItem_t MenuDemo::m_MessageMap[] =
  82. {
  83. MAP_MESSAGE( MenuDemo, "Maggie", OnMaggie ), // from outermenu
  84. };
  85. IMPLEMENT_PANELMAP(MenuDemo, DemoPage);
  86. Panel* MenuDemo_Create(Panel *parent)
  87. {
  88. return new MenuDemo(parent, "MenuDemo");
  89. }