reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 8 | #include <algorithm> |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkMatrix.h" |
| 11 | #include "include/core/SkTime.h" |
| 12 | #include "tools/viewer/TouchGesture.h" |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 13 | |
| 14 | #define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true |
| 15 | |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 16 | static const SkScalar MAX_FLING_SPEED = SkIntToScalar(1500); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 17 | |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 18 | static SkScalar pin_max_fling(SkScalar speed) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 19 | if (speed > MAX_FLING_SPEED) { |
| 20 | speed = MAX_FLING_SPEED; |
| 21 | } |
| 22 | return speed; |
| 23 | } |
| 24 | |
| 25 | static double getseconds() { |
| 26 | return SkTime::GetMSecs() * 0.001; |
| 27 | } |
| 28 | |
| 29 | // returns +1 or -1, depending on the sign of x |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 30 | // returns +1 if z is zero |
| 31 | static SkScalar SkScalarSignNonZero(SkScalar x) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 32 | SkScalar sign = SK_Scalar1; |
| 33 | if (x < 0) { |
| 34 | sign = -sign; |
| 35 | } |
| 36 | return sign; |
| 37 | } |
| 38 | |
| 39 | static void unit_axis_align(SkVector* unit) { |
| 40 | const SkScalar TOLERANCE = SkDoubleToScalar(0.15); |
| 41 | if (SkScalarAbs(unit->fX) < TOLERANCE) { |
| 42 | unit->fX = 0; |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 43 | unit->fY = SkScalarSignNonZero(unit->fY); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 44 | } else if (SkScalarAbs(unit->fY) < TOLERANCE) { |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 45 | unit->fX = SkScalarSignNonZero(unit->fX); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 46 | unit->fY = 0; |
| 47 | } |
| 48 | } |
| 49 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 50 | void TouchGesture::FlingState::reset(float sx, float sy) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 51 | fActive = true; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 52 | fDirection.set(sx, sy); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 53 | fSpeed0 = SkPoint::Normalize(&fDirection); |
| 54 | fSpeed0 = pin_max_fling(fSpeed0); |
| 55 | fTime0 = getseconds(); |
| 56 | |
| 57 | unit_axis_align(&fDirection); |
| 58 | // printf("---- speed %g dir %g %g\n", fSpeed0, fDirection.fX, fDirection.fY); |
| 59 | } |
| 60 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 61 | bool TouchGesture::FlingState::evaluateMatrix(SkMatrix* matrix) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 62 | if (!fActive) { |
| 63 | return false; |
| 64 | } |
| 65 | |
reed@google.com | c31a9d6 | 2011-03-18 20:53:44 +0000 | [diff] [blame] | 66 | const float t = (float)(getseconds() - fTime0); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 67 | const float MIN_SPEED = 2; |
reed@google.com | c31a9d6 | 2011-03-18 20:53:44 +0000 | [diff] [blame] | 68 | const float K0 = 5; |
| 69 | const float K1 = 0.02f; |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 70 | const float speed = fSpeed0 * (sk_float_exp(- K0 * t) - K1); |
| 71 | if (speed <= MIN_SPEED) { |
| 72 | fActive = false; |
| 73 | return false; |
| 74 | } |
| 75 | float dist = (fSpeed0 - speed) / K0; |
| 76 | |
| 77 | // printf("---- time %g speed %g dist %g\n", t, speed, dist); |
| 78 | float tx = fDirection.fX * dist; |
| 79 | float ty = fDirection.fY * dist; |
| 80 | if (DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER) { |
reed@google.com | c31a9d6 | 2011-03-18 20:53:44 +0000 | [diff] [blame] | 81 | tx = (float)sk_float_round2int(tx); |
| 82 | ty = (float)sk_float_round2int(ty); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 83 | } |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 84 | matrix->setTranslate(tx, ty); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 85 | // printf("---- evaluate (%g %g)\n", tx, ty); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | static const SkMSec MAX_DBL_TAP_INTERVAL = 300; |
| 93 | static const float MAX_DBL_TAP_DISTANCE = 100; |
| 94 | static const float MAX_JITTER_RADIUS = 2; |
| 95 | |
| 96 | // if true, then ignore the touch-move, 'cause its probably just jitter |
| 97 | static bool close_enough_for_jitter(float x0, float y0, float x1, float y1) { |
| 98 | return sk_float_abs(x0 - x1) <= MAX_JITTER_RADIUS && |
| 99 | sk_float_abs(y0 - y1) <= MAX_JITTER_RADIUS; |
| 100 | } |
| 101 | |
| 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 104 | TouchGesture::TouchGesture() { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 105 | this->reset(); |
| 106 | } |
| 107 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 108 | TouchGesture::~TouchGesture() { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 111 | void TouchGesture::resetTouchState() { |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 112 | fIsTransLimited = false; |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 113 | fTouches.reset(); |
| 114 | fState = kEmpty_State; |
| 115 | fLocalM.reset(); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 116 | |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 117 | fLastUpMillis = SkTime::GetMSecs() - 2*MAX_DBL_TAP_INTERVAL; |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 118 | fLastUpP.set(0, 0); |
| 119 | } |
| 120 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 121 | void TouchGesture::reset() { |
Brian Salomon | 88d99df | 2018-01-05 11:58:16 -0500 | [diff] [blame] | 122 | fGlobalM.reset(); |
| 123 | this->resetTouchState(); |
| 124 | } |
| 125 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 126 | void TouchGesture::flushLocalM() { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 127 | fGlobalM.postConcat(fLocalM); |
| 128 | fLocalM.reset(); |
| 129 | } |
| 130 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 131 | const SkMatrix& TouchGesture::localM() { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 132 | if (fFlinger.isActive()) { |
| 133 | if (!fFlinger.evaluateMatrix(&fLocalM)) { |
| 134 | this->flushLocalM(); |
| 135 | } |
| 136 | } |
| 137 | return fLocalM; |
| 138 | } |
| 139 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 140 | void TouchGesture::appendNewRec(void* owner, float x, float y) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 141 | Rec* rec = fTouches.append(); |
| 142 | rec->fOwner = owner; |
| 143 | rec->fStartX = rec->fPrevX = rec->fLastX = x; |
| 144 | rec->fStartY = rec->fPrevY = rec->fLastY = y; |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 145 | rec->fLastT = rec->fPrevT = static_cast<float>(SkTime::GetSecs()); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 148 | void TouchGesture::touchBegin(void* owner, float x, float y) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 149 | // SkDebugf("--- %d touchBegin %p %g %g\n", fTouches.count(), owner, x, y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 150 | |
| 151 | int index = this->findRec(owner); |
| 152 | if (index >= 0) { |
| 153 | this->flushLocalM(); |
| 154 | fTouches.removeShuffle(index); |
| 155 | SkDebugf("---- already exists, removing\n"); |
| 156 | } |
| 157 | |
| 158 | if (fTouches.count() == 2) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | this->flushLocalM(); |
| 163 | fFlinger.stop(); |
| 164 | |
| 165 | this->appendNewRec(owner, x, y); |
| 166 | |
| 167 | switch (fTouches.count()) { |
| 168 | case 1: |
| 169 | fState = kTranslate_State; |
| 170 | break; |
| 171 | case 2: |
Jim Van Verth | d0cf5da | 2019-09-09 16:53:39 -0400 | [diff] [blame] | 172 | this->startZoom(); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 173 | break; |
| 174 | default: |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 179 | int TouchGesture::findRec(void* owner) const { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 180 | for (int i = 0; i < fTouches.count(); i++) { |
| 181 | if (owner == fTouches[i].fOwner) { |
| 182 | return i; |
| 183 | } |
| 184 | } |
| 185 | return -1; |
| 186 | } |
| 187 | |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 188 | static SkScalar center(float pos0, float pos1) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 189 | return (pos0 + pos1) * 0.5f; |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static const float MAX_ZOOM_SCALE = 4; |
| 193 | static const float MIN_ZOOM_SCALE = 0.25f; |
| 194 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 195 | float TouchGesture::limitTotalZoom(float scale) const { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 196 | // this query works 'cause we know that we're square-scale w/ no skew/rotation |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 197 | const float curr = SkScalarToFloat(fGlobalM[0]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 198 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 199 | if (scale > 1 && curr * scale > MAX_ZOOM_SCALE) { |
| 200 | scale = MAX_ZOOM_SCALE / curr; |
| 201 | } else if (scale < 1 && curr * scale < MIN_ZOOM_SCALE) { |
| 202 | scale = MIN_ZOOM_SCALE / curr; |
| 203 | } |
| 204 | return scale; |
| 205 | } |
| 206 | |
Jim Van Verth | d0cf5da | 2019-09-09 16:53:39 -0400 | [diff] [blame] | 207 | void TouchGesture::startZoom() { |
| 208 | fState = kZoom_State; |
| 209 | } |
| 210 | |
| 211 | void TouchGesture::updateZoom(float scale, float startX, float startY, float lastX, float lastY) { |
| 212 | scale = this->limitTotalZoom(scale); |
| 213 | |
| 214 | fLocalM.setTranslate(-startX, -startY); |
| 215 | fLocalM.postScale(scale, scale); |
| 216 | fLocalM.postTranslate(lastX, lastY); |
| 217 | } |
| 218 | |
| 219 | void TouchGesture::endZoom() { |
| 220 | this->flushLocalM(); |
| 221 | SkASSERT(kZoom_State == fState); |
| 222 | fState = kEmpty_State; |
| 223 | } |
| 224 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 225 | void TouchGesture::touchMoved(void* owner, float x, float y) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 226 | // SkDebugf("--- %d touchMoved %p %g %g\n", fTouches.count(), owner, x, y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 227 | |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 228 | if (kEmpty_State == fState) { |
| 229 | return; |
| 230 | } |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 231 | |
| 232 | int index = this->findRec(owner); |
| 233 | if (index < 0) { |
Brian Osman | b53f48c | 2017-06-07 10:00:30 -0400 | [diff] [blame] | 234 | SkDebugf("---- ignoring move without begin\n"); |
| 235 | return; |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | Rec& rec = fTouches[index]; |
| 239 | |
| 240 | // not sure how valuable this is |
| 241 | if (fTouches.count() == 2) { |
| 242 | if (close_enough_for_jitter(rec.fLastX, rec.fLastY, x, y)) { |
Brian Osman | b53f48c | 2017-06-07 10:00:30 -0400 | [diff] [blame] | 243 | // SkDebugf("--- drop touchMove, within jitter tolerance %g %g\n", rec.fLastX - x, rec.fLastY - y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | rec.fPrevX = rec.fLastX; rec.fLastX = x; |
| 249 | rec.fPrevY = rec.fLastY; rec.fLastY = y; |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 250 | rec.fPrevT = rec.fLastT; |
| 251 | rec.fLastT = static_cast<float>(SkTime::GetSecs()); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 252 | |
| 253 | switch (fTouches.count()) { |
| 254 | case 1: { |
| 255 | float dx = rec.fLastX - rec.fStartX; |
| 256 | float dy = rec.fLastY - rec.fStartY; |
| 257 | dx = (float)sk_float_round2int(dx); |
| 258 | dy = (float)sk_float_round2int(dy); |
| 259 | fLocalM.setTranslate(dx, dy); |
| 260 | } break; |
| 261 | case 2: { |
| 262 | SkASSERT(kZoom_State == fState); |
| 263 | const Rec& rec0 = fTouches[0]; |
| 264 | const Rec& rec1 = fTouches[1]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 265 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 266 | float scale = this->computePinch(rec0, rec1); |
Jim Van Verth | d0cf5da | 2019-09-09 16:53:39 -0400 | [diff] [blame] | 267 | this->updateZoom(scale, |
| 268 | center(rec0.fStartX, rec1.fStartX), |
| 269 | center(rec0.fStartY, rec1.fStartY), |
| 270 | center(rec0.fLastX, rec1.fLastX), |
| 271 | center(rec0.fLastY, rec1.fLastY)); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 272 | } break; |
| 273 | default: |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 278 | void TouchGesture::touchEnd(void* owner) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 279 | // SkDebugf("--- %d touchEnd %p\n", fTouches.count(), owner); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 280 | |
| 281 | int index = this->findRec(owner); |
| 282 | if (index < 0) { |
| 283 | SkDebugf("--- not found\n"); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | const Rec& rec = fTouches[index]; |
| 288 | if (this->handleDblTap(rec.fLastX, rec.fLastY)) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | // count() reflects the number before we removed the owner |
| 293 | switch (fTouches.count()) { |
| 294 | case 1: { |
| 295 | this->flushLocalM(); |
| 296 | float dx = rec.fLastX - rec.fPrevX; |
| 297 | float dy = rec.fLastY - rec.fPrevY; |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 298 | float dur = rec.fLastT - rec.fPrevT; |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 299 | if (dur > 0) { |
| 300 | fFlinger.reset(dx / dur, dy / dur); |
| 301 | } |
| 302 | fState = kEmpty_State; |
| 303 | } break; |
| 304 | case 2: |
Jim Van Verth | d0cf5da | 2019-09-09 16:53:39 -0400 | [diff] [blame] | 305 | this->endZoom(); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 306 | break; |
| 307 | default: |
| 308 | SkASSERT(kZoom_State == fState); |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | fTouches.removeShuffle(index); |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 313 | |
| 314 | limitTrans(); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 317 | bool TouchGesture::isFling(SkPoint* dir) { |
Jim Van Verth | 234e5a2 | 2018-07-23 13:46:01 -0400 | [diff] [blame] | 318 | if (fFlinger.isActive()) { |
| 319 | SkScalar speed; |
| 320 | fFlinger.get(dir, &speed); |
| 321 | if (speed > 1000) { |
| 322 | return true; |
| 323 | } |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 328 | float TouchGesture::computePinch(const Rec& rec0, const Rec& rec1) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 329 | double dx = rec0.fStartX - rec1.fStartX; |
| 330 | double dy = rec0.fStartY - rec1.fStartY; |
| 331 | double dist0 = sqrt(dx*dx + dy*dy); |
| 332 | |
| 333 | dx = rec0.fLastX - rec1.fLastX; |
| 334 | dy = rec0.fLastY - rec1.fLastY; |
| 335 | double dist1 = sqrt(dx*dx + dy*dy); |
| 336 | |
| 337 | double scale = dist1 / dist0; |
| 338 | return (float)scale; |
| 339 | } |
| 340 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 341 | bool TouchGesture::handleDblTap(float x, float y) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 342 | bool found = false; |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 343 | double now = SkTime::GetMSecs(); |
| 344 | if (now - fLastUpMillis <= MAX_DBL_TAP_INTERVAL) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 345 | if (SkPoint::Length(fLastUpP.fX - x, |
| 346 | fLastUpP.fY - y) <= MAX_DBL_TAP_DISTANCE) { |
| 347 | fFlinger.stop(); |
| 348 | fLocalM.reset(); |
| 349 | fGlobalM.reset(); |
| 350 | fTouches.reset(); |
| 351 | fState = kEmpty_State; |
| 352 | found = true; |
| 353 | } |
| 354 | } |
| 355 | |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 356 | fLastUpMillis = now; |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 357 | fLastUpP.set(x, y); |
| 358 | return found; |
| 359 | } |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 360 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 361 | void TouchGesture::setTransLimit(const SkRect& contentRect, const SkRect& windowRect, |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 362 | const SkMatrix& preTouchMatrix) { |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 363 | fIsTransLimited = true; |
| 364 | fContentRect = contentRect; |
| 365 | fWindowRect = windowRect; |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 366 | fPreTouchM = preTouchMatrix; |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 369 | void TouchGesture::limitTrans() { |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 370 | if (!fIsTransLimited) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | SkRect scaledContent = fContentRect; |
Brian Osman | 42bb6ac | 2017-06-05 08:46:04 -0400 | [diff] [blame] | 375 | fPreTouchM.mapRect(&scaledContent); |
liyuqian | e46e4f0 | 2016-05-20 07:32:19 -0700 | [diff] [blame] | 376 | fGlobalM.mapRect(&scaledContent); |
| 377 | const SkScalar ZERO = 0; |
| 378 | |
| 379 | fGlobalM.postTranslate(ZERO, std::min(ZERO, fWindowRect.fBottom - scaledContent.fTop)); |
| 380 | fGlobalM.postTranslate(ZERO, std::max(ZERO, fWindowRect.fTop - scaledContent.fBottom)); |
| 381 | fGlobalM.postTranslate(std::min(ZERO, fWindowRect.fRight - scaledContent.fLeft), ZERO); |
| 382 | fGlobalM.postTranslate(std::max(ZERO, fWindowRect.fLeft - scaledContent.fRight), ZERO); |
| 383 | } |