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 frome touch sequences. | |
|
sky
2011/11/15 16:54:44
'frome' -> from
Gajen
2011/11/16 15:08:41
Done.
| |
| 24 // | |
|
sky
2011/11/15 16:54:44
remove this line.
Gajen
2011/11/16 15:08:41
Done.
| |
| 25 class VIEWS_EXPORT GestureRecognizer { | |
| 26 public: | |
| 27 // Gesture state. | |
| 28 enum GestureState { | |
| 29 GS_NO_GESTURE, | |
| 30 GS_PENDING_SYNTHETIC_CLICK, | |
| 31 GS_SCROLL, | |
| 32 }; | |
| 33 | |
| 34 // Touch state. | |
| 35 enum TouchState { | |
| 36 TS_RELEASED, | |
| 37 TS_PRESSED, | |
| 38 TS_MOVED, | |
| 39 TS_STATIONARY, | |
| 40 TS_CANCELLED, | |
| 41 TS_UNKNOWN, | |
| 42 }; | |
| 43 | |
| 44 // List of GestureEvent* | |
| 45 typedef std::vector< linked_ptr<GestureEvent> > Gestures; | |
|
sky
2011/11/15 16:54:44
Do these need to be public? Also, we generally don
Gajen
2011/11/16 15:08:41
Done.
| |
| 46 typedef bool (GestureRecognizer::*GestureTransitionFunction)( | |
| 47 const TouchEvent&, Gestures*); | |
| 48 | |
| 49 GestureRecognizer(); | |
| 50 virtual ~GestureRecognizer(); | |
| 51 | |
| 52 static GestureRecognizer* GetInstance(); | |
| 53 | |
| 54 // Invoked for each touch event that could contribute to the current gesture. | |
| 55 // Returns list of zero or more GestureEvents identified after processing | |
| 56 // TouchEvent. | |
| 57 // Caller would be responsible for freeing up Gestures. | |
| 58 virtual Gestures* ProcessTouchEventForGesture(const TouchEvent& event, | |
| 59 ui::TouchStatus status); | |
| 60 | |
| 61 // Clears the GestureRecognizer to its initial state. | |
| 62 virtual void Reset(); | |
| 63 | |
| 64 // Accessor function. | |
| 65 GestureState GetState() { return state_; } | |
| 66 | |
| 67 private: | |
| 68 friend struct DefaultSingletonTraits<GestureRecognizer>; | |
| 69 | |
| 70 // Get equivalent TouchState from EventType |type| | |
| 71 // This is required in order to fit TouchState in 3 bits of Signature. | |
| 72 static TouchState TouchEventTypeToTouchState(ui::EventType type); | |
| 73 | |
| 74 // Create a 32 bit signature to identify GestureTransitionFunction. | |
| 75 static unsigned int Signature(GestureState state, | |
| 76 unsigned int touch_id, ui::EventType type, | |
| 77 bool touch_handled); | |
| 78 | |
| 79 // Creates a map of Singnatures and corresponding GestureTransitionFunction. | |
| 80 void AddEdgeFunction(GestureState state, | |
| 81 unsigned int touch_id, | |
| 82 ui::EventType type, | |
| 83 bool touch_handled, | |
| 84 GestureTransitionFunction function); | |
| 85 | |
| 86 // Various statistical functions to manipulate gestures. | |
| 87 bool IsInClickTimeWindow(); | |
| 88 bool IsInSecondClickTimeWindow(); | |
| 89 bool IsInsideManhattanSquare(const TouchEvent& event); | |
| 90 bool IsSecondClickInsideManhattanSquare(const TouchEvent& event); | |
| 91 bool IsOverMinFlickSpeed(); | |
| 92 | |
| 93 // Functions to be called to add GestureEvents, after succesful recognition. | |
| 94 void AppendTapDownGestureEvent(const TouchEvent& event, Gestures* gestures); | |
| 95 void AppendClickGestureEvent(const TouchEvent& event, Gestures* gestures); | |
| 96 void AppendDoubleClickGestureEvent(const TouchEvent& event, | |
| 97 Gestures* gestures); | |
| 98 void AppendScrollGestureBegin(const TouchEvent& event, Gestures* gestures); | |
| 99 void AppendScrollGestureEnd(const TouchEvent& event, | |
| 100 Gestures* gestures, | |
| 101 float x_velocity, float y_velocity); | |
| 102 void AppendScrollGestureUpdate(const TouchEvent& event, Gestures* gestures); | |
| 103 | |
| 104 void UpdateValues(const TouchEvent& event); | |
| 105 void SetState(const GestureState state ) { state_ = state; } | |
| 106 | |
| 107 // Various GestureTransitionFunctions for a signature. | |
| 108 // There is, 1:many mapping from GestureTransitionFunction to Signature | |
| 109 // But a Signature have only one GestureTransitionFunction. | |
| 110 bool Click(const TouchEvent& event, Gestures* gestures); | |
| 111 bool InClickOrScroll(const TouchEvent& event, Gestures* gestures); | |
| 112 bool InScroll(const TouchEvent& event, Gestures* gestures); | |
| 113 bool NoGesture(const TouchEvent& event, Gestures* gestures); | |
| 114 bool TouchDown(const TouchEvent& event, Gestures* gestures); | |
| 115 bool ScrollEnd(const TouchEvent& event, Gestures* gestures); | |
| 116 | |
| 117 typedef std::map<int, GestureTransitionFunction> GestureTransitionFunctionMap; | |
|
sky
2011/11/15 16:54:44
typedefs are generally first in a section.
Gajen
2011/11/16 15:08:41
Done.
| |
| 118 GestureTransitionFunctionMap edge_functions_; | |
| 119 | |
| 120 gfx::Point first_touch_position_; | |
|
sky
2011/11/15 16:54:44
Add descriptions for your fields.
Gajen
2011/11/16 15:08:41
Done.
| |
| 121 | |
| 122 double first_touch_time_; | |
| 123 GestureState state_; | |
| 124 | |
| 125 double last_touch_time_; | |
| 126 double last_click_time_; | |
| 127 gfx::Point last_click_position_; | |
| 128 gfx::Point last_touch_position_; | |
| 129 float x_velocity_; | |
| 130 float y_velocity_; | |
| 131 int flags_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(GestureRecognizer); | |
| 134 }; | |
| 135 | |
| 136 } // namespace views | |
| 137 | |
| 138 #endif // VIEWS_TOUCHUI_GESTURE_RECOGNIZER_H_ | |
| OLD | NEW |