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