tb_scroller.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_SCROLLER_H
  6. #define TB_SCROLLER_H
  7. #include "tb_core.h"
  8. #include "tb_msg.h"
  9. namespace tb {
  10. class TBWidget;
  11. /** TBScrollerFunction does the calculations of time, speed and distance
  12. that decides how the slow down of a scroll will happen.
  13. Note: Speed is in pixels per millisecond. Duration is in milliseconds
  14. and distance is in pixels. Distance and speed may be negative! */
  15. class TBScrollerFunction
  16. {
  17. public:
  18. TBScrollerFunction(float decay) : m_decay(decay) {}
  19. /** Calculate the duration needed until the end distance is reached
  20. from the given start speed. */
  21. float GetDurationFromSpeed(float start_speed);
  22. /** Calculate the start speed needed to reach the given distance. */
  23. float GetSpeedFromDistance(float distance);
  24. /** Calculate the distance reached at the given elapsed_time_ms with the given start_speed. */
  25. float GetDistanceAtTime(float start_speed, float elapsed_time_ms);
  26. /** Same as GetDistanceAtTime but rounded to integer. */
  27. int GetDistanceAtTimeInt(float start_speed, float elapsed_time_ms);
  28. private:
  29. float m_decay;
  30. };
  31. /** TBScrollerSnapListener may override the target scroll position of a TBScroller. */
  32. class TBScrollerSnapListener
  33. {
  34. public:
  35. virtual ~TBScrollerSnapListener() {};
  36. /** Called when the target scroll position is calculated.
  37. target_widget is the widget being scroller.
  38. target_x, target_y is the suggested target scroll position which may be changed
  39. to something else in this call.
  40. Note: The scroll positions are relative to the target widget (inner scrolled TBWidget).
  41. If there's nested scrollable widgets, only the inner scrolled widget applies snapping. */
  42. virtual void OnScrollSnap(TBWidget *target_widget, int &target_x, int &target_y) = 0;
  43. };
  44. /** TBScroller handles panning while the pointer is down and measure the pan
  45. speed over time. It also handles continued scrolling when the pointer has
  46. been released with a flick. */
  47. class TBScroller : private TBMessageHandler
  48. {
  49. public:
  50. TBScroller(TBWidget *target);
  51. ~TBScroller();
  52. /** Set the listener that may override the target scroll position. */
  53. void SetSnapListener(TBScrollerSnapListener *listener) { m_snap_listener = listener; }
  54. /** Start tracking pan movement from calls to OnPan. */
  55. void Start();
  56. /** Stop tracking pan movement from calls to OnPan,
  57. or stop any ongoing scrolling. */
  58. void Stop();
  59. /** Return true if the pan tracking is started or. */
  60. bool IsStarted() const { return m_is_started; }
  61. /** Get the widget that will be panned/scrolled. Any parent of this
  62. widget may also be panned/scrolled. */
  63. TBWidget *GetTarget() const { return m_target; }
  64. /** Pan the target widget (or any parent) with the given deltas.
  65. Should be called while the pointer is down.
  66. This will track the pan speed over time. */
  67. bool OnPan(int dx, int dy);
  68. /** The panning ends and the scroller should start scrolling.
  69. Should be called when the pointer is released. */
  70. void OnPanReleased();
  71. /** Start the scroller based on the given delta. Doesn't
  72. require previous calls to OnPan or OnPanReleased.
  73. If accumulative is true, the given delta will be
  74. added to any on going scroll. If it's false, any
  75. ongoing scroll will be canceled. */
  76. void OnScrollBy(int dx, int dy, bool accumulative);
  77. private:
  78. virtual void OnMessageReceived(TBMessage *msg);
  79. bool IsScrolling();
  80. bool StopIfAlmostStill();
  81. void StopOrSnapScroll();
  82. void Reset();
  83. void AdjustToSnappingAndScroll(float ppms_x, float ppms_y);
  84. void Scroll(float start_speed_ppms_x, float start_speed_ppms_y);
  85. void GetTargetChildTranslation(int &x, int &y) const;
  86. void GetTargetScrollXY(int &x, int &y) const;
  87. TBWidget *m_target;
  88. TBScrollerSnapListener *m_snap_listener;
  89. TBScrollerFunction m_func;
  90. bool m_is_started;
  91. float m_pan_dx, m_pan_dy;
  92. float m_previous_pan_dx, m_previous_pan_dy;
  93. double m_pan_time_ms;
  94. double m_pan_delta_time_ms;
  95. float m_scroll_start_speed_ppms_x, m_scroll_start_speed_ppms_y;
  96. double m_scroll_start_ms;
  97. float m_scroll_duration_x_ms, m_scroll_duration_y_ms;
  98. int m_scroll_start_scroll_x, m_scroll_start_scroll_y;
  99. float m_pan_power_multiplier_x;
  100. float m_pan_power_multiplier_y;
  101. int m_expected_scroll_x;
  102. int m_expected_scroll_y;
  103. };
  104. }; // namespace tb
  105. #endif // TB_SCROLLER_H