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.

147 lines
4.4 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/Controls.h>
  10. #include <Keyvalues.h>
  11. #include <vgui_controls/Button.h>
  12. #include <vgui_controls/QueryBox.h>
  13. using namespace vgui;
  14. // Query boxes are windows that pop up in response to events.
  15. // They are useful for asking questions (like... "Are you sure you want to do this?").
  16. // In this example we will trigger the opening of a query box when
  17. // a button is pressed.
  18. // Query boxes are Message boxes that have an OK and a Cancel button,
  19. // each button may be linked to an additional command in order to trigger an
  20. // appropriate response.
  21. class QueryBoxDemo: public DemoPage
  22. {
  23. public:
  24. QueryBoxDemo(Panel *parent, const char *name);
  25. ~QueryBoxDemo();
  26. void OnButtonClicked();
  27. void ShowQueryBox();
  28. void OnOK();
  29. void OnCancel();
  30. private:
  31. Button *m_pButton;
  32. DECLARE_PANELMAP();
  33. };
  34. //-----------------------------------------------------------------------------
  35. // Purpose: Constructor
  36. //-----------------------------------------------------------------------------
  37. QueryBoxDemo::QueryBoxDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  38. {
  39. // Create a button to trigger the message box.
  40. m_pButton = new Button(this, "AButton", "Click Me For A Question");
  41. // Size the button to its text.
  42. m_pButton->SizeToContents();
  43. // Set its position.
  44. m_pButton->SetPos(100, 100);
  45. // Install a command that will be executed when the button is pressed
  46. // Here we use a KeyValues command, this is mapped using the Message map
  47. // below to a function.
  48. m_pButton->SetCommand(new KeyValues ("ButtonClicked"));
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose: Destructor
  52. //-----------------------------------------------------------------------------
  53. QueryBoxDemo::~QueryBoxDemo()
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Respond to a message based action signal
  58. // Popup the query box.
  59. //-----------------------------------------------------------------------------
  60. void QueryBoxDemo::OnButtonClicked()
  61. {
  62. ivgui()->DPrintf("Button was clicked.\n");
  63. // When the button is clicked we open the message box in response.
  64. ShowQueryBox();
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Display a query box
  68. //-----------------------------------------------------------------------------
  69. void QueryBoxDemo::ShowQueryBox()
  70. {
  71. // create a new message box.
  72. // The first arg is the name of the window and will be across the top.
  73. // The second arg is the text that will appear in the message box.
  74. QueryBox *pQuery = new QueryBox ("Message Window", "Will you pick OK or Cancel?");
  75. // Make ourselves the target of the button messages
  76. pQuery->AddActionSignalTarget(this);
  77. // Install the message to be sent when the ok button is clicked.
  78. pQuery->SetOKCommand(new KeyValues("OKClicked"));
  79. // Install the message to be sent when the cancel button is clicked.
  80. pQuery->SetCancelCommand(new KeyValues("Cancel"));
  81. // This command will pop up the message box and hold it there until we click
  82. // a button. When a button is clicked the query box object is destroyed.
  83. pQuery->DoModal();
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose: Respond to a message based action signal
  87. // Respond to the OK button in the query box.
  88. //-----------------------------------------------------------------------------
  89. void QueryBoxDemo::OnOK()
  90. {
  91. ivgui()->DPrintf("Query received the OK.\n");
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Purpose: Respond to a message based action signal
  95. // Respond to the Cancel button in the query box
  96. //-----------------------------------------------------------------------------
  97. void QueryBoxDemo::OnCancel()
  98. {
  99. ivgui()->DPrintf("Query was canceled.\n");
  100. }
  101. MessageMapItem_t QueryBoxDemo::m_MessageMap[] =
  102. {
  103. MAP_MESSAGE( QueryBoxDemo, "ButtonClicked", OnButtonClicked ),
  104. MAP_MESSAGE( QueryBoxDemo, "OKClicked", OnOK ),
  105. MAP_MESSAGE( QueryBoxDemo, "Cancel", OnCancel ),
  106. };
  107. IMPLEMENT_PANELMAP(QueryBoxDemo, DemoPage);
  108. Panel* QueryBoxDemo_Create(Panel *parent)
  109. {
  110. return new QueryBoxDemo(parent, "QueryBoxDemo");
  111. }