OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/events/gesture_detection/scale_gesture_detector.h" |
| 6 |
| 7 #include <limits.h> |
| 8 #include <math.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "ui/events/gesture_detection/motion_event.h" |
| 12 |
| 13 using base::TimeDelta; |
| 14 using base::TimeTicks; |
| 15 |
| 16 namespace ui { |
| 17 namespace { |
| 18 |
| 19 const int kTouchStabilizeTimeMs = 128; |
| 20 |
| 21 const float kScaleFactor = .5f; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 // Note: These constants were taken directly from the default (unscaled) |
| 26 // versions found in Android's ViewConfiguration. |
| 27 ScaleGestureDetector::Config::Config() |
| 28 : quick_scale_enabled(false), |
| 29 min_scaling_touch_major(48), |
| 30 min_scaling_span(200) {} |
| 31 |
| 32 ScaleGestureDetector::Config::~Config() {} |
| 33 |
| 34 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScale( |
| 35 const ScaleGestureDetector&) { |
| 36 return false; |
| 37 } |
| 38 |
| 39 bool ScaleGestureDetector::SimpleScaleGestureListener::OnScaleBegin( |
| 40 const ScaleGestureDetector&) { |
| 41 return true; |
| 42 } |
| 43 |
| 44 void ScaleGestureDetector::SimpleScaleGestureListener::OnScaleEnd( |
| 45 const ScaleGestureDetector&) {} |
| 46 |
| 47 ScaleGestureDetector::ScaleGestureDetector(const Config& config, |
| 48 ScaleGestureListener* listener) |
| 49 : listener_(listener), |
| 50 config_(config), |
| 51 focus_x_(0), |
| 52 focus_y_(0), |
| 53 quick_scale_enabled_(false), |
| 54 curr_span_(0), |
| 55 prev_span_(0), |
| 56 initial_span_(0), |
| 57 curr_span_x_(0), |
| 58 curr_span_y_(0), |
| 59 prev_span_x_(0), |
| 60 prev_span_y_(0), |
| 61 in_progress_(0), |
| 62 span_slop_(0), |
| 63 min_span_(0), |
| 64 touch_upper_(0), |
| 65 touch_lower_(0), |
| 66 touch_history_last_accepted_(0), |
| 67 touch_history_direction_(0), |
| 68 touch_min_major_(0), |
| 69 double_tap_focus_x_(0), |
| 70 double_tap_focus_y_(0), |
| 71 double_tap_mode_(DOUBLE_TAP_MODE_NONE), |
| 72 event_before_or_above_starting_gesture_event_(false) { |
| 73 DCHECK(listener_); |
| 74 span_slop_ = config.gesture_detector_config.scaled_touch_slop * 2; |
| 75 touch_min_major_ = config.min_scaling_touch_major; |
| 76 min_span_ = config.min_scaling_span; |
| 77 SetQuickScaleEnabled(config.quick_scale_enabled); |
| 78 } |
| 79 |
| 80 ScaleGestureDetector::~ScaleGestureDetector() {} |
| 81 |
| 82 bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { |
| 83 curr_time_ = event.GetEventTime(); |
| 84 |
| 85 const int action = event.GetAction(); |
| 86 |
| 87 // Forward the event to check for double tap gesture. |
| 88 if (quick_scale_enabled_) { |
| 89 DCHECK(gesture_detector_); |
| 90 gesture_detector_->OnTouchEvent(event); |
| 91 } |
| 92 |
| 93 const bool stream_complete = |
| 94 action == MotionEvent::ACTION_UP || action == MotionEvent::ACTION_CANCEL; |
| 95 |
| 96 if (action == MotionEvent::ACTION_DOWN || stream_complete) { |
| 97 // Reset any scale in progress with the listener. |
| 98 // If it's an ACTION_DOWN we're beginning a new event stream. |
| 99 // This means the app probably didn't give us all the events. Shame on it. |
| 100 if (in_progress_) { |
| 101 listener_->OnScaleEnd(*this); |
| 102 in_progress_ = false; |
| 103 initial_span_ = 0; |
| 104 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; |
| 105 } else if (double_tap_mode_ == DOUBLE_TAP_MODE_IN_PROGRESS && |
| 106 stream_complete) { |
| 107 in_progress_ = false; |
| 108 initial_span_ = 0; |
| 109 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; |
| 110 } |
| 111 |
| 112 if (stream_complete) { |
| 113 ClearTouchHistory(); |
| 114 return true; |
| 115 } |
| 116 } |
| 117 |
| 118 const bool config_changed = action == MotionEvent::ACTION_DOWN || |
| 119 action == MotionEvent::ACTION_POINTER_UP || |
| 120 action == MotionEvent::ACTION_POINTER_DOWN; |
| 121 |
| 122 const bool pointer_up = action == MotionEvent::ACTION_POINTER_UP; |
| 123 const int skip_index = pointer_up ? event.GetActionIndex() : -1; |
| 124 |
| 125 // Determine focal point. |
| 126 float sum_x = 0, sum_y = 0; |
| 127 const int count = event.GetPointerCount(); |
| 128 const int div = pointer_up ? count - 1 : count; |
| 129 float focus_x; |
| 130 float focus_y; |
| 131 if (double_tap_mode_ == DOUBLE_TAP_MODE_IN_PROGRESS) { |
| 132 // In double tap mode, the focal pt is always where the double tap |
| 133 // gesture started. |
| 134 focus_x = double_tap_focus_x_; |
| 135 focus_y = double_tap_focus_y_; |
| 136 if (event.GetY() < focus_y) { |
| 137 event_before_or_above_starting_gesture_event_ = true; |
| 138 } else { |
| 139 event_before_or_above_starting_gesture_event_ = false; |
| 140 } |
| 141 } else { |
| 142 for (int i = 0; i < count; i++) { |
| 143 if (skip_index == i) |
| 144 continue; |
| 145 sum_x += event.GetX(i); |
| 146 sum_y += event.GetY(i); |
| 147 } |
| 148 |
| 149 focus_x = sum_x / div; |
| 150 focus_y = sum_y / div; |
| 151 } |
| 152 |
| 153 AddTouchHistory(event); |
| 154 |
| 155 // Determine average deviation from focal point. |
| 156 float dev_sum_x = 0, dev_sum_y = 0; |
| 157 for (int i = 0; i < count; i++) { |
| 158 if (skip_index == i) |
| 159 continue; |
| 160 |
| 161 // Convert the resulting diameter into a radius. |
| 162 const float touch_size = touch_history_last_accepted_ / 2; |
| 163 dev_sum_x += std::abs(event.GetX(i) - focus_x) + touch_size; |
| 164 dev_sum_y += std::abs(event.GetY(i) - focus_y) + touch_size; |
| 165 } |
| 166 const float dev_x = dev_sum_x / div; |
| 167 const float dev_y = dev_sum_y / div; |
| 168 |
| 169 // Span is the average distance between touch points through the focal point; |
| 170 // i.e. the diameter of the circle with a radius of the average deviation from |
| 171 // the focal point. |
| 172 const float span_x = dev_x * 2; |
| 173 const float span_y = dev_y * 2; |
| 174 float span; |
| 175 if (InDoubleTapMode()) { |
| 176 span = span_y; |
| 177 } else { |
| 178 span = std::sqrt(span_x * span_x + span_y * span_y); |
| 179 } |
| 180 |
| 181 // Dispatch begin/end events as needed. |
| 182 // If the configuration changes, notify the app to reset its current state by |
| 183 // beginning a fresh scale event stream. |
| 184 const bool was_in_progress = in_progress_; |
| 185 focus_x_ = focus_x; |
| 186 focus_y_ = focus_y; |
| 187 if (!InDoubleTapMode() && in_progress_ && |
| 188 (span < min_span_ || config_changed)) { |
| 189 listener_->OnScaleEnd(*this); |
| 190 in_progress_ = false; |
| 191 initial_span_ = span; |
| 192 double_tap_mode_ = DOUBLE_TAP_MODE_NONE; |
| 193 } |
| 194 if (config_changed) { |
| 195 prev_span_x_ = curr_span_x_ = span_x; |
| 196 prev_span_y_ = curr_span_y_ = span_y; |
| 197 initial_span_ = prev_span_ = curr_span_ = span; |
| 198 } |
| 199 |
| 200 const int min_span = InDoubleTapMode() ? span_slop_ : min_span_; |
| 201 if (!in_progress_ && span >= min_span && |
| 202 (was_in_progress || std::abs(span - initial_span_) > span_slop_)) { |
| 203 prev_span_x_ = curr_span_x_ = span_x; |
| 204 prev_span_y_ = curr_span_y_ = span_y; |
| 205 prev_span_ = curr_span_ = span; |
| 206 prev_time_ = curr_time_; |
| 207 in_progress_ = listener_->OnScaleBegin(*this); |
| 208 } |
| 209 |
| 210 // Handle motion; focal point and span/scale factor are changing. |
| 211 if (action == MotionEvent::ACTION_MOVE) { |
| 212 curr_span_x_ = span_x; |
| 213 curr_span_y_ = span_y; |
| 214 curr_span_ = span; |
| 215 |
| 216 bool update_prev = true; |
| 217 |
| 218 if (in_progress_) { |
| 219 update_prev = listener_->OnScale(*this); |
| 220 } |
| 221 |
| 222 if (update_prev) { |
| 223 prev_span_x_ = curr_span_x_; |
| 224 prev_span_y_ = curr_span_y_; |
| 225 prev_span_ = curr_span_; |
| 226 prev_time_ = curr_time_; |
| 227 } |
| 228 } |
| 229 |
| 230 return true; |
| 231 } |
| 232 |
| 233 void ScaleGestureDetector::SetQuickScaleEnabled(bool scales) { |
| 234 quick_scale_enabled_ = scales; |
| 235 if (quick_scale_enabled_ && !gesture_detector_) { |
| 236 gesture_detector_.reset( |
| 237 new GestureDetector(config_.gesture_detector_config, this, this)); |
| 238 } |
| 239 } |
| 240 |
| 241 bool ScaleGestureDetector::IsQuickScaleEnabled() const { |
| 242 return quick_scale_enabled_; |
| 243 } |
| 244 |
| 245 bool ScaleGestureDetector::IsInProgress() const { return in_progress_; } |
| 246 |
| 247 float ScaleGestureDetector::GetFocusX() const { return focus_x_; } |
| 248 |
| 249 float ScaleGestureDetector::GetFocusY() const { return focus_y_; } |
| 250 |
| 251 float ScaleGestureDetector::GetCurrentSpan() const { return curr_span_; } |
| 252 |
| 253 float ScaleGestureDetector::GetCurrentSpanX() const { return curr_span_x_; } |
| 254 |
| 255 float ScaleGestureDetector::GetCurrentSpanY() const { return curr_span_y_; } |
| 256 |
| 257 float ScaleGestureDetector::GetPreviousSpan() const { return prev_span_; } |
| 258 |
| 259 float ScaleGestureDetector::GetPreviousSpanX() const { return prev_span_x_; } |
| 260 |
| 261 float ScaleGestureDetector::GetPreviousSpanY() const { return prev_span_y_; } |
| 262 |
| 263 float ScaleGestureDetector::GetScaleFactor() const { |
| 264 if (InDoubleTapMode()) { |
| 265 // Drag is moving up; the further away from the gesture start, the smaller |
| 266 // the span should be, the closer, the larger the span, and therefore the |
| 267 // larger the scale. |
| 268 const bool scale_up = (event_before_or_above_starting_gesture_event_ && |
| 269 (curr_span_ < prev_span_)) || |
| 270 (!event_before_or_above_starting_gesture_event_ && |
| 271 (curr_span_ > prev_span_)); |
| 272 const float span_diff = |
| 273 (std::abs(1.f - (curr_span_ / prev_span_)) * kScaleFactor); |
| 274 return prev_span_ <= 0 ? 1.f |
| 275 : (scale_up ? (1.f + span_diff) : (1.f - span_diff)); |
| 276 } |
| 277 return prev_span_ > 0 ? curr_span_ / prev_span_ : 1; |
| 278 } |
| 279 |
| 280 base::TimeDelta ScaleGestureDetector::GetTimeDelta() const { |
| 281 return curr_time_ - prev_time_; |
| 282 } |
| 283 |
| 284 base::TimeTicks ScaleGestureDetector::GetEventTime() const { |
| 285 return curr_time_; |
| 286 } |
| 287 |
| 288 bool ScaleGestureDetector::OnDoubleTap(const MotionEvent& ev) { |
| 289 // Double tap: start watching for a swipe. |
| 290 double_tap_focus_x_ = ev.GetX(); |
| 291 double_tap_focus_y_ = ev.GetY(); |
| 292 double_tap_mode_ = DOUBLE_TAP_MODE_IN_PROGRESS; |
| 293 return true; |
| 294 } |
| 295 |
| 296 void ScaleGestureDetector::AddTouchHistory(const MotionEvent& ev) { |
| 297 const base::TimeTicks current_time = base::TimeTicks::Now(); |
| 298 const int count = ev.GetPointerCount(); |
| 299 bool accept = (current_time - touch_history_last_accepted_time_) |
| 300 .InMilliseconds() >= kTouchStabilizeTimeMs; |
| 301 float total = 0; |
| 302 int sample_count = 0; |
| 303 for (int i = 0; i < count; i++) { |
| 304 const bool has_last_accepted = !std::isnan(touch_history_last_accepted_); |
| 305 const int history_size = ev.GetHistorySize(); |
| 306 const int pointersample_count = history_size + 1; |
| 307 for (int h = 0; h < pointersample_count; h++) { |
| 308 float major; |
| 309 if (h < history_size) { |
| 310 major = ev.GetHistoricalTouchMajor(i, h); |
| 311 } else { |
| 312 major = ev.GetTouchMajor(i); |
| 313 } |
| 314 if (major < touch_min_major_) |
| 315 major = touch_min_major_; |
| 316 total += major; |
| 317 |
| 318 if (std::isnan(touch_upper_) || major > touch_upper_) { |
| 319 touch_upper_ = major; |
| 320 } |
| 321 if (std::isnan(touch_lower_) || major < touch_lower_) { |
| 322 touch_lower_ = major; |
| 323 } |
| 324 |
| 325 if (has_last_accepted) { |
| 326 const float major_delta = major - touch_history_last_accepted_; |
| 327 const int direction_sig = |
| 328 major_delta > 0 ? 1 : (major_delta < 0 ? -1 : 0); |
| 329 if (direction_sig != touch_history_direction_ || |
| 330 (direction_sig == 0 && touch_history_direction_ == 0)) { |
| 331 touch_history_direction_ = direction_sig; |
| 332 touch_history_last_accepted_time_ = h < history_size |
| 333 ? ev.GetHistoricalEventTime(h) |
| 334 : ev.GetEventTime(); |
| 335 accept = false; |
| 336 } |
| 337 } |
| 338 } |
| 339 sample_count += pointersample_count; |
| 340 } |
| 341 |
| 342 const float avg = total / sample_count; |
| 343 |
| 344 if (accept) { |
| 345 float new_accepted = (touch_upper_ + touch_lower_ + avg) / 3; |
| 346 touch_upper_ = (touch_upper_ + new_accepted) / 2; |
| 347 touch_lower_ = (touch_lower_ + new_accepted) / 2; |
| 348 touch_history_last_accepted_ = new_accepted; |
| 349 touch_history_direction_ = 0; |
| 350 touch_history_last_accepted_time_ = ev.GetEventTime(); |
| 351 } |
| 352 } |
| 353 |
| 354 void ScaleGestureDetector::ClearTouchHistory() { |
| 355 touch_upper_ = std::numeric_limits<float>::quiet_NaN(); |
| 356 touch_lower_ = std::numeric_limits<float>::quiet_NaN(); |
| 357 touch_history_last_accepted_ = std::numeric_limits<float>::quiet_NaN(); |
| 358 touch_history_direction_ = 0; |
| 359 touch_history_last_accepted_time_ = base::TimeTicks(); |
| 360 } |
| 361 |
| 362 bool ScaleGestureDetector::InDoubleTapMode() const { |
| 363 return double_tap_mode_ == DOUBLE_TAP_MODE_IN_PROGRESS; |
| 364 } |
| 365 |
| 366 } // namespace ui |
OLD | NEW |