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 #include "ui/base/gestures/gesture_sequence.h" | 5 #include "ui/base/gestures/gesture_sequence.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "ui/base/event.h" |
12 #include "ui/base/events.h" | 13 #include "ui/base/events.h" |
13 #include "ui/base/gestures/gesture_configuration.h" | 14 #include "ui/base/gestures/gesture_configuration.h" |
14 #include "ui/base/gestures/gesture_util.h" | 15 #include "ui/base/gestures/gesture_util.h" |
15 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
16 | 17 |
17 namespace ui { | 18 namespace ui { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // ui::EventType is mapped to TouchState so it can fit into 3 bits of | 22 // ui::EventType is mapped to TouchState so it can fit into 3 bits of |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 helper_(helper) { | 277 helper_(helper) { |
277 } | 278 } |
278 | 279 |
279 GestureSequence::~GestureSequence() { | 280 GestureSequence::~GestureSequence() { |
280 } | 281 } |
281 | 282 |
282 GestureSequence::Gestures* GestureSequence::ProcessTouchEventForGesture( | 283 GestureSequence::Gestures* GestureSequence::ProcessTouchEventForGesture( |
283 const TouchEvent& event, | 284 const TouchEvent& event, |
284 ui::TouchStatus status) { | 285 ui::TouchStatus status) { |
285 StopLongPressTimerIfRequired(event); | 286 StopLongPressTimerIfRequired(event); |
286 last_touch_location_ = event.GetLocation(); | 287 last_touch_location_ = event.location(); |
287 if (status == ui::TOUCH_STATUS_QUEUED || | 288 if (status == ui::TOUCH_STATUS_QUEUED || |
288 status == ui::TOUCH_STATUS_QUEUED_END) | 289 status == ui::TOUCH_STATUS_QUEUED_END) |
289 return NULL; | 290 return NULL; |
290 | 291 |
291 // Set a limit on the number of simultaneous touches in a gesture. | 292 // Set a limit on the number of simultaneous touches in a gesture. |
292 if (event.GetTouchId() >= kMaxGesturePoints) | 293 if (event.touch_id() >= kMaxGesturePoints) |
293 return NULL; | 294 return NULL; |
294 | 295 |
295 if (event.GetEventType() == ui::ET_TOUCH_PRESSED) { | 296 if (event.type() == ui::ET_TOUCH_PRESSED) { |
296 if (point_count_ == kMaxGesturePoints) | 297 if (point_count_ == kMaxGesturePoints) |
297 return NULL; | 298 return NULL; |
298 GesturePoint* new_point = &points_[event.GetTouchId()]; | 299 GesturePoint* new_point = &points_[event.touch_id()]; |
299 // We shouldn't be able to get two PRESSED events from the same | 300 // We shouldn't be able to get two PRESSED events from the same |
300 // finger without either a RELEASE or CANCEL in between. | 301 // finger without either a RELEASE or CANCEL in between. |
301 DCHECK(!new_point->in_use()); | 302 DCHECK(!new_point->in_use()); |
302 new_point->set_point_id(point_count_++); | 303 new_point->set_point_id(point_count_++); |
303 new_point->set_touch_id(event.GetTouchId()); | 304 new_point->set_touch_id(event.touch_id()); |
304 } | 305 } |
305 | 306 |
306 GestureState last_state = state_; | 307 GestureState last_state = state_; |
307 | 308 |
308 // NOTE: when modifying these state transitions, also update gestures.dot | 309 // NOTE: when modifying these state transitions, also update gestures.dot |
309 scoped_ptr<Gestures> gestures(new Gestures()); | 310 scoped_ptr<Gestures> gestures(new Gestures()); |
310 GesturePoint& point = GesturePointForEvent(event); | 311 GesturePoint& point = GesturePointForEvent(event); |
311 point.UpdateValues(event); | 312 point.UpdateValues(event); |
312 RecreateBoundingBox(); | 313 RecreateBoundingBox(); |
313 flags_ = event.GetEventFlags(); | 314 flags_ = event.flags(); |
314 const int point_id = points_[event.GetTouchId()].point_id(); | 315 const int point_id = points_[event.touch_id()].point_id(); |
315 if (point_id < 0) | 316 if (point_id < 0) |
316 return NULL; | 317 return NULL; |
317 | 318 |
318 // Send GESTURE_BEGIN for any touch pressed. | 319 // Send GESTURE_BEGIN for any touch pressed. |
319 if (event.GetEventType() == ui::ET_TOUCH_PRESSED) | 320 if (event.type() == ui::ET_TOUCH_PRESSED) |
320 AppendBeginGestureEvent(point, gestures.get()); | 321 AppendBeginGestureEvent(point, gestures.get()); |
321 | 322 |
322 TouchStatusInternal status_internal = (status == ui::TOUCH_STATUS_UNKNOWN) ? | 323 TouchStatusInternal status_internal = (status == ui::TOUCH_STATUS_UNKNOWN) ? |
323 TSI_NOT_PROCESSED : TSI_PROCESSED; | 324 TSI_NOT_PROCESSED : TSI_PROCESSED; |
324 | 325 |
325 EdgeStateSignatureType signature = Signature(state_, point_id, | 326 EdgeStateSignatureType signature = Signature(state_, point_id, |
326 event.GetEventType(), status_internal); | 327 event.type(), status_internal); |
327 | 328 |
328 if (signature == GST_INVALID) | 329 if (signature == GST_INVALID) |
329 signature = Signature(state_, point_id, event.GetEventType(), TSI_ALWAYS); | 330 signature = Signature(state_, point_id, event.type(), TSI_ALWAYS); |
330 | 331 |
331 switch (signature) { | 332 switch (signature) { |
332 case GST_INVALID: | 333 case GST_INVALID: |
333 break; | 334 break; |
334 | 335 |
335 case GST_NO_GESTURE_FIRST_PRESSED: | 336 case GST_NO_GESTURE_FIRST_PRESSED: |
336 TouchDown(event, point, gestures.get()); | 337 TouchDown(event, point, gestures.get()); |
337 set_state(GS_PENDING_SYNTHETIC_CLICK); | 338 set_state(GS_PENDING_SYNTHETIC_CLICK); |
338 break; | 339 break; |
339 case GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED: | 340 case GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED: |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 ResetVelocities(); | 433 ResetVelocities(); |
433 break; | 434 break; |
434 case GST_PINCH_THIRD_PRESSED: | 435 case GST_PINCH_THIRD_PRESSED: |
435 case GST_PINCH_FOURTH_PRESSED: | 436 case GST_PINCH_FOURTH_PRESSED: |
436 case GST_PINCH_FIFTH_PRESSED: | 437 case GST_PINCH_FIFTH_PRESSED: |
437 pinch_distance_current_ = BoundingBoxDiagonal(bounding_box_); | 438 pinch_distance_current_ = BoundingBoxDiagonal(bounding_box_); |
438 pinch_distance_start_ = pinch_distance_current_; | 439 pinch_distance_start_ = pinch_distance_current_; |
439 break; | 440 break; |
440 } | 441 } |
441 | 442 |
442 if (event.GetEventType() == ui::ET_TOUCH_RELEASED || | 443 if (event.type() == ui::ET_TOUCH_RELEASED || |
443 event.GetEventType() == ui::ET_TOUCH_CANCELLED) | 444 event.type() == ui::ET_TOUCH_CANCELLED) |
444 AppendEndGestureEvent(point, gestures.get()); | 445 AppendEndGestureEvent(point, gestures.get()); |
445 | 446 |
446 if (state_ != last_state) | 447 if (state_ != last_state) |
447 DVLOG(4) << "Gesture Sequence" | 448 DVLOG(4) << "Gesture Sequence" |
448 << " State: " << state_ | 449 << " State: " << state_ |
449 << " touch id: " << event.GetTouchId(); | 450 << " touch id: " << event.touch_id(); |
450 | 451 |
451 if (last_state == GS_PENDING_SYNTHETIC_CLICK && state_ != last_state) | 452 if (last_state == GS_PENDING_SYNTHETIC_CLICK && state_ != last_state) |
452 long_press_timer_->Stop(); | 453 long_press_timer_->Stop(); |
453 | 454 |
454 // The set of point_ids must be contiguous and include 0. | 455 // The set of point_ids must be contiguous and include 0. |
455 // When a touch point is released, all points with ids greater than the | 456 // When a touch point is released, all points with ids greater than the |
456 // released point must have their ids decremented, or the set of point_ids | 457 // released point must have their ids decremented, or the set of point_ids |
457 // could end up with gaps. | 458 // could end up with gaps. |
458 if (event.GetEventType() == ui::ET_TOUCH_RELEASED || | 459 if (event.type() == ui::ET_TOUCH_RELEASED || |
459 event.GetEventType() == ui::ET_TOUCH_CANCELLED) { | 460 event.type() == ui::ET_TOUCH_CANCELLED) { |
460 GesturePoint& old_point = points_[event.GetTouchId()]; | 461 GesturePoint& old_point = points_[event.touch_id()]; |
461 for (int i = 0; i < kMaxGesturePoints; ++i) { | 462 for (int i = 0; i < kMaxGesturePoints; ++i) { |
462 GesturePoint& point = points_[i]; | 463 GesturePoint& point = points_[i]; |
463 if (point.point_id() > old_point.point_id()) | 464 if (point.point_id() > old_point.point_id()) |
464 point.set_point_id(point.point_id() - 1); | 465 point.set_point_id(point.point_id() - 1); |
465 } | 466 } |
466 | 467 |
467 if (old_point.in_use()) { | 468 if (old_point.in_use()) { |
468 old_point.Reset(); | 469 old_point.Reset(); |
469 --point_count_; | 470 --point_count_; |
470 DCHECK_GE(point_count_, 0); | 471 DCHECK_GE(point_count_, 0); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 | 520 |
520 base::OneShotTimer<GestureSequence>* GestureSequence::CreateTimer() { | 521 base::OneShotTimer<GestureSequence>* GestureSequence::CreateTimer() { |
521 return new base::OneShotTimer<GestureSequence>(); | 522 return new base::OneShotTimer<GestureSequence>(); |
522 } | 523 } |
523 | 524 |
524 //////////////////////////////////////////////////////////////////////////////// | 525 //////////////////////////////////////////////////////////////////////////////// |
525 // GestureSequence Private: | 526 // GestureSequence Private: |
526 | 527 |
527 GesturePoint& GestureSequence::GesturePointForEvent( | 528 GesturePoint& GestureSequence::GesturePointForEvent( |
528 const TouchEvent& event) { | 529 const TouchEvent& event) { |
529 return points_[event.GetTouchId()]; | 530 return points_[event.touch_id()]; |
530 } | 531 } |
531 | 532 |
532 GesturePoint* GestureSequence::GetPointByPointId(int point_id) { | 533 GesturePoint* GestureSequence::GetPointByPointId(int point_id) { |
533 DCHECK(0 <= point_id && point_id < kMaxGesturePoints); | 534 DCHECK(0 <= point_id && point_id < kMaxGesturePoints); |
534 for (int i = 0; i < kMaxGesturePoints; ++i) { | 535 for (int i = 0; i < kMaxGesturePoints; ++i) { |
535 GesturePoint& point = points_[i]; | 536 GesturePoint& point = points_[i]; |
536 if (point.in_use() && point.point_id() == point_id) | 537 if (point.in_use() && point.point_id() == point_id) |
537 return &point; | 538 return &point; |
538 } | 539 } |
539 NOTREACHED(); | 540 NOTREACHED(); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
817 return true; | 818 return true; |
818 } | 819 } |
819 | 820 |
820 bool GestureSequence::TwoFingerTouchDown(const TouchEvent& event, | 821 bool GestureSequence::TwoFingerTouchDown(const TouchEvent& event, |
821 const GesturePoint& point, Gestures* gestures) { | 822 const GesturePoint& point, Gestures* gestures) { |
822 DCHECK(state_ == GS_PENDING_SYNTHETIC_CLICK || state_ == GS_SCROLL); | 823 DCHECK(state_ == GS_PENDING_SYNTHETIC_CLICK || state_ == GS_SCROLL); |
823 if (state_ == GS_SCROLL) { | 824 if (state_ == GS_SCROLL) { |
824 AppendScrollGestureEnd(point, point.last_touch_position(), gestures, | 825 AppendScrollGestureEnd(point, point.last_touch_position(), gestures, |
825 0.f, 0.f); | 826 0.f, 0.f); |
826 } | 827 } |
827 second_touch_time_ = event.GetTimestamp(); | 828 second_touch_time_ = event.time_stamp(); |
828 return true; | 829 return true; |
829 } | 830 } |
830 | 831 |
831 bool GestureSequence::TwoFingerTouchMove(const TouchEvent& event, | 832 bool GestureSequence::TwoFingerTouchMove(const TouchEvent& event, |
832 const GesturePoint& point, Gestures* gestures) { | 833 const GesturePoint& point, Gestures* gestures) { |
833 DCHECK(state_ == GS_PENDING_TWO_FINGER_TAP); | 834 DCHECK(state_ == GS_PENDING_TWO_FINGER_TAP); |
834 | 835 |
835 base::TimeDelta time_delta = event.GetTimestamp() - second_touch_time_; | 836 base::TimeDelta time_delta = event.time_stamp() - second_touch_time_; |
836 base::TimeDelta max_delta = base::TimeDelta::FromMilliseconds(1000 * | 837 base::TimeDelta max_delta = base::TimeDelta::FromMilliseconds(1000 * |
837 ui::GestureConfiguration::max_touch_down_duration_in_seconds_for_click()); | 838 ui::GestureConfiguration::max_touch_down_duration_in_seconds_for_click()); |
838 if (time_delta > max_delta || !point.IsInsideManhattanSquare(event)) { | 839 if (time_delta > max_delta || !point.IsInsideManhattanSquare(event)) { |
839 PinchStart(event, point, gestures); | 840 PinchStart(event, point, gestures); |
840 return true; | 841 return true; |
841 } | 842 } |
842 return false; | 843 return false; |
843 } | 844 } |
844 | 845 |
845 bool GestureSequence::TwoFingerTouchReleased(const TouchEvent& event, | 846 bool GestureSequence::TwoFingerTouchReleased(const TouchEvent& event, |
846 const GesturePoint& point, Gestures* gestures) { | 847 const GesturePoint& point, Gestures* gestures) { |
847 DCHECK(state_ == GS_PENDING_TWO_FINGER_TAP); | 848 DCHECK(state_ == GS_PENDING_TWO_FINGER_TAP); |
848 base::TimeDelta time_delta = event.GetTimestamp() - second_touch_time_; | 849 base::TimeDelta time_delta = event.time_stamp() - second_touch_time_; |
849 base::TimeDelta max_delta = base::TimeDelta::FromMilliseconds(1000 * | 850 base::TimeDelta max_delta = base::TimeDelta::FromMilliseconds(1000 * |
850 ui::GestureConfiguration::max_touch_down_duration_in_seconds_for_click()); | 851 ui::GestureConfiguration::max_touch_down_duration_in_seconds_for_click()); |
851 if (time_delta < max_delta && point.IsInsideManhattanSquare(event)) | 852 if (time_delta < max_delta && point.IsInsideManhattanSquare(event)) |
852 AppendTwoFingerTapGestureEvent(gestures); | 853 AppendTwoFingerTapGestureEvent(gestures); |
853 return true; | 854 return true; |
854 } | 855 } |
855 | 856 |
856 void GestureSequence::AppendLongPressGestureEvent() { | 857 void GestureSequence::AppendLongPressGestureEvent() { |
857 const GesturePoint* point = GetPointByPointId(0); | 858 const GesturePoint* point = GetPointByPointId(0); |
858 scoped_ptr<GestureEvent> gesture(CreateGestureEvent( | 859 scoped_ptr<GestureEvent> gesture(CreateGestureEvent( |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1015 else | 1016 else |
1016 sign_x = 0; | 1017 sign_x = 0; |
1017 | 1018 |
1018 AppendSwipeGesture(point, sign_x, sign_y, gestures); | 1019 AppendSwipeGesture(point, sign_x, sign_y, gestures); |
1019 | 1020 |
1020 return true; | 1021 return true; |
1021 } | 1022 } |
1022 | 1023 |
1023 void GestureSequence::StopLongPressTimerIfRequired(const TouchEvent& event) { | 1024 void GestureSequence::StopLongPressTimerIfRequired(const TouchEvent& event) { |
1024 if (!long_press_timer_->IsRunning() || | 1025 if (!long_press_timer_->IsRunning() || |
1025 event.GetEventType() != ui::ET_TOUCH_MOVED) | 1026 event.type() != ui::ET_TOUCH_MOVED) |
1026 return; | 1027 return; |
1027 | 1028 |
1028 // Since long press timer has been started, there should be a non-NULL point. | 1029 // Since long press timer has been started, there should be a non-NULL point. |
1029 const GesturePoint* point = GetPointByPointId(0); | 1030 const GesturePoint* point = GetPointByPointId(0); |
1030 if (!ui::gestures::IsInsideManhattanSquare(point->first_touch_position(), | 1031 if (!ui::gestures::IsInsideManhattanSquare(point->first_touch_position(), |
1031 event.GetLocation())) | 1032 event.location())) |
1032 long_press_timer_->Stop(); | 1033 long_press_timer_->Stop(); |
1033 } | 1034 } |
1034 | 1035 |
1035 } // namespace ui | 1036 } // namespace ui |
OLD | NEW |