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