Home | History | Annotate | Download | only in gestures
      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_UI_MOTION_EVENT_H_
      6 #define UI_EVENTS_GESTURE_DETECTION_UI_MOTION_EVENT_H_
      7 
      8 #include "ui/events/gesture_detection/motion_event.h"
      9 
     10 #include <map>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/time/time.h"
     14 #include "ui/events/event.h"
     15 #include "ui/events/events_export.h"
     16 #include "ui/events/gestures/gesture_sequence.h"
     17 
     18 namespace ui {
     19 
     20 // Implementation of MotionEvent which takes a stream of ui::TouchEvents.
     21 class EVENTS_EXPORT MotionEventAura : public MotionEvent {
     22  public:
     23   MotionEventAura();
     24   virtual ~MotionEventAura();
     25 
     26   void OnTouch(const TouchEvent& touch);
     27 
     28   // MotionEvent implementation.
     29   virtual int GetId() const OVERRIDE;
     30   virtual Action GetAction() const OVERRIDE;
     31   virtual int GetActionIndex() const OVERRIDE;
     32   virtual size_t GetPointerCount() const OVERRIDE;
     33   virtual int GetPointerId(size_t pointer_index) const OVERRIDE;
     34   virtual float GetX(size_t pointer_index) const OVERRIDE;
     35   virtual float GetY(size_t pointer_index) const OVERRIDE;
     36   virtual float GetRawX(size_t pointer_index) const OVERRIDE;
     37   virtual float GetRawY(size_t pointer_index) const OVERRIDE;
     38   virtual float GetTouchMajor(size_t pointer_index) const OVERRIDE;
     39   virtual float GetPressure(size_t pointer_index) const OVERRIDE;
     40   virtual base::TimeTicks GetEventTime() const OVERRIDE;
     41 
     42   virtual size_t GetHistorySize() const OVERRIDE;
     43   virtual base::TimeTicks GetHistoricalEventTime(size_t historical_index) const
     44       OVERRIDE;
     45   virtual float GetHistoricalTouchMajor(size_t pointer_index,
     46                                         size_t historical_index) const OVERRIDE;
     47   virtual float GetHistoricalX(size_t pointer_index,
     48                                size_t historical_index) const OVERRIDE;
     49   virtual float GetHistoricalY(size_t pointer_index,
     50                                size_t historical_index) const OVERRIDE;
     51   virtual ToolType GetToolType(size_t pointer_index) const OVERRIDE;
     52   virtual int GetButtonState() const OVERRIDE;
     53 
     54   virtual scoped_ptr<MotionEvent> Clone() const OVERRIDE;
     55   virtual scoped_ptr<MotionEvent> Cancel() const OVERRIDE;
     56 
     57   int GetSourceDeviceId(size_t pointer_index) const;
     58 
     59   // We can't cleanup removed touch points immediately upon receipt of a
     60   // TouchCancel or TouchRelease, as the MotionEvent needs to be able to report
     61   // information about those touch events. Once the MotionEvent has been
     62   // processed, we call CleanupRemovedTouchPoints to do the required
     63   // book-keeping.
     64   void CleanupRemovedTouchPoints(const TouchEvent& event);
     65 
     66  private:
     67   struct PointData {
     68     PointData();
     69     float x;
     70     float y;
     71     float raw_x;
     72     float raw_y;
     73     int touch_id;
     74     float pressure;
     75     int source_device_id;
     76     float major_radius;
     77   };
     78 
     79   MotionEventAura(
     80       size_t pointer_count,
     81       const base::TimeTicks& last_touch_time,
     82       Action cached_action,
     83       int cached_action_index,
     84       const PointData (&active_touches)[GestureSequence::kMaxGesturePoints]);
     85 
     86   static PointData GetPointDataFromTouchEvent(const TouchEvent& touch);
     87   void AddTouch(const TouchEvent& touch);
     88   void UpdateTouch(const TouchEvent& touch);
     89   void UpdateCachedAction(const TouchEvent& touch);
     90   size_t GetIndexFromId(int id) const;
     91 
     92   size_t pointer_count_;
     93   base::TimeTicks last_touch_time_;
     94   Action cached_action_;
     95   // The index of the touch responsible for last ACTION_POINTER_DOWN or
     96   // ACTION_POINTER_UP. -1 if no such action has occurred.
     97   int cached_action_index_;
     98 
     99   // We want constant time indexing by pointer_index, and fast indexing by id.
    100   // TODO(tdresser): figure out which constant to use here.
    101   PointData active_touches_[GestureSequence::kMaxGesturePoints];
    102 
    103   DISALLOW_COPY_AND_ASSIGN(MotionEventAura);
    104 };
    105 
    106 }  // namespace ui
    107 
    108 #endif  // UI_EVENTS_GESTURE_DETECTION_UI_MOTION_EVENT_H_
    109