summaryrefslogtreecommitdiffstats
path: root/ui/base/gestures/gesture_point.h
blob: 2ddf50c411b5149b3e0e6dc098934dcb1831408c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_GESTURES_GESTURE_POINT_H_
#define UI_BASE_GESTURES_GESTURE_POINT_H_

#include "base/basictypes.h"
#include "ui/base/gestures/velocity_calculator.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"

namespace ui {
class TouchEvent;

// A GesturePoint represents a single touch-point/finger during a gesture
// recognition process.
class GesturePoint {
 public:
  GesturePoint();
  ~GesturePoint();

  // Resets various states.
  void Reset();

  void ResetVelocity();

  // Updates some states when a Tap gesture has been recognized for this point.
  void UpdateForTap();

  // Updates some states when a Scroll gesture has been recognized for this
  // point.
  void UpdateForScroll();

  // Updates states depending on the event and the gesture-state.
  void UpdateValues(const TouchEvent& event);

  // Responds according to the state of the gesture point (i.e. the point can
  // represent a click or scroll etc.)
  bool IsInClickWindow(const TouchEvent& event) const;
  bool IsInDoubleClickWindow(const TouchEvent& event) const;
  bool IsInScrollWindow(const TouchEvent& event) const;
  bool IsInFlickWindow(const TouchEvent& event);
  bool IsInHorizontalRailWindow() const;
  bool IsInVerticalRailWindow() const;
  bool IsInsideManhattanSquare(const TouchEvent& event) const;
  bool HasEnoughDataToEstablishRail() const;
  bool BreaksHorizontalRail();
  bool BreaksVerticalRail();
  bool DidScroll(const TouchEvent& event, int distance) const;

  const gfx::Point& first_touch_position() const {
    return first_touch_position_;
  }

  double last_touch_time() const { return last_touch_time_; }
  const gfx::Point& last_touch_position() const { return last_touch_position_; }
  int x() const { return last_touch_position_.x(); }
  int y() const { return last_touch_position_.y(); }

  // point_id_ is used to drive GestureSequence::ProcessTouchEventForGesture.
  // point_ids are maintained such that the set of point_ids is always
  // contiguous, from 0 to the number of current touches.
  // A lower point_id indicates that a touch occurred first.
  // A negative point_id indicates that the GesturePoint is not currently
  // associated with a touch.
  void set_point_id(int point_id) { point_id_ = point_id; }
  int point_id() const { return point_id_; }

  void set_touch_id(int touch_id) { touch_id_ = touch_id; }
  int touch_id() const { return touch_id_; }

  bool in_use() const { return point_id_ >= 0; }

  double x_delta() const {
    return last_touch_position_.x() - first_touch_position_.x();
  }

  double y_delta() const {
    return last_touch_position_.y() - first_touch_position_.y();
  }

  float XVelocity() { return velocity_calculator_.XVelocity(); }
  float YVelocity() { return velocity_calculator_.YVelocity(); }

  const gfx::Rect& enclosing_rectangle() const { return enclosing_rect_; }

 private:
  // Various statistical functions to manipulate gestures.
  bool IsInClickTimeWindow() const;
  bool IsInSecondClickTimeWindow() const;
  bool IsSecondClickInsideManhattanSquare(const TouchEvent& event) const;
  bool IsOverMinFlickSpeed();

  // The enclosing rectangle represents a rectangular touch region generated
  // by a sequence of ET_TOUCH_PRESSED, ET_TOUCH_MOVED, and ET_TOUCH_RELEASED
  // events forming a GESTURE_TAP event. The enclosing rectangle is updated
  // to be the union of the touch data from each of these events. It is
  // cleared on a ET_TOUCH_PRESSED event (i.e., at the beginning of a possible
  // GESTURE_TAP event) or when Reset is called.
  void UpdateEnclosingRectangle(const TouchEvent& event);
  void clear_enclosing_rectangle() { enclosing_rect_ = gfx::Rect(); }

  gfx::Point first_touch_position_;
  double first_touch_time_;
  gfx::Point last_touch_position_;
  double last_touch_time_;

  double last_tap_time_;
  gfx::Point last_tap_position_;

  VelocityCalculator velocity_calculator_;

  int point_id_;
  int touch_id_;

  // Represents the rectangle that encloses the circles/ellipses
  // generated by a sequence of touch events
  gfx::Rect enclosing_rect_;

  DISALLOW_COPY_AND_ASSIGN(GesturePoint);
};

}  // namespace ui

#endif  // UI_BASE_GESTURES_GESTURE_POINT_H_