| 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 #include "views/touchui/gesture_recognizer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ui/base/events.h" |
| 9 #include "views/events/event.h" |
| 10 |
| 11 namespace { |
| 12 // TODO(Gajen): Make these configurable in sync with this CL http://code.google. |
| 13 // com/p/chromium/issues/detail?id=100773. |
| 14 const double kMaximumTouchDownDurationInSecondsForClick = 0.8; |
| 15 const double kMinimumTouchDownDurationInSecondsForClick = 0.01; |
| 16 const double kMaximumSecondsBetweenDoubleClick = 0.7; |
| 17 const int kMaximumTouchMoveInPixelsForClick = 20; |
| 18 const float kMinFlickSpeedSquared = 550.f * 550.f; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 namespace views { |
| 23 |
| 24 GestureRecognizer::PassGestureEvent::PassGestureEvent(GestureEvent* event) |
| 25 : event_(event) { |
| 26 } |
| 27 |
| 28 GestureRecognizer::PassGestureEvent::~PassGestureEvent() { |
| 29 free(event_); |
| 30 event_ = NULL; |
| 31 } |
| 32 |
| 33 //////////////////////////////////////////////////////////////////////////////// |
| 34 // GestureRecognizer Public: |
| 35 |
| 36 GestureRecognizer::GestureRecognizer() |
| 37 : first_touch_time_(0.0), |
| 38 state_(GestureRecognizer::GS_NO_GESTURE), |
| 39 last_touch_time_(0.0), |
| 40 last_click_time_(0.0), |
| 41 x_velocity_(0.0), |
| 42 y_velocity_(0.0), |
| 43 flags_(0) { |
| 44 const unsigned int first_finger = 0; |
| 45 const ui::EventType released = ui::ET_TOUCH_RELEASED; |
| 46 const ui::EventType pressed = ui::ET_TOUCH_PRESSED; |
| 47 const ui::EventType moved = ui::ET_TOUCH_MOVED; |
| 48 const ui::EventType stationary = ui::ET_TOUCH_STATIONARY; |
| 49 const ui::EventType cancelled = ui::ET_TOUCH_CANCELLED; |
| 50 |
| 51 AddEdgeFunction(GS_NO_GESTURE, first_finger, pressed, false, |
| 52 &GestureRecognizer::TouchDown); |
| 53 AddEdgeFunction(GS_PENDING_SYNTHETIC_CLICK, first_finger, cancelled, false, |
| 54 &GestureRecognizer::NoGesture); |
| 55 AddEdgeFunction(GS_PENDING_SYNTHETIC_CLICK, first_finger, released, false, |
| 56 &GestureRecognizer::Click); |
| 57 AddEdgeFunction(GS_PENDING_SYNTHETIC_CLICK, first_finger, moved, false, |
| 58 &GestureRecognizer::IsClickOrScroll); |
| 59 AddEdgeFunction(GS_PENDING_SYNTHETIC_CLICK, first_finger, stationary, false, |
| 60 &GestureRecognizer::IsClickOrScroll); |
| 61 AddEdgeFunction(GS_SCROLL, first_finger, moved, false, |
| 62 &GestureRecognizer::InScroll); |
| 63 AddEdgeFunction(GS_SCROLL, first_finger, released, false, |
| 64 &GestureRecognizer::ScrollEnd); |
| 65 AddEdgeFunction(GS_SCROLL, first_finger, cancelled, false, |
| 66 &GestureRecognizer::ScrollEnd); |
| 67 } |
| 68 |
| 69 GestureRecognizer::~GestureRecognizer() { |
| 70 } |
| 71 |
| 72 GestureRecognizer::Gestures* GestureRecognizer::ProcessTouchEventForGesture( |
| 73 const TouchEvent& event, |
| 74 bool touch_handled) { |
| 75 flags_ = event.flags(); |
| 76 scoped_ptr<Gestures> gestures(new Gestures()); |
| 77 UpdateValues(event); |
| 78 GestureTransitionFunctionMap::iterator it = edge_functions_.find( |
| 79 Signature(state_, event.identity(), event.type(), touch_handled)); |
| 80 if (edge_functions_.end() != it) { |
| 81 GestureTransitionFunction ef = it->second; |
| 82 ((*this).*ef)(event, gestures.get()); |
| 83 } |
| 84 return gestures.release(); |
| 85 } |
| 86 |
| 87 void GestureRecognizer::Reset() { |
| 88 first_touch_time_ = 0.0; |
| 89 state_ = GestureRecognizer::GS_NO_GESTURE; |
| 90 last_touch_time_ = 0.0; |
| 91 last_touch_position_.SetPoint(0, 0); |
| 92 x_velocity_ = 0.0; |
| 93 y_velocity_ = 0.0; |
| 94 } |
| 95 |
| 96 //////////////////////////////////////////////////////////////////////////////// |
| 97 // GestureRecognizer Private: |
| 98 |
| 99 GestureRecognizer::TouchState GestureRecognizer::TouchEventTypeToTouchState( |
| 100 ui::EventType type) { |
| 101 switch (type) { |
| 102 case ui::ET_TOUCH_RELEASED: return TS_RELEASED; |
| 103 case ui::ET_TOUCH_PRESSED: return TS_PRESSED; |
| 104 case ui::ET_TOUCH_MOVED: return TS_MOVED; |
| 105 case ui::ET_TOUCH_STATIONARY: return TS_STATIONARY; |
| 106 case ui::ET_TOUCH_CANCELLED: return TS_CANCELLED; |
| 107 default: |
| 108 VLOG(1) << "Unknown Touch Event type"; |
| 109 } |
| 110 return TS_UNKNOWN; |
| 111 } |
| 112 |
| 113 // Builds a signature. Signatures are assembled by joining together |
| 114 // multiple bits. |
| 115 // 1 LSB bit so that the computed signature is always greater than 0 |
| 116 // 3 bits for the |type|. |
| 117 // 1 bit for |touch_handled| |
| 118 // 12 bits for |touch_id| |
| 119 // 15 bits for the |gesture_state|. |
| 120 unsigned int GestureRecognizer::Signature(GestureState gesture_state, |
| 121 unsigned int touch_id, ui::EventType type, |
| 122 bool touch_handled) { |
| 123 CHECK((touch_id & 0xfff) == touch_id); |
| 124 TouchState touch_state = TouchEventTypeToTouchState(type); |
| 125 return 1 + ((touch_state & 0x7) << 1 | (touch_handled ? 1 << 4 : 0) | |
| 126 ((touch_id & 0xfff) << 5) | (gesture_state << 17)); |
| 127 } |
| 128 |
| 129 void GestureRecognizer::AddEdgeFunction(GestureState state, |
| 130 unsigned int touch_id, |
| 131 ui::EventType type, |
| 132 bool touch_handled, |
| 133 GestureTransitionFunction function) { |
| 134 edge_functions_[Signature(state, touch_id, type, touch_handled)] = function; |
| 135 } |
| 136 |
| 137 bool GestureRecognizer::IsInClickTimeWindow() { |
| 138 double duration(last_touch_time_ - first_touch_time_); |
| 139 return duration >= kMinimumTouchDownDurationInSecondsForClick && |
| 140 duration < kMaximumTouchDownDurationInSecondsForClick; |
| 141 } |
| 142 |
| 143 bool GestureRecognizer::IsInSecondClickTimeWindow() { |
| 144 double duration(last_touch_time_ - last_click_time_); |
| 145 return duration < kMaximumSecondsBetweenDoubleClick; |
| 146 } |
| 147 |
| 148 bool GestureRecognizer::IsInsideManhattanSquare(const TouchEvent& event) { |
| 149 int manhattanDistance = abs(event.x() - first_touch_position_.x()) + |
| 150 abs(event.y() - first_touch_position_.y()); |
| 151 return manhattanDistance < kMaximumTouchMoveInPixelsForClick; |
| 152 } |
| 153 |
| 154 bool GestureRecognizer::IsSecondClickInsideManhattanSquare( |
| 155 const TouchEvent& event) { |
| 156 int manhattanDistance = abs(event.x() - last_click_position_.x()) + |
| 157 abs(event.y() - last_click_position_.y()); |
| 158 return manhattanDistance < kMaximumTouchMoveInPixelsForClick; |
| 159 } |
| 160 |
| 161 bool GestureRecognizer::IsOverMinFlickSpeed() { |
| 162 return (x_velocity_ * x_velocity_ + y_velocity_ * y_velocity_) > |
| 163 kMinFlickSpeedSquared; |
| 164 } |
| 165 |
| 166 void GestureRecognizer::AppendTapDownGestureEvent(const TouchEvent& event, |
| 167 Gestures* gestures) { |
| 168 gestures->push_back(PassGestureEvent(new GestureEvent(ui::ET_GESTURE_TAP_DOWN, |
| 169 first_touch_position_.x(), |
| 170 first_touch_position_.y(), |
| 171 event.flags(), |
| 172 base::Time::FromDoubleT(last_touch_time_), |
| 173 0.f, 0.f))); |
| 174 } |
| 175 |
| 176 void GestureRecognizer::AppendClickGestureEvent(const TouchEvent& event, |
| 177 Gestures* gestures) { |
| 178 gestures->push_back(PassGestureEvent(new GestureEvent(ui::ET_GESTURE_TAP, |
| 179 first_touch_position_.x(), |
| 180 first_touch_position_.y(), |
| 181 event.flags(), |
| 182 base::Time::FromDoubleT(last_touch_time_), |
| 183 0.f, 0.f))); |
| 184 } |
| 185 |
| 186 void GestureRecognizer::AppendDoubleClickGestureEvent(const TouchEvent& event, |
| 187 Gestures* gestures) { |
| 188 gestures->push_back(PassGestureEvent(new GestureEvent( |
| 189 ui::ET_GESTURE_DOUBLE_TAP, |
| 190 first_touch_position_.x(), |
| 191 first_touch_position_.y(), |
| 192 event.flags(), |
| 193 base::Time::FromDoubleT(last_touch_time_), |
| 194 0.f, 0.f))); |
| 195 } |
| 196 |
| 197 void GestureRecognizer::AppendScrollGestureBegin(const TouchEvent& event, |
| 198 Gestures* gestures) { |
| 199 gestures->push_back(PassGestureEvent(new GestureEvent( |
| 200 ui::ET_GESTURE_SCROLL_BEGIN, |
| 201 event.x(), |
| 202 event.y(), |
| 203 event.flags(), |
| 204 base::Time::FromDoubleT(last_touch_time_), |
| 205 0.f, 0.f))); |
| 206 } |
| 207 |
| 208 void GestureRecognizer::AppendScrollGestureEnd(const TouchEvent& event, |
| 209 Gestures* gestures, |
| 210 float x_velocity, |
| 211 float y_velocity) { |
| 212 gestures->push_back(PassGestureEvent(new GestureEvent( |
| 213 ui::ET_GESTURE_SCROLL_END, |
| 214 event.x(), |
| 215 event.y(), |
| 216 event.flags(), |
| 217 base::Time::FromDoubleT(last_touch_time_), |
| 218 x_velocity, y_velocity))); |
| 219 } |
| 220 |
| 221 void GestureRecognizer:: AppendScrollGestureUpdate(const TouchEvent& event, |
| 222 Gestures* gestures) { |
| 223 float delta_x(event.x() - first_touch_position_.x()); |
| 224 float delta_y(event.y() - first_touch_position_.y()); |
| 225 |
| 226 gestures->push_back(PassGestureEvent(new GestureEvent( |
| 227 ui::ET_GESTURE_SCROLL_UPDATE, |
| 228 event.x(), |
| 229 event.y(), |
| 230 event.flags(), |
| 231 base::Time::FromDoubleT(last_touch_time_), |
| 232 delta_x, delta_y))); |
| 233 |
| 234 first_touch_position_ = event.location(); |
| 235 } |
| 236 |
| 237 void GestureRecognizer::UpdateValues(const TouchEvent& event) { |
| 238 if (state_ != GS_NO_GESTURE && event.type() == ui::ET_TOUCH_MOVED) { |
| 239 double interval(event.time_stamp().ToDoubleT() - last_touch_time_); |
| 240 x_velocity_ = (event.x() - last_touch_position_.x()) / interval; |
| 241 y_velocity_ = (event.y() - last_touch_position_.y()) / interval; |
| 242 } |
| 243 last_touch_time_ = event.time_stamp().ToDoubleT(); |
| 244 last_touch_position_ = event.location(); |
| 245 if (state_ == GS_NO_GESTURE) { |
| 246 first_touch_time_ = last_touch_time_; |
| 247 first_touch_position_ = event.location(); |
| 248 x_velocity_ = 0.0; |
| 249 y_velocity_ = 0.0; |
| 250 } |
| 251 } |
| 252 |
| 253 bool GestureRecognizer::Click(const TouchEvent& event, Gestures* gestures) { |
| 254 bool gesture_added = false; |
| 255 if (IsInClickTimeWindow() && IsInsideManhattanSquare(event)) { |
| 256 gesture_added = true; |
| 257 AppendClickGestureEvent(event, gestures); |
| 258 if (IsInSecondClickTimeWindow() && |
| 259 IsSecondClickInsideManhattanSquare(event)) |
| 260 AppendDoubleClickGestureEvent(event, gestures); |
| 261 last_click_time_ = last_touch_time_; |
| 262 last_click_position_ = last_touch_position_; |
| 263 } |
| 264 Reset(); |
| 265 return gesture_added; |
| 266 } |
| 267 |
| 268 bool GestureRecognizer::IsClickOrScroll(const TouchEvent& event, |
| 269 Gestures* gestures) { |
| 270 if (IsInClickTimeWindow() && IsInsideManhattanSquare(event)) { |
| 271 SetState(GS_PENDING_SYNTHETIC_CLICK); |
| 272 return false; |
| 273 } |
| 274 if (event.type() == ui::ET_TOUCH_MOVED && !IsInsideManhattanSquare(event)) { |
| 275 AppendScrollGestureBegin(event, gestures); |
| 276 AppendScrollGestureUpdate(event, gestures); |
| 277 SetState(GS_SCROLL); |
| 278 return true; |
| 279 } |
| 280 return false; |
| 281 } |
| 282 |
| 283 bool GestureRecognizer::InScroll(const TouchEvent& event, Gestures* gestures) { |
| 284 AppendScrollGestureUpdate(event, gestures); |
| 285 return true; |
| 286 } |
| 287 |
| 288 bool GestureRecognizer::NoGesture(const TouchEvent&, Gestures*) { |
| 289 Reset(); |
| 290 return false; |
| 291 } |
| 292 |
| 293 bool GestureRecognizer::TouchDown(const TouchEvent& event, Gestures* gestures) { |
| 294 AppendTapDownGestureEvent(event, gestures); |
| 295 SetState(GS_PENDING_SYNTHETIC_CLICK); |
| 296 return false; |
| 297 } |
| 298 |
| 299 bool GestureRecognizer::ScrollEnd(const TouchEvent& event, Gestures* gestures) { |
| 300 if (IsOverMinFlickSpeed() && event.type() != ui::ET_TOUCH_CANCELLED) |
| 301 AppendScrollGestureEnd(event, gestures, x_velocity_, y_velocity_); |
| 302 else |
| 303 AppendScrollGestureEnd(event, gestures, 0.f, 0.f); |
| 304 SetState(GS_NO_GESTURE); |
| 305 Reset(); |
| 306 return false; |
| 307 } |
| 308 |
| 309 } // namespace views |
| OLD | NEW |