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_point.h" | 5 #include "ui/base/gestures/gesture_point.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "ui/base/events/event.h" | 10 #include "ui/base/events/event.h" |
11 #include "ui/base/events/event_constants.h" | 11 #include "ui/base/events/event_constants.h" |
12 #include "ui/base/gestures/gesture_configuration.h" | 12 #include "ui/base/gestures/gesture_configuration.h" |
13 #include "ui/base/gestures/gesture_types.h" | 13 #include "ui/base/gestures/gesture_types.h" |
14 #include "ui/base/gestures/gesture_util.h" | 14 #include "ui/base/gestures/gesture_util.h" |
15 | 15 |
16 namespace ui { | 16 namespace ui { |
17 | 17 |
18 GesturePoint::GesturePoint() | 18 GesturePoint::GesturePoint() |
19 : first_touch_time_(0.0), | 19 : first_touch_time_(0.0), |
| 20 second_last_touch_time_(0.0), |
20 last_touch_time_(0.0), | 21 last_touch_time_(0.0), |
21 last_tap_time_(0.0), | 22 last_tap_time_(0.0), |
22 velocity_calculator_( | 23 velocity_calculator_( |
23 GestureConfiguration::points_buffered_for_velocity()), | 24 GestureConfiguration::points_buffered_for_velocity()), |
24 point_id_(-1), | 25 point_id_(-1), |
25 touch_id_(-1) { | 26 touch_id_(-1) { |
26 } | 27 } |
27 | 28 |
28 GesturePoint::~GesturePoint() {} | 29 GesturePoint::~GesturePoint() {} |
29 | 30 |
30 void GesturePoint::Reset() { | 31 void GesturePoint::Reset() { |
31 first_touch_time_ = last_touch_time_ = 0.0; | 32 first_touch_time_ = second_last_touch_time_ = last_touch_time_ = 0.0; |
32 velocity_calculator_.ClearHistory(); | 33 velocity_calculator_.ClearHistory(); |
33 point_id_ = -1; | 34 point_id_ = -1; |
34 clear_enclosing_rectangle(); | 35 clear_enclosing_rectangle(); |
35 } | 36 } |
36 | 37 |
37 void GesturePoint::ResetVelocity() { | 38 void GesturePoint::ResetVelocity() { |
38 velocity_calculator_.ClearHistory(); | 39 velocity_calculator_.ClearHistory(); |
39 } | 40 } |
40 | 41 |
| 42 gfx::Vector2d GesturePoint::ScrollDelta() { |
| 43 return last_touch_position_ - second_last_touch_position_; |
| 44 } |
| 45 |
41 void GesturePoint::UpdateValues(const TouchEvent& event) { | 46 void GesturePoint::UpdateValues(const TouchEvent& event) { |
42 const int64 event_timestamp_microseconds = | 47 const int64 event_timestamp_microseconds = |
43 event.time_stamp().InMicroseconds(); | 48 event.time_stamp().InMicroseconds(); |
44 if (event.type() == ui::ET_TOUCH_MOVED) { | 49 if (event.type() == ui::ET_TOUCH_MOVED) { |
45 velocity_calculator_.PointSeen(event.location().x(), | 50 velocity_calculator_.PointSeen(event.location().x(), |
46 event.location().y(), | 51 event.location().y(), |
47 event_timestamp_microseconds); | 52 event_timestamp_microseconds); |
48 } | 53 } |
49 | 54 |
50 last_touch_time_ = event.time_stamp().InSecondsF(); | 55 last_touch_time_ = event.time_stamp().InSecondsF(); |
51 last_touch_position_ = event.location(); | 56 last_touch_position_ = event.location(); |
52 | 57 |
53 if (event.type() == ui::ET_TOUCH_PRESSED) { | 58 if (event.type() == ui::ET_TOUCH_PRESSED) { |
54 first_touch_time_ = last_touch_time_; | 59 first_touch_time_ = last_touch_time_; |
55 first_touch_position_ = event.location(); | 60 first_touch_position_ = event.location(); |
| 61 second_last_touch_position_ = last_touch_position_; |
| 62 second_last_touch_time_ = last_touch_time_; |
| 63 |
56 velocity_calculator_.ClearHistory(); | 64 velocity_calculator_.ClearHistory(); |
57 velocity_calculator_.PointSeen(event.location().x(), | 65 velocity_calculator_.PointSeen(event.location().x(), |
58 event.location().y(), | 66 event.location().y(), |
59 event_timestamp_microseconds); | 67 event_timestamp_microseconds); |
60 clear_enclosing_rectangle(); | 68 clear_enclosing_rectangle(); |
61 } | 69 } |
62 | 70 |
63 UpdateEnclosingRectangle(event); | 71 UpdateEnclosingRectangle(event); |
64 } | 72 } |
65 | 73 |
66 void GesturePoint::UpdateForTap() { | 74 void GesturePoint::UpdateForTap() { |
67 // Update the tap-position and time, and reset every other state. | 75 // Update the tap-position and time, and reset every other state. |
68 last_tap_time_ = last_touch_time_; | 76 last_tap_time_ = last_touch_time_; |
69 last_tap_position_ = last_touch_position_; | 77 last_tap_position_ = last_touch_position_; |
70 } | 78 } |
71 | 79 |
72 void GesturePoint::UpdateForScroll() { | 80 void GesturePoint::UpdateForScroll() { |
73 // Update the first-touch position and time so that the scroll-delta and | 81 second_last_touch_position_ = last_touch_position_; |
74 // scroll-velocity can be computed correctly for the next scroll gesture | 82 second_last_touch_time_ = last_touch_time_; |
75 // event. | |
76 first_touch_position_ = last_touch_position_; | |
77 first_touch_time_ = last_touch_time_; | |
78 } | 83 } |
79 | 84 |
80 bool GesturePoint::IsInClickWindow(const TouchEvent& event) const { | 85 bool GesturePoint::IsInClickWindow(const TouchEvent& event) const { |
81 return IsInClickTimeWindow() && IsInsideManhattanSquare(event); | 86 return IsInClickTimeWindow() && IsInsideManhattanSquare(event); |
82 } | 87 } |
83 | 88 |
84 bool GesturePoint::IsInDoubleClickWindow(const TouchEvent& event) const { | 89 bool GesturePoint::IsInDoubleClickWindow(const TouchEvent& event) const { |
85 return IsInSecondClickTimeWindow() && | 90 return IsInSecondClickTimeWindow() && |
86 IsSecondClickInsideManhattanSquare(event); | 91 IsSecondClickInsideManhattanSquare(event); |
87 } | 92 } |
88 | 93 |
89 bool GesturePoint::IsInScrollWindow(const TouchEvent& event) const { | 94 bool GesturePoint::IsInScrollWindow(const TouchEvent& event) const { |
90 return event.type() == ui::ET_TOUCH_MOVED && | 95 return event.type() == ui::ET_TOUCH_MOVED && |
91 !IsInsideManhattanSquare(event); | 96 !IsInsideManhattanSquare(event); |
92 } | 97 } |
93 | 98 |
94 bool GesturePoint::IsInFlickWindow(const TouchEvent& event) { | 99 bool GesturePoint::IsInFlickWindow(const TouchEvent& event) { |
95 return IsOverMinFlickSpeed() && | 100 return IsOverMinFlickSpeed() && |
96 event.type() != ui::ET_TOUCH_CANCELLED; | 101 event.type() != ui::ET_TOUCH_CANCELLED; |
97 } | 102 } |
98 | 103 |
99 bool GesturePoint::DidScroll(const TouchEvent& event, int dist) const { | 104 bool GesturePoint::DidScroll(const TouchEvent& event, int dist) const { |
100 return abs(last_touch_position_.x() - first_touch_position_.x()) > dist || | 105 gfx::Vector2d d = last_touch_position_ - second_last_touch_position_; |
101 abs(last_touch_position_.y() - first_touch_position_.y()) > dist; | 106 return abs(d.x()) > dist || abs(d.y()) > dist; |
102 } | 107 } |
103 | 108 |
104 bool GesturePoint::HasEnoughDataToEstablishRail() const { | 109 bool GesturePoint::HasEnoughDataToEstablishRail() const { |
105 int dx = x_delta(); | 110 gfx::Vector2d d = last_touch_position_ - first_touch_position_; |
106 int dy = y_delta(); | 111 int64 delta_squared = d.LengthSquared(); |
107 | |
108 int delta_squared = dx * dx + dy * dy; | |
109 return delta_squared > GestureConfiguration::min_scroll_delta_squared(); | 112 return delta_squared > GestureConfiguration::min_scroll_delta_squared(); |
110 } | 113 } |
111 | 114 |
112 bool GesturePoint::IsInHorizontalRailWindow() const { | 115 bool GesturePoint::IsInHorizontalRailWindow() const { |
113 int dx = x_delta(); | 116 gfx::Vector2d d = last_touch_position_ - second_last_touch_position_; |
114 int dy = y_delta(); | 117 return abs(d.x()) > |
115 return abs(dx) > GestureConfiguration::rail_start_proportion() * abs(dy); | 118 GestureConfiguration::rail_start_proportion() * abs(d.y()); |
116 } | 119 } |
117 | 120 |
118 bool GesturePoint::IsInVerticalRailWindow() const { | 121 bool GesturePoint::IsInVerticalRailWindow() const { |
119 int dx = x_delta(); | 122 gfx::Vector2d d = last_touch_position_ - second_last_touch_position_; |
120 int dy = y_delta(); | 123 return abs(d.y()) > |
121 return abs(dy) > GestureConfiguration::rail_start_proportion() * abs(dx); | 124 GestureConfiguration::rail_start_proportion() * abs(d.x()); |
122 } | 125 } |
123 | 126 |
124 bool GesturePoint::BreaksHorizontalRail() { | 127 bool GesturePoint::BreaksHorizontalRail() { |
125 float vx = XVelocity(); | 128 float vx = XVelocity(); |
126 float vy = YVelocity(); | 129 float vy = YVelocity(); |
127 return fabs(vy) > GestureConfiguration::rail_break_proportion() * fabs(vx) + | 130 return fabs(vy) > GestureConfiguration::rail_break_proportion() * fabs(vx) + |
128 GestureConfiguration::min_rail_break_velocity(); | 131 GestureConfiguration::min_rail_break_velocity(); |
129 } | 132 } |
130 | 133 |
131 bool GesturePoint::BreaksVerticalRail() { | 134 bool GesturePoint::BreaksVerticalRail() { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 event.location().y() - radius, | 193 event.location().y() - radius, |
191 radius * 2, | 194 radius * 2, |
192 radius * 2); | 195 radius * 2); |
193 if (IsInClickWindow(event)) | 196 if (IsInClickWindow(event)) |
194 enclosing_rect_.Union(rect); | 197 enclosing_rect_.Union(rect); |
195 else | 198 else |
196 enclosing_rect_ = rect; | 199 enclosing_rect_ = rect; |
197 } | 200 } |
198 | 201 |
199 } // namespace ui | 202 } // namespace ui |
OLD | NEW |