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 13 matching lines...) Expand all Loading... |
24 | 24 |
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 : span_slop(16), |
| 35 min_scaling_touch_major(48), |
35 min_scaling_span(200), | 36 min_scaling_span(200), |
36 quick_scale_enabled(true), | |
37 min_pinch_update_span_delta(0) { | 37 min_pinch_update_span_delta(0) { |
38 } | 38 } |
39 | 39 |
40 ScaleGestureDetector::Config::~Config() {} | 40 ScaleGestureDetector::Config::~Config() {} |
41 | 41 |
42 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScale( | 42 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScale( |
43 const ScaleGestureDetector&, const MotionEvent&) { | 43 const ScaleGestureDetector&, const MotionEvent&) { |
44 return false; | 44 return false; |
45 } | 45 } |
46 | 46 |
47 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScaleBegin( | 47 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScaleBegin( |
48 const ScaleGestureDetector&, const MotionEvent&) { | 48 const ScaleGestureDetector&, const MotionEvent&) { |
49 return true; | 49 return true; |
50 } | 50 } |
51 | 51 |
52 void ScaleGestureDetector::SimpleScaleGestureListener::OnScaleEnd( | 52 void ScaleGestureDetector::SimpleScaleGestureListener::OnScaleEnd( |
53 const ScaleGestureDetector&, const MotionEvent&) {} | 53 const ScaleGestureDetector&, const MotionEvent&) {} |
54 | 54 |
55 ScaleGestureDetector::ScaleGestureDetector(const Config& config, | 55 ScaleGestureDetector::ScaleGestureDetector(const Config& config, |
56 ScaleGestureListener* listener) | 56 ScaleGestureListener* listener) |
57 : listener_(listener), | 57 : listener_(listener), |
58 config_(config), | |
59 focus_x_(0), | 58 focus_x_(0), |
60 focus_y_(0), | 59 focus_y_(0), |
61 quick_scale_enabled_(false), | |
62 curr_span_(0), | 60 curr_span_(0), |
63 prev_span_(0), | 61 prev_span_(0), |
64 initial_span_(0), | 62 initial_span_(0), |
65 curr_span_x_(0), | 63 curr_span_x_(0), |
66 curr_span_y_(0), | 64 curr_span_y_(0), |
67 prev_span_x_(0), | 65 prev_span_x_(0), |
68 prev_span_y_(0), | 66 prev_span_y_(0), |
69 in_progress_(0), | 67 in_progress_(0), |
70 span_slop_(0), | 68 span_slop_(0), |
71 min_span_(0), | 69 min_span_(0), |
72 touch_upper_(0), | 70 touch_upper_(0), |
73 touch_lower_(0), | 71 touch_lower_(0), |
74 touch_history_last_accepted_(0), | 72 touch_history_last_accepted_(0), |
75 touch_history_direction_(0), | 73 touch_history_direction_(0), |
76 touch_min_major_(0), | 74 touch_min_major_(0), |
77 touch_max_major_(0), | 75 touch_max_major_(0), |
78 double_tap_focus_x_(0), | 76 double_tap_focus_x_(0), |
79 double_tap_focus_y_(0), | 77 double_tap_focus_y_(0), |
80 double_tap_mode_(DOUBLE_TAP_MODE_NONE), | 78 double_tap_mode_(DOUBLE_TAP_MODE_NONE), |
81 event_before_or_above_starting_gesture_event_(false) { | 79 event_before_or_above_starting_gesture_event_(false) { |
82 DCHECK(listener_); | 80 DCHECK(listener_); |
83 span_slop_ = (config.gesture_detector_config.touch_slop + kSlopEpsilon) * 2; | 81 span_slop_ = config.span_slop + kSlopEpsilon; |
84 touch_min_major_ = config.min_scaling_touch_major; | 82 touch_min_major_ = config.min_scaling_touch_major; |
85 touch_max_major_ = std::min(config.min_scaling_span / std::sqrt(2.f), | 83 touch_max_major_ = std::min(config.min_scaling_span / std::sqrt(2.f), |
86 2.f * touch_min_major_); | 84 2.f * touch_min_major_); |
87 min_span_ = config.min_scaling_span + kSlopEpsilon; | 85 min_span_ = config.min_scaling_span + kSlopEpsilon; |
88 ResetTouchHistory(); | 86 ResetTouchHistory(); |
89 SetQuickScaleEnabled(config.quick_scale_enabled); | |
90 } | 87 } |
91 | 88 |
92 ScaleGestureDetector::~ScaleGestureDetector() {} | 89 ScaleGestureDetector::~ScaleGestureDetector() {} |
93 | 90 |
94 bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { | 91 bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { |
95 curr_time_ = event.GetEventTime(); | 92 curr_time_ = event.GetEventTime(); |
96 | 93 |
97 const int action = event.GetAction(); | 94 const int action = event.GetAction(); |
98 | 95 |
99 // Forward the event to check for double tap gesture. | |
100 if (quick_scale_enabled_) { | |
101 DCHECK(gesture_detector_); | |
102 gesture_detector_->OnTouchEvent(event); | |
103 } | |
104 | |
105 const bool stream_complete = | 96 const bool stream_complete = |
106 action == MotionEvent::ACTION_UP || | 97 action == MotionEvent::ACTION_UP || |
107 action == MotionEvent::ACTION_CANCEL || | 98 action == MotionEvent::ACTION_CANCEL || |
108 (action == MotionEvent::ACTION_POINTER_DOWN && InDoubleTapMode()); | 99 (action == MotionEvent::ACTION_POINTER_DOWN && InDoubleTapMode()); |
109 | 100 |
110 if (action == MotionEvent::ACTION_DOWN || stream_complete) { | 101 if (action == MotionEvent::ACTION_DOWN || stream_complete) { |
111 // Reset any scale in progress with the listener. | 102 // Reset any scale in progress with the listener. |
112 // If it's an ACTION_DOWN we're beginning a new event stream. | 103 // If it's an ACTION_DOWN we're beginning a new event stream. |
113 // This means the app probably didn't give us all the events. Shame on it. | 104 // This means the app probably didn't give us all the events. Shame on it. |
114 if (in_progress_) { | 105 if (in_progress_) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 prev_span_x_ = curr_span_x_; | 225 prev_span_x_ = curr_span_x_; |
235 prev_span_y_ = curr_span_y_; | 226 prev_span_y_ = curr_span_y_; |
236 prev_span_ = curr_span_; | 227 prev_span_ = curr_span_; |
237 prev_time_ = curr_time_; | 228 prev_time_ = curr_time_; |
238 } | 229 } |
239 } | 230 } |
240 | 231 |
241 return true; | 232 return true; |
242 } | 233 } |
243 | 234 |
244 void ScaleGestureDetector::SetQuickScaleEnabled(bool scales) { | |
245 quick_scale_enabled_ = scales; | |
246 if (quick_scale_enabled_ && !gesture_detector_) { | |
247 gesture_detector_.reset( | |
248 new GestureDetector(config_.gesture_detector_config, this, this)); | |
249 } | |
250 } | |
251 | |
252 bool ScaleGestureDetector::IsQuickScaleEnabled() const { | |
253 return quick_scale_enabled_; | |
254 } | |
255 | |
256 bool ScaleGestureDetector::IsInProgress() const { return in_progress_; } | 235 bool ScaleGestureDetector::IsInProgress() const { return in_progress_; } |
257 | 236 |
258 bool ScaleGestureDetector::InDoubleTapMode() const { | 237 bool ScaleGestureDetector::InDoubleTapMode() const { |
259 return double_tap_mode_ == DOUBLE_TAP_MODE_IN_PROGRESS; | 238 return double_tap_mode_ == DOUBLE_TAP_MODE_IN_PROGRESS; |
260 } | 239 } |
261 | 240 |
262 float ScaleGestureDetector::GetFocusX() const { return focus_x_; } | 241 float ScaleGestureDetector::GetFocusX() const { return focus_x_; } |
263 | 242 |
264 float ScaleGestureDetector::GetFocusY() const { return focus_y_; } | 243 float ScaleGestureDetector::GetFocusY() const { return focus_y_; } |
265 | 244 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 touch_history_last_accepted_time_ = base::TimeTicks(); | 357 touch_history_last_accepted_time_ = base::TimeTicks(); |
379 } | 358 } |
380 | 359 |
381 void ScaleGestureDetector::ResetScaleWithSpan(float span) { | 360 void ScaleGestureDetector::ResetScaleWithSpan(float span) { |
382 in_progress_ = false; | 361 in_progress_ = false; |
383 initial_span_ = span; | 362 initial_span_ = span; |
384 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; | 363 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; |
385 } | 364 } |
386 | 365 |
387 } // namespace ui | 366 } // namespace ui |
OLD | NEW |