2 * Copyright 2010 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkTime.h"
12 #include "tools/viewer/TouchGesture.h"
14 #define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true
16 static const SkScalar MAX_FLING_SPEED = SkIntToScalar(1500);
18 static SkScalar pin_max_fling(SkScalar speed) {
19 if (speed > MAX_FLING_SPEED) {
20 speed = MAX_FLING_SPEED;
25 static double getseconds() {
26 return SkTime::GetMSecs() * 0.001;
29 // returns +1 or -1, depending on the sign of x
30 // returns +1 if z is zero
31 static SkScalar SkScalarSignNonZero(SkScalar x) {
32 SkScalar sign = SK_Scalar1;
39 static void unit_axis_align(SkVector* unit) {
40 const SkScalar TOLERANCE = SkDoubleToScalar(0.15);
41 if (SkScalarAbs(unit->fX) < TOLERANCE) {
43 unit->fY = SkScalarSignNonZero(unit->fY);
44 } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
45 unit->fX = SkScalarSignNonZero(unit->fX);
50 void TouchGesture::FlingState::reset(float sx, float sy) {
52 fDirection.set(sx, sy);
53 fSpeed0 = SkPoint::Normalize(&fDirection);
54 fSpeed0 = pin_max_fling(fSpeed0);
55 fTime0 = getseconds();
57 unit_axis_align(&fDirection);
58 // printf("---- speed %g dir %g %g\n", fSpeed0, fDirection.fX, fDirection.fY);
61 bool TouchGesture::FlingState::evaluateMatrix(SkMatrix* matrix) {
66 const float t = (float)(getseconds() - fTime0);
67 const float MIN_SPEED = 2;
69 const float K1 = 0.02f;
70 const float speed = fSpeed0 * (sk_float_exp(- K0 * t) - K1);
71 if (speed <= MIN_SPEED) {
75 float dist = (fSpeed0 - speed) / K0;
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);
84 matrix->setTranslate(tx, ty);
85 // printf("---- evaluate (%g %g)\n", tx, ty);
90 ///////////////////////////////////////////////////////////////////////////////
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;
96 // if true, then ignore the touch-move, 'cause its probably just jitter
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;
102 ///////////////////////////////////////////////////////////////////////////////
104 TouchGesture::TouchGesture() {
108 TouchGesture::~TouchGesture() {
111 void TouchGesture::resetTouchState() {
112 fIsTransLimited = false;
114 fState = kEmpty_State;
117 fLastUpMillis = SkTime::GetMSecs() - 2*MAX_DBL_TAP_INTERVAL;
121 void TouchGesture::reset() {
123 this->resetTouchState();
126 void TouchGesture::flushLocalM() {
127 fGlobalM.postConcat(fLocalM);
131 const SkMatrix& TouchGesture::localM() {
132 if (fFlinger.isActive()) {
133 if (!fFlinger.evaluateMatrix(&fLocalM)) {
140 void TouchGesture::appendNewRec(void* owner, float x, float y) {
141 Rec* rec = fTouches.append();
143 rec->fStartX = rec->fPrevX = rec->fLastX = x;
144 rec->fStartY = rec->fPrevY = rec->fLastY = y;
145 rec->fLastT = rec->fPrevT = static_cast<float>(SkTime::GetSecs());
148 void TouchGesture::touchBegin(void* owner, float x, float y) {
149 // SkDebugf("--- %d touchBegin %p %g %g\n", fTouches.count(), owner, x, y);
151 int index = this->findRec(owner);
154 fTouches.removeShuffle(index);
155 SkDebugf("---- already exists, removing\n");
158 if (fTouches.count() == 2) {
165 this->appendNewRec(owner, x, y);
167 switch (fTouches.count()) {
169 fState = kTranslate_State;
179 int TouchGesture::findRec(void* owner) const {
180 for (int i = 0; i < fTouches.count(); i++) {
181 if (owner == fTouches[i].fOwner) {
188 static SkScalar center(float pos0, float pos1) {
189 return (pos0 + pos1) * 0.5f;
192 void TouchGesture::startZoom() {
193 fState = kZoom_State;
196 void TouchGesture::updateZoom(float scale, float startX, float startY, float lastX, float lastY) {
197 fLocalM.setTranslate(-startX, -startY);
198 fLocalM.postScale(scale, scale);
199 fLocalM.postTranslate(lastX, lastY);
202 void TouchGesture::endZoom() {
204 SkASSERT(kZoom_State == fState);
205 fState = kEmpty_State;
208 void TouchGesture::touchMoved(void* owner, float x, float y) {
209 // SkDebugf("--- %d touchMoved %p %g %g\n", fTouches.count(), owner, x, y);
211 if (kEmpty_State == fState) {
215 int index = this->findRec(owner);
217 SkDebugf("---- ignoring move without begin\n");
221 Rec& rec = fTouches[index];
223 // not sure how valuable this is
224 if (fTouches.count() == 2) {
225 if (close_enough_for_jitter(rec.fLastX, rec.fLastY, x, y)) {
226 // SkDebugf("--- drop touchMove, within jitter tolerance %g %g\n", rec.fLastX - x, rec.fLastY - y);
231 rec.fPrevX = rec.fLastX; rec.fLastX = x;
232 rec.fPrevY = rec.fLastY; rec.fLastY = y;
233 rec.fPrevT = rec.fLastT;
234 rec.fLastT = static_cast<float>(SkTime::GetSecs());
236 switch (fTouches.count()) {
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);
245 SkASSERT(kZoom_State == fState);
246 const Rec& rec0 = fTouches[0];
247 const Rec& rec1 = fTouches[1];
249 float scale = this->computePinch(rec0, rec1);
250 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));
261 void TouchGesture::touchEnd(void* owner) {
262 // SkDebugf("--- %d touchEnd %p\n", fTouches.count(), owner);
264 int index = this->findRec(owner);
266 SkDebugf("--- not found\n");
270 const Rec& rec = fTouches[index];
271 if (this->handleDblTap(rec.fLastX, rec.fLastY)) {
275 // count() reflects the number before we removed the owner
276 switch (fTouches.count()) {
279 float dx = rec.fLastX - rec.fPrevX;
280 float dy = rec.fLastY - rec.fPrevY;
281 float dur = rec.fLastT - rec.fPrevT;
283 fFlinger.reset(dx / dur, dy / dur);
285 fState = kEmpty_State;
291 SkASSERT(kZoom_State == fState);
295 fTouches.removeShuffle(index);
300 bool TouchGesture::isFling(SkPoint* dir) {
301 if (fFlinger.isActive()) {
303 fFlinger.get(dir, &speed);
311 float TouchGesture::computePinch(const Rec& rec0, const Rec& rec1) {
312 double dx = rec0.fStartX - rec1.fStartX;
313 double dy = rec0.fStartY - rec1.fStartY;
314 double dist0 = sqrt(dx*dx + dy*dy);
316 dx = rec0.fLastX - rec1.fLastX;
317 dy = rec0.fLastY - rec1.fLastY;
318 double dist1 = sqrt(dx*dx + dy*dy);
320 double scale = dist1 / dist0;
324 bool TouchGesture::handleDblTap(float x, float y) {
326 double now = SkTime::GetMSecs();
327 if (now - fLastUpMillis <= MAX_DBL_TAP_INTERVAL) {
328 if (SkPoint::Length(fLastUpP.fX - x,
329 fLastUpP.fY - y) <= MAX_DBL_TAP_DISTANCE) {
334 fState = kEmpty_State;
344 void TouchGesture::setTransLimit(const SkRect& contentRect, const SkRect& windowRect,
345 const SkMatrix& preTouchMatrix) {
346 fIsTransLimited = true;
347 fContentRect = contentRect;
348 fWindowRect = windowRect;
349 fPreTouchM = preTouchMatrix;
352 void TouchGesture::limitTrans() {
353 if (!fIsTransLimited) {
357 SkRect scaledContent = fContentRect;
358 fPreTouchM.mapRect(&scaledContent);
359 fGlobalM.mapRect(&scaledContent);
360 const SkScalar ZERO = 0;
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);