Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_scroller.h
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 
6 #ifndef TB_SCROLLER_H
7 #define TB_SCROLLER_H
8 
9 #include "tb_core.h"
10 #include "tb_msg.h"
11 
12 namespace tb {
13 
14 class TBWidget;
15 
23 {
24 public:
25  TBScrollerFunction(float decay) : m_decay(decay) {}
26 
29  float GetDurationFromSpeed(float start_speed);
30 
32  float GetSpeedFromDistance(float distance);
33 
35  float GetDistanceAtTime(float start_speed, float elapsed_time_ms);
36 
38  int GetDistanceAtTimeInt(float start_speed, float elapsed_time_ms);
39 private:
40  float m_decay;
41 };
42 
46 {
47 public:
48  virtual ~TBScrollerSnapListener() {};
49 
58  virtual void OnScrollSnap(TBWidget *target_widget, int &target_x, int &target_y) = 0;
59 };
60 
65 {
66 public:
67  TBScroller(TBWidget *target);
68  ~TBScroller();
69 
71  void SetSnapListener(TBScrollerSnapListener *listener) { m_snap_listener = listener; }
72 
74  void Start();
75 
78  void Stop();
79 
81  bool IsStarted() const { return m_is_started; }
82 
85  TBWidget *GetTarget() const { return m_target; }
86 
90  bool OnPan(int dx, int dy);
91 
94  void OnPanReleased();
95 
102  void OnScrollBy(int dx, int dy, bool accumulative);
103 private:
104  virtual void OnMessageReceived(TBMessage *msg);
105  bool IsScrolling();
106  bool StopIfAlmostStill();
107  void StopOrSnapScroll();
108  void Reset();
109  void AdjustToSnappingAndScroll(float ppms_x, float ppms_y);
110  void Scroll(float start_speed_ppms_x, float start_speed_ppms_y);
111  void GetTargetChildTranslation(int &x, int &y) const;
112  void GetTargetScrollXY(int &x, int &y) const;
113  TBWidget *m_target;
114  TBScrollerSnapListener *m_snap_listener;
115  TBScrollerFunction m_func;
116  bool m_is_started;
117  float m_pan_dx, m_pan_dy;
118  float m_previous_pan_dx, m_previous_pan_dy;
119  double m_pan_time_ms;
120  double m_pan_delta_time_ms;
121  float m_scroll_start_speed_ppms_x, m_scroll_start_speed_ppms_y;
122  double m_scroll_start_ms;
123  float m_scroll_duration_x_ms, m_scroll_duration_y_ms;
124  int m_scroll_start_scroll_x, m_scroll_start_scroll_y;
125  float m_pan_power_multiplier_x;
126  float m_pan_power_multiplier_y;
127  int m_expected_scroll_x;
128  int m_expected_scroll_y;
129 };
130 
131 } // namespace tb
132 
133 #endif // TB_SCROLLER_H
The base TBWidget class.
Definition: tb_widgets.h:418
void OnScrollBy(int dx, int dy, bool accumulative)
Start the scroller based on the given delta.
Definition: tb_scroller.cpp:95
void Start()
Start tracking pan movement from calls to OnPan.
Definition: tb_scroller.cpp:195
float GetSpeedFromDistance(float distance)
Calculate the start speed needed to reach the given distance.
Definition: tb_scroller.cpp:39
void SetSnapListener(TBScrollerSnapListener *listener)
Set the listener that may override the target scroll position.
Definition: tb_scroller.h:71
TBWidget * GetTarget() const
Get the widget that will be panned/scrolled.
Definition: tb_scroller.h:85
void OnPanReleased()
The panning ends and the scroller should start scrolling.
Definition: tb_scroller.cpp:165
TBScrollerFunction does the calculations of time, speed and distance that decides how the slow down o...
Definition: tb_scroller.h:22
int GetDistanceAtTimeInt(float start_speed, float elapsed_time_ms)
Same as GetDistanceAtTime but rounded to integer.
Definition: tb_scroller.cpp:55
TBMessageHandler handles a list of pending messages posted to itself.
Definition: tb_msg.h:72
float GetDistanceAtTime(float start_speed, float elapsed_time_ms)
Calculate the distance reached at the given elapsed_time_ms with the given start_speed.
Definition: tb_scroller.cpp:49
TBMessage is a message created and owned by TBMessageHandler.
Definition: tb_msg.h:47
bool OnPan(int dx, int dy)
Pan the target widget (or any parent) with the given deltas.
Definition: tb_scroller.cpp:130
bool IsStarted() const
Return true if the pan tracking is started or.
Definition: tb_scroller.h:81
TBScroller handles panning while the pointer is down and measure the pan speed over time...
Definition: tb_scroller.h:64
TBScrollerSnapListener may override the target scroll position of a TBScroller.
Definition: tb_scroller.h:45
void Stop()
Stop tracking pan movement from calls to OnPan, or stop any ongoing scrolling.
Definition: tb_scroller.cpp:212
float GetDurationFromSpeed(float start_speed)
Calculate the duration needed until the end distance is reached from the given start speed...
Definition: tb_scroller.cpp:31
virtual void OnScrollSnap(TBWidget *target_widget, int &target_x, int &target_y)=0
Called when the target scroll position is calculated.