1 2 /* 3 * Copyright 2011 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. 7 */ 8 #ifndef SkTouchGesture_DEFINED 9 #define SkTouchGesture_DEFINED 10 11 #include "../private/SkTDArray.h" 12 #include "SkMatrix.h" 13 14 struct SkFlingState { SkFlingStateSkFlingState15 SkFlingState() : fActive(false) {} 16 isActiveSkFlingState17 bool isActive() const { return fActive; } stopSkFlingState18 void stop() { fActive = false; } 19 20 void reset(float sx, float sy); 21 bool evaluateMatrix(SkMatrix* matrix); 22 23 private: 24 SkPoint fDirection; 25 SkScalar fSpeed0; 26 double fTime0; 27 bool fActive; 28 }; 29 30 class SkTouchGesture { 31 public: 32 SkTouchGesture(); 33 ~SkTouchGesture(); 34 35 void touchBegin(void* owner, float x, float y); 36 void touchMoved(void* owner, float x, float y); 37 void touchEnd(void* owner); 38 void reset(); 39 void resetTouchState(); 40 isActive()41 bool isActive() { return fFlinger.isActive(); } stop()42 void stop() { fFlinger.stop(); } isBeingTouched()43 bool isBeingTouched() { return kEmpty_State != fState; } 44 45 const SkMatrix& localM(); globalM()46 const SkMatrix& globalM() const { return fGlobalM; } 47 48 void setTransLimit(const SkRect& contentRect, const SkRect& windowRect, 49 const SkMatrix& preTouchM); 50 51 private: 52 enum State { 53 kEmpty_State, 54 kTranslate_State, 55 kZoom_State, 56 }; 57 58 struct Rec { 59 void* fOwner; 60 float fStartX, fStartY; 61 float fPrevX, fPrevY; 62 float fLastX, fLastY; 63 float fPrevT, fLastT; 64 }; 65 SkTDArray<Rec> fTouches; 66 67 State fState; 68 SkMatrix fLocalM, fGlobalM, fPreTouchM; 69 SkFlingState fFlinger; 70 double fLastUpMillis; 71 SkPoint fLastUpP; 72 73 // The following rects are used to limit the translation so the content never leaves the window 74 SkRect fContentRect, fWindowRect; 75 bool fIsTransLimited = false; 76 77 void limitTrans(); // here we only limit the translation with respect to globalM 78 void flushLocalM(); 79 int findRec(void* owner) const; 80 void appendNewRec(void* owner, float x, float y); 81 float computePinch(const Rec&, const Rec&); 82 float limitTotalZoom(float scale) const; 83 bool handleDblTap(float, float); 84 }; 85 86 #endif 87