Home | History | Annotate | Download | only in gestures
      1 // Copyright (c) 2012 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_GESTURES_GESTURE_TYPES_H_
      6 #define UI_EVENTS_GESTURES_GESTURE_TYPES_H_
      7 
      8 #include "base/logging.h"
      9 #include "ui/events/event_constants.h"
     10 #include "ui/events/events_export.h"
     11 #include "ui/gfx/rect.h"
     12 
     13 namespace ui {
     14 
     15 class GestureEvent;
     16 class TouchEvent;
     17 
     18 struct EVENTS_EXPORT GestureEventDetails {
     19  public:
     20   GestureEventDetails(EventType type, float delta_x, float delta_y);
     21   GestureEventDetails(EventType type,
     22                       float delta_x, float delta_y,
     23                       float delta_x_ordinal, float delta_y_ordinal);
     24 
     25   EventType type() const { return type_; }
     26 
     27   int touch_points() const { return touch_points_; }
     28   void set_touch_points(int touch_points) { touch_points_ = touch_points; }
     29 
     30   const gfx::Rect& bounding_box() const { return bounding_box_; }
     31   void set_bounding_box(const gfx::Rect& box) { bounding_box_ = box; }
     32 
     33   void SetScrollVelocity(float velocity_x, float velocity_y,
     34                          float velocity_x_ordinal, float velocity_y_ordinal);
     35 
     36   float scroll_x() const {
     37     CHECK_EQ(ui::ET_GESTURE_SCROLL_UPDATE, type_);
     38     return data.scroll_update.x;
     39   }
     40 
     41   float scroll_y() const {
     42     CHECK_EQ(ui::ET_GESTURE_SCROLL_UPDATE, type_);
     43     return data.scroll_update.y;
     44   }
     45 
     46   float velocity_x() const {
     47     CHECK(type_ == ui::ET_GESTURE_SCROLL_UPDATE ||
     48           type_ == ui::ET_SCROLL_FLING_START);
     49     return type_ == ui::ET_SCROLL_FLING_START ? data.fling_velocity.x :
     50                                                 data.scroll_update.velocity_x;
     51   }
     52 
     53   float velocity_y() const {
     54     CHECK(type_ == ui::ET_GESTURE_SCROLL_UPDATE ||
     55           type_ == ui::ET_SCROLL_FLING_START);
     56     return type_ == ui::ET_SCROLL_FLING_START ? data.fling_velocity.y :
     57                                                 data.scroll_update.velocity_y;
     58   }
     59 
     60   // *_ordinal values are unmodified by rail based clamping.
     61   float scroll_x_ordinal() const {
     62     CHECK_EQ(ui::ET_GESTURE_SCROLL_UPDATE, type_);
     63     return data.scroll_update.x_ordinal;
     64   }
     65 
     66   float scroll_y_ordinal() const {
     67     CHECK_EQ(ui::ET_GESTURE_SCROLL_UPDATE, type_);
     68     return data.scroll_update.y_ordinal;
     69   }
     70 
     71   float velocity_x_ordinal() const {
     72     CHECK(type_ == ui::ET_GESTURE_SCROLL_UPDATE ||
     73           type_ == ui::ET_SCROLL_FLING_START);
     74     return type_ == ui::ET_SCROLL_FLING_START ?
     75         data.fling_velocity.x_ordinal :
     76         data.scroll_update.velocity_x_ordinal;
     77   }
     78 
     79   float velocity_y_ordinal() const {
     80     CHECK(type_ == ui::ET_GESTURE_SCROLL_UPDATE ||
     81           type_ == ui::ET_SCROLL_FLING_START);
     82     return type_ == ui::ET_SCROLL_FLING_START ?
     83         data.fling_velocity.y_ordinal :
     84         data.scroll_update.velocity_y_ordinal;
     85   }
     86 
     87   int touch_id() const {
     88     CHECK_EQ(ui::ET_GESTURE_LONG_PRESS, type_);
     89     return data.touch_id;
     90   }
     91 
     92   float first_finger_width() const {
     93     CHECK_EQ(ui::ET_GESTURE_TWO_FINGER_TAP, type_);
     94     return data.first_finger_enclosing_rectangle.width;
     95   }
     96 
     97   float first_finger_height() const {
     98     CHECK_EQ(ui::ET_GESTURE_TWO_FINGER_TAP, type_);
     99     return data.first_finger_enclosing_rectangle.height;
    100   }
    101 
    102   float scale() const {
    103     CHECK_EQ(ui::ET_GESTURE_PINCH_UPDATE, type_);
    104     return data.scale;
    105   }
    106 
    107   bool swipe_left() const {
    108     CHECK_EQ(ui::ET_GESTURE_MULTIFINGER_SWIPE, type_);
    109     return data.swipe.left;
    110   }
    111 
    112   bool swipe_right() const {
    113     CHECK_EQ(ui::ET_GESTURE_MULTIFINGER_SWIPE, type_);
    114     return data.swipe.right;
    115   }
    116 
    117   bool swipe_up() const {
    118     CHECK_EQ(ui::ET_GESTURE_MULTIFINGER_SWIPE, type_);
    119     return data.swipe.up;
    120   }
    121 
    122   bool swipe_down() const {
    123     CHECK_EQ(ui::ET_GESTURE_MULTIFINGER_SWIPE, type_);
    124     return data.swipe.down;
    125   }
    126 
    127   int tap_count() const {
    128     CHECK_EQ(ui::ET_GESTURE_TAP, type_);
    129     return data.tap_count;
    130   }
    131 
    132  private:
    133   ui::EventType type_;
    134   union {
    135     struct {  // SCROLL delta.
    136       float x;
    137       float y;
    138       float velocity_x;
    139       float velocity_y;
    140       float x_ordinal;
    141       float y_ordinal;
    142       float velocity_x_ordinal;
    143       float velocity_y_ordinal;
    144     } scroll_update;
    145 
    146     float scale;  // PINCH scale.
    147 
    148     struct {  // FLING velocity.
    149       float x;
    150       float y;
    151       float x_ordinal;
    152       float y_ordinal;
    153     } fling_velocity;
    154 
    155     int touch_id;  // LONG_PRESS touch-id.
    156 
    157     // Dimensions of the first finger's enclosing rectangle for TWO_FINGER_TAP.
    158     struct {
    159       float width;
    160       float height;
    161     } first_finger_enclosing_rectangle;
    162 
    163     struct {  // SWIPE direction.
    164       bool left;
    165       bool right;
    166       bool up;
    167       bool down;
    168     } swipe;
    169 
    170     int tap_count;  // TAP repeat count.
    171   } data;
    172 
    173   int touch_points_;  // Number of active touch points in the gesture.
    174 
    175   // Bounding box is an axis-aligned rectangle that contains all the
    176   // enclosing rectangles of the touch-points in the gesture.
    177   gfx::Rect bounding_box_;
    178 };
    179 
    180 // An abstract type for consumers of gesture-events created by the
    181 // gesture-recognizer.
    182 class EVENTS_EXPORT GestureConsumer {
    183  public:
    184   virtual ~GestureConsumer() {}
    185 };
    186 
    187 // GestureEventHelper creates implementation-specific gesture events and
    188 // can dispatch them.
    189 class EVENTS_EXPORT GestureEventHelper {
    190  public:
    191   virtual ~GestureEventHelper() {
    192   }
    193 
    194   // Returns true if this helper can dispatch events to |consumer|.
    195   virtual bool CanDispatchToConsumer(GestureConsumer* consumer) = 0;
    196   virtual void DispatchPostponedGestureEvent(GestureEvent* event) = 0;
    197   virtual void DispatchCancelTouchEvent(TouchEvent* event) = 0;
    198 };
    199 
    200 }  // namespace ui
    201 
    202 #endif  // UI_EVENTS_GESTURES_GESTURE_TYPES_H_
    203