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