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_MOTION_EVENT_H_
6#define UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/time/time.h"
10#include "ui/events/gesture_detection/gesture_detection_export.h"
11
12namespace ui {
13
14// Abstract class for a generic motion-related event, patterned after that
15// subset of Android's MotionEvent API used in gesture detection.
16class GESTURE_DETECTION_EXPORT MotionEvent {
17 public:
18  enum Action {
19    ACTION_DOWN,
20    ACTION_UP,
21    ACTION_MOVE,
22    ACTION_CANCEL,
23    ACTION_POINTER_DOWN,
24    ACTION_POINTER_UP,
25  };
26
27  enum ToolType {
28    TOOL_TYPE_UNKNOWN,
29    TOOL_TYPE_FINGER,
30    TOOL_TYPE_STYLUS,
31    TOOL_TYPE_MOUSE,
32    TOOL_TYPE_ERASER
33  };
34
35  enum ButtonType {
36    BUTTON_PRIMARY = 1 << 0,
37    BUTTON_SECONDARY = 1 << 1,
38    BUTTON_TERTIARY = 1 << 2,
39    BUTTON_BACK = 1 << 3,
40    BUTTON_FORWARD = 1 << 4,
41  };
42
43  // The implementer promises that |GetPointerId()| will never exceed
44  // MAX_POINTER_ID.
45  enum { MAX_POINTER_ID = 31, MAX_TOUCH_POINT_COUNT = 12 };
46
47  virtual ~MotionEvent() {}
48
49  virtual int GetId() const = 0;
50  virtual Action GetAction() const = 0;
51  // Only valid if |GetAction()| returns ACTION_POINTER_UP or
52  // ACTION_POINTER_DOWN.
53  virtual int GetActionIndex() const = 0;
54  virtual size_t GetPointerCount() const = 0;
55  virtual int GetPointerId(size_t pointer_index) const = 0;
56  virtual float GetX(size_t pointer_index) const = 0;
57  virtual float GetY(size_t pointer_index) const = 0;
58  virtual float GetRawX(size_t pointer_index) const = 0;
59  virtual float GetRawY(size_t pointer_index) const = 0;
60  virtual float GetTouchMajor(size_t pointer_index) const = 0;
61  virtual float GetTouchMinor(size_t pointer_index) const = 0;
62  virtual float GetOrientation(size_t pointer_index) const = 0;
63  virtual float GetPressure(size_t pointer_index) const = 0;
64  virtual ToolType GetToolType(size_t pointer_index) const = 0;
65  virtual int GetButtonState() const = 0;
66  virtual int GetFlags() const = 0;
67  virtual base::TimeTicks GetEventTime() const = 0;
68
69  // Optional historical data, default implementation provides an empty history.
70  virtual size_t GetHistorySize() const;
71  virtual base::TimeTicks GetHistoricalEventTime(size_t historical_index) const;
72  virtual float GetHistoricalTouchMajor(size_t pointer_index,
73                                        size_t historical_index) const;
74  virtual float GetHistoricalX(size_t pointer_index,
75                               size_t historical_index) const;
76  virtual float GetHistoricalY(size_t pointer_index,
77                               size_t historical_index) const;
78
79  virtual scoped_ptr<MotionEvent> Clone() const = 0;
80  virtual scoped_ptr<MotionEvent> Cancel() const = 0;
81
82  float GetX() const { return GetX(0); }
83  float GetY() const { return GetY(0); }
84  float GetRawX() const { return GetRawX(0); }
85  float GetRawY() const { return GetRawY(0); }
86  float GetRawOffsetX() const { return GetRawX() - GetX(); }
87  float GetRawOffsetY() const { return GetRawY() - GetY(); }
88
89  float GetTouchMajor() const { return GetTouchMajor(0); }
90  float GetTouchMinor() const { return GetTouchMinor(0); }
91
92  // Returns the orientation of the major axis clockwise from vertical, in
93  // radians. The return value lies in [-PI/2, PI/2].
94  float GetOrientation() const { return GetOrientation(0); }
95
96  float GetPressure() const { return GetPressure(0); }
97  ToolType GetToolType() const { return GetToolType(0); }
98
99  // O(N) search of pointers (use sparingly!). Returns -1 if |id| nonexistent.
100  int FindPointerIndexOfId(int id) const;
101};
102
103GESTURE_DETECTION_EXPORT bool operator==(const MotionEvent& lhs,
104                                         const MotionEvent& rhs);
105GESTURE_DETECTION_EXPORT bool operator!=(const MotionEvent& lhs,
106                                         const MotionEvent& rhs);
107
108}  // namespace ui
109
110#endif  // UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_H_
111