| 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 // ui::EventType is mapped to TouchState so it can fit into 3 bits of |
| 34 // Signature. |
| 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; |
| 46 |
| 47 GestureRecognizer(); |
| 48 virtual ~GestureRecognizer(); |
| 49 |
| 50 static GestureRecognizer* GetInstance(); |
| 51 |
| 52 // Invoked for each touch event that could contribute to the current gesture. |
| 53 // Returns list of zero or more GestureEvents identified after processing |
| 54 // TouchEvent. |
| 55 // Caller would be responsible for freeing up Gestures. |
| 56 virtual Gestures* ProcessTouchEventForGesture(const TouchEvent& event, |
| 57 ui::TouchStatus status); |
| 58 |
| 59 // Clears the GestureRecognizer to its initial state. |
| 60 virtual void Reset(); |
| 61 |
| 62 // Accessor function. |
| 63 GestureState GetState() const { return state_; } |
| 64 |
| 65 private: |
| 66 friend struct DefaultSingletonTraits<GestureRecognizer>; |
| 67 |
| 68 // Gesture signature types for different values of combination (GestureState, |
| 69 // touch_id, ui::EventType, touch_handled), see GestureRecognizer::Signature() |
| 70 // for more info. |
| 71 // |
| 72 // Note: New addition of types should be placed as per their Signature value. |
| 73 enum GestureSignatureType { |
| 74 // For input combination (GS_NO_GESTURE, 0, ui::ET_TOUCH_PRESSED, false). |
| 75 GST_NO_GESTURE_FIRST_PRESSED = 0x00000003, |
| 76 |
| 77 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_RELEASED, false). |
| 78 GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED = 0x00020001, |
| 79 |
| 80 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_MOVED, false). |
| 81 GST_PENDING_SYNTHETIC_CLICK_FIRST_MOVED = 0x00020005, |
| 82 |
| 83 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_STATIONARY, false). |
| 84 GST_PENDING_SYNTHETIC_CLICK_FIRST_STATIONARY = 0x00020007, |
| 85 |
| 86 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_CANCELLED, false). |
| 87 GST_PENDING_SYNTHETIC_CLICK_FIRST_CANCELLED = 0x00020009, |
| 88 |
| 89 // (GS_SCROLL, 0, ui::ET_TOUCH_RELEASED, false). |
| 90 GST_SCROLL_FIRST_RELEASED = 0x00040001, |
| 91 |
| 92 // (GS_SCROLL, 0, ui::ET_TOUCH_MOVED, false). |
| 93 GST_SCROLL_FIRST_MOVED = 0x00040005, |
| 94 |
| 95 // (GS_SCROLL, 0, ui::ET_TOUCH_CANCELLED, false). |
| 96 GST_SCROLL_FIRST_CANCELLED = 0x00040009, |
| 97 }; |
| 98 |
| 99 // Get equivalent TouchState from EventType |type|. |
| 100 static TouchState TouchEventTypeToTouchState(ui::EventType type); |
| 101 |
| 102 // Builds a signature. Signatures are assembled by joining together |
| 103 // multiple bits. |
| 104 // 1 LSB bit so that the computed signature is always greater than 0 |
| 105 // 3 bits for the |type|. |
| 106 // 1 bit for |touch_handled| |
| 107 // 12 bits for |touch_id| |
| 108 // 15 bits for the |gesture_state|. |
| 109 static unsigned int Signature(GestureState state, |
| 110 unsigned int touch_id, ui::EventType type, |
| 111 bool touch_handled); |
| 112 |
| 113 // Various statistical functions to manipulate gestures. |
| 114 bool IsInClickTimeWindow(); |
| 115 bool IsInSecondClickTimeWindow(); |
| 116 bool IsInsideManhattanSquare(const TouchEvent& event); |
| 117 bool IsSecondClickInsideManhattanSquare(const TouchEvent& event); |
| 118 bool IsOverMinFlickSpeed(); |
| 119 |
| 120 // Functions to be called to add GestureEvents, after succesful recognition. |
| 121 void AppendTapDownGestureEvent(const TouchEvent& event, Gestures* gestures); |
| 122 void AppendClickGestureEvent(const TouchEvent& event, Gestures* gestures); |
| 123 void AppendDoubleClickGestureEvent(const TouchEvent& event, |
| 124 Gestures* gestures); |
| 125 void AppendScrollGestureBegin(const TouchEvent& event, Gestures* gestures); |
| 126 void AppendScrollGestureEnd(const TouchEvent& event, |
| 127 Gestures* gestures, |
| 128 float x_velocity, float y_velocity); |
| 129 void AppendScrollGestureUpdate(const TouchEvent& event, Gestures* gestures); |
| 130 |
| 131 void UpdateValues(const TouchEvent& event); |
| 132 void SetState(const GestureState state ) { state_ = state; } |
| 133 |
| 134 // Various GestureTransitionFunctions for a signature. |
| 135 // There is, 1:many mapping from GestureTransitionFunction to Signature |
| 136 // But a Signature have only one GestureTransitionFunction. |
| 137 bool Click(const TouchEvent& event, Gestures* gestures); |
| 138 bool InClickOrScroll(const TouchEvent& event, Gestures* gestures); |
| 139 bool InScroll(const TouchEvent& event, Gestures* gestures); |
| 140 bool NoGesture(const TouchEvent& event, Gestures* gestures); |
| 141 bool TouchDown(const TouchEvent& event, Gestures* gestures); |
| 142 bool ScrollEnd(const TouchEvent& event, Gestures* gestures); |
| 143 |
| 144 // Location of first touch event in a touch sequence. |
| 145 gfx::Point first_touch_position_; |
| 146 |
| 147 // Time of first touch event in a touch sequence. |
| 148 double first_touch_time_; |
| 149 |
| 150 // Current state of gesture recognizer. |
| 151 GestureState state_; |
| 152 |
| 153 // Time of current touch event in a touch sequence. |
| 154 double last_touch_time_; |
| 155 |
| 156 // Time of click gesture. |
| 157 double last_click_time_; |
| 158 |
| 159 // Location of click gesture. |
| 160 gfx::Point last_click_position_; |
| 161 |
| 162 // Location of current touch event in a touch sequence. |
| 163 gfx::Point last_touch_position_; |
| 164 |
| 165 // Velocity in x and y direction. |
| 166 float x_velocity_; |
| 167 float y_velocity_; |
| 168 |
| 169 // ui::EventFlags. |
| 170 int flags_; |
| 171 |
| 172 DISALLOW_COPY_AND_ASSIGN(GestureRecognizer); |
| 173 }; |
| 174 |
| 175 } // namespace views |
| 176 |
| 177 #endif // VIEWS_TOUCHUI_GESTURE_RECOGNIZER_H_ |
| OLD | NEW |