OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
7 | 7 |
8 #include "ui/events/gesture_detection/gesture_detector.h" | 8 #include "ui/events/gesture_detection/gesture_detector.h" |
9 | 9 |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 if (dot < 0) { | 265 if (dot < 0) { |
266 vx_total = 0; | 266 vx_total = 0; |
267 vy_total = 0; | 267 vy_total = 0; |
268 velocity_tracker_.Clear(); | 268 velocity_tracker_.Clear(); |
269 break; | 269 break; |
270 } | 270 } |
271 vx_total += vx2; | 271 vx_total += vx2; |
272 vy_total += vy2; | 272 vy_total += vy2; |
273 } | 273 } |
274 | 274 |
275 if (swipe_enabled_ && (vx_total || vy_total)) { | 275 handled = handleSwipeIfNeeded(ev, vx_total / count, vy_total / count); |
276 float vx = vx_total / count; | |
277 float vy = vy_total / count; | |
278 float vx_abs = std::abs(vx); | |
279 float vy_abs = std::abs(vy); | |
280 | |
281 if (vx_abs < min_swipe_velocity_) | |
282 vx_abs = vx = 0; | |
283 if (vy_abs < min_swipe_velocity_) | |
284 vy_abs = vy = 0; | |
285 | |
286 // Note that the ratio will be 0 if both velocites are below the min. | |
287 float ratio = vx_abs > vy_abs ? vx_abs / std::max(vy_abs, 0.001f) | |
288 : vy_abs / std::max(vx_abs, 0.001f); | |
289 if (ratio > min_swipe_direction_component_ratio_) { | |
290 if (vx_abs > vy_abs) | |
291 vy = 0; | |
292 else | |
293 vx = 0; | |
294 | |
295 handled = listener_->OnSwipe(*current_down_event_, ev, vx, vy); | |
296 } | |
297 } | |
298 | 276 |
299 if (two_finger_tap_allowed_for_gesture_ && ev.GetPointerCount() == 2 && | 277 if (two_finger_tap_allowed_for_gesture_ && ev.GetPointerCount() == 2 && |
300 (ev.GetEventTime() - secondary_pointer_down_event_->GetEventTime() <= | 278 (ev.GetEventTime() - secondary_pointer_down_event_->GetEventTime() <= |
301 two_finger_tap_timeout_)) { | 279 two_finger_tap_timeout_)) { |
302 handled = listener_->OnTwoFingerTap(*current_down_event_, ev); | 280 handled = listener_->OnTwoFingerTap(*current_down_event_, ev); |
303 } | 281 } |
304 two_finger_tap_allowed_for_gesture_ = false; | 282 two_finger_tap_allowed_for_gesture_ = false; |
305 } break; | 283 } break; |
306 | 284 |
307 case MotionEvent::ACTION_DOWN: | 285 case MotionEvent::ACTION_DOWN: |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 const int pointer_id = ev.GetPointerId(0); | 404 const int pointer_id = ev.GetPointerId(0); |
427 velocity_tracker_.ComputeCurrentVelocity(1000, max_fling_velocity_); | 405 velocity_tracker_.ComputeCurrentVelocity(1000, max_fling_velocity_); |
428 const float velocity_y = velocity_tracker_.GetYVelocity(pointer_id); | 406 const float velocity_y = velocity_tracker_.GetYVelocity(pointer_id); |
429 const float velocity_x = velocity_tracker_.GetXVelocity(pointer_id); | 407 const float velocity_x = velocity_tracker_.GetXVelocity(pointer_id); |
430 | 408 |
431 if ((std::abs(velocity_y) > min_fling_velocity_) || | 409 if ((std::abs(velocity_y) > min_fling_velocity_) || |
432 (std::abs(velocity_x) > min_fling_velocity_)) { | 410 (std::abs(velocity_x) > min_fling_velocity_)) { |
433 handled = listener_->OnFling( | 411 handled = listener_->OnFling( |
434 *current_down_event_, ev, velocity_x, velocity_y); | 412 *current_down_event_, ev, velocity_x, velocity_y); |
435 } | 413 } |
414 | |
415 handled |= handleSwipeIfNeeded(ev, velocity_x, velocity_y); | |
436 } | 416 } |
437 | 417 |
438 previous_up_event_ = ev.Clone(); | 418 previous_up_event_ = ev.Clone(); |
439 | 419 |
440 velocity_tracker_.Clear(); | 420 velocity_tracker_.Clear(); |
441 is_double_tapping_ = false; | 421 is_double_tapping_ = false; |
442 defer_confirm_single_tap_ = false; | 422 defer_confirm_single_tap_ = false; |
443 timeout_handler_->StopTimeout(SHOW_PRESS); | 423 timeout_handler_->StopTimeout(SHOW_PRESS); |
444 timeout_handler_->StopTimeout(LONG_PRESS); | 424 timeout_handler_->StopTimeout(LONG_PRESS); |
445 } | 425 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 | 527 |
548 if (second_down.GetEventTime() - first_up.GetEventTime() > | 528 if (second_down.GetEventTime() - first_up.GetEventTime() > |
549 double_tap_timeout_) | 529 double_tap_timeout_) |
550 return false; | 530 return false; |
551 | 531 |
552 const float delta_x = first_down.GetX() - second_down.GetX(); | 532 const float delta_x = first_down.GetX() - second_down.GetX(); |
553 const float delta_y = first_down.GetY() - second_down.GetY(); | 533 const float delta_y = first_down.GetY() - second_down.GetY(); |
554 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square_); | 534 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square_); |
555 } | 535 } |
556 | 536 |
537 bool GestureDetector::handleSwipeIfNeeded(const MotionEvent& up, | |
538 float vx, | |
539 float vy) { | |
540 if (!swipe_enabled_ || (!vx && !vy)) | |
541 return false; | |
542 float vx_abs = std::abs(vx); | |
543 float vy_abs = std::abs(vy); | |
544 | |
545 if (vx_abs < min_swipe_velocity_) | |
546 vx_abs = vx = 0; | |
547 if (vy_abs < min_swipe_velocity_) | |
548 vy_abs = vy = 0; | |
549 | |
550 // Note that the ratio will be 0 if both velocites are below the min. | |
551 float ratio = vx_abs > vy_abs ? vx_abs / std::max(vy_abs, 0.001f) | |
552 : vy_abs / std::max(vx_abs, 0.001f); | |
553 | |
554 if (ratio > min_swipe_direction_component_ratio_) { | |
jdduke (slow)
2014/05/26 21:19:42
Nit: I would early out here in the unsatisfied cas
mfomitchev
2014/05/26 22:07:14
Done.
| |
555 if (vx_abs > vy_abs) | |
556 vy = 0; | |
557 else | |
558 vx = 0; | |
559 | |
560 return listener_->OnSwipe(*current_down_event_, up, vx, vy); | |
561 } | |
562 return false; | |
563 } | |
564 | |
557 } // namespace ui | 565 } // namespace ui |
OLD | NEW |