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