| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_AURA_GESTURES_GESTURE_RECOGNIZER_AURA_H_ | 5 #ifndef UI_AURA_GESTURES_GESTURE_RECOGNIZER_AURA_H_ |
| 6 #define UI_AURA_GESTURES_GESTURE_RECOGNIZER_AURA_H_ | 6 #define UI_AURA_GESTURES_GESTURE_RECOGNIZER_AURA_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <queue> | 10 #include <queue> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "ui/aura/aura_export.h" | 15 #include "ui/aura/aura_export.h" |
| 16 #include "ui/aura/gestures/gesture_recognizer.h" | 16 #include "ui/aura/gestures/gesture_recognizer.h" |
| 17 #include "ui/base/events.h" | 17 #include "ui/base/events.h" |
| 18 #include "ui/gfx/point.h" | 18 #include "ui/gfx/point.h" |
| 19 | 19 |
| 20 namespace aura { | 20 namespace aura { |
| 21 class TouchEvent; | 21 class TouchEvent; |
| 22 class GestureEvent; | 22 class GestureEvent; |
| 23 | 23 class GestureSequence; |
| 24 // A GestureSequence recognizes gestures from touch sequences. | |
| 25 class AURA_EXPORT GestureSequence { | |
| 26 public: | |
| 27 // Gesture state. | |
| 28 enum GestureState { | |
| 29 GS_NO_GESTURE, | |
| 30 GS_PENDING_SYNTHETIC_CLICK, | |
| 31 GS_SCROLL, | |
| 32 }; | |
| 33 | |
| 34 // ui::EventType is mapped to TouchState so it can fit into 3 bits of | |
| 35 // Signature. | |
| 36 enum TouchState { | |
| 37 TS_RELEASED, | |
| 38 TS_PRESSED, | |
| 39 TS_MOVED, | |
| 40 TS_STATIONARY, | |
| 41 TS_CANCELLED, | |
| 42 TS_UNKNOWN, | |
| 43 }; | |
| 44 | |
| 45 GestureSequence(); | |
| 46 virtual ~GestureSequence(); | |
| 47 | |
| 48 typedef GestureRecognizer::Gestures Gestures; | |
| 49 | |
| 50 // Invoked for each touch event that could contribute to the current gesture. | |
| 51 // Returns list of zero or more GestureEvents identified after processing | |
| 52 // TouchEvent. | |
| 53 // Caller would be responsible for freeing up Gestures. | |
| 54 virtual Gestures* ProcessTouchEventForGesture(const TouchEvent& event, | |
| 55 ui::TouchStatus status); | |
| 56 | |
| 57 private: | |
| 58 // Gesture signature types for different values of combination (GestureState, | |
| 59 // touch_id, ui::EventType, touch_handled), see GestureSequence::Signature() | |
| 60 // for more info. | |
| 61 // | |
| 62 // Note: New addition of types should be placed as per their Signature value. | |
| 63 enum GestureSignatureType { | |
| 64 // For input combination (GS_NO_GESTURE, 0, ui::ET_TOUCH_PRESSED, false). | |
| 65 GST_NO_GESTURE_FIRST_PRESSED = 0x00000003, | |
| 66 | |
| 67 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_RELEASED, false). | |
| 68 GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED = 0x00020001, | |
| 69 | |
| 70 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_MOVED, false). | |
| 71 GST_PENDING_SYNTHETIC_CLICK_FIRST_MOVED = 0x00020005, | |
| 72 | |
| 73 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_STATIONARY, false). | |
| 74 GST_PENDING_SYNTHETIC_CLICK_FIRST_STATIONARY = 0x00020007, | |
| 75 | |
| 76 // (GS_PENDING_SYNTHETIC_CLICK, 0, ui::ET_TOUCH_CANCELLED, false). | |
| 77 GST_PENDING_SYNTHETIC_CLICK_FIRST_CANCELLED = 0x00020009, | |
| 78 | |
| 79 // (GS_SCROLL, 0, ui::ET_TOUCH_RELEASED, false). | |
| 80 GST_SCROLL_FIRST_RELEASED = 0x00040001, | |
| 81 | |
| 82 // (GS_SCROLL, 0, ui::ET_TOUCH_MOVED, false). | |
| 83 GST_SCROLL_FIRST_MOVED = 0x00040005, | |
| 84 | |
| 85 // (GS_SCROLL, 0, ui::ET_TOUCH_CANCELLED, false). | |
| 86 GST_SCROLL_FIRST_CANCELLED = 0x00040009, | |
| 87 }; | |
| 88 | |
| 89 // Builds a signature. Signatures are assembled by joining together | |
| 90 // multiple bits. | |
| 91 // 1 LSB bit so that the computed signature is always greater than 0 | |
| 92 // 3 bits for the |type|. | |
| 93 // 1 bit for |touch_handled| | |
| 94 // 12 bits for |touch_id| | |
| 95 // 15 bits for the |gesture_state|. | |
| 96 static unsigned int Signature(GestureState state, | |
| 97 unsigned int touch_id, ui::EventType type, | |
| 98 bool touch_handled); | |
| 99 | |
| 100 void Reset(); | |
| 101 | |
| 102 // Various statistical functions to manipulate gestures. | |
| 103 bool IsInClickTimeWindow(); | |
| 104 bool IsInSecondClickTimeWindow(); | |
| 105 bool IsInsideManhattanSquare(const TouchEvent& event); | |
| 106 bool IsSecondClickInsideManhattanSquare(const TouchEvent& event); | |
| 107 bool IsOverMinFlickSpeed(); | |
| 108 | |
| 109 // Functions to be called to add GestureEvents, after succesful recognition. | |
| 110 void AppendTapDownGestureEvent(const TouchEvent& event, Gestures* gestures); | |
| 111 void AppendClickGestureEvent(const TouchEvent& event, Gestures* gestures); | |
| 112 void AppendDoubleClickGestureEvent(const TouchEvent& event, | |
| 113 Gestures* gestures); | |
| 114 void AppendScrollGestureBegin(const TouchEvent& event, Gestures* gestures); | |
| 115 void AppendScrollGestureEnd(const TouchEvent& event, | |
| 116 Gestures* gestures, | |
| 117 float x_velocity, float y_velocity); | |
| 118 void AppendScrollGestureUpdate(const TouchEvent& event, Gestures* gestures); | |
| 119 | |
| 120 void UpdateValues(const TouchEvent& event); | |
| 121 void SetState(const GestureState state ) { state_ = state; } | |
| 122 | |
| 123 // Various GestureTransitionFunctions for a signature. | |
| 124 // There is, 1:many mapping from GestureTransitionFunction to Signature | |
| 125 // But a Signature have only one GestureTransitionFunction. | |
| 126 bool Click(const TouchEvent& event, Gestures* gestures); | |
| 127 bool InClickOrScroll(const TouchEvent& event, Gestures* gestures); | |
| 128 bool InScroll(const TouchEvent& event, Gestures* gestures); | |
| 129 bool NoGesture(const TouchEvent& event, Gestures* gestures); | |
| 130 bool TouchDown(const TouchEvent& event, Gestures* gestures); | |
| 131 bool ScrollEnd(const TouchEvent& event, Gestures* gestures); | |
| 132 | |
| 133 // Location of first touch event in a touch sequence. | |
| 134 gfx::Point first_touch_position_; | |
| 135 | |
| 136 // Time of first touch event in a touch sequence. | |
| 137 double first_touch_time_; | |
| 138 | |
| 139 // Current state of gesture recognizer. | |
| 140 GestureState state_; | |
| 141 | |
| 142 // Time of current touch event in a touch sequence. | |
| 143 double last_touch_time_; | |
| 144 | |
| 145 // Time of click gesture. | |
| 146 double last_click_time_; | |
| 147 | |
| 148 // Location of click gesture. | |
| 149 gfx::Point last_click_position_; | |
| 150 | |
| 151 // Location of current touch event in a touch sequence. | |
| 152 gfx::Point last_touch_position_; | |
| 153 | |
| 154 // Velocity in x and y direction. | |
| 155 float x_velocity_; | |
| 156 float y_velocity_; | |
| 157 | |
| 158 // ui::EventFlags. | |
| 159 int flags_; | |
| 160 | |
| 161 DISALLOW_COPY_AND_ASSIGN(GestureSequence); | |
| 162 }; | |
| 163 | 24 |
| 164 class AURA_EXPORT GestureRecognizerAura : public GestureRecognizer { | 25 class AURA_EXPORT GestureRecognizerAura : public GestureRecognizer { |
| 165 public: | 26 public: |
| 166 GestureRecognizerAura(); | 27 GestureRecognizerAura(); |
| 167 virtual ~GestureRecognizerAura(); | 28 virtual ~GestureRecognizerAura(); |
| 168 | 29 |
| 169 private: | 30 private: |
| 170 // Overridden from GestureRecognizer | 31 // Overridden from GestureRecognizer |
| 171 virtual Gestures* ProcessTouchEventForGesture( | 32 virtual Gestures* ProcessTouchEventForGesture( |
| 172 const TouchEvent& event, | 33 const TouchEvent& event, |
| 173 ui::TouchStatus status) OVERRIDE; | 34 ui::TouchStatus status) OVERRIDE; |
| 174 virtual void QueueTouchEventForGesture(Window* window, | 35 virtual void QueueTouchEventForGesture(Window* window, |
| 175 const TouchEvent& event) OVERRIDE; | 36 const TouchEvent& event) OVERRIDE; |
| 176 virtual Gestures* AdvanceTouchQueue(Window* window, bool processed) OVERRIDE; | 37 virtual Gestures* AdvanceTouchQueue(Window* window, bool processed) OVERRIDE; |
| 177 virtual void FlushTouchQueue(Window* window) OVERRIDE; | 38 virtual void FlushTouchQueue(Window* window) OVERRIDE; |
| 178 | 39 |
| 179 scoped_ptr<GestureSequence> default_sequence_; | 40 scoped_ptr<GestureSequence> default_sequence_; |
| 180 | 41 |
| 181 typedef std::queue<TouchEvent*> TouchEventQueue; | 42 typedef std::queue<TouchEvent*> TouchEventQueue; |
| 182 std::map<Window*, TouchEventQueue*> event_queue_; | 43 std::map<Window*, TouchEventQueue*> event_queue_; |
| 183 std::map<Window*, GestureSequence*> window_sequence_; | 44 std::map<Window*, GestureSequence*> window_sequence_; |
| 184 | 45 |
| 185 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerAura); | 46 DISALLOW_COPY_AND_ASSIGN(GestureRecognizerAura); |
| 186 }; | 47 }; |
| 187 | 48 |
| 188 } // namespace aura | 49 } // namespace aura |
| 189 | 50 |
| 190 #endif // UI_AURA_GESTURES_GESTURE_RECOGNIZER_AURA_H_ | 51 #endif // UI_AURA_GESTURES_GESTURE_RECOGNIZER_AURA_H_ |
| OLD | NEW |