blob: 82f59f2862382a04e12e943e24a0dc99ce755e84 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@google.com52f57e12011-03-16 12:10:02 +00008#ifndef SkTouchGesture_DEFINED
9#define SkTouchGesture_DEFINED
10
bungemana7e9f052016-02-18 08:53:33 -080011#include "../private/SkTDArray.h"
reed@google.com52f57e12011-03-16 12:10:02 +000012#include "SkMatrix.h"
13
14struct SkFlingState {
15 SkFlingState() : fActive(false) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000016
reed@google.com52f57e12011-03-16 12:10:02 +000017 bool isActive() const { return fActive; }
18 void stop() { fActive = false; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000019
reed@google.com52f57e12011-03-16 12:10:02 +000020 void reset(float sx, float sy);
21 bool evaluateMatrix(SkMatrix* matrix);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000022
reed@google.com52f57e12011-03-16 12:10:02 +000023private:
24 SkPoint fDirection;
25 SkScalar fSpeed0;
26 double fTime0;
27 bool fActive;
28};
29
30class SkTouchGesture {
31public:
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();
Brian Salomon88d99df2018-01-05 11:58:16 -050039 void resetTouchState();
reed@google.com52f57e12011-03-16 12:10:02 +000040
41 bool isActive() { return fFlinger.isActive(); }
42 void stop() { fFlinger.stop(); }
Brian Osmanb53f48c2017-06-07 10:00:30 -040043 bool isBeingTouched() { return kEmpty_State != fState; }
reed@google.com52f57e12011-03-16 12:10:02 +000044
45 const SkMatrix& localM();
46 const SkMatrix& globalM() const { return fGlobalM; }
47
Brian Osman42bb6ac2017-06-05 08:46:04 -040048 void setTransLimit(const SkRect& contentRect, const SkRect& windowRect,
49 const SkMatrix& preTouchM);
liyuqiane46e4f02016-05-20 07:32:19 -070050
reed@google.com52f57e12011-03-16 12:10:02 +000051private:
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;
benjaminwagnerec4d4d72016-03-25 12:59:53 -070063 float fPrevT, fLastT;
reed@google.com52f57e12011-03-16 12:10:02 +000064 };
65 SkTDArray<Rec> fTouches;
66
67 State fState;
Brian Osman42bb6ac2017-06-05 08:46:04 -040068 SkMatrix fLocalM, fGlobalM, fPreTouchM;
reed@google.com52f57e12011-03-16 12:10:02 +000069 SkFlingState fFlinger;
benjaminwagnerec4d4d72016-03-25 12:59:53 -070070 double fLastUpMillis;
reed@google.com52f57e12011-03-16 12:10:02 +000071 SkPoint fLastUpP;
72
liyuqiane46e4f02016-05-20 07:32:19 -070073 // The following rects are used to limit the translation so the content never leaves the window
74 SkRect fContentRect, fWindowRect;
75 bool fIsTransLimited = false;
reed@google.com52f57e12011-03-16 12:10:02 +000076
liyuqiane46e4f02016-05-20 07:32:19 -070077 void limitTrans(); // here we only limit the translation with respect to globalM
reed@google.com52f57e12011-03-16 12:10:02 +000078 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