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 "include/core/SkMatrix.h"
12 #include "include/private/SkTDArray.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     void startZoom();
31     void updateZoom(float scale, float startX, float startY, float lastX, float lastY);
32     void endZoom();
33 
34     const SkMatrix& localM();
globalM()35     const SkMatrix& globalM() const { return fGlobalM; }
36 
37     void setTransLimit(const SkRect& contentRect, const SkRect& windowRect,
38                        const SkMatrix& preTouchM);
39 
40 private:
41     enum State {
42         kEmpty_State,
43         kTranslate_State,
44         kZoom_State,
45     };
46 
47     struct Rec {
48         void*   fOwner;
49         float   fStartX, fStartY;
50         float   fPrevX, fPrevY;
51         float   fLastX, fLastY;
52         float   fPrevT, fLastT;
53     };
54     SkTDArray<Rec> fTouches;
55 
56     State           fState;
57     SkMatrix        fLocalM, fGlobalM, fPreTouchM;
58 
59     struct FlingState {
FlingStateFlingState60         FlingState() : fActive(false) {}
61 
isActiveFlingState62         bool isActive() const { return fActive; }
stopFlingState63         void stop() { fActive = false; }
64 
65         void reset(float sx, float sy);
66         bool evaluateMatrix(SkMatrix* matrix);
67 
getFlingState68         void get(SkPoint* dir, SkScalar* speed) {
69             *dir = fDirection;
70             *speed = fSpeed0;
71         }
72 
73     private:
74         SkPoint     fDirection;
75         SkScalar    fSpeed0;
76         double      fTime0;
77         bool        fActive;
78     };
79     FlingState      fFlinger;
80     double          fLastUpMillis;
81     SkPoint         fLastUpP;
82 
83     // The following rects are used to limit the translation so the content never leaves the window
84     SkRect          fContentRect, fWindowRect;
85     bool            fIsTransLimited = false;
86 
87     void limitTrans(); // here we only limit the translation with respect to globalM
88     void flushLocalM();
89     int findRec(void* owner) const;
90     void appendNewRec(void* owner, float x, float y);
91     float computePinch(const Rec&, const Rec&);
92     bool handleDblTap(float, float);
93 };
94 
95 #endif
96