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.

126 lines
4.2 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/IScheme.h>
  10. #include <vgui_controls/ScrollBar.h>
  11. #include <vgui_controls/Label.h>
  12. #include <stdio.h>
  13. using namespace vgui;
  14. //-----------------------------------------------------------------------------
  15. // A ScrollBar is an class for selecting a numerical value from a range.
  16. // E.g. in the case of a scrolling text window we use the scroll bar to select
  17. // what line/char of text display should start at in the text window.
  18. // Some terms:
  19. //
  20. // There are arrow buttons on either end of the scroll bar.
  21. // These move the scroll bar 'slider' or 'nob' across the space between the arrows.
  22. // The nob moves over a user specified 'range' of numbers.
  23. //
  24. // ----------------------------------------------------
  25. // | / |..............| |................ | \ |
  26. // | \ |..............| nob |.................| / |
  27. // ----------------------------------------------------
  28. //
  29. // In this demo we create a vertical scroll bar that is not attached to anything.
  30. // We display the current value of the slider nob next to the scroll bar.
  31. //-----------------------------------------------------------------------------
  32. class ScrollBarDemo: public DemoPage
  33. {
  34. public:
  35. ScrollBarDemo(Panel *parent, const char *name);
  36. ~ScrollBarDemo();
  37. void OnSliderMoved();
  38. private:
  39. ScrollBar *m_pScrollbar;
  40. Label *m_pScrollValue;
  41. DECLARE_PANELMAP();
  42. };
  43. //-----------------------------------------------------------------------------
  44. // Purpose: Constructor
  45. //-----------------------------------------------------------------------------
  46. ScrollBarDemo::ScrollBarDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  47. {
  48. // A vertical slider
  49. m_pScrollbar = new ScrollBar (this, "ASscrollbar", true);
  50. // Set the position of the bar
  51. m_pScrollbar->SetPos(100, 100);
  52. // Set the size of the bar
  53. m_pScrollbar->SetSize (20, 200);
  54. // Set the size of the bar nob, which is actually proportionally
  55. // related to how many lines of info fit into the window the
  56. // scroll bar is attatched to vs how many total lines there are.
  57. // With a size of 10, the scroll bar value will pass from 0 to 100.
  58. m_pScrollbar->SetRangeWindow(10);
  59. // Set the range of the bar,
  60. // We want our range displayed to go from 0 to 100.
  61. // We must take size of the bar nob into account, and set the max to 110.
  62. m_pScrollbar->SetRange(0, 110);
  63. // Set how far the scroll bar slider moves
  64. // when a scroll bar arrow button is pressed
  65. m_pScrollbar->SetButtonPressedScrollValue(5);
  66. // Set the starting value of the bar nob.
  67. // This will put the nob at the top of the vertical scroll bar.
  68. m_pScrollbar->SetValue(0);
  69. // Finally we create a little label to tell us what the current value
  70. // of the scroll bar is.
  71. // We will update it every time the slider is moved.
  72. m_pScrollValue = new Label (this, "ScrollBarValue", "0");
  73. // Stick the label next to the scroll bar.
  74. m_pScrollValue->SetPos(130, 100);
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose: Destructor
  78. //-----------------------------------------------------------------------------
  79. ScrollBarDemo::~ScrollBarDemo()
  80. {
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: Respond to movement of the scroll bar nob by updating the label's
  84. // text with the current value of the scrollbar.
  85. //-----------------------------------------------------------------------------
  86. void ScrollBarDemo::OnSliderMoved()
  87. {
  88. char number[6];
  89. sprintf (number, "%d", m_pScrollbar->GetValue());
  90. m_pScrollValue->SetText(number);
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose: Message map
  94. //-----------------------------------------------------------------------------
  95. MessageMapItem_t ScrollBarDemo::m_MessageMap[] =
  96. {
  97. MAP_MESSAGE( ScrollBarDemo, "ScrollBarSliderMoved", OnSliderMoved ),
  98. };
  99. IMPLEMENT_PANELMAP(ScrollBarDemo, Panel);
  100. Panel* ScrollBarDemo_Create(Panel *parent)
  101. {
  102. return new ScrollBarDemo(parent, "ScrollBarDemo");
  103. }