ScrollControl.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Controls/ScrollControl.h"
  7. #include "Gwen/Controls/ScrollBar.h"
  8. #include "Gwen/Controls/VerticalScrollBar.h"
  9. #include "Gwen/Controls/HorizontalScrollBar.h"
  10. #include "Gwen/Utility.h"
  11. using namespace Gwen;
  12. using namespace Gwen::Controls;
  13. using namespace Gwen::ControlsInternal;
  14. GWEN_CONTROL_CONSTRUCTOR( ScrollControl )
  15. {
  16. SetMouseInputEnabled( false );
  17. m_VerticalScrollBar = new VerticalScrollBar( this );
  18. m_VerticalScrollBar->Dock(Pos::Right);
  19. m_VerticalScrollBar->onBarMoved.Add( this, &ScrollControl::VBarMoved );
  20. m_VerticalScrollBar->SetNudgeAmount( 30 );
  21. m_bCanScrollV = true;
  22. m_HorizontalScrollBar = new HorizontalScrollBar( this );
  23. m_HorizontalScrollBar->Dock( Pos::Bottom );
  24. m_HorizontalScrollBar->onBarMoved.Add( this, &ScrollControl::HBarMoved );
  25. m_bCanScrollH = true;
  26. m_HorizontalScrollBar->SetNudgeAmount( 30 );
  27. m_InnerPanel = new Base( this );
  28. m_InnerPanel->SetPos(0, 0);
  29. m_InnerPanel->SetMargin( Margin(5,5,5,5));
  30. m_InnerPanel->SendToBack();
  31. m_InnerPanel->SetMouseInputEnabled( false );
  32. m_bAutoHideBars = false;
  33. }
  34. void ScrollControl::SetScroll( bool h, bool v )
  35. {
  36. m_bCanScrollV = v;
  37. m_bCanScrollH = h;
  38. m_VerticalScrollBar->SetHidden( !m_bCanScrollV );
  39. m_HorizontalScrollBar->SetHidden( !m_bCanScrollH );
  40. }
  41. void ScrollControl::SetInnerSize( int w, int h )
  42. {
  43. m_InnerPanel->SetSize( w, h );
  44. }
  45. void ScrollControl::VBarMoved( Controls::Base * /*control*/ )
  46. {
  47. Invalidate();
  48. }
  49. void ScrollControl::HBarMoved( Controls::Base * /*control*/ )
  50. {
  51. Invalidate();
  52. }
  53. void ScrollControl::OnChildBoundsChanged( Gwen::Rect /*oldChildBounds*/, Base* /*pChild*/ )
  54. {
  55. UpdateScrollBars();
  56. }
  57. void ScrollControl::Layout( Skin::Base* skin )
  58. {
  59. UpdateScrollBars();
  60. BaseClass::Layout(skin);
  61. }
  62. bool ScrollControl::OnMouseWheeled( int iDelta )
  63. {
  64. if ( CanScrollV() && m_VerticalScrollBar->Visible() )
  65. {
  66. if ( m_VerticalScrollBar->SetScrolledAmount( m_VerticalScrollBar->GetScrolledAmount() - m_VerticalScrollBar->GetNudgeAmount() * ( (float)iDelta / 60.0f ), true) )
  67. return true;
  68. }
  69. if ( CanScrollH() && m_HorizontalScrollBar->Visible() )
  70. {
  71. if ( m_HorizontalScrollBar->SetScrolledAmount( m_HorizontalScrollBar->GetScrolledAmount() - m_HorizontalScrollBar->GetNudgeAmount() * ( (float)iDelta / 60.0f ), true) )
  72. return true;
  73. }
  74. return false;
  75. }
  76. void ScrollControl::Render( Skin::Base* skin )
  77. {
  78. #if 0
  79. // Debug render - this shouldn't render ANYTHING REALLY - it should be up to the parent!
  80. Gwen::Rect rect = GetRenderBounds();
  81. Gwen::Renderer::Base* render = skin->GetRender();
  82. render->SetDrawColor( Gwen::Color( 255, 255, 0, 100 ) );
  83. render->DrawFilledRect( rect );
  84. render->SetDrawColor( Gwen::Color( 255, 0, 0, 100 ) );
  85. render->DrawFilledRect( m_InnerPanel->GetBounds() );
  86. render->RenderText( skin->GetDefaultFont(), Gwen::Point( 0, 0 ), Utility::Format( L"Offset: %i %i", m_InnerPanel->X(), m_InnerPanel->Y() ) );
  87. #else //0
  88. (void)skin;
  89. #endif //0
  90. }
  91. void ScrollControl::UpdateScrollBars()
  92. {
  93. if ( !m_InnerPanel )
  94. return;
  95. int childrenWidth = 0;
  96. int childrenHeight = 0;
  97. //Get the max size of all our children together
  98. for ( Base::List::iterator iter = m_InnerPanel->Children.begin(); iter != m_InnerPanel->Children.end(); ++iter )
  99. {
  100. Base* pChild = *iter;
  101. childrenWidth = Utility::Max( childrenWidth, pChild->Right() );
  102. childrenHeight = Utility::Max( childrenHeight, pChild->Bottom() );
  103. }
  104. m_InnerPanel->SetSize( Utility::Max(Width(), childrenWidth), Utility::Max(Height(), childrenHeight));
  105. float hg = (float)(childrenWidth + (m_VerticalScrollBar->Hidden() ? 0 : m_VerticalScrollBar->Width()));
  106. if (hg==0.f)
  107. hg = 0.00001f;
  108. float wPercent = (float)Width() / hg;
  109. hg = (float)(childrenHeight + (m_HorizontalScrollBar->Hidden() ? 0 : m_HorizontalScrollBar->Height()));
  110. if (hg==0.f)
  111. hg = 0.00001f;
  112. float hPercent = (float)Height() / hg;
  113. if ( m_bCanScrollV )
  114. SetVScrollRequired( hPercent >= 1 );
  115. else
  116. m_VerticalScrollBar->SetHidden( true );
  117. if ( m_bCanScrollH )
  118. SetHScrollRequired( wPercent >= 1 );
  119. else
  120. m_HorizontalScrollBar->SetHidden( true );
  121. m_VerticalScrollBar->SetContentSize( m_InnerPanel->Height() );
  122. m_VerticalScrollBar->SetViewableContentSize( Height() - (m_HorizontalScrollBar->Hidden() ? 0 : m_HorizontalScrollBar->Height()));
  123. m_HorizontalScrollBar->SetContentSize( m_InnerPanel->Width() );
  124. m_HorizontalScrollBar->SetViewableContentSize( Width() - (m_VerticalScrollBar->Hidden() ? 0 : m_VerticalScrollBar->Width()) );
  125. int newInnerPanelPosX = 0;
  126. int newInnerPanelPosY = 0;
  127. if ( CanScrollV() && !m_VerticalScrollBar->Hidden() )
  128. {
  129. newInnerPanelPosY = -( ( m_InnerPanel->Height() ) - Height() + (m_HorizontalScrollBar->Hidden() ? 0 : m_HorizontalScrollBar->Height()) ) * m_VerticalScrollBar->GetScrolledAmount();
  130. }
  131. if ( CanScrollH() && !m_HorizontalScrollBar->Hidden() )
  132. {
  133. newInnerPanelPosX = - ( ( m_InnerPanel->Width() ) - Width() + (m_VerticalScrollBar->Hidden() ? 0 : m_VerticalScrollBar->Width())) * m_HorizontalScrollBar->GetScrolledAmount();
  134. }
  135. m_InnerPanel->SetPos( newInnerPanelPosX , newInnerPanelPosY );
  136. }
  137. void ScrollControl::SetVScrollRequired(bool req)
  138. {
  139. if ( req )
  140. {
  141. m_VerticalScrollBar->SetScrolledAmount( 0, true );
  142. m_VerticalScrollBar->SetDisabled( true );
  143. if ( m_bAutoHideBars )
  144. m_VerticalScrollBar->SetHidden( true );
  145. }
  146. else
  147. {
  148. m_VerticalScrollBar->SetHidden( false );
  149. m_VerticalScrollBar->SetDisabled( false );
  150. }
  151. }
  152. void ScrollControl::SetHScrollRequired(bool req)
  153. {
  154. if ( req )
  155. {
  156. m_HorizontalScrollBar->SetScrolledAmount( 0, true );
  157. m_HorizontalScrollBar->SetDisabled( true );
  158. if ( m_bAutoHideBars )
  159. m_HorizontalScrollBar->SetHidden( true );
  160. }
  161. else
  162. {
  163. m_HorizontalScrollBar->SetHidden( false );
  164. m_HorizontalScrollBar->SetDisabled( true );
  165. }
  166. }
  167. void ScrollControl::ScrollToBottom()
  168. {
  169. if ( CanScrollV() )
  170. {
  171. UpdateScrollBars();
  172. m_VerticalScrollBar->ScrollToBottom();
  173. }
  174. }
  175. void ScrollControl::ScrollToTop()
  176. {
  177. if ( CanScrollV() )
  178. {
  179. UpdateScrollBars();
  180. m_VerticalScrollBar->ScrollToTop();
  181. }
  182. }
  183. void ScrollControl::ScrollToLeft()
  184. {
  185. if ( CanScrollH() )
  186. {
  187. UpdateScrollBars();
  188. m_HorizontalScrollBar->ScrollToLeft();
  189. }
  190. }
  191. void ScrollControl::ScrollToRight()
  192. {
  193. if ( CanScrollH() )
  194. {
  195. UpdateScrollBars();
  196. m_HorizontalScrollBar->ScrollToRight();
  197. }
  198. }
  199. void ScrollControl::Clear()
  200. {
  201. m_InnerPanel->RemoveAllChildren();
  202. }