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.

146 lines
4.6 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/TextEntry.h>
  10. #include <vgui/ISystem.h>
  11. #include <vgui_controls/Controls.h>
  12. #include <stdio.h>
  13. using namespace vgui;
  14. static const int TIMEOUT = 1000; // 1 second timeout
  15. //-----------------------------------------------------------------------------
  16. // Text Entry controls are notepad-like windows that hold text.
  17. // In this demo we create a NON-editable text entry window that holds multiple lines
  18. // of text. We initialize it with some starting text and add a scroll bar to the
  19. // window.
  20. // Then we use a tick function to add some more text to the window every one second.
  21. // As the window fills with text, the window scrolls vertically.
  22. // The scroll bar will appear after a few lines and the scroll bar
  23. // slider will shrink as even more text is added.
  24. //-----------------------------------------------------------------------------
  25. class TextEntryDemo4: public DemoPage
  26. {
  27. public:
  28. TextEntryDemo4(Panel *parent, const char *name);
  29. ~TextEntryDemo4();
  30. void SetVisible(bool state);
  31. void OnTick();
  32. private:
  33. TextEntry *m_pTextEntry;
  34. int m_iTimeoutTime;
  35. };
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Constructor
  38. //-----------------------------------------------------------------------------
  39. TextEntryDemo4::TextEntryDemo4(Panel *parent, const char *name) : DemoPage(parent, name)
  40. {
  41. m_pTextEntry = new TextEntry(this, "FancyTextEntry");
  42. // Position the window and make it nice and wide.
  43. // Make it tall enough to fit several lines of text.
  44. m_pTextEntry->SetBounds(100, 100, 400, 200);
  45. // Make this window hold multiple lines of text.
  46. // This will turn off horizontal scrolling,
  47. // and wrap text from line to line.
  48. m_pTextEntry->SetMultiline(true);
  49. // Add a vertical scroll bar.
  50. m_pTextEntry->SetVerticalScrollbar(true);
  51. // Insert text after you have set the size and position of the window
  52. m_pTextEntry->InsertString("Some starting text and a pile of text. ");
  53. m_pTextEntry->InsertString("Some more text to make mutiple lines. ");
  54. m_pTextEntry->InsertString("Even more scrumptious, chocolatey delicious text. ");
  55. m_pTextEntry->InsertString("Enough text to get that scroll bar a-scrolling. ");
  56. m_pTextEntry->InsertString("That's it a nice number of chars.\n");
  57. // This Text window is not editable by the user. It will only display.
  58. m_pTextEntry->SetEditable(false);
  59. // This makes panel receive a 'Tick' message every frame
  60. // (~50ms, depending on sleep times/framerate)
  61. // Panel is automatically removed from tick signal list when it's deleted
  62. ivgui()->AddTickSignal(this->GetVPanel());
  63. m_iTimeoutTime = 0;
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: Destructor
  67. //-----------------------------------------------------------------------------
  68. TextEntryDemo4::~TextEntryDemo4()
  69. {
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose: When the page is shown, initialize the time
  73. //-----------------------------------------------------------------------------
  74. void TextEntryDemo4::SetVisible(bool state)
  75. {
  76. if (state)
  77. {
  78. // Set the timeout time.
  79. m_iTimeoutTime = system()->GetTimeMillis() + TIMEOUT;
  80. }
  81. DemoPage::SetVisible(state);
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose: Adds lines to the text entry every second.
  85. //-----------------------------------------------------------------------------
  86. void TextEntryDemo4::OnTick()
  87. {
  88. if (m_iTimeoutTime)
  89. {
  90. int currentTime = system()->GetTimeMillis();
  91. // Check for timeout
  92. if (currentTime > m_iTimeoutTime)
  93. {
  94. char buf[125];
  95. sprintf (buf, "Additional Text %d\n", m_iTimeoutTime);
  96. // Move to the end of the history before we add some new text.
  97. // Its important to call this and explicitly move to the
  98. // correct position in case someone clicked in
  99. // the window (this moves the cursor)
  100. // If you comment out this line and rerun you will see
  101. // that if you click in the text window additional
  102. // text will be added where you clicked.
  103. m_pTextEntry->GotoTextEnd();
  104. // Add some text to the text entry window
  105. m_pTextEntry->InsertString(buf);
  106. // Timed out, make a new timeout time
  107. m_iTimeoutTime = system()->GetTimeMillis() + TIMEOUT;
  108. }
  109. }
  110. }
  111. Panel* TextEntryDemo4_Create(Panel *parent)
  112. {
  113. return new TextEntryDemo4(parent, "TextEntryDemo4");
  114. }