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