blob: 3a0cfce18d6ea4b0cc2fb7c136671c78a563b04e [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
11#include "SkTDArray.h"
12#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();
39
40 bool isActive() { return fFlinger.isActive(); }
41 void stop() { fFlinger.stop(); }
42
43 const SkMatrix& localM();
44 const SkMatrix& globalM() const { return fGlobalM; }
45
46private:
47 enum State {
48 kEmpty_State,
49 kTranslate_State,
50 kZoom_State,
51 };
52
53 struct Rec {
54 void* fOwner;
55 float fStartX, fStartY;
56 float fPrevX, fPrevY;
57 float fLastX, fLastY;
58 SkMSec fPrevT, fLastT;
59 };
60 SkTDArray<Rec> fTouches;
61
62 State fState;
63 SkMatrix fLocalM, fGlobalM;
64 SkFlingState fFlinger;
65 SkMSec fLastUpT;
66 SkPoint fLastUpP;
67
68
69 void flushLocalM();
70 int findRec(void* owner) const;
71 void appendNewRec(void* owner, float x, float y);
72 float computePinch(const Rec&, const Rec&);
73 float limitTotalZoom(float scale) const;
74 bool handleDblTap(float, float);
75};
76
77#endif