Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / events / gestures / gesture_point.cc
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.
4
5 #include "ui/events/gestures/gesture_point.h"
6
7 #include <cmath>
8
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"
14
15 namespace ui {
16
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),
22       last_tap_time_(0.0),
23       velocity_calculator_(
24           GestureConfiguration::points_buffered_for_velocity()),
25       point_id_(-1),
26       touch_id_(-1),
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();
34 }
35
36 GesturePoint::~GesturePoint() {}
37
38 void GesturePoint::Reset() {
39   first_touch_time_ = second_last_touch_time_ = last_touch_time_ = 0.0;
40   ResetVelocity();
41   point_id_ = -1;
42   clear_enclosing_rectangle();
43   source_device_id_ = -1;
44 }
45
46 void GesturePoint::ResetVelocity() {
47   velocity_calculator_.ClearHistory();
48   same_direction_count_ = gfx::Vector2d();
49 }
50
51 gfx::Vector2dF GesturePoint::ScrollDelta() const {
52   return last_touch_position_ - second_last_touch_position_;
53 }
54
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(),
60                                    event.location().y(),
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;
65   }
66
67   last_touch_time_ = event.time_stamp().InSecondsF();
68   last_touch_position_ = event.location_f();
69
70   if (event.type() == ui::ET_TOUCH_PRESSED) {
71     ResetVelocity();
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(),
78                                    event.location().y(),
79                                    event_timestamp_microseconds);
80   }
81
82   UpdateEnclosingRectangle(event);
83 }
84
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_;
91 }
92
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();
97 }
98
99 bool GesturePoint::IsInClickWindow(const TouchEvent& event) const {
100   return IsInClickTimeWindow() && IsInsideTouchSlopRegion(event);
101 }
102
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_);
107 }
108
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_);
116 }
117
118 bool GesturePoint::IsInScrollWindow(const TouchEvent& event) const {
119   return event.type() == ui::ET_TOUCH_MOVED &&
120          !IsInsideTouchSlopRegion(event);
121 }
122
123 bool GesturePoint::IsInFlickWindow(const TouchEvent& event) {
124   return IsOverMinFlickSpeed() &&
125          event.type() != ui::ET_TOUCH_CANCELLED;
126 }
127
128 int GesturePoint::ScrollVelocityDirection(float v) {
129   if (v < -GestureConfiguration::min_scroll_velocity())
130     return -1;
131   else if (v > GestureConfiguration::min_scroll_velocity())
132     return 1;
133   else
134     return 0;
135 }
136
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;
140 }
141
142 bool GesturePoint::IsInHorizontalRailWindow() const {
143   gfx::Vector2dF d = last_touch_position_ - second_last_touch_position_;
144   return abs(d.x()) >
145       GestureConfiguration::rail_start_proportion() * abs(d.y());
146 }
147
148 bool GesturePoint::IsInVerticalRailWindow() const {
149   gfx::Vector2dF d = last_touch_position_ - second_last_touch_position_;
150   return abs(d.y()) >
151       GestureConfiguration::rail_start_proportion() * abs(d.x());
152 }
153
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();
159 }
160
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();
166 }
167
168 bool GesturePoint::IsInClickTimeWindow() const {
169   double duration = last_touch_time_ - first_touch_time_;
170   return duration >=
171       GestureConfiguration::min_touch_down_duration_in_seconds_for_click() &&
172       duration <
173       GestureConfiguration::max_touch_down_duration_in_seconds_for_click();
174 }
175
176 bool GesturePoint::IsInClickAggregateTimeWindow(double before,
177                                                 double after) const {
178   double duration =  after - before;
179   return duration < GestureConfiguration::max_seconds_between_double_click();
180 }
181
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_;
189 }
190
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_;
197 }
198
199 bool GesturePoint::IsOverMinFlickSpeed() {
200   return velocity_calculator_.VelocitySquared() >
201       GestureConfiguration::min_flick_speed_squared();
202 }
203
204 void GesturePoint::UpdateEnclosingRectangle(const TouchEvent& event) {
205   int radius;
206
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())
211     return;
212
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());
221   else
222     radius = GestureConfiguration::default_radius();
223
224   gfx::RectF rect(event.location_f().x() - radius,
225                   event.location_f().y() - radius,
226                   radius * 2,
227                   radius * 2);
228   if (IsInClickWindow(event))
229     enclosing_rect_.Union(rect);
230   else
231     enclosing_rect_ = rect;
232 }
233
234 }  // namespace ui