1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_EVENTS_GESTURES_GESTURE_SEQUENCE_H_
6 #define UI_EVENTS_GESTURES_GESTURE_SEQUENCE_H_
8 #include "base/timer/timer.h"
9 #include "ui/events/event_constants.h"
10 #include "ui/events/gestures/gesture_point.h"
11 #include "ui/events/gestures/gesture_recognizer.h"
12 #include "ui/gfx/rect.h"
21 GS_PENDING_SYNTHETIC_CLICK,
22 GS_PENDING_SYNTHETIC_CLICK_NO_SCROLL,
25 GS_PENDING_TWO_FINGER_TAP,
26 GS_PENDING_TWO_FINGER_TAP_NO_PINCH,
28 GS_PENDING_PINCH_NO_PINCH,
37 // Delegates dispatch of gesture events for which the GestureSequence does not
38 // have enough context to dispatch itself.
39 class EVENTS_EXPORT GestureSequenceDelegate {
41 virtual void DispatchPostponedGestureEvent(GestureEvent* event) = 0;
44 virtual ~GestureSequenceDelegate() {}
47 // A GestureSequence recognizes gestures from touch sequences.
48 class EVENTS_EXPORT GestureSequence {
50 // Maximum number of points in a single gesture.
51 static const int kMaxGesturePoints = 12;
53 explicit GestureSequence(GestureSequenceDelegate* delegate);
54 virtual ~GestureSequence();
56 typedef GestureRecognizer::Gestures Gestures;
58 // Invoked for each touch event that could contribute to the current gesture.
59 // Returns list of zero or more GestureEvents identified after processing
61 // Caller would be responsible for freeing up Gestures.
62 virtual Gestures* ProcessTouchEventForGesture(const TouchEvent& event,
64 const GesturePoint* points() const { return points_; }
65 int point_count() const { return point_count_; }
67 const gfx::Point& last_touch_location() const { return last_touch_location_; }
70 virtual base::OneShotTimer<GestureSequence>* CreateTimer();
71 base::OneShotTimer<GestureSequence>* GetLongPressTimer();
72 base::OneShotTimer<GestureSequence>* GetShowPressTimer();
75 // Recreates the axis-aligned bounding box that contains all the touch-points
76 // at their most recent position.
77 void RecreateBoundingBox();
79 void ResetVelocities();
81 GesturePoint& GesturePointForEvent(const TouchEvent& event);
83 // Do a linear scan through points_ to find the GesturePoint
84 // with id |point_id|.
85 GesturePoint* GetPointByPointId(int point_id);
87 bool IsSecondTouchDownCloseEnoughForTwoFingerTap();
89 // Creates a gesture event with the specified parameters. The function
90 // includes some common information (e.g. number of touch-points in the
91 // gesture etc.) in the gesture event as well.
92 GestureEvent* CreateGestureEvent(const GestureEventDetails& details,
93 const gfx::Point& location,
96 unsigned int touch_id_bitmask);
98 // Functions to be called to add GestureEvents, after successful recognition.
101 void AppendTapDownGestureEvent(const GesturePoint& point, Gestures* gestures);
102 void PrependTapCancelGestureEvent(const GesturePoint& point,
104 void AppendBeginGestureEvent(const GesturePoint& point, Gestures* gestures);
105 void AppendEndGestureEvent(const GesturePoint& point, Gestures* gestures);
106 void AppendClickGestureEvent(const GesturePoint& point,
109 void AppendDoubleClickGestureEvent(const GesturePoint& point,
111 void AppendLongPressGestureEvent();
112 void AppendShowPressGestureEvent();
113 void AppendLongTapGestureEvent(const GesturePoint& point,
117 void AppendScrollGestureBegin(const GesturePoint& point,
118 const gfx::Point& location,
120 void AppendScrollGestureEnd(const GesturePoint& point,
121 const gfx::Point& location,
125 void AppendScrollGestureUpdate(GesturePoint& point,
129 void AppendPinchGestureBegin(const GesturePoint& p1,
130 const GesturePoint& p2,
132 void AppendPinchGestureEnd(const GesturePoint& p1,
133 const GesturePoint& p2,
136 void AppendPinchGestureUpdate(const GesturePoint& point,
139 void AppendSwipeGesture(const GesturePoint& point,
143 void AppendTwoFingerTapGestureEvent(Gestures* gestures);
145 void set_state(const GestureState state) { state_ = state; }
147 // Various GestureTransitionFunctions for a signature.
148 // There is, 1:many mapping from GestureTransitionFunction to Signature
149 // But a Signature have only one GestureTransitionFunction.
150 bool Click(const TouchEvent& event,
151 const GesturePoint& point,
153 bool ScrollStart(const TouchEvent& event,
156 void BreakRailScroll(const TouchEvent& event,
159 bool ScrollUpdate(const TouchEvent& event,
162 bool TouchDown(const TouchEvent& event,
163 const GesturePoint& point,
165 bool TwoFingerTouchDown(const TouchEvent& event,
166 const GesturePoint& point,
168 bool TwoFingerTouchMove(const TouchEvent& event,
169 const GesturePoint& point,
171 bool TwoFingerTouchReleased(const TouchEvent& event,
172 const GesturePoint& point,
174 bool ScrollEnd(const TouchEvent& event,
177 bool PinchStart(const TouchEvent& event,
178 const GesturePoint& point,
180 bool PinchUpdate(const TouchEvent& event,
183 bool PinchEnd(const TouchEvent& event,
184 const GesturePoint& point,
186 bool MaybeSwipe(const TouchEvent& event,
187 const GesturePoint& point,
190 void TwoFingerTapOrPinch(const TouchEvent& event,
191 const GesturePoint& point,
194 void StopTimersIfRequired(const TouchEvent& event);
196 // Current state of gesture recognizer.
202 // We maintain the smallest axis-aligned rectangle that contains all the
203 // current touch-points. This box is updated after every touch-event.
204 gfx::Rect bounding_box_;
206 // The center of the bounding box used in the latest multi-finger scroll
208 gfx::Point latest_multi_scroll_update_location_;
210 // The last scroll update prediction offset. This is removed from the scroll
211 // distance on the next update since the page has already been scrolled this
213 gfx::Vector2dF last_scroll_prediction_offset_;
215 // For pinch, the 'distance' represents the diagonal distance of
218 // The distance between the two points at PINCH_START.
219 float pinch_distance_start_;
221 // This distance is updated after each PINCH_UPDATE.
222 float pinch_distance_current_;
224 // This is the time when second touch down was received. Used for determining
225 // if a two finger double tap has happened.
226 base::TimeDelta second_touch_time_;
228 ScrollType scroll_type_;
229 scoped_ptr<base::OneShotTimer<GestureSequence> > long_press_timer_;
230 scoped_ptr<base::OneShotTimer<GestureSequence> > show_press_timer_;
232 GesturePoint points_[kMaxGesturePoints];
235 // Location of the last touch event.
236 gfx::Point last_touch_location_;
238 GestureSequenceDelegate* delegate_;
240 DISALLOW_COPY_AND_ASSIGN(GestureSequence);
245 #endif // UI_EVENTS_GESTURES_GESTURE_SEQUENCE_H_