3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "SkTouchGesture.h"
15 #define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true
17 static const SkScalar MAX_FLING_SPEED = SkIntToScalar(1500);
19 static SkScalar pin_max_fling(SkScalar speed) {
20 if (speed > MAX_FLING_SPEED) {
21 speed = MAX_FLING_SPEED;
26 static double getseconds() {
27 return SkTime::GetMSecs() * 0.001;
30 // returns +1 or -1, depending on the sign of x
31 // returns +1 if z is zero
32 static SkScalar SkScalarSignNonZero(SkScalar x) {
33 SkScalar sign = SK_Scalar1;
40 static void unit_axis_align(SkVector* unit) {
41 const SkScalar TOLERANCE = SkDoubleToScalar(0.15);
42 if (SkScalarAbs(unit->fX) < TOLERANCE) {
44 unit->fY = SkScalarSignNonZero(unit->fY);
45 } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
46 unit->fX = SkScalarSignNonZero(unit->fX);
51 void SkFlingState::reset(float sx, float sy) {
53 fDirection.set(sx, sy);
54 fSpeed0 = SkPoint::Normalize(&fDirection);
55 fSpeed0 = pin_max_fling(fSpeed0);
56 fTime0 = getseconds();
58 unit_axis_align(&fDirection);
59 // printf("---- speed %g dir %g %g\n", fSpeed0, fDirection.fX, fDirection.fY);
62 bool SkFlingState::evaluateMatrix(SkMatrix* matrix) {
67 const float t = (float)(getseconds() - fTime0);
68 const float MIN_SPEED = 2;
70 const float K1 = 0.02f;
71 const float speed = fSpeed0 * (sk_float_exp(- K0 * t) - K1);
72 if (speed <= MIN_SPEED) {
76 float dist = (fSpeed0 - speed) / K0;
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) {
82 tx = (float)sk_float_round2int(tx);
83 ty = (float)sk_float_round2int(ty);
85 matrix->setTranslate(tx, ty);
86 // printf("---- evaluate (%g %g)\n", tx, ty);
91 ///////////////////////////////////////////////////////////////////////////////
93 static const SkMSec MAX_DBL_TAP_INTERVAL = 300;
94 static const float MAX_DBL_TAP_DISTANCE = 100;
95 static const float MAX_JITTER_RADIUS = 2;
97 // if true, then ignore the touch-move, 'cause its probably just jitter
98 static 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;
103 ///////////////////////////////////////////////////////////////////////////////
105 SkTouchGesture::SkTouchGesture() {
109 SkTouchGesture::~SkTouchGesture() {
112 void SkTouchGesture::reset() {
114 fState = kEmpty_State;
118 fLastUpT = SkTime::GetMSecs() - 2*MAX_DBL_TAP_INTERVAL;
122 void SkTouchGesture::flushLocalM() {
123 fGlobalM.postConcat(fLocalM);
127 const SkMatrix& SkTouchGesture::localM() {
128 if (fFlinger.isActive()) {
129 if (!fFlinger.evaluateMatrix(&fLocalM)) {
136 void SkTouchGesture::appendNewRec(void* owner, float x, float y) {
137 Rec* rec = fTouches.append();
139 rec->fStartX = rec->fPrevX = rec->fLastX = x;
140 rec->fStartY = rec->fPrevY = rec->fLastY = y;
141 rec->fLastT = rec->fPrevT = SkTime::GetMSecs();
144 void SkTouchGesture::touchBegin(void* owner, float x, float y) {
145 // SkDebugf("--- %d touchBegin %p %g %g\n", fTouches.count(), owner, x, y);
147 int index = this->findRec(owner);
150 fTouches.removeShuffle(index);
151 SkDebugf("---- already exists, removing\n");
154 if (fTouches.count() == 2) {
161 this->appendNewRec(owner, x, y);
163 switch (fTouches.count()) {
165 fState = kTranslate_State;
168 fState = kZoom_State;
175 int SkTouchGesture::findRec(void* owner) const {
176 for (int i = 0; i < fTouches.count(); i++) {
177 if (owner == fTouches[i].fOwner) {
184 static SkScalar center(float pos0, float pos1) {
185 return (pos0 + pos1) * 0.5f;
188 static const float MAX_ZOOM_SCALE = 4;
189 static const float MIN_ZOOM_SCALE = 0.25f;
191 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]);
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;
203 void SkTouchGesture::touchMoved(void* owner, float x, float y) {
204 // SkDebugf("--- %d touchMoved %p %g %g\n", fTouches.count(), owner, x, y);
206 if (kEmpty_State == fState) {
210 int index = this->findRec(owner);
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;
218 Rec& rec = fTouches[index];
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);
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();
232 switch (fTouches.count()) {
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);
241 SkASSERT(kZoom_State == fState);
242 const Rec& rec0 = fTouches[0];
243 const Rec& rec1 = fTouches[1];
245 float scale = this->computePinch(rec0, rec1);
246 scale = this->limitTotalZoom(scale);
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));
259 void SkTouchGesture::touchEnd(void* owner) {
260 // SkDebugf("--- %d touchEnd %p\n", fTouches.count(), owner);
262 int index = this->findRec(owner);
264 SkDebugf("--- not found\n");
268 const Rec& rec = fTouches[index];
269 if (this->handleDblTap(rec.fLastX, rec.fLastY)) {
273 // count() reflects the number before we removed the owner
274 switch (fTouches.count()) {
277 float dx = rec.fLastX - rec.fPrevX;
278 float dy = rec.fLastY - rec.fPrevY;
279 float dur = (rec.fLastT - rec.fPrevT) * 0.001f;
281 fFlinger.reset(dx / dur, dy / dur);
283 fState = kEmpty_State;
287 SkASSERT(kZoom_State == fState);
288 fState = kEmpty_State;
291 SkASSERT(kZoom_State == fState);
295 fTouches.removeShuffle(index);
298 float 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);
303 dx = rec0.fLastX - rec1.fLastX;
304 dy = rec0.fLastY - rec1.fLastY;
305 double dist1 = sqrt(dx*dx + dy*dy);
307 double scale = dist1 / dist0;
311 bool SkTouchGesture::handleDblTap(float x, float y) {
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) {
321 fState = kEmpty_State;