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.

109 lines
3.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/ISystem.h>
  10. #include <vgui_controls/Controls.h>
  11. #include <vgui_controls/ProgressBar.h>
  12. using namespace vgui;
  13. static const int TIMEOUT = 5000; // 5 second timeout
  14. //-----------------------------------------------------------------------------
  15. // Progress bars are used to illustrate a waiting period (such as
  16. // connecting to a server.
  17. // Here we create a progress bar that fills up every 5 seconds.
  18. //-----------------------------------------------------------------------------
  19. class ProgressBarDemo: public DemoPage
  20. {
  21. public:
  22. ProgressBarDemo(Panel *parent, const char *name);
  23. ~ProgressBarDemo();
  24. void OnTick();
  25. void SetVisible(bool status);
  26. private:
  27. ProgressBar *m_pProgressBar;
  28. int m_iTimeoutTime;
  29. };
  30. //-----------------------------------------------------------------------------
  31. // Purpose: Constructor
  32. //-----------------------------------------------------------------------------
  33. ProgressBarDemo::ProgressBarDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  34. {
  35. m_pProgressBar = new ProgressBar(this, "AProgressBar");
  36. m_pProgressBar->SetPos(100, 100);
  37. m_pProgressBar->SetWide(300);
  38. // This makes panel receive a 'Tick' message every frame
  39. // (~50ms, depending on sleep times/framerate)
  40. // Panel is automatically removed from tick signal list when it's deleted
  41. ivgui()->AddTickSignal(this->GetVPanel());
  42. m_iTimeoutTime = 0;
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Purpose: Destructor
  46. //-----------------------------------------------------------------------------
  47. ProgressBarDemo::~ProgressBarDemo()
  48. {
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose: When the page is shown, initialize the progress bar.
  52. //-----------------------------------------------------------------------------
  53. void ProgressBarDemo::SetVisible(bool status)
  54. {
  55. if (status)
  56. {
  57. // Set the timeout time.
  58. m_iTimeoutTime = system()->GetTimeMillis() + TIMEOUT;
  59. // Set progress bar to be none of the way done
  60. m_pProgressBar->SetProgress(0);
  61. }
  62. DemoPage::SetVisible(status);
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose: Advances the status bar to the end, then starts over
  66. //-----------------------------------------------------------------------------
  67. void ProgressBarDemo::OnTick()
  68. {
  69. if (m_iTimeoutTime)
  70. {
  71. int currentTime = system()->GetTimeMillis();
  72. // Check for timeout
  73. if (currentTime > m_iTimeoutTime)
  74. {
  75. // Timed out, make a new timeout time
  76. m_iTimeoutTime = system()->GetTimeMillis() + TIMEOUT;
  77. }
  78. else
  79. {
  80. // Advance the status bar, in the range 0% to 100%
  81. float timePassedPercentage = (float)(currentTime - m_iTimeoutTime + TIMEOUT) / (float)TIMEOUT;
  82. m_pProgressBar->SetProgress(timePassedPercentage);
  83. }
  84. }
  85. }
  86. Panel* ProgressBarDemo_Create(Panel *parent)
  87. {
  88. return new ProgressBarDemo(parent, "ProgressBarDemo");
  89. }