scale_gesture_detector.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_
6#define UI_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/time/time.h"
10#include "ui/events/gesture_detection/gesture_detection_export.h"
11#include "ui/events/gesture_detection/gesture_detector.h"
12
13namespace ui {
14
15class MotionEvent;
16
17// Port of ScaleGestureDetector.java from Android
18// * platform/frameworks/base/core/java/android/view/ScaleGestureDetector.java
19// * Change-Id: I3e7926a4f6f9ab4951f380bd004499c78b3bda69
20// * Please update the Change-Id as upstream Android changes are pulled.
21class ScaleGestureDetector : public GestureDetector::SimpleGestureListener {
22 public:
23  struct GESTURE_DETECTION_EXPORT Config {
24    Config();
25    ~Config();
26    GestureDetector::Config gesture_detector_config;
27    bool quick_scale_enabled;
28    int min_scaling_touch_major;
29    int min_scaling_span;
30  };
31
32  class ScaleGestureListener {
33   public:
34    virtual ~ScaleGestureListener() {}
35    virtual bool OnScale(const ScaleGestureDetector& detector,
36                         const MotionEvent& e) = 0;
37    virtual bool OnScaleBegin(const ScaleGestureDetector& detector,
38                              const MotionEvent& e) = 0;
39    virtual void OnScaleEnd(const ScaleGestureDetector& detector,
40                            const MotionEvent& e) = 0;
41  };
42
43  // A convenience class to extend when you only want to listen for a subset of
44  // scaling-related events. This implements all methods in
45  // |ScaleGestureListener| but does nothing.
46  // |OnScale()| returns false so that a subclass can retrieve the accumulated
47  // scale factor in an overridden |OnScaleEnd()|.
48  // |OnScaleBegin() returns true.
49  class SimpleScaleGestureListener : public ScaleGestureListener {
50   public:
51    // ScaleGestureListener implementation.
52    virtual bool OnScale(const ScaleGestureDetector&,
53                         const MotionEvent&) OVERRIDE;
54    virtual bool OnScaleBegin(const ScaleGestureDetector&,
55                              const MotionEvent&) OVERRIDE;
56    virtual void OnScaleEnd(const ScaleGestureDetector&,
57                            const MotionEvent&) OVERRIDE;
58  };
59
60  ScaleGestureDetector(const Config& config, ScaleGestureListener* listener);
61  virtual ~ScaleGestureDetector();
62
63  // Accepts MotionEvents and dispatches events to a |ScaleGestureListener|
64  // when appropriate.
65  //
66  // Note: Applications should pass a complete and consistent event stream to
67  // this method. A complete and consistent event stream involves all
68  // MotionEvents from the initial ACTION_DOWN to the final ACTION_UP or
69  // ACTION_CANCEL.
70  //
71  // Returns true if the event was processed and the detector wants to receive
72  // the rest of the MotionEvents in this event stream.
73  bool OnTouchEvent(const MotionEvent& event);
74
75  // Set whether the associated |ScaleGestureListener| should receive
76  // OnScale callbacks when the user performs a doubletap followed by a swipe.
77  void SetQuickScaleEnabled(bool scales);
78  bool IsQuickScaleEnabled() const;
79  bool IsInProgress() const;
80  bool InDoubleTapMode() const;
81  float GetFocusX() const;
82  float GetFocusY() const;
83  float GetCurrentSpan() const;
84  float GetCurrentSpanX() const;
85  float GetCurrentSpanY() const;
86  float GetPreviousSpan() const;
87  float GetPreviousSpanX() const;
88  float GetPreviousSpanY() const;
89  float GetScaleFactor() const;
90  base::TimeDelta GetTimeDelta() const;
91  base::TimeTicks GetEventTime() const;
92
93 private:
94  enum DoubleTapMode { DOUBLE_TAP_MODE_NONE, DOUBLE_TAP_MODE_IN_PROGRESS };
95
96  // DoubleTapListener implementation.
97  virtual bool OnDoubleTap(const MotionEvent& ev) OVERRIDE;
98
99  // The TouchMajor/TouchMinor elements of a MotionEvent can flutter/jitter on
100  // some hardware/driver combos. Smooth out to get kinder, gentler behavior.
101  void AddTouchHistory(const MotionEvent& ev);
102  void ClearTouchHistory();
103
104  ScaleGestureListener* const listener_;
105
106  Config config_;
107
108  float focus_x_;
109  float focus_y_;
110
111  bool quick_scale_enabled_;
112
113  float curr_span_;
114  float prev_span_;
115  float initial_span_;
116  float curr_span_x_;
117  float curr_span_y_;
118  float prev_span_x_;
119  float prev_span_y_;
120  base::TimeTicks curr_time_;
121  base::TimeTicks prev_time_;
122  bool in_progress_;
123  int span_slop_;
124  int min_span_;
125
126  // Bounds for recently seen values.
127  float touch_upper_;
128  float touch_lower_;
129  float touch_history_last_accepted_;
130  int touch_history_direction_;
131  base::TimeTicks touch_history_last_accepted_time_;
132  int touch_min_major_;
133  float double_tap_focus_x_;
134  float double_tap_focus_y_;
135  DoubleTapMode double_tap_mode_;
136
137  bool event_before_or_above_starting_gesture_event_;
138
139  scoped_ptr<GestureDetector> gesture_detector_;
140
141  DISALLOW_COPY_AND_ASSIGN(ScaleGestureDetector);
142};
143
144}  // namespace ui
145
146#endif  // UI_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_
147