1 | #ifndef FASTUIDRAW_DEMO_PANZOOMTRACKER_HPP |
2 | #define FASTUIDRAW_DEMO_PANZOOMTRACKER_HPP |
3 | |
4 | #include <SDL.h> |
5 | #include "ScaleTranslate.hpp" |
6 | #include "simple_time.hpp" |
7 | |
8 | /*!\class PanZoomTracker |
9 | A PanZoomTracker implements the following gesture: |
10 | - panning while dragging |
11 | - hold button down (long) time, then up or left zoom out, |
12 | down or right zoom in. |
13 | */ |
14 | class PanZoomTracker |
15 | { |
16 | public: |
17 | enum zoom_direction_t |
18 | { |
19 | zoom_direction_negative_y, |
20 | zoom_direction_positive_y |
21 | }; |
22 | |
23 | PanZoomTracker(int32_t zoom_gesture_begin_time_ms=500, |
24 | float zoom_divider=40.0f): |
25 | m_scale_zooming(1.0f), |
26 | m_zoom_direction(zoom_direction_positive_y), |
27 | m_zoom_gesture_begin_time(zoom_gesture_begin_time_ms), |
28 | m_zoom_divider(zoom_divider), |
29 | m_is_zooming(false), |
30 | m_button_down(false) |
31 | {} |
32 | |
33 | |
34 | const ScaleTranslate<float>& |
35 | transformation(void) const |
36 | { |
37 | return m_transformation; |
38 | } |
39 | |
40 | void |
41 | transformation(const ScaleTranslate<float> &v); |
42 | |
43 | /*!\fn |
44 | Tell PanZoomTracker of a motion event |
45 | \param pos position of event |
46 | \param delta amunt of motion of event of event |
47 | */ |
48 | void |
49 | handle_motion(const fastuidraw::vec2 &pos, |
50 | const fastuidraw::vec2 &delta); |
51 | |
52 | /*!\fn |
53 | Tell PanZoomTracker of down (i.e begin gesture) event |
54 | \param pos position of event |
55 | */ |
56 | void |
57 | handle_down(const fastuidraw::vec2 &pos); |
58 | |
59 | /*!\fn |
60 | Tell PanZoomTracker of button up (i.e. end gesture) event |
61 | */ |
62 | void |
63 | handle_up(void); |
64 | |
65 | /*! |
66 | Scale zooming factor |
67 | */ |
68 | float m_scale_zooming; |
69 | |
70 | enum zoom_direction_t m_zoom_direction; |
71 | |
72 | private: |
73 | |
74 | int32_t m_zoom_gesture_begin_time; |
75 | float m_zoom_divider; |
76 | |
77 | fastuidraw::vec2 m_zoom_pivot; |
78 | simple_time m_zoom_time; |
79 | bool m_is_zooming, m_button_down; |
80 | |
81 | ScaleTranslate<float> m_transformation, m_start_gesture; |
82 | }; |
83 | |
84 | |
85 | class PanZoomTrackerSDLEvent:public PanZoomTracker |
86 | { |
87 | public: |
88 | PanZoomTrackerSDLEvent(int32_t zoom_gesture_begin_time_ms=500, |
89 | float zoom_divider=40.0f): |
90 | PanZoomTracker(zoom_gesture_begin_time_ms, |
91 | zoom_divider), |
92 | m_scale_event(1.0f, 1.0f), |
93 | m_translate_event(0.0f, 0.0f) |
94 | {} |
95 | |
96 | /*!\fn |
97 | Maps to handle_drag, handle_button_down, handle_button_up |
98 | from events on mouse button 0 |
99 | */ |
100 | void |
101 | handle_event(const SDL_Event &ev); |
102 | |
103 | /*!\fn |
104 | Amount by which to scale events |
105 | */ |
106 | fastuidraw::vec2 m_scale_event, m_translate_event; |
107 | }; |
108 | |
109 | #endif |
110 | |