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