Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #ifndef VIEWS_TOUCHUI_GESTURE_RECOGNIZER_H_ | |
| 6 #define VIEWS_TOUCHUI_GESTURE_RECOGNIZER_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/memory/linked_ptr.h" | |
| 13 #include "base/memory/singleton.h" | |
| 14 #include "ui/base/events.h" | |
| 15 #include "ui/gfx/point.h" | |
| 16 #include "views/views_export.h" | |
| 17 | |
| 18 namespace views { | |
| 19 class GestureManager; | |
| 20 class TouchEvent; | |
| 21 class GestureEvent; | |
| 22 | |
| 23 // A GestureRecognizer recognizes gestures from touch sequences. | |
| 24 class VIEWS_EXPORT GestureRecognizer { | |
| 25 public: | |
| 26 // Gesture state. | |
| 27 enum GestureState { | |
| 28 GS_NO_GESTURE, | |
| 29 GS_PENDING_SYNTHETIC_CLICK, | |
| 30 GS_SCROLL, | |
| 31 }; | |
| 32 | |
| 33 // Touch state. | |
| 34 enum TouchState { | |
|
sadrul
2011/11/18 19:01:50
Let's move the comment from line 68 to here to exp
Gajen
2011/11/21 15:56:50
Done.
| |
| 35 TS_RELEASED, | |
| 36 TS_PRESSED, | |
| 37 TS_MOVED, | |
| 38 TS_STATIONARY, | |
| 39 TS_CANCELLED, | |
| 40 TS_UNKNOWN, | |
| 41 }; | |
| 42 | |
| 43 // List of GestureEvent*. | |
| 44 typedef std::vector<linked_ptr<GestureEvent> > Gestures; | |
| 45 | |
| 46 GestureRecognizer(); | |
| 47 virtual ~GestureRecognizer(); | |
| 48 | |
| 49 static GestureRecognizer* GetInstance(); | |
| 50 | |
| 51 // Invoked for each touch event that could contribute to the current gesture. | |
| 52 // Returns list of zero or more GestureEvents identified after processing | |
| 53 // TouchEvent. | |
| 54 // Caller would be responsible for freeing up Gestures. | |
| 55 virtual Gestures* ProcessTouchEventForGesture(const TouchEvent& event, | |
| 56 ui::TouchStatus status); | |
| 57 | |
| 58 // Clears the GestureRecognizer to its initial state. | |
| 59 virtual void Reset(); | |
| 60 | |
| 61 // Accessor function. | |
| 62 GestureState GetState() { return state_; } | |
|
sadrul
2011/11/18 19:01:50
Simple accessors are usually like this:
GestureSt
Gajen
2011/11/21 15:56:50
Done.
| |
| 63 | |
| 64 private: | |
| 65 friend struct DefaultSingletonTraits<GestureRecognizer>; | |
| 66 | |
| 67 // Get equivalent TouchState from EventType |type| | |
| 68 // This is required in order to fit TouchState in 3 bits of Signature. | |
| 69 static TouchState TouchEventTypeToTouchState(ui::EventType type); | |
| 70 | |
| 71 // Create a 32 bit signature to identify GestureTransitionFunction. | |
| 72 static unsigned int Signature(GestureState state, | |
| 73 unsigned int touch_id, ui::EventType type, | |
| 74 bool touch_handled); | |
| 75 | |
| 76 // Various statistical functions to manipulate gestures. | |
| 77 bool IsInClickTimeWindow(); | |
| 78 bool IsInSecondClickTimeWindow(); | |
| 79 bool IsInsideManhattanSquare(const TouchEvent& event); | |
| 80 bool IsSecondClickInsideManhattanSquare(const TouchEvent& event); | |
| 81 bool IsOverMinFlickSpeed(); | |
| 82 | |
| 83 // Functions to be called to add GestureEvents, after succesful recognition. | |
| 84 void AppendTapDownGestureEvent(const TouchEvent& event, Gestures* gestures); | |
| 85 void AppendClickGestureEvent(const TouchEvent& event, Gestures* gestures); | |
| 86 void AppendDoubleClickGestureEvent(const TouchEvent& event, | |
| 87 Gestures* gestures); | |
| 88 void AppendScrollGestureBegin(const TouchEvent& event, Gestures* gestures); | |
| 89 void AppendScrollGestureEnd(const TouchEvent& event, | |
| 90 Gestures* gestures, | |
| 91 float x_velocity, float y_velocity); | |
| 92 void AppendScrollGestureUpdate(const TouchEvent& event, Gestures* gestures); | |
| 93 | |
| 94 void UpdateValues(const TouchEvent& event); | |
| 95 void SetState(const GestureState state ) { state_ = state; } | |
| 96 | |
| 97 // Various GestureTransitionFunctions for a signature. | |
| 98 // There is, 1:many mapping from GestureTransitionFunction to Signature | |
| 99 // But a Signature have only one GestureTransitionFunction. | |
| 100 bool Click(const TouchEvent& event, Gestures* gestures); | |
| 101 bool InClickOrScroll(const TouchEvent& event, Gestures* gestures); | |
| 102 bool InScroll(const TouchEvent& event, Gestures* gestures); | |
| 103 bool NoGesture(const TouchEvent& event, Gestures* gestures); | |
| 104 bool TouchDown(const TouchEvent& event, Gestures* gestures); | |
| 105 bool ScrollEnd(const TouchEvent& event, Gestures* gestures); | |
| 106 | |
| 107 // Location of first touch event in a touch sequence. | |
| 108 gfx::Point first_touch_position_; | |
| 109 | |
| 110 // Time of first touch event in a touch sequence. | |
| 111 double first_touch_time_; | |
| 112 | |
| 113 // Current state of gesture recognizer. | |
| 114 GestureState state_; | |
| 115 | |
| 116 // Time of current touch event in a touch sequence. | |
| 117 double last_touch_time_; | |
| 118 | |
| 119 // Time of click gesture. | |
| 120 double last_click_time_; | |
| 121 | |
| 122 // Location of click gesture. | |
| 123 gfx::Point last_click_position_; | |
| 124 | |
| 125 // Location of current touch event in a touch sequence. | |
| 126 gfx::Point last_touch_position_; | |
| 127 | |
| 128 // Velocity in x and y direction. | |
| 129 float x_velocity_; | |
| 130 float y_velocity_; | |
| 131 | |
| 132 // Event flags. | |
| 133 int flags_; | |
|
sadrul
2011/11/18 19:01:50
Are these ui::EventFlags?
Gajen
2011/11/21 15:56:50
Yes, and updated comments too.
| |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(GestureRecognizer); | |
| 136 }; | |
| 137 | |
| 138 } // namespace views | |
| 139 | |
| 140 #endif // VIEWS_TOUCHUI_GESTURE_RECOGNIZER_H_ | |
| OLD | NEW |