scrollbarcontainer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // -----------------------------------------------------------
  2. class ScrollBarContainer : ScriptedWidgetEventHandler
  3. {
  4. reference bool Invert;
  5. protected Widget Content;
  6. protected Widget ScrollBar;
  7. protected Widget Scroller;
  8. protected Widget m_root;
  9. const int WHEEL_STEP = 20;
  10. protected float m_root_height;
  11. protected float m_content_height;
  12. protected float m_position;
  13. protected bool m_scrolling;
  14. protected float m_scrolling_start_pos;
  15. protected int m_scrolling_mouse_pos;
  16. void ~ScrollBarContainer()
  17. {
  18. //if(GetGame() != NULL)
  19. //GetGame().GetDragQueue().RemoveCalls(this);
  20. }
  21. void ScrollFixedAmount( bool down, float amount )
  22. {
  23. m_root.Update();
  24. Content.Update();
  25. float width;
  26. m_root.GetScreenSize(width, m_root_height);
  27. Content.GetScreenSize(width, m_content_height);
  28. float diff = m_root_height / m_content_height;
  29. float one_percent = diff / 100;
  30. float percents = amount / m_content_height;
  31. //float step = (1.0 / (m_content_height - m_root_height)) * WHEEL_STEP;
  32. float step = (percents/100);
  33. if(down)
  34. m_position += 1 * ( percents + 0.05 );
  35. else
  36. m_position -= 1 * ( percents + 0.05 );
  37. if (m_position < 0) m_position = 0;
  38. if (m_position > 1 - diff) m_position = 1 - diff;
  39. UpdateScroller();
  40. }
  41. void ScrollToPos( float pos )
  42. {
  43. m_root.Update();
  44. Content.Update();
  45. float width;
  46. m_root.GetScreenSize(width, m_root_height);
  47. Content.GetScreenSize(width, m_content_height);
  48. float diff = m_root_height / m_content_height;
  49. float percents = pos / m_content_height;
  50. m_position = percents;
  51. if (m_position < 0)
  52. m_position = 0;
  53. if (m_position > 1 - diff)
  54. m_position = 1 - diff;
  55. UpdateScroller();
  56. }
  57. void ScrollToBottom()
  58. {
  59. m_root.Update();
  60. Content.Update();
  61. float width;
  62. m_root.GetScreenSize(width, m_root_height);
  63. Content.GetScreenSize(width, m_content_height);
  64. float diff = m_root_height / m_content_height;
  65. m_position = 1 - diff;
  66. UpdateScroller();
  67. }
  68. void ScrollToTop()
  69. {
  70. if( m_position != 0 )
  71. {
  72. m_position = 0;
  73. UpdateScroller();
  74. }
  75. }
  76. float GetContentYPos()
  77. {
  78. float x, y;
  79. Content.GetPos(x, y);
  80. return y;
  81. }
  82. float GetRootHeight()
  83. {
  84. return m_root_height;
  85. }
  86. void UpdateScroller()
  87. {
  88. m_root.Update();
  89. Content.Update();
  90. float width;
  91. float height;
  92. float diff;
  93. float scroller_height;
  94. m_root.GetScreenSize(width, m_root_height);
  95. Content.GetScreenSize(width, m_content_height);
  96. diff = m_content_height - m_root_height;
  97. if (diff <= 0)
  98. {
  99. Content.SetPos(0,0);
  100. Scroller.Show(false);
  101. ScrollBar.Show(false);
  102. m_position = 0;
  103. return;
  104. }
  105. scroller_height = (m_root_height / m_content_height) * m_root_height;
  106. ScrollBar.Show(true);
  107. Scroller.Show(true);
  108. Scroller.GetSize(width, height);
  109. Scroller.SetSize(width, scroller_height);
  110. float pos = ( -m_content_height * m_position );
  111. if( pos <= -diff )
  112. pos = -diff;
  113. Scroller.SetPos(0, -pos);
  114. if(Invert)
  115. Content.SetPos(0, -(diff + (-diff * m_position)));
  116. else
  117. Content.SetPos(0, pos);
  118. }
  119. void OnWidgetScriptInit(Widget w)
  120. {
  121. m_root = w;
  122. m_root.SetHandler(this);
  123. m_root.SetFlags(WidgetFlags.VEXACTPOS);
  124. m_scrolling = false;
  125. UpdateScroller();
  126. }
  127. protected void StopScrolling()
  128. {
  129. if (m_scrolling)
  130. {
  131. GetGame().GetDragQueue().RemoveCalls(this);
  132. m_scrolling = false;
  133. }
  134. }
  135. protected void UpdateScroll(int mouse_x, int mouse_y, bool is_dragging)
  136. {
  137. m_root.Update();
  138. Content.Update();
  139. float width;
  140. m_root.GetScreenSize(width, m_root_height);
  141. Content.GetScreenSize(width, m_content_height);
  142. if (m_scrolling)
  143. {
  144. if (is_dragging)
  145. {
  146. float diff = (mouse_y - m_scrolling_mouse_pos);
  147. float scroller_height = (m_root_height / m_content_height) * m_root_height;
  148. m_position = m_scrolling_start_pos + (diff / (m_root_height - scroller_height));
  149. if (m_position < 0) m_position = 0;
  150. if (m_position > 1) m_position = 1;
  151. }
  152. else
  153. {
  154. m_scrolling = false;
  155. StopScrolling();
  156. }
  157. }
  158. UpdateScroller();
  159. }
  160. // ScriptedWidgetEventHandler override
  161. //--------------------------------------------------------------------------
  162. override bool OnMouseButtonDown(Widget w, int x, int y, int button)
  163. {
  164. if (button == MouseState.LEFT && w == Scroller && !m_scrolling)
  165. {
  166. m_scrolling = true;
  167. m_scrolling_start_pos = m_position;
  168. int mouse_x;
  169. GetMousePos(mouse_x, m_scrolling_mouse_pos);
  170. GetGame().GetDragQueue().Call(this, "UpdateScroll");
  171. return true;
  172. }
  173. return false;
  174. }
  175. //--------------------------------------------------------------------------
  176. override bool OnMouseButtonUp(Widget w, int x, int y, int button)
  177. {
  178. StopScrolling();
  179. return false;
  180. }
  181. //--------------------------------------------------------------------------
  182. override bool OnMouseWheel(Widget w, int x, int y, int wheel)
  183. {
  184. if (m_scrolling || m_content_height <= m_root_height) return false;
  185. float step = (1.0 / (m_content_height - m_root_height)) * WHEEL_STEP;
  186. m_position -= wheel * step;
  187. if (m_position < 0) m_position = 0;
  188. if (m_position > 1) m_position = 1;
  189. UpdateScroller();
  190. return true;
  191. }
  192. override bool OnResize( Widget w, int x, int y) {
  193. if (w == m_root || w == Content)
  194. {
  195. UpdateScroller();
  196. }
  197. return false;
  198. }
  199. };