1 /*
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.
6  */
7 
8 #include <algorithm>
9 
10 #include "SkTouchGesture.h"
11 #include "SkMatrix.h"
12 #include "SkTime.h"
13 
14 #define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER   true
15 
16 static const SkScalar MAX_FLING_SPEED = SkIntToScalar(1500);
17 
pin_max_fling(SkScalar speed)18 static SkScalar pin_max_fling(SkScalar speed) {
19     if (speed > MAX_FLING_SPEED) {
20         speed = MAX_FLING_SPEED;
21     }
22     return speed;
23 }
24 
getseconds()25 static double getseconds() {
26     return SkTime::GetMSecs() * 0.001;
27 }
28 
29 // returns +1 or -1, depending on the sign of x
30 // returns +1 if z is zero
SkScalarSignNonZero(SkScalar x)31 static SkScalar SkScalarSignNonZero(SkScalar x) {
32     SkScalar sign = SK_Scalar1;
33     if (x < 0) {
34         sign = -sign;
35     }
36     return sign;
37 }
38 
unit_axis_align(SkVector * unit)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;
43         unit->fY = SkScalarSignNonZero(unit->fY);
44     } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
45         unit->fX = SkScalarSignNonZero(unit->fX);
46         unit->fY = 0;
47     }
48 }
49 
reset(float sx,float sy)50 void SkFlingState::reset(float sx, float sy) {
51     fActive = true;
52     fDirection.set(sx, sy);
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 
evaluateMatrix(SkMatrix * matrix)61 bool SkFlingState::evaluateMatrix(SkMatrix* matrix) {
62     if (!fActive) {
63         return false;
64     }
65 
66     const float t =  (float)(getseconds() - fTime0);
67     const float MIN_SPEED = 2;
68     const float K0 = 5;
69     const float K1 = 0.02f;
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) {
81         tx = (float)sk_float_round2int(tx);
82         ty = (float)sk_float_round2int(ty);
83     }
84     matrix->setTranslate(tx, ty);
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
close_enough_for_jitter(float x0,float y0,float x1,float y1)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 
SkTouchGesture()104 SkTouchGesture::SkTouchGesture() {
105     this->reset();
106 }
107 
~SkTouchGesture()108 SkTouchGesture::~SkTouchGesture() {
109 }
110 
reset()111 void SkTouchGesture::reset() {
112     fIsTransLimited = false;
113     fTouches.reset();
114     fState = kEmpty_State;
115     fLocalM.reset();
116     fGlobalM.reset();
117 
118     fLastUpMillis = SkTime::GetMSecs() - 2*MAX_DBL_TAP_INTERVAL;
119     fLastUpP.set(0, 0);
120 }
121 
flushLocalM()122 void SkTouchGesture::flushLocalM() {
123     fGlobalM.postConcat(fLocalM);
124     fLocalM.reset();
125 }
126 
localM()127 const SkMatrix& SkTouchGesture::localM() {
128     if (fFlinger.isActive()) {
129         if (!fFlinger.evaluateMatrix(&fLocalM)) {
130             this->flushLocalM();
131         }
132     }
133     return fLocalM;
134 }
135 
appendNewRec(void * owner,float x,float y)136 void 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 = static_cast<float>(SkTime::GetSecs());
142 }
143 
touchBegin(void * owner,float x,float y)144 void SkTouchGesture::touchBegin(void* owner, float x, float y) {
145 //    SkDebugf("--- %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 
findRec(void * owner) const175 int 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 
center(float pos0,float pos1)184 static SkScalar center(float pos0, float pos1) {
185     return (pos0 + pos1) * 0.5f;
186 }
187 
188 static const float MAX_ZOOM_SCALE = 4;
189 static const float MIN_ZOOM_SCALE = 0.25f;
190 
limitTotalZoom(float scale) const191 float SkTouchGesture::limitTotalZoom(float scale) const {
192     // this query works 'cause we know that we're square-scale w/ no skew/rotation
193     const float curr = SkScalarToFloat(fGlobalM[0]);
194 
195     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 
touchMoved(void * owner,float x,float y)203 void SkTouchGesture::touchMoved(void* owner, float x, float y) {
204 //    SkDebugf("--- %d touchMoved %p %g %g\n", fTouches.count(), owner, x, y);
205 
206     if (kEmpty_State == fState) {
207         return;
208     }
209 
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 //            SkDebugf("--- 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;
231     rec.fLastT = static_cast<float>(SkTime::GetSecs());
232 
233     switch (fTouches.count()) {
234         case 1: {
235             float dx = rec.fLastX - rec.fStartX;
236             float dy = rec.fLastY - rec.fStartY;
237             dx = (float)sk_float_round2int(dx);
238             dy = (float)sk_float_round2int(dy);
239             fLocalM.setTranslate(dx, dy);
240         } break;
241         case 2: {
242             SkASSERT(kZoom_State == fState);
243             const Rec& rec0 = fTouches[0];
244             const Rec& rec1 = fTouches[1];
245 
246             float scale = this->computePinch(rec0, rec1);
247             scale = this->limitTotalZoom(scale);
248 
249             fLocalM.setTranslate(-center(rec0.fStartX, rec1.fStartX),
250                                  -center(rec0.fStartY, rec1.fStartY));
251             fLocalM.postScale(scale, scale);
252             fLocalM.postTranslate(center(rec0.fLastX, rec1.fLastX),
253                                   center(rec0.fLastY, rec1.fLastY));
254         } break;
255         default:
256             break;
257     }
258 }
259 
touchEnd(void * owner)260 void SkTouchGesture::touchEnd(void* owner) {
261 //    SkDebugf("--- %d touchEnd   %p\n", fTouches.count(), owner);
262 
263     int index = this->findRec(owner);
264     if (index < 0) {
265         SkDebugf("--- not found\n");
266         return;
267     }
268 
269     const Rec& rec = fTouches[index];
270     if (this->handleDblTap(rec.fLastX, rec.fLastY)) {
271         return;
272     }
273 
274     // count() reflects the number before we removed the owner
275     switch (fTouches.count()) {
276         case 1: {
277             this->flushLocalM();
278             float dx = rec.fLastX - rec.fPrevX;
279             float dy = rec.fLastY - rec.fPrevY;
280             float dur = rec.fLastT - rec.fPrevT;
281             if (dur > 0) {
282                 fFlinger.reset(dx / dur, dy / dur);
283             }
284             fState = kEmpty_State;
285         } break;
286         case 2:
287             this->flushLocalM();
288             SkASSERT(kZoom_State == fState);
289             fState = kEmpty_State;
290             break;
291         default:
292             SkASSERT(kZoom_State == fState);
293             break;
294     }
295 
296     fTouches.removeShuffle(index);
297 
298     limitTrans();
299 }
300 
computePinch(const Rec & rec0,const Rec & rec1)301 float SkTouchGesture::computePinch(const Rec& rec0, const Rec& rec1) {
302     double dx = rec0.fStartX - rec1.fStartX;
303     double dy = rec0.fStartY - rec1.fStartY;
304     double dist0 = sqrt(dx*dx + dy*dy);
305 
306     dx = rec0.fLastX - rec1.fLastX;
307     dy = rec0.fLastY - rec1.fLastY;
308     double dist1 = sqrt(dx*dx + dy*dy);
309 
310     double scale = dist1 / dist0;
311     return (float)scale;
312 }
313 
handleDblTap(float x,float y)314 bool SkTouchGesture::handleDblTap(float x, float y) {
315     bool found = false;
316     double now = SkTime::GetMSecs();
317     if (now - fLastUpMillis <= MAX_DBL_TAP_INTERVAL) {
318         if (SkPoint::Length(fLastUpP.fX - x,
319                             fLastUpP.fY - y) <= MAX_DBL_TAP_DISTANCE) {
320             fFlinger.stop();
321             fLocalM.reset();
322             fGlobalM.reset();
323             fTouches.reset();
324             fState = kEmpty_State;
325             found = true;
326         }
327     }
328 
329     fLastUpMillis = now;
330     fLastUpP.set(x, y);
331     return found;
332 }
333 
setTransLimit(const SkRect & contentRect,const SkRect & windowRect)334 void SkTouchGesture::setTransLimit(const SkRect& contentRect, const SkRect& windowRect) {
335     fIsTransLimited = true;
336     fContentRect = contentRect;
337     fWindowRect = windowRect;
338 }
339 
limitTrans()340 void SkTouchGesture::limitTrans() {
341     if (!fIsTransLimited) {
342         return;
343     }
344 
345     SkRect scaledContent = fContentRect;
346     fGlobalM.mapRect(&scaledContent);
347     const SkScalar ZERO = 0;
348 
349     fGlobalM.postTranslate(ZERO, std::min(ZERO, fWindowRect.fBottom - scaledContent.fTop));
350     fGlobalM.postTranslate(ZERO, std::max(ZERO, fWindowRect.fTop - scaledContent.fBottom));
351     fGlobalM.postTranslate(std::min(ZERO, fWindowRect.fRight - scaledContent.fLeft), ZERO);
352     fGlobalM.postTranslate(std::max(ZERO, fWindowRect.fLeft - scaledContent.fRight), ZERO);
353 }
354