OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/events/gesture_detection/scale_gesture_detector.h" | 5 #include "ui/events/gesture_detection/scale_gesture_detector.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 const int kTouchStabilizeTimeMs = 128; | 25 const int kTouchStabilizeTimeMs = 128; |
26 | 26 |
27 const float kScaleFactor = .5f; | 27 const float kScaleFactor = .5f; |
28 | 28 |
29 } // namespace | 29 } // namespace |
30 | 30 |
31 // Note: These constants were taken directly from the default (unscaled) | 31 // Note: These constants were taken directly from the default (unscaled) |
32 // versions found in Android's ViewConfiguration. | 32 // versions found in Android's ViewConfiguration. |
33 ScaleGestureDetector::Config::Config() | 33 ScaleGestureDetector::Config::Config() |
34 : min_scaling_touch_major(48), | 34 : min_scaling_touch_major(48), |
35 min_scaling_span(100), | 35 min_scaling_span(200), |
36 use_touch_major_in_span(false), | |
37 quick_scale_enabled(true), | 36 quick_scale_enabled(true), |
38 min_pinch_update_span_delta(0) { | 37 min_pinch_update_span_delta(0) { |
39 } | 38 } |
40 | 39 |
41 ScaleGestureDetector::Config::~Config() {} | 40 ScaleGestureDetector::Config::~Config() {} |
42 | 41 |
43 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScale( | 42 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScale( |
44 const ScaleGestureDetector&, const MotionEvent&) { | 43 const ScaleGestureDetector&, const MotionEvent&) { |
45 return false; | 44 return false; |
46 } | 45 } |
(...skipping 16 matching lines...) Expand all Loading... |
63 curr_span_(0), | 62 curr_span_(0), |
64 prev_span_(0), | 63 prev_span_(0), |
65 initial_span_(0), | 64 initial_span_(0), |
66 curr_span_x_(0), | 65 curr_span_x_(0), |
67 curr_span_y_(0), | 66 curr_span_y_(0), |
68 prev_span_x_(0), | 67 prev_span_x_(0), |
69 prev_span_y_(0), | 68 prev_span_y_(0), |
70 in_progress_(0), | 69 in_progress_(0), |
71 span_slop_(0), | 70 span_slop_(0), |
72 min_span_(0), | 71 min_span_(0), |
73 use_touch_major_in_span_(config.use_touch_major_in_span), | |
74 touch_upper_(0), | 72 touch_upper_(0), |
75 touch_lower_(0), | 73 touch_lower_(0), |
76 touch_history_last_accepted_(0), | 74 touch_history_last_accepted_(0), |
77 touch_history_direction_(0), | 75 touch_history_direction_(0), |
78 touch_min_major_(0), | 76 touch_min_major_(0), |
79 touch_max_major_(0), | 77 touch_max_major_(0), |
80 double_tap_focus_x_(0), | 78 double_tap_focus_x_(0), |
81 double_tap_focus_y_(0), | 79 double_tap_focus_y_(0), |
82 double_tap_mode_(DOUBLE_TAP_MODE_NONE), | 80 double_tap_mode_(DOUBLE_TAP_MODE_NONE), |
83 event_before_or_above_starting_gesture_event_(false) { | 81 event_before_or_above_starting_gesture_event_(false) { |
84 DCHECK(listener_); | 82 DCHECK(listener_); |
85 span_slop_ = (config.gesture_detector_config.touch_slop + kSlopEpsilon) * 2; | 83 span_slop_ = (config.gesture_detector_config.touch_slop + kSlopEpsilon) * 2; |
86 touch_min_major_ = config.min_scaling_touch_major; | 84 touch_min_major_ = config.min_scaling_touch_major; |
87 touch_max_major_ = std::min(config.min_scaling_span, 2.f * touch_min_major_); | 85 touch_max_major_ = std::min(config.min_scaling_span / std::sqrt(2.f), |
| 86 2.f * touch_min_major_); |
88 min_span_ = config.min_scaling_span + kSlopEpsilon; | 87 min_span_ = config.min_scaling_span + kSlopEpsilon; |
89 ResetTouchHistory(); | 88 ResetTouchHistory(); |
90 SetQuickScaleEnabled(config.quick_scale_enabled); | 89 SetQuickScaleEnabled(config.quick_scale_enabled); |
91 } | 90 } |
92 | 91 |
93 ScaleGestureDetector::~ScaleGestureDetector() {} | 92 ScaleGestureDetector::~ScaleGestureDetector() {} |
94 | 93 |
95 bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { | 94 bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { |
96 curr_time_ = event.GetEventTime(); | 95 curr_time_ = event.GetEventTime(); |
97 | 96 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 if (skip_index == i) | 154 if (skip_index == i) |
156 continue; | 155 continue; |
157 sum_x += event.GetX(i); | 156 sum_x += event.GetX(i); |
158 sum_y += event.GetY(i); | 157 sum_y += event.GetY(i); |
159 } | 158 } |
160 | 159 |
161 focus_x = sum_x * inverse_unreleased_point_count; | 160 focus_x = sum_x * inverse_unreleased_point_count; |
162 focus_y = sum_y * inverse_unreleased_point_count; | 161 focus_y = sum_y * inverse_unreleased_point_count; |
163 } | 162 } |
164 | 163 |
165 if (use_touch_major_in_span_) | 164 AddTouchHistory(event); |
166 AddTouchHistory(event); | |
167 | 165 |
168 // Determine average deviation from focal point. | 166 // Determine average deviation from focal point. |
169 float dev_sum_x = 0, dev_sum_y = 0; | 167 float dev_sum_x = 0, dev_sum_y = 0; |
170 for (int i = 0; i < count; i++) { | 168 for (int i = 0; i < count; i++) { |
171 if (skip_index == i) | 169 if (skip_index == i) |
172 continue; | 170 continue; |
173 | 171 |
174 dev_sum_x += std::abs(event.GetX(i) - focus_x); | 172 dev_sum_x += std::abs(event.GetX(i) - focus_x); |
175 dev_sum_y += std::abs(event.GetY(i) - focus_y); | 173 dev_sum_y += std::abs(event.GetY(i) - focus_y); |
176 } | 174 } |
177 // Convert the resulting diameter into a radius, to include touch | 175 // Convert the resulting diameter into a radius, to include touch |
178 // radius in overall deviation. | 176 // radius in overall deviation. |
179 const float touch_radius = | 177 const float touch_radius = touch_history_last_accepted_ / 2; |
180 use_touch_major_in_span_ ? touch_history_last_accepted_ / 2 : 0; | |
181 | 178 |
182 const float dev_x = dev_sum_x * inverse_unreleased_point_count + touch_radius; | 179 const float dev_x = dev_sum_x * inverse_unreleased_point_count + touch_radius; |
183 const float dev_y = dev_sum_y * inverse_unreleased_point_count + touch_radius; | 180 const float dev_y = dev_sum_y * inverse_unreleased_point_count + touch_radius; |
184 | 181 |
185 // Span is the average distance between touch points through the focal point; | 182 // Span is the average distance between touch points through the focal point; |
186 // i.e. the diameter of the circle with a radius of the average deviation from | 183 // i.e. the diameter of the circle with a radius of the average deviation from |
187 // the focal point. | 184 // the focal point. |
188 const float span_x = dev_x * 2; | 185 const float span_x = dev_x * 2; |
189 const float span_y = dev_y * 2; | 186 const float span_y = dev_y * 2; |
190 float span; | 187 float span; |
(...skipping 14 matching lines...) Expand all Loading... |
205 listener_->OnScaleEnd(*this, event); | 202 listener_->OnScaleEnd(*this, event); |
206 ResetScaleWithSpan(span); | 203 ResetScaleWithSpan(span); |
207 } | 204 } |
208 if (config_changed) { | 205 if (config_changed) { |
209 prev_span_x_ = curr_span_x_ = span_x; | 206 prev_span_x_ = curr_span_x_ = span_x; |
210 prev_span_y_ = curr_span_y_ = span_y; | 207 prev_span_y_ = curr_span_y_ = span_y; |
211 initial_span_ = prev_span_ = curr_span_ = span; | 208 initial_span_ = prev_span_ = curr_span_ = span; |
212 } | 209 } |
213 | 210 |
214 const float min_span = InDoubleTapMode() ? span_slop_ : min_span_; | 211 const float min_span = InDoubleTapMode() ? span_slop_ : min_span_; |
215 if (!in_progress_ && span >= min_span && (InDoubleTapMode() || count > 1) && | 212 if (!in_progress_ && span >= min_span && |
216 (was_in_progress || std::abs(span - initial_span_) > span_slop_)) { | 213 (was_in_progress || std::abs(span - initial_span_) > span_slop_)) { |
217 prev_span_x_ = curr_span_x_ = span_x; | 214 prev_span_x_ = curr_span_x_ = span_x; |
218 prev_span_y_ = curr_span_y_ = span_y; | 215 prev_span_y_ = curr_span_y_ = span_y; |
219 prev_span_ = curr_span_ = span; | 216 prev_span_ = curr_span_ = span; |
220 prev_time_ = curr_time_; | 217 prev_time_ = curr_time_; |
221 in_progress_ = listener_->OnScaleBegin(*this, event); | 218 in_progress_ = listener_->OnScaleBegin(*this, event); |
222 } | 219 } |
223 | 220 |
224 // Handle motion; focal point and span/scale factor are changing. | 221 // Handle motion; focal point and span/scale factor are changing. |
225 if (action == MotionEvent::ACTION_MOVE) { | 222 if (action == MotionEvent::ACTION_MOVE) { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 touch_history_last_accepted_time_ = base::TimeTicks(); | 378 touch_history_last_accepted_time_ = base::TimeTicks(); |
382 } | 379 } |
383 | 380 |
384 void ScaleGestureDetector::ResetScaleWithSpan(float span) { | 381 void ScaleGestureDetector::ResetScaleWithSpan(float span) { |
385 in_progress_ = false; | 382 in_progress_ = false; |
386 initial_span_ = span; | 383 initial_span_ = span; |
387 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; | 384 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; |
388 } | 385 } |
389 | 386 |
390 } // namespace ui | 387 } // namespace ui |
OLD | NEW |