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 IsInsideManhattanSquare(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::Point& first_touch_position() const {
     53     return first_touch_position_;
     54   }
     55 
     56   double last_touch_time() const { return last_touch_time_; }
     57   const gfx::Point& last_touch_position() const { return last_touch_position_; }
     58   int x() const { return last_touch_position_.x(); }
     59   int y() const { return last_touch_position_.y(); }
     60 
     61   // point_id_ is used to drive GestureSequence::ProcessTouchEventForGesture.
     62   // point_ids are maintained such that the set of point_ids is always
     63   // contiguous, from 0 to the number of current touches.
     64   // A lower point_id indicates that a touch occurred first.
     65   // A negative point_id indicates that the GesturePoint is not currently
     66   // associated with a touch.
     67   void set_point_id(int point_id) { point_id_ = point_id; }
     68   int point_id() const { return point_id_; }
     69 
     70   void set_touch_id(int touch_id) { touch_id_ = touch_id; }
     71   int touch_id() const { return touch_id_; }
     72 
     73   bool in_use() const { return point_id_ >= 0; }
     74 
     75   gfx::Vector2d ScrollDelta();
     76 
     77   float XVelocity() { return velocity_calculator_.XVelocity(); }
     78   float YVelocity() { return velocity_calculator_.YVelocity(); }
     79 
     80   const gfx::Rect& enclosing_rectangle() const { return enclosing_rect_; }
     81 
     82  private:
     83   // Various statistical functions to manipulate gestures.
     84 
     85   // Tests if the point has a consistent scroll vector across a window of touch
     86   // move events.
     87   bool IsConsistentScrollingActionUnderway() const;
     88   bool IsInClickTimeWindow() const;
     89   bool IsInClickAggregateTimeWindow(double before, double after) const;
     90   bool IsPointInsideManhattanSquare(gfx::Point p1, gfx::Point p2) const;
     91   bool IsOverMinFlickSpeed();
     92 
     93   // Returns -1, 0, 1 for |v| below the negative velocity threshold,
     94   // in [-threshold, threshold] or above respectively.
     95   int ScrollVelocityDirection(float v);
     96 
     97   // The enclosing rectangle represents a rectangular touch region generated
     98   // by a sequence of ET_TOUCH_PRESSED, ET_TOUCH_MOVED, and ET_TOUCH_RELEASED
     99   // events forming a GESTURE_TAP event. The enclosing rectangle is updated
    100   // to be the union of the touch data from each of these events. It is
    101   // cleared on a ET_TOUCH_PRESSED event (i.e., at the beginning of a possible
    102   // GESTURE_TAP event) or when Reset is called.
    103   void UpdateEnclosingRectangle(const TouchEvent& event);
    104   void clear_enclosing_rectangle() { enclosing_rect_ = gfx::Rect(); }
    105 
    106   // The position of the first touchdown event.
    107   gfx::Point first_touch_position_;
    108   double first_touch_time_;
    109 
    110   gfx::Point second_last_touch_position_;
    111   double second_last_touch_time_;
    112 
    113   gfx::Point last_touch_position_;
    114   double last_touch_time_;
    115 
    116   double second_last_tap_time_;
    117   gfx::Point second_last_tap_position_;
    118 
    119   double last_tap_time_;
    120   gfx::Point last_tap_position_;
    121 
    122   VelocityCalculator velocity_calculator_;
    123 
    124   int point_id_;
    125   int touch_id_;
    126 
    127   // Represents the rectangle that encloses the circles/ellipses
    128   // generated by a sequence of touch events
    129   gfx::Rect enclosing_rect_;
    130 
    131   // Count of the number of events with same direction.
    132   gfx::Vector2d same_direction_count_;
    133 
    134   DISALLOW_COPY_AND_ASSIGN(GesturePoint);
    135 };
    136 
    137 }  // namespace ui
    138 
    139 #endif  // UI_EVENTS_GESTURES_GESTURE_POINT_H_
    140