blob: 34f889fc0e8e5156191c565ba7a29378d6649451 [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#ifndef TouchGesture_DEFINED
9#define TouchGesture_DEFINED
reed@google.com52f57e12011-03-16 12:10:02 +000010
bungemana7e9f052016-02-18 08:53:33 -080011#include "../private/SkTDArray.h"
reed@google.com52f57e12011-03-16 12:10:02 +000012#include "SkMatrix.h"
13
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040014class TouchGesture {
reed@google.com52f57e12011-03-16 12:10:02 +000015public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040016 TouchGesture();
17 ~TouchGesture();
reed@google.com52f57e12011-03-16 12:10:02 +000018
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();
Brian Salomon88d99df2018-01-05 11:58:16 -050023 void resetTouchState();
reed@google.com52f57e12011-03-16 12:10:02 +000024
25 bool isActive() { return fFlinger.isActive(); }
26 void stop() { fFlinger.stop(); }
Brian Osmanb53f48c2017-06-07 10:00:30 -040027 bool isBeingTouched() { return kEmpty_State != fState; }
Jim Van Verth234e5a22018-07-23 13:46:01 -040028 bool isFling(SkPoint* dir);
reed@google.com52f57e12011-03-16 12:10:02 +000029
30 const SkMatrix& localM();
31 const SkMatrix& globalM() const { return fGlobalM; }
32
Brian Osman42bb6ac2017-06-05 08:46:04 -040033 void setTransLimit(const SkRect& contentRect, const SkRect& windowRect,
34 const SkMatrix& preTouchM);
liyuqiane46e4f02016-05-20 07:32:19 -070035
reed@google.com52f57e12011-03-16 12:10:02 +000036private:
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;
benjaminwagnerec4d4d72016-03-25 12:59:53 -070048 float fPrevT, fLastT;
reed@google.com52f57e12011-03-16 12:10:02 +000049 };
50 SkTDArray<Rec> fTouches;
51
52 State fState;
Brian Osman42bb6ac2017-06-05 08:46:04 -040053 SkMatrix fLocalM, fGlobalM, fPreTouchM;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040054
55 struct FlingState {
56 FlingState() : fActive(false) {}
57
58 bool isActive() const { return fActive; }
59 void stop() { fActive = false; }
60
61 void reset(float sx, float sy);
62 bool evaluateMatrix(SkMatrix* matrix);
63
64 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;
benjaminwagnerec4d4d72016-03-25 12:59:53 -070076 double fLastUpMillis;
reed@google.com52f57e12011-03-16 12:10:02 +000077 SkPoint fLastUpP;
78
liyuqiane46e4f02016-05-20 07:32:19 -070079 // The following rects are used to limit the translation so the content never leaves the window
80 SkRect fContentRect, fWindowRect;
81 bool fIsTransLimited = false;
reed@google.com52f57e12011-03-16 12:10:02 +000082
liyuqiane46e4f02016-05-20 07:32:19 -070083 void limitTrans(); // here we only limit the translation with respect to globalM
reed@google.com52f57e12011-03-16 12:10:02 +000084 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