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_GESTURE_EVENT_DETAILS_H_
6#define UI_EVENTS_GESTURE_DETECTION_GESTURE_EVENT_DETAILS_H_
7
8#include "base/logging.h"
9#include "ui/events/event_constants.h"
10#include "ui/events/events_base_export.h"
11#include "ui/gfx/rect.h"
12#include "ui/gfx/rect_conversions.h"
13
14namespace ui {
15
16struct EVENTS_BASE_EXPORT GestureEventDetails {
17 public:
18  GestureEventDetails();
19  GestureEventDetails(EventType type, float delta_x, float delta_y);
20
21  EventType type() const { return type_; }
22
23  int touch_points() const { return touch_points_; }
24  void set_touch_points(int touch_points) {
25    DCHECK_GT(touch_points, 0);
26    touch_points_ = touch_points;
27  }
28
29  // TODO(tdresser): Return RectF. See crbug.com/337824.
30  const gfx::Rect bounding_box() const {
31    return ToEnclosingRect(bounding_box_);
32  }
33
34  const gfx::RectF& bounding_box_f() const {
35    return bounding_box_;
36  }
37
38  void set_bounding_box(const gfx::RectF& box) { bounding_box_ = box; }
39
40  float scroll_x_hint() const {
41    DCHECK_EQ(ET_GESTURE_SCROLL_BEGIN, type_);
42    return data.scroll_begin.x_hint;
43  }
44
45  float scroll_y_hint() const {
46    DCHECK_EQ(ET_GESTURE_SCROLL_BEGIN, type_);
47    return data.scroll_begin.y_hint;
48  }
49
50  float scroll_x() const {
51    DCHECK_EQ(ET_GESTURE_SCROLL_UPDATE, type_);
52    return data.scroll_update.x;
53  }
54
55  float scroll_y() const {
56    DCHECK_EQ(ET_GESTURE_SCROLL_UPDATE, type_);
57    return data.scroll_update.y;
58  }
59
60  float velocity_x() const {
61    DCHECK_EQ(ET_SCROLL_FLING_START, type_);
62    return data.fling_velocity.x;
63  }
64
65  float velocity_y() const {
66    DCHECK_EQ(ET_SCROLL_FLING_START, type_);
67    return data.fling_velocity.y;
68  }
69
70  float first_finger_width() const {
71    DCHECK_EQ(ET_GESTURE_TWO_FINGER_TAP, type_);
72    return data.first_finger_enclosing_rectangle.width;
73  }
74
75  float first_finger_height() const {
76    DCHECK_EQ(ET_GESTURE_TWO_FINGER_TAP, type_);
77    return data.first_finger_enclosing_rectangle.height;
78  }
79
80  float scale() const {
81    DCHECK_EQ(ET_GESTURE_PINCH_UPDATE, type_);
82    return data.scale;
83  }
84
85  bool swipe_left() const {
86    DCHECK_EQ(ET_GESTURE_SWIPE, type_);
87    return data.swipe.left;
88  }
89
90  bool swipe_right() const {
91    DCHECK_EQ(ET_GESTURE_SWIPE, type_);
92    return data.swipe.right;
93  }
94
95  bool swipe_up() const {
96    DCHECK_EQ(ET_GESTURE_SWIPE, type_);
97    return data.swipe.up;
98  }
99
100  bool swipe_down() const {
101    DCHECK_EQ(ET_GESTURE_SWIPE, type_);
102    return data.swipe.down;
103  }
104
105  int tap_count() const {
106    DCHECK(type_ == ET_GESTURE_TAP ||
107           type_ == ET_GESTURE_TAP_UNCONFIRMED ||
108           type_ == ET_GESTURE_DOUBLE_TAP);
109    return data.tap_count;
110  }
111
112  void set_tap_count(int tap_count) {
113    DCHECK_GE(tap_count, 0);
114    DCHECK(type_ == ET_GESTURE_TAP ||
115           type_ == ET_GESTURE_TAP_UNCONFIRMED ||
116           type_ == ET_GESTURE_DOUBLE_TAP);
117    data.tap_count = tap_count;
118  }
119
120 private:
121  EventType type_;
122  union Details {
123    Details();
124    struct {  // SCROLL start details.
125      // Distance that caused the scroll to start.  Generally redundant with
126      // the x/y values from the first scroll_update.
127      float x_hint;
128      float y_hint;
129    } scroll_begin;
130
131    struct {  // SCROLL delta.
132      float x;
133      float y;
134    } scroll_update;
135
136    float scale;  // PINCH scale.
137
138    struct {  // FLING velocity.
139      float x;
140      float y;
141    } fling_velocity;
142
143    // Dimensions of the first finger's enclosing rectangle for
144    // TWO_FINGER_TAP.
145    struct {
146      float width;
147      float height;
148    } first_finger_enclosing_rectangle;
149
150    struct {  // SWIPE direction.
151      bool left;
152      bool right;
153      bool up;
154      bool down;
155    } swipe;
156
157    // Tap information must be set for ET_GESTURE_TAP,
158    // ET_GESTURE_TAP_UNCONFIRMED, and ET_GESTURE_DOUBLE_TAP events.
159    int tap_count;  // TAP repeat count.
160  } data;
161
162  int touch_points_;  // Number of active touch points in the gesture.
163
164  // Bounding box is an axis-aligned rectangle that contains all the
165  // enclosing rectangles of the touch-points in the gesture.
166  gfx::RectF bounding_box_;
167};
168
169}  // namespace ui
170
171#endif  // UI_EVENTS_GESTURE_DETECTION_GESTURE_EVENT_DETAILS_H_
172