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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 | 522 |
543 if (second_down.GetEventTime() - first_up.GetEventTime() > | 523 if (second_down.GetEventTime() - first_up.GetEventTime() > |
544 double_tap_timeout_) | 524 double_tap_timeout_) |
545 return false; | 525 return false; |
546 | 526 |
547 const float delta_x = first_down.GetX() - second_down.GetX(); | 527 const float delta_x = first_down.GetX() - second_down.GetX(); |
548 const float delta_y = first_down.GetY() - second_down.GetY(); | 528 const float delta_y = first_down.GetY() - second_down.GetY(); |
549 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square_); | 529 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square_); |
550 } | 530 } |
551 | 531 |
| 532 bool GestureDetector::HandleSwipeIfNeeded(const MotionEvent& up, |
| 533 float vx, |
| 534 float vy) { |
| 535 if (!swipe_enabled_ || (!vx && !vy)) |
| 536 return false; |
| 537 float vx_abs = std::abs(vx); |
| 538 float vy_abs = std::abs(vy); |
| 539 |
| 540 if (vx_abs < min_swipe_velocity_) |
| 541 vx_abs = vx = 0; |
| 542 if (vy_abs < min_swipe_velocity_) |
| 543 vy_abs = vy = 0; |
| 544 |
| 545 // Note that the ratio will be 0 if both velocites are below the min. |
| 546 float ratio = vx_abs > vy_abs ? vx_abs / std::max(vy_abs, 0.001f) |
| 547 : vy_abs / std::max(vx_abs, 0.001f); |
| 548 |
| 549 if (ratio < min_swipe_direction_component_ratio_) |
| 550 return false; |
| 551 |
| 552 if (vx_abs > vy_abs) |
| 553 vy = 0; |
| 554 else |
| 555 vx = 0; |
| 556 return listener_->OnSwipe(*current_down_event_, up, vx, vy); |
| 557 } |
| 558 |
552 } // namespace ui | 559 } // namespace ui |
OLD | NEW |