blob: a531f706e458d9eb7c9b86bbb2137b53c55f58f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/aura/gestures/gesture_point.h"
#include <cmath>
#include "base/basictypes.h"
#include "ui/aura/event.h"
#include "ui/aura/gestures/gesture_configuration.h"
#include "ui/base/events.h"
namespace {
const int kMinRailBreakVelocity = 200;
const int kMinScrollDeltaSquared = 5 * 5;
const int kRailBreakProportion = 15;
const int kRailStartProportion = 2;
const int kBufferedPoints = 10;
} // namespace
namespace aura {
GesturePoint::GesturePoint()
: first_touch_time_(0.0),
last_touch_time_(0.0),
last_tap_time_(0.0),
velocity_calculator_(kBufferedPoints),
point_id_(-1) {
}
GesturePoint::~GesturePoint() {}
void GesturePoint::Reset() {
first_touch_time_ = last_touch_time_ = 0.0;
velocity_calculator_.ClearHistory();
point_id_ = -1;
}
void GesturePoint::UpdateValues(const TouchEvent& event) {
const int64 event_timestamp_microseconds =
event.time_stamp().InMicroseconds();
if (event.type() == ui::ET_TOUCH_MOVED) {
velocity_calculator_.PointSeen(event.x(),
event.y(),
event_timestamp_microseconds);
}
last_touch_time_ = event.time_stamp().InSecondsF();
last_touch_position_ = event.location();
if (event.type() == ui::ET_TOUCH_PRESSED) {
first_touch_time_ = last_touch_time_;
first_touch_position_ = event.location();
velocity_calculator_.ClearHistory();
velocity_calculator_.PointSeen(event.x(),
event.y(),
event_timestamp_microseconds);
}
}
void GesturePoint::UpdateForTap() {
// Update the tap-position and time, and reset every other state.
last_tap_time_ = last_touch_time_;
last_tap_position_ = last_touch_position_;
Reset();
}
void GesturePoint::UpdateForScroll() {
// Update the first-touch position and time so that the scroll-delta and
// scroll-velocity can be computed correctly for the next scroll gesture
// event.
first_touch_position_ = last_touch_position_;
first_touch_time_ = last_touch_time_;
}
bool GesturePoint::IsInClickWindow(const TouchEvent& event) const {
return IsInClickTimeWindow() && IsInsideManhattanSquare(event);
}
bool GesturePoint::IsInDoubleClickWindow(const TouchEvent& event) const {
return IsInSecondClickTimeWindow() &&
IsSecondClickInsideManhattanSquare(event);
}
bool GesturePoint::IsInScrollWindow(const TouchEvent& event) const {
return event.type() == ui::ET_TOUCH_MOVED &&
!IsInsideManhattanSquare(event);
}
bool GesturePoint::IsInFlickWindow(const TouchEvent& event) {
return IsOverMinFlickSpeed() && event.type() != ui::ET_TOUCH_CANCELLED;
}
bool GesturePoint::DidScroll(const TouchEvent& event, int dist) const {
return abs(last_touch_position_.x() - first_touch_position_.x()) > dist ||
abs(last_touch_position_.y() - first_touch_position_.y()) > dist;
}
float GesturePoint::Distance(const GesturePoint& point) const {
float x_diff = point.last_touch_position_.x() - last_touch_position_.x();
float y_diff = point.last_touch_position_.y() - last_touch_position_.y();
return sqrt(x_diff * x_diff + y_diff * y_diff);
}
bool GesturePoint::HasEnoughDataToEstablishRail() const {
int dx = x_delta();
int dy = y_delta();
int delta_squared = dx * dx + dy * dy;
return delta_squared > kMinScrollDeltaSquared;
}
bool GesturePoint::IsInHorizontalRailWindow() const {
int dx = x_delta();
int dy = y_delta();
return abs(dx) > kRailStartProportion * abs(dy);
}
bool GesturePoint::IsInVerticalRailWindow() const {
int dx = x_delta();
int dy = y_delta();
return abs(dy) > kRailStartProportion * abs(dx);
}
bool GesturePoint::BreaksHorizontalRail() {
float vx = XVelocity();
float vy = YVelocity();
return fabs(vy) > kRailBreakProportion * fabs(vx) + kMinRailBreakVelocity;
}
bool GesturePoint::BreaksVerticalRail() {
float vx = XVelocity();
float vy = YVelocity();
return fabs(vx) > kRailBreakProportion * fabs(vy) + kMinRailBreakVelocity;
}
bool GesturePoint::IsInClickTimeWindow() const {
double duration = last_touch_time_ - first_touch_time_;
return duration >=
GestureConfiguration::min_touch_down_duration_in_seconds_for_click() &&
duration <
GestureConfiguration::max_touch_down_duration_in_seconds_for_click();
}
bool GesturePoint::IsInSecondClickTimeWindow() const {
double duration = last_touch_time_ - last_tap_time_;
return duration < GestureConfiguration::max_seconds_between_double_click();
}
bool GesturePoint::IsInsideManhattanSquare(const TouchEvent& event) const {
int manhattanDistance = abs(event.x() - first_touch_position_.x()) +
abs(event.y() - first_touch_position_.y());
return manhattanDistance <
GestureConfiguration::max_touch_move_in_pixels_for_click();
}
bool GesturePoint::IsSecondClickInsideManhattanSquare(
const TouchEvent& event) const {
int manhattanDistance = abs(event.x() - last_tap_position_.x()) +
abs(event.y() - last_tap_position_.y());
return manhattanDistance <
GestureConfiguration::max_touch_move_in_pixels_for_click();
}
bool GesturePoint::IsOverMinFlickSpeed() {
return velocity_calculator_.VelocitySquared() >
GestureConfiguration::min_flick_speed_squared();
}
} // namespace aura
|