blob: e6a8eaeec66ab19c607818141b1bf5196c2c0423 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.com52f57e12011-03-16 12:10:02 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.com52f57e12011-03-16 12:10:02 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.com52f57e12011-03-16 12:10:02 +000011#include "SkTouchGesture.h"
12#include "SkMatrix.h"
13#include "SkTime.h"
14
15#define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true
16
robertphillips@google.com59f46b82012-07-10 17:30:58 +000017static const SkScalar MAX_FLING_SPEED = SkIntToScalar(1500);
reed@google.com52f57e12011-03-16 12:10:02 +000018
robertphillips@google.com59f46b82012-07-10 17:30:58 +000019static SkScalar pin_max_fling(SkScalar speed) {
reed@google.com52f57e12011-03-16 12:10:02 +000020 if (speed > MAX_FLING_SPEED) {
21 speed = MAX_FLING_SPEED;
22 }
23 return speed;
24}
25
26static double getseconds() {
27 return SkTime::GetMSecs() * 0.001;
28}
29
30// returns +1 or -1, depending on the sign of x
bsalomon@google.com647a8042011-08-23 14:39:01 +000031// returns +1 if z is zero
32static SkScalar SkScalarSignNonZero(SkScalar x) {
reed@google.com52f57e12011-03-16 12:10:02 +000033 SkScalar sign = SK_Scalar1;
34 if (x < 0) {
35 sign = -sign;
36 }
37 return sign;
38}
39
40static void unit_axis_align(SkVector* unit) {
41 const SkScalar TOLERANCE = SkDoubleToScalar(0.15);
42 if (SkScalarAbs(unit->fX) < TOLERANCE) {
43 unit->fX = 0;
bsalomon@google.com647a8042011-08-23 14:39:01 +000044 unit->fY = SkScalarSignNonZero(unit->fY);
reed@google.com52f57e12011-03-16 12:10:02 +000045 } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
bsalomon@google.com647a8042011-08-23 14:39:01 +000046 unit->fX = SkScalarSignNonZero(unit->fX);
reed@google.com52f57e12011-03-16 12:10:02 +000047 unit->fY = 0;
48 }
49}
50
51void SkFlingState::reset(float sx, float sy) {
52 fActive = true;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000053 fDirection.set(sx, sy);
reed@google.com52f57e12011-03-16 12:10:02 +000054 fSpeed0 = SkPoint::Normalize(&fDirection);
55 fSpeed0 = pin_max_fling(fSpeed0);
56 fTime0 = getseconds();
57
58 unit_axis_align(&fDirection);
59// printf("---- speed %g dir %g %g\n", fSpeed0, fDirection.fX, fDirection.fY);
60}
61
62bool SkFlingState::evaluateMatrix(SkMatrix* matrix) {
63 if (!fActive) {
64 return false;
65 }
66
reed@google.comc31a9d62011-03-18 20:53:44 +000067 const float t = (float)(getseconds() - fTime0);
reed@google.com52f57e12011-03-16 12:10:02 +000068 const float MIN_SPEED = 2;
reed@google.comc31a9d62011-03-18 20:53:44 +000069 const float K0 = 5;
70 const float K1 = 0.02f;
reed@google.com52f57e12011-03-16 12:10:02 +000071 const float speed = fSpeed0 * (sk_float_exp(- K0 * t) - K1);
72 if (speed <= MIN_SPEED) {
73 fActive = false;
74 return false;
75 }
76 float dist = (fSpeed0 - speed) / K0;
77
78// printf("---- time %g speed %g dist %g\n", t, speed, dist);
79 float tx = fDirection.fX * dist;
80 float ty = fDirection.fY * dist;
81 if (DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER) {
reed@google.comc31a9d62011-03-18 20:53:44 +000082 tx = (float)sk_float_round2int(tx);
83 ty = (float)sk_float_round2int(ty);
reed@google.com52f57e12011-03-16 12:10:02 +000084 }
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000085 matrix->setTranslate(tx, ty);
reed@google.com52f57e12011-03-16 12:10:02 +000086// printf("---- evaluate (%g %g)\n", tx, ty);
87
88 return true;
89}
90
91///////////////////////////////////////////////////////////////////////////////
92
93static const SkMSec MAX_DBL_TAP_INTERVAL = 300;
94static const float MAX_DBL_TAP_DISTANCE = 100;
95static const float MAX_JITTER_RADIUS = 2;
96
97// if true, then ignore the touch-move, 'cause its probably just jitter
98static bool close_enough_for_jitter(float x0, float y0, float x1, float y1) {
99 return sk_float_abs(x0 - x1) <= MAX_JITTER_RADIUS &&
100 sk_float_abs(y0 - y1) <= MAX_JITTER_RADIUS;
101}
102
103///////////////////////////////////////////////////////////////////////////////
104
105SkTouchGesture::SkTouchGesture() {
106 this->reset();
107}
108
109SkTouchGesture::~SkTouchGesture() {
110}
111
112void SkTouchGesture::reset() {
113 fTouches.reset();
114 fState = kEmpty_State;
115 fLocalM.reset();
116 fGlobalM.reset();
117
118 fLastUpT = SkTime::GetMSecs() - 2*MAX_DBL_TAP_INTERVAL;
119 fLastUpP.set(0, 0);
120}
121
122void SkTouchGesture::flushLocalM() {
123 fGlobalM.postConcat(fLocalM);
124 fLocalM.reset();
125}
126
127const SkMatrix& SkTouchGesture::localM() {
128 if (fFlinger.isActive()) {
129 if (!fFlinger.evaluateMatrix(&fLocalM)) {
130 this->flushLocalM();
131 }
132 }
133 return fLocalM;
134}
135
136void SkTouchGesture::appendNewRec(void* owner, float x, float y) {
137 Rec* rec = fTouches.append();
138 rec->fOwner = owner;
139 rec->fStartX = rec->fPrevX = rec->fLastX = x;
140 rec->fStartY = rec->fPrevY = rec->fLastY = y;
141 rec->fLastT = rec->fPrevT = SkTime::GetMSecs();
142}
143
144void SkTouchGesture::touchBegin(void* owner, float x, float y) {
145// GrPrintf("--- %d touchBegin %p %g %g\n", fTouches.count(), owner, x, y);
146
147 int index = this->findRec(owner);
148 if (index >= 0) {
149 this->flushLocalM();
150 fTouches.removeShuffle(index);
151 SkDebugf("---- already exists, removing\n");
152 }
153
154 if (fTouches.count() == 2) {
155 return;
156 }
157
158 this->flushLocalM();
159 fFlinger.stop();
160
161 this->appendNewRec(owner, x, y);
162
163 switch (fTouches.count()) {
164 case 1:
165 fState = kTranslate_State;
166 break;
167 case 2:
168 fState = kZoom_State;
169 break;
170 default:
171 break;
172 }
173}
174
175int SkTouchGesture::findRec(void* owner) const {
176 for (int i = 0; i < fTouches.count(); i++) {
177 if (owner == fTouches[i].fOwner) {
178 return i;
179 }
180 }
181 return -1;
182}
183
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000184static SkScalar center(float pos0, float pos1) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000185 return (pos0 + pos1) * 0.5f;
reed@google.com52f57e12011-03-16 12:10:02 +0000186}
187
188static const float MAX_ZOOM_SCALE = 4;
189static const float MIN_ZOOM_SCALE = 0.25f;
190
191float SkTouchGesture::limitTotalZoom(float scale) const {
192 // this query works 'cause we know that we're square-scale w/ no skew/rotation
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000193 const float curr = SkScalarToFloat(fGlobalM[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
reed@google.com52f57e12011-03-16 12:10:02 +0000195 if (scale > 1 && curr * scale > MAX_ZOOM_SCALE) {
196 scale = MAX_ZOOM_SCALE / curr;
197 } else if (scale < 1 && curr * scale < MIN_ZOOM_SCALE) {
198 scale = MIN_ZOOM_SCALE / curr;
199 }
200 return scale;
201}
202
203void SkTouchGesture::touchMoved(void* owner, float x, float y) {
204// GrPrintf("--- %d touchMoved %p %g %g\n", fTouches.count(), owner, x, y);
205
caryclark936b7342014-07-11 12:14:51 -0700206 if (kEmpty_State == fState) {
207 return;
208 }
reed@google.com52f57e12011-03-16 12:10:02 +0000209
210 int index = this->findRec(owner);
211 if (index < 0) {
212 // not found, so I guess we should add it...
213 SkDebugf("---- add missing begin\n");
214 this->appendNewRec(owner, x, y);
215 index = fTouches.count() - 1;
216 }
217
218 Rec& rec = fTouches[index];
219
220 // not sure how valuable this is
221 if (fTouches.count() == 2) {
222 if (close_enough_for_jitter(rec.fLastX, rec.fLastY, x, y)) {
223// GrPrintf("--- drop touchMove, withing jitter tolerance %g %g\n", rec.fLastX - x, rec.fLastY - y);
224 return;
225 }
226 }
227
228 rec.fPrevX = rec.fLastX; rec.fLastX = x;
229 rec.fPrevY = rec.fLastY; rec.fLastY = y;
230 rec.fPrevT = rec.fLastT; rec.fLastT = SkTime::GetMSecs();
231
232 switch (fTouches.count()) {
233 case 1: {
234 float dx = rec.fLastX - rec.fStartX;
235 float dy = rec.fLastY - rec.fStartY;
236 dx = (float)sk_float_round2int(dx);
237 dy = (float)sk_float_round2int(dy);
238 fLocalM.setTranslate(dx, dy);
239 } break;
240 case 2: {
241 SkASSERT(kZoom_State == fState);
242 const Rec& rec0 = fTouches[0];
243 const Rec& rec1 = fTouches[1];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000244
reed@google.com52f57e12011-03-16 12:10:02 +0000245 float scale = this->computePinch(rec0, rec1);
246 scale = this->limitTotalZoom(scale);
247
248 fLocalM.setTranslate(-center(rec0.fStartX, rec1.fStartX),
249 -center(rec0.fStartY, rec1.fStartY));
250 fLocalM.postScale(scale, scale);
251 fLocalM.postTranslate(center(rec0.fLastX, rec1.fLastX),
252 center(rec0.fLastY, rec1.fLastY));
253 } break;
254 default:
255 break;
256 }
257}
258
259void SkTouchGesture::touchEnd(void* owner) {
260// GrPrintf("--- %d touchEnd %p\n", fTouches.count(), owner);
261
262 int index = this->findRec(owner);
263 if (index < 0) {
264 SkDebugf("--- not found\n");
265 return;
266 }
267
268 const Rec& rec = fTouches[index];
269 if (this->handleDblTap(rec.fLastX, rec.fLastY)) {
270 return;
271 }
272
273 // count() reflects the number before we removed the owner
274 switch (fTouches.count()) {
275 case 1: {
276 this->flushLocalM();
277 float dx = rec.fLastX - rec.fPrevX;
278 float dy = rec.fLastY - rec.fPrevY;
279 float dur = (rec.fLastT - rec.fPrevT) * 0.001f;
280 if (dur > 0) {
281 fFlinger.reset(dx / dur, dy / dur);
282 }
283 fState = kEmpty_State;
284 } break;
285 case 2:
286 this->flushLocalM();
287 SkASSERT(kZoom_State == fState);
288 fState = kEmpty_State;
289 break;
290 default:
291 SkASSERT(kZoom_State == fState);
292 break;
293 }
294
295 fTouches.removeShuffle(index);
296}
297
298float SkTouchGesture::computePinch(const Rec& rec0, const Rec& rec1) {
299 double dx = rec0.fStartX - rec1.fStartX;
300 double dy = rec0.fStartY - rec1.fStartY;
301 double dist0 = sqrt(dx*dx + dy*dy);
302
303 dx = rec0.fLastX - rec1.fLastX;
304 dy = rec0.fLastY - rec1.fLastY;
305 double dist1 = sqrt(dx*dx + dy*dy);
306
307 double scale = dist1 / dist0;
308 return (float)scale;
309}
310
311bool SkTouchGesture::handleDblTap(float x, float y) {
312 bool found = false;
313 SkMSec now = SkTime::GetMSecs();
314 if (now - fLastUpT <= MAX_DBL_TAP_INTERVAL) {
315 if (SkPoint::Length(fLastUpP.fX - x,
316 fLastUpP.fY - y) <= MAX_DBL_TAP_DISTANCE) {
317 fFlinger.stop();
318 fLocalM.reset();
319 fGlobalM.reset();
320 fTouches.reset();
321 fState = kEmpty_State;
322 found = true;
323 }
324 }
325
326 fLastUpT = now;
327 fLastUpP.set(x, y);
328 return found;
329}