TouchGesture.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef TouchGesture_DEFINED
  8. #define TouchGesture_DEFINED
  9. #include "include/core/SkMatrix.h"
  10. #include "include/private/SkTDArray.h"
  11. class TouchGesture {
  12. public:
  13. TouchGesture();
  14. ~TouchGesture();
  15. void touchBegin(void* owner, float x, float y);
  16. void touchMoved(void* owner, float x, float y);
  17. void touchEnd(void* owner);
  18. void reset();
  19. void resetTouchState();
  20. bool isActive() { return fFlinger.isActive(); }
  21. void stop() { fFlinger.stop(); }
  22. bool isBeingTouched() { return kEmpty_State != fState; }
  23. bool isFling(SkPoint* dir);
  24. const SkMatrix& localM();
  25. const SkMatrix& globalM() const { return fGlobalM; }
  26. void setTransLimit(const SkRect& contentRect, const SkRect& windowRect,
  27. const SkMatrix& preTouchM);
  28. private:
  29. enum State {
  30. kEmpty_State,
  31. kTranslate_State,
  32. kZoom_State,
  33. };
  34. struct Rec {
  35. void* fOwner;
  36. float fStartX, fStartY;
  37. float fPrevX, fPrevY;
  38. float fLastX, fLastY;
  39. float fPrevT, fLastT;
  40. };
  41. SkTDArray<Rec> fTouches;
  42. State fState;
  43. SkMatrix fLocalM, fGlobalM, fPreTouchM;
  44. struct FlingState {
  45. FlingState() : fActive(false) {}
  46. bool isActive() const { return fActive; }
  47. void stop() { fActive = false; }
  48. void reset(float sx, float sy);
  49. bool evaluateMatrix(SkMatrix* matrix);
  50. void get(SkPoint* dir, SkScalar* speed) {
  51. *dir = fDirection;
  52. *speed = fSpeed0;
  53. }
  54. private:
  55. SkPoint fDirection;
  56. SkScalar fSpeed0;
  57. double fTime0;
  58. bool fActive;
  59. };
  60. FlingState fFlinger;
  61. double fLastUpMillis;
  62. SkPoint fLastUpP;
  63. // The following rects are used to limit the translation so the content never leaves the window
  64. SkRect fContentRect, fWindowRect;
  65. bool fIsTransLimited = false;
  66. void limitTrans(); // here we only limit the translation with respect to globalM
  67. void flushLocalM();
  68. int findRec(void* owner) const;
  69. void appendNewRec(void* owner, float x, float y);
  70. float computePinch(const Rec&, const Rec&);
  71. float limitTotalZoom(float scale) const;
  72. bool handleDblTap(float, float);
  73. };
  74. #endif