blob: 4ce4114352c8522700e773d4fdda8eff6ef12847 [file] [log] [blame]
reed@google.com52f57e12011-03-16 12:10:02 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.com52f57e12011-03-16 12:10:02 +00006 */
7
liyuqiane46e4f02016-05-20 07:32:19 -07008#include <algorithm>
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkMatrix.h"
11#include "include/core/SkTime.h"
12#include "tools/viewer/TouchGesture.h"
reed@google.com52f57e12011-03-16 12:10:02 +000013
14#define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true
15
robertphillips@google.com59f46b82012-07-10 17:30:58 +000016static const SkScalar MAX_FLING_SPEED = SkIntToScalar(1500);
reed@google.com52f57e12011-03-16 12:10:02 +000017
robertphillips@google.com59f46b82012-07-10 17:30:58 +000018static SkScalar pin_max_fling(SkScalar speed) {
reed@google.com52f57e12011-03-16 12:10:02 +000019 if (speed > MAX_FLING_SPEED) {
20 speed = MAX_FLING_SPEED;
21 }
22 return speed;
23}
24
25static double getseconds() {
26 return SkTime::GetMSecs() * 0.001;
27}
28
29// returns +1 or -1, depending on the sign of x
bsalomon@google.com647a8042011-08-23 14:39:01 +000030// returns +1 if z is zero
31static SkScalar SkScalarSignNonZero(SkScalar x) {
reed@google.com52f57e12011-03-16 12:10:02 +000032 SkScalar sign = SK_Scalar1;
33 if (x < 0) {
34 sign = -sign;
35 }
36 return sign;
37}
38
39static 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.com647a8042011-08-23 14:39:01 +000043 unit->fY = SkScalarSignNonZero(unit->fY);
reed@google.com52f57e12011-03-16 12:10:02 +000044 } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
bsalomon@google.com647a8042011-08-23 14:39:01 +000045 unit->fX = SkScalarSignNonZero(unit->fX);
reed@google.com52f57e12011-03-16 12:10:02 +000046 unit->fY = 0;
47 }
48}
49
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040050void TouchGesture::FlingState::reset(float sx, float sy) {
reed@google.com52f57e12011-03-16 12:10:02 +000051 fActive = true;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000052 fDirection.set(sx, sy);
reed@google.com52f57e12011-03-16 12:10:02 +000053 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 Wagnerb2c4ea62018-08-08 11:36:17 -040061bool TouchGesture::FlingState::evaluateMatrix(SkMatrix* matrix) {
reed@google.com52f57e12011-03-16 12:10:02 +000062 if (!fActive) {
63 return false;
64 }
65
reed@google.comc31a9d62011-03-18 20:53:44 +000066 const float t = (float)(getseconds() - fTime0);
reed@google.com52f57e12011-03-16 12:10:02 +000067 const float MIN_SPEED = 2;
reed@google.comc31a9d62011-03-18 20:53:44 +000068 const float K0 = 5;
69 const float K1 = 0.02f;
reed@google.com52f57e12011-03-16 12:10:02 +000070 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.comc31a9d62011-03-18 20:53:44 +000081 tx = (float)sk_float_round2int(tx);
82 ty = (float)sk_float_round2int(ty);
reed@google.com52f57e12011-03-16 12:10:02 +000083 }
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000084 matrix->setTranslate(tx, ty);
reed@google.com52f57e12011-03-16 12:10:02 +000085// printf("---- evaluate (%g %g)\n", tx, ty);
86
87 return true;
88}
89
90///////////////////////////////////////////////////////////////////////////////
91
92static const SkMSec MAX_DBL_TAP_INTERVAL = 300;
93static const float MAX_DBL_TAP_DISTANCE = 100;
94static const float MAX_JITTER_RADIUS = 2;
95
96// if true, then ignore the touch-move, 'cause its probably just jitter
97static 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 Wagnerb2c4ea62018-08-08 11:36:17 -0400104TouchGesture::TouchGesture() {
reed@google.com52f57e12011-03-16 12:10:02 +0000105 this->reset();
106}
107
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108TouchGesture::~TouchGesture() {
reed@google.com52f57e12011-03-16 12:10:02 +0000109}
110
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400111void TouchGesture::resetTouchState() {
liyuqiane46e4f02016-05-20 07:32:19 -0700112 fIsTransLimited = false;
reed@google.com52f57e12011-03-16 12:10:02 +0000113 fTouches.reset();
114 fState = kEmpty_State;
115 fLocalM.reset();
reed@google.com52f57e12011-03-16 12:10:02 +0000116
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700117 fLastUpMillis = SkTime::GetMSecs() - 2*MAX_DBL_TAP_INTERVAL;
reed@google.com52f57e12011-03-16 12:10:02 +0000118 fLastUpP.set(0, 0);
119}
120
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400121void TouchGesture::reset() {
Brian Salomon88d99df2018-01-05 11:58:16 -0500122 fGlobalM.reset();
123 this->resetTouchState();
124}
125
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400126void TouchGesture::flushLocalM() {
reed@google.com52f57e12011-03-16 12:10:02 +0000127 fGlobalM.postConcat(fLocalM);
128 fLocalM.reset();
129}
130
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400131const SkMatrix& TouchGesture::localM() {
reed@google.com52f57e12011-03-16 12:10:02 +0000132 if (fFlinger.isActive()) {
133 if (!fFlinger.evaluateMatrix(&fLocalM)) {
134 this->flushLocalM();
135 }
136 }
137 return fLocalM;
138}
139
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400140void TouchGesture::appendNewRec(void* owner, float x, float y) {
reed@google.com52f57e12011-03-16 12:10:02 +0000141 Rec* rec = fTouches.append();
142 rec->fOwner = owner;
143 rec->fStartX = rec->fPrevX = rec->fLastX = x;
144 rec->fStartY = rec->fPrevY = rec->fLastY = y;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700145 rec->fLastT = rec->fPrevT = static_cast<float>(SkTime::GetSecs());
reed@google.com52f57e12011-03-16 12:10:02 +0000146}
147
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400148void TouchGesture::touchBegin(void* owner, float x, float y) {
tfarina38406c82014-10-31 07:11:12 -0700149// SkDebugf("--- %d touchBegin %p %g %g\n", fTouches.count(), owner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +0000150
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 Verthd0cf5da2019-09-09 16:53:39 -0400172 this->startZoom();
reed@google.com52f57e12011-03-16 12:10:02 +0000173 break;
174 default:
175 break;
176 }
177}
178
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400179int TouchGesture::findRec(void* owner) const {
reed@google.com52f57e12011-03-16 12:10:02 +0000180 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.com59f46b82012-07-10 17:30:58 +0000188static SkScalar center(float pos0, float pos1) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000189 return (pos0 + pos1) * 0.5f;
reed@google.com52f57e12011-03-16 12:10:02 +0000190}
191
Jim Van Verthd0cf5da2019-09-09 16:53:39 -0400192void TouchGesture::startZoom() {
193 fState = kZoom_State;
194}
195
196void TouchGesture::updateZoom(float scale, float startX, float startY, float lastX, float lastY) {
Jim Van Verthd0cf5da2019-09-09 16:53:39 -0400197 fLocalM.setTranslate(-startX, -startY);
198 fLocalM.postScale(scale, scale);
199 fLocalM.postTranslate(lastX, lastY);
200}
201
202void TouchGesture::endZoom() {
203 this->flushLocalM();
204 SkASSERT(kZoom_State == fState);
205 fState = kEmpty_State;
206}
207
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400208void TouchGesture::touchMoved(void* owner, float x, float y) {
tfarina38406c82014-10-31 07:11:12 -0700209// SkDebugf("--- %d touchMoved %p %g %g\n", fTouches.count(), owner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +0000210
caryclark936b7342014-07-11 12:14:51 -0700211 if (kEmpty_State == fState) {
212 return;
213 }
reed@google.com52f57e12011-03-16 12:10:02 +0000214
215 int index = this->findRec(owner);
216 if (index < 0) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400217 SkDebugf("---- ignoring move without begin\n");
218 return;
reed@google.com52f57e12011-03-16 12:10:02 +0000219 }
220
221 Rec& rec = fTouches[index];
222
223 // not sure how valuable this is
224 if (fTouches.count() == 2) {
225 if (close_enough_for_jitter(rec.fLastX, rec.fLastY, x, y)) {
Brian Osmanb53f48c2017-06-07 10:00:30 -0400226// SkDebugf("--- drop touchMove, within jitter tolerance %g %g\n", rec.fLastX - x, rec.fLastY - y);
reed@google.com52f57e12011-03-16 12:10:02 +0000227 return;
228 }
229 }
230
231 rec.fPrevX = rec.fLastX; rec.fLastX = x;
232 rec.fPrevY = rec.fLastY; rec.fLastY = y;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700233 rec.fPrevT = rec.fLastT;
234 rec.fLastT = static_cast<float>(SkTime::GetSecs());
reed@google.com52f57e12011-03-16 12:10:02 +0000235
236 switch (fTouches.count()) {
237 case 1: {
238 float dx = rec.fLastX - rec.fStartX;
239 float dy = rec.fLastY - rec.fStartY;
240 dx = (float)sk_float_round2int(dx);
241 dy = (float)sk_float_round2int(dy);
242 fLocalM.setTranslate(dx, dy);
243 } break;
244 case 2: {
245 SkASSERT(kZoom_State == fState);
246 const Rec& rec0 = fTouches[0];
247 const Rec& rec1 = fTouches[1];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
reed@google.com52f57e12011-03-16 12:10:02 +0000249 float scale = this->computePinch(rec0, rec1);
Jim Van Verthd0cf5da2019-09-09 16:53:39 -0400250 this->updateZoom(scale,
251 center(rec0.fStartX, rec1.fStartX),
252 center(rec0.fStartY, rec1.fStartY),
253 center(rec0.fLastX, rec1.fLastX),
254 center(rec0.fLastY, rec1.fLastY));
reed@google.com52f57e12011-03-16 12:10:02 +0000255 } break;
256 default:
257 break;
258 }
259}
260
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400261void TouchGesture::touchEnd(void* owner) {
tfarina38406c82014-10-31 07:11:12 -0700262// SkDebugf("--- %d touchEnd %p\n", fTouches.count(), owner);
reed@google.com52f57e12011-03-16 12:10:02 +0000263
264 int index = this->findRec(owner);
265 if (index < 0) {
266 SkDebugf("--- not found\n");
267 return;
268 }
269
270 const Rec& rec = fTouches[index];
271 if (this->handleDblTap(rec.fLastX, rec.fLastY)) {
272 return;
273 }
274
275 // count() reflects the number before we removed the owner
276 switch (fTouches.count()) {
277 case 1: {
278 this->flushLocalM();
279 float dx = rec.fLastX - rec.fPrevX;
280 float dy = rec.fLastY - rec.fPrevY;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700281 float dur = rec.fLastT - rec.fPrevT;
reed@google.com52f57e12011-03-16 12:10:02 +0000282 if (dur > 0) {
283 fFlinger.reset(dx / dur, dy / dur);
284 }
285 fState = kEmpty_State;
286 } break;
287 case 2:
Jim Van Verthd0cf5da2019-09-09 16:53:39 -0400288 this->endZoom();
reed@google.com52f57e12011-03-16 12:10:02 +0000289 break;
290 default:
291 SkASSERT(kZoom_State == fState);
292 break;
293 }
294
295 fTouches.removeShuffle(index);
liyuqiane46e4f02016-05-20 07:32:19 -0700296
297 limitTrans();
reed@google.com52f57e12011-03-16 12:10:02 +0000298}
299
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400300bool TouchGesture::isFling(SkPoint* dir) {
Jim Van Verth234e5a22018-07-23 13:46:01 -0400301 if (fFlinger.isActive()) {
302 SkScalar speed;
303 fFlinger.get(dir, &speed);
304 if (speed > 1000) {
305 return true;
306 }
307 }
308 return false;
309}
310
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400311float TouchGesture::computePinch(const Rec& rec0, const Rec& rec1) {
reed@google.com52f57e12011-03-16 12:10:02 +0000312 double dx = rec0.fStartX - rec1.fStartX;
313 double dy = rec0.fStartY - rec1.fStartY;
314 double dist0 = sqrt(dx*dx + dy*dy);
315
316 dx = rec0.fLastX - rec1.fLastX;
317 dy = rec0.fLastY - rec1.fLastY;
318 double dist1 = sqrt(dx*dx + dy*dy);
319
320 double scale = dist1 / dist0;
321 return (float)scale;
322}
323
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400324bool TouchGesture::handleDblTap(float x, float y) {
reed@google.com52f57e12011-03-16 12:10:02 +0000325 bool found = false;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700326 double now = SkTime::GetMSecs();
327 if (now - fLastUpMillis <= MAX_DBL_TAP_INTERVAL) {
reed@google.com52f57e12011-03-16 12:10:02 +0000328 if (SkPoint::Length(fLastUpP.fX - x,
329 fLastUpP.fY - y) <= MAX_DBL_TAP_DISTANCE) {
330 fFlinger.stop();
331 fLocalM.reset();
332 fGlobalM.reset();
333 fTouches.reset();
334 fState = kEmpty_State;
335 found = true;
336 }
337 }
338
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700339 fLastUpMillis = now;
reed@google.com52f57e12011-03-16 12:10:02 +0000340 fLastUpP.set(x, y);
341 return found;
342}
liyuqiane46e4f02016-05-20 07:32:19 -0700343
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400344void TouchGesture::setTransLimit(const SkRect& contentRect, const SkRect& windowRect,
Brian Osman42bb6ac2017-06-05 08:46:04 -0400345 const SkMatrix& preTouchMatrix) {
liyuqiane46e4f02016-05-20 07:32:19 -0700346 fIsTransLimited = true;
347 fContentRect = contentRect;
348 fWindowRect = windowRect;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400349 fPreTouchM = preTouchMatrix;
liyuqiane46e4f02016-05-20 07:32:19 -0700350}
351
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400352void TouchGesture::limitTrans() {
liyuqiane46e4f02016-05-20 07:32:19 -0700353 if (!fIsTransLimited) {
354 return;
355 }
356
357 SkRect scaledContent = fContentRect;
Brian Osman42bb6ac2017-06-05 08:46:04 -0400358 fPreTouchM.mapRect(&scaledContent);
liyuqiane46e4f02016-05-20 07:32:19 -0700359 fGlobalM.mapRect(&scaledContent);
360 const SkScalar ZERO = 0;
361
362 fGlobalM.postTranslate(ZERO, std::min(ZERO, fWindowRect.fBottom - scaledContent.fTop));
363 fGlobalM.postTranslate(ZERO, std::max(ZERO, fWindowRect.fTop - scaledContent.fBottom));
364 fGlobalM.postTranslate(std::min(ZERO, fWindowRect.fRight - scaledContent.fLeft), ZERO);
365 fGlobalM.postTranslate(std::max(ZERO, fWindowRect.fLeft - scaledContent.fRight), ZERO);
366}