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
3 // found in the LICENSE file.
5 #include "ui/events/gestures/gesture_point.h"
9 #include "base/basictypes.h"
10 #include "ui/events/event.h"
11 #include "ui/events/event_constants.h"
12 #include "ui/events/gestures/gesture_configuration.h"
13 #include "ui/events/gestures/gesture_types.h"
17 GesturePoint::GesturePoint()
18 : first_touch_time_(0.0),
19 second_last_touch_time_(0.0),
20 last_touch_time_(0.0),
21 second_last_tap_time_(0.0),
24 GestureConfiguration::points_buffered_for_velocity()),
27 source_device_id_(-1) {
28 max_touch_move_in_pixels_for_click_squared_ =
29 GestureConfiguration::max_touch_move_in_pixels_for_click() *
30 GestureConfiguration::max_touch_move_in_pixels_for_click();
31 max_distance_between_taps_for_double_tap_squared_ =
32 GestureConfiguration::max_distance_between_taps_for_double_tap() *
33 GestureConfiguration::max_distance_between_taps_for_double_tap();
36 GesturePoint::~GesturePoint() {}
38 void GesturePoint::Reset() {
39 first_touch_time_ = second_last_touch_time_ = last_touch_time_ = 0.0;
42 clear_enclosing_rectangle();
43 source_device_id_ = -1;
46 void GesturePoint::ResetVelocity() {
47 velocity_calculator_.ClearHistory();
48 same_direction_count_ = gfx::Vector2d();
51 gfx::Vector2dF GesturePoint::ScrollDelta() const {
52 return last_touch_position_ - second_last_touch_position_;
55 void GesturePoint::UpdateValues(const TouchEvent& event) {
56 const int64 event_timestamp_microseconds =
57 event.time_stamp().InMicroseconds();
58 if (event.type() == ui::ET_TOUCH_MOVED) {
59 velocity_calculator_.PointSeen(event.location().x(),
61 event_timestamp_microseconds);
62 gfx::Vector2d sd(ScrollVelocityDirection(velocity_calculator_.XVelocity()),
63 ScrollVelocityDirection(velocity_calculator_.YVelocity()));
64 same_direction_count_ = same_direction_count_ + sd;
67 last_touch_time_ = event.time_stamp().InSecondsF();
68 last_touch_position_ = event.location_f();
70 if (event.type() == ui::ET_TOUCH_PRESSED) {
72 clear_enclosing_rectangle();
73 first_touch_time_ = last_touch_time_;
74 first_touch_position_ = event.location();
75 second_last_touch_position_ = last_touch_position_;
76 second_last_touch_time_ = last_touch_time_;
77 velocity_calculator_.PointSeen(event.location().x(),
79 event_timestamp_microseconds);
82 UpdateEnclosingRectangle(event);
85 void GesturePoint::UpdateForTap() {
86 // Update the tap-position and time, and reset every other state.
87 second_last_tap_position_ = last_tap_position_;
88 second_last_tap_time_ = last_tap_time_;
89 last_tap_time_ = last_touch_time_;
90 last_tap_position_ = last_touch_position_;
93 void GesturePoint::UpdateForScroll() {
94 second_last_touch_position_ = last_touch_position_;
95 second_last_touch_time_ = last_touch_time_;
96 same_direction_count_ = gfx::Vector2d();
99 bool GesturePoint::IsInClickWindow(const TouchEvent& event) const {
100 return IsInClickTimeWindow() && IsInsideTouchSlopRegion(event);
103 bool GesturePoint::IsInDoubleClickWindow(const TouchEvent& event) const {
104 return IsInClickAggregateTimeWindow(last_tap_time_, last_touch_time_) &&
105 IsPointInsideDoubleTapTouchSlopRegion(
106 event.location(), last_tap_position_);
109 bool GesturePoint::IsInTripleClickWindow(const TouchEvent& event) const {
110 return IsInClickAggregateTimeWindow(last_tap_time_, last_touch_time_) &&
111 IsInClickAggregateTimeWindow(second_last_tap_time_, last_tap_time_) &&
112 IsPointInsideDoubleTapTouchSlopRegion(
113 event.location(), last_tap_position_) &&
114 IsPointInsideDoubleTapTouchSlopRegion(last_tap_position_,
115 second_last_tap_position_);
118 bool GesturePoint::IsInScrollWindow(const TouchEvent& event) const {
119 return event.type() == ui::ET_TOUCH_MOVED &&
120 !IsInsideTouchSlopRegion(event);
123 bool GesturePoint::IsInFlickWindow(const TouchEvent& event) {
124 return IsOverMinFlickSpeed() &&
125 event.type() != ui::ET_TOUCH_CANCELLED;
128 int GesturePoint::ScrollVelocityDirection(float v) {
129 if (v < -GestureConfiguration::min_scroll_velocity())
131 else if (v > GestureConfiguration::min_scroll_velocity())
137 bool GesturePoint::DidScroll(const TouchEvent& event, int dist) const {
138 gfx::Vector2dF d = last_touch_position_ - second_last_touch_position_;
139 return fabs(d.x()) > dist || fabs(d.y()) > dist;
142 bool GesturePoint::IsInHorizontalRailWindow() const {
143 gfx::Vector2dF d = last_touch_position_ - second_last_touch_position_;
145 GestureConfiguration::rail_start_proportion() * abs(d.y());
148 bool GesturePoint::IsInVerticalRailWindow() const {
149 gfx::Vector2dF d = last_touch_position_ - second_last_touch_position_;
151 GestureConfiguration::rail_start_proportion() * abs(d.x());
154 bool GesturePoint::BreaksHorizontalRail() {
155 float vx = XVelocity();
156 float vy = YVelocity();
157 return fabs(vy) > GestureConfiguration::rail_break_proportion() * fabs(vx) +
158 GestureConfiguration::min_rail_break_velocity();
161 bool GesturePoint::BreaksVerticalRail() {
162 float vx = XVelocity();
163 float vy = YVelocity();
164 return fabs(vx) > GestureConfiguration::rail_break_proportion() * fabs(vy) +
165 GestureConfiguration::min_rail_break_velocity();
168 bool GesturePoint::IsInClickTimeWindow() const {
169 double duration = last_touch_time_ - first_touch_time_;
171 GestureConfiguration::min_touch_down_duration_in_seconds_for_click() &&
173 GestureConfiguration::max_touch_down_duration_in_seconds_for_click();
176 bool GesturePoint::IsInClickAggregateTimeWindow(double before,
177 double after) const {
178 double duration = after - before;
179 return duration < GestureConfiguration::max_seconds_between_double_click();
182 bool GesturePoint::IsInsideTouchSlopRegion(const TouchEvent& event) const {
183 const gfx::PointF& p1 = event.location();
184 const gfx::PointF& p2 = first_touch_position_;
185 float dx = p1.x() - p2.x();
186 float dy = p1.y() - p2.y();
187 float distance = dx * dx + dy * dy;
188 return distance < max_touch_move_in_pixels_for_click_squared_;
191 bool GesturePoint::IsPointInsideDoubleTapTouchSlopRegion(gfx::PointF p1,
192 gfx::PointF p2) const {
193 float dx = p1.x() - p2.x();
194 float dy = p1.y() - p2.y();
195 float distance = dx * dx + dy * dy;
196 return distance < max_distance_between_taps_for_double_tap_squared_;
199 bool GesturePoint::IsOverMinFlickSpeed() {
200 return velocity_calculator_.VelocitySquared() >
201 GestureConfiguration::min_flick_speed_squared();
204 void GesturePoint::UpdateEnclosingRectangle(const TouchEvent& event) {
207 // Ignore this TouchEvent if it has a radius larger than the maximum
208 // allowed radius size.
209 if (event.radius_x() > GestureConfiguration::max_radius() ||
210 event.radius_y() > GestureConfiguration::max_radius())
213 // If the device provides at least one of the radius values, take the larger
214 // of the two and use this as both the x radius and the y radius of the
215 // touch region. Otherwise use the default radius value.
216 // TODO(tdanderson): Implement a more specific check for the exact
217 // information provided by the device (0-2 radii values, force, angle) and
218 // use this to compute a more representative rectangular touch region.
219 if (event.radius_x() || event.radius_y())
220 radius = std::max(event.radius_x(), event.radius_y());
222 radius = GestureConfiguration::default_radius();
224 gfx::RectF rect(event.location_f().x() - radius,
225 event.location_f().y() - radius,
228 if (IsInClickWindow(event))
229 enclosing_rect_.Union(rect);
231 enclosing_rect_ = rect;