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_POINT_H_
      6 #define UI_EVENTS_GESTURES_GESTURE_POINT_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "ui/events/gestures/velocity_calculator.h"
     10 #include "ui/gfx/point.h"
     11 #include "ui/gfx/rect.h"
     12 
     13 namespace ui {
     14 class TouchEvent;
     15 
     16 // A GesturePoint represents a single touch-point/finger during a gesture
     17 // recognition process.
     18 class GesturePoint {
     19  public:
     20   GesturePoint();
     21   ~GesturePoint();
     22 
     23   // Resets various states.
     24   void Reset();
     25 
     26   void ResetVelocity();
     27 
     28   // Updates some states when a Tap gesture has been recognized for this point.
     29   void UpdateForTap();
     30 
     31   // Updates some states when a Scroll gesture has been recognized for this
     32   // point.
     33   void UpdateForScroll();
     34 
     35   // Updates states depending on the event and the gesture-state.
     36   void UpdateValues(const TouchEvent& event);
     37 
     38   // Responds according to the state of the gesture point (i.e. the point can
     39   // represent a click or scroll etc.)
     40   bool IsInClickWindow(const TouchEvent& event) const;
     41   bool IsInDoubleClickWindow(const TouchEvent& event) const;
     42   bool IsInTripleClickWindow(const TouchEvent& event) const;
     43   bool IsInFlickWindow(const TouchEvent& event);
     44   bool IsInHorizontalRailWindow() const;
     45   bool IsInVerticalRailWindow() const;
     46   bool IsInsideTouchSlopRegion(const TouchEvent& event) const;
     47   bool IsInScrollWindow(const TouchEvent& event) const;
     48   bool BreaksHorizontalRail();
     49   bool BreaksVerticalRail();
     50   bool DidScroll(const TouchEvent& event, int distance) const;
     51 
     52   const gfx::PointF& first_touch_position() const {
     53     return first_touch_position_;
     54   }
     55 
     56   double last_touch_time() const { return last_touch_time_; }
     57   const gfx::PointF& last_touch_position() const {
     58     return last_touch_position_;
     59   }
     60   float x() const { return last_touch_position_.x(); }
     61   float y() const { return last_touch_position_.y(); }
     62 
     63   // point_id_ is used to drive GestureSequence::ProcessTouchEventForGesture.
     64   // point_ids are maintained such that the set of point_ids is always
     65   // contiguous, from 0 to the number of current touches.
     66   // A lower point_id indicates that a touch occurred first.
     67   // A negative point_id indicates that the GesturePoint is not currently
     68   // associated with a touch.
     69   void set_point_id(int point_id) { point_id_ = point_id; }
     70   int point_id() const { return point_id_; }
     71 
     72   void set_touch_id(int touch_id) { touch_id_ = touch_id; }
     73   int touch_id() const { return touch_id_; }
     74 
     75   bool in_use() const { return point_id_ >= 0; }
     76 
     77   gfx::Vector2dF ScrollDelta() const;
     78 
     79   float XVelocity() { return velocity_calculator_.XVelocity(); }
     80   float YVelocity() { return velocity_calculator_.YVelocity(); }
     81 
     82   const gfx::RectF& enclosing_rectangle() const { return enclosing_rect_; }
     83 
     84   void set_source_device_id(int source_device_id) {
     85     source_device_id_ = source_device_id;
     86   }
     87   int source_device_id() const { return source_device_id_; }
     88 
     89  private:
     90   // Various statistical functions to manipulate gestures.
     91 
     92   bool IsInClickTimeWindow() const;
     93   bool IsInClickAggregateTimeWindow(double before, double after) const;
     94   bool IsPointInsideDoubleTapTouchSlopRegion(
     95       gfx::PointF p1, gfx::PointF p2) const;
     96   bool IsOverMinFlickSpeed();
     97 
     98   // Returns -1, 0, 1 for |v| below the negative velocity threshold,
     99   // in [-threshold, threshold] or above respectively.
    100   int ScrollVelocityDirection(float v);
    101 
    102   // The enclosing rectangle represents a rectangular touch region generated
    103   // by a sequence of ET_TOUCH_PRESSED, ET_TOUCH_MOVED, and ET_TOUCH_RELEASED
    104   // events forming a GESTURE_TAP event. The enclosing rectangle is updated
    105   // to be the union of the touch data from each of these events. It is
    106   // cleared on a ET_TOUCH_PRESSED event (i.e., at the beginning of a possible
    107   // GESTURE_TAP event) or when Reset is called.
    108   void UpdateEnclosingRectangle(const TouchEvent& event);
    109   void clear_enclosing_rectangle() { enclosing_rect_ = gfx::RectF(); }
    110 
    111   // The position of the first touchdown event.
    112   gfx::PointF first_touch_position_;
    113   double first_touch_time_;
    114 
    115   gfx::PointF second_last_touch_position_;
    116   double second_last_touch_time_;
    117 
    118   gfx::PointF last_touch_position_;
    119   double last_touch_time_;
    120 
    121   double second_last_tap_time_;
    122   gfx::PointF second_last_tap_position_;
    123 
    124   double last_tap_time_;
    125   gfx::PointF last_tap_position_;
    126 
    127   VelocityCalculator velocity_calculator_;
    128 
    129   int point_id_;
    130   int touch_id_;
    131 
    132   // Represents the rectangle that encloses the circles/ellipses
    133   // generated by a sequence of touch events
    134   gfx::RectF enclosing_rect_;
    135 
    136   int source_device_id_;
    137 
    138   float max_touch_move_in_pixels_for_click_squared_;
    139   float max_distance_between_taps_for_double_tap_squared_;
    140 
    141   DISALLOW_COPY_AND_ASSIGN(GesturePoint);
    142 };
    143 
    144 }  // namespace ui
    145 
    146 #endif  // UI_EVENTS_GESTURES_GESTURE_POINT_H_
    147