VerticalScrollBar.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Controls/ScrollBar.h"
  7. #include "Gwen/Controls/VerticalScrollBar.h"
  8. using namespace Gwen;
  9. using namespace Gwen::Controls;
  10. GWEN_CONTROL_CONSTRUCTOR( VerticalScrollBar )
  11. {
  12. m_Bar->SetVertical();
  13. m_ScrollButton[SCROLL_BUTTON_UP]->SetDirectionUp();
  14. m_ScrollButton[SCROLL_BUTTON_UP]->onPress.Add( this, &VerticalScrollBar::NudgeUp );
  15. m_ScrollButton[SCROLL_BUTTON_DOWN]->SetDirectionDown();
  16. m_ScrollButton[SCROLL_BUTTON_DOWN]->onPress.Add( this, &VerticalScrollBar::NudgeDown );
  17. m_Bar->onDragged.Add( this, &VerticalScrollBar::OnBarMoved );
  18. }
  19. void VerticalScrollBar::Layout( Skin::Base* skin )
  20. {
  21. BaseClass::Layout( skin );
  22. m_ScrollButton[SCROLL_BUTTON_UP]->Dock(Pos::Top);
  23. m_ScrollButton[SCROLL_BUTTON_UP]->SetHeight( Width() );
  24. m_ScrollButton[SCROLL_BUTTON_DOWN]->Dock(Pos::Bottom);
  25. m_ScrollButton[SCROLL_BUTTON_DOWN]->SetHeight( Width() );
  26. m_Bar->SetWidth( GetButtonSize() );
  27. //Add padding
  28. m_Bar->SetPadding( Padding(0, GetButtonSize(), 0, GetButtonSize() ) );
  29. //Calculate bar sizes
  30. float barHeight = (m_fViewableContentSize / m_fContentSize) * (Height() - (GetButtonSize() * 2));
  31. if ( barHeight < GetButtonSize() * 0.5 )
  32. barHeight = GetButtonSize() * 0.5;
  33. m_Bar->SetHeight(barHeight);
  34. m_Bar->SetHidden( Height() - (GetButtonSize() * 2) <= barHeight );
  35. if ( Hidden() )
  36. SetScrolledAmount(0, true);
  37. //Based on our last scroll amount, produce a position for the bar
  38. if ( !m_Bar->IsDepressed() )
  39. {
  40. SetScrolledAmount( GetScrolledAmount(), true );
  41. }
  42. }
  43. void VerticalScrollBar::ScrollToTop()
  44. {
  45. SetScrolledAmount(0, true);
  46. }
  47. void VerticalScrollBar::ScrollToBottom()
  48. {
  49. SetScrolledAmount(1, true);
  50. }
  51. void VerticalScrollBar::NudgeUp( Base* /*control*/ )
  52. {
  53. if ( !IsDisabled() )
  54. SetScrolledAmount(GetScrolledAmount() - GetNudgeAmount(), true);
  55. }
  56. void VerticalScrollBar::NudgeDown( Base* /*control*/ )
  57. {
  58. if ( !IsDisabled() )
  59. SetScrolledAmount(GetScrolledAmount() + GetNudgeAmount(), true);
  60. }
  61. float VerticalScrollBar::GetNudgeAmount()
  62. {
  63. if ( m_bDepressed )
  64. return m_fViewableContentSize / m_fContentSize;
  65. else
  66. return BaseClass::GetNudgeAmount();
  67. }
  68. void VerticalScrollBar::OnMouseClickLeft( int x, int y, bool bDown )
  69. {
  70. if ( bDown )
  71. {
  72. m_bDepressed = true;
  73. Gwen::MouseFocus = this;
  74. }
  75. else
  76. {
  77. Gwen::Point clickPos = CanvasPosToLocal( Gwen::Point( x, y ) );
  78. if ( clickPos.y < m_Bar->Y() )
  79. NudgeUp( this );
  80. else if ( clickPos.y > m_Bar->Y() + m_Bar->Height() )
  81. NudgeDown( this );
  82. m_bDepressed = false;
  83. Gwen::MouseFocus = NULL;
  84. }
  85. }
  86. float VerticalScrollBar::CalculateScrolledAmount()
  87. {
  88. return (float)(m_Bar->Y() - GetButtonSize()) / (float)(Height() - m_Bar->Height() - (GetButtonSize() * 2 ));
  89. }
  90. bool VerticalScrollBar::SetScrolledAmount(float amount, bool forceUpdate)
  91. {
  92. amount = Gwen::Clamp( amount, 0, 1 );
  93. if ( !BaseClass::SetScrolledAmount( amount, forceUpdate ) )
  94. return false;
  95. if ( forceUpdate )
  96. {
  97. int newY = GetButtonSize() + (amount * ((Height() - m_Bar->Height()) - (GetButtonSize()*2)));
  98. m_Bar->MoveTo( m_Bar->X(), newY);
  99. }
  100. return true;
  101. }
  102. void VerticalScrollBar::OnBarMoved( Controls::Base* control )
  103. {
  104. if ( m_Bar->IsDepressed() )
  105. {
  106. SetScrolledAmount( CalculateScrolledAmount(), false );
  107. BaseClass::OnBarMoved(control);
  108. }
  109. else
  110. InvalidateParent();
  111. }