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.
5 #ifndef UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_H_
6 #define UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_H_
13 #include "base/time/time.h"
14 #include "ui/events/gesture_detection/gesture_detection_export.h"
18 // Abstract class for a generic motion-related event, patterned after that
19 // subset of Android's MotionEvent API used in gesture detection.
20 class GESTURE_DETECTION_EXPORT MotionEvent {
38 enum class ToolType { UNKNOWN, FINGER, STYLUS, MOUSE, ERASER, LAST = ERASER };
41 BUTTON_PRIMARY = 1 << 0,
42 BUTTON_SECONDARY = 1 << 1,
43 BUTTON_TERTIARY = 1 << 2,
45 BUTTON_FORWARD = 1 << 4,
46 BUTTON_STYLUS_PRIMARY = 1 << 5,
47 BUTTON_STYLUS_SECONDARY = 1 << 6
50 // The implementer promises that |GetPointerId()| will never exceed
52 enum { MAX_POINTER_ID = 31, MAX_TOUCH_POINT_COUNT = 16 };
54 virtual ~MotionEvent() {}
56 // An unique identifier this motion event.
57 virtual uint32_t GetUniqueEventId() const = 0;
58 virtual Action GetAction() const = 0;
59 // Only valid if |GetAction()| returns Action::POINTER_UP or
60 // Action::POINTER_DOWN.
61 virtual int GetActionIndex() const = 0;
62 virtual size_t GetPointerCount() const = 0;
63 virtual int GetPointerId(size_t pointer_index) const = 0;
64 virtual float GetX(size_t pointer_index) const = 0;
65 virtual float GetY(size_t pointer_index) const = 0;
66 virtual float GetRawX(size_t pointer_index) const = 0;
67 virtual float GetRawY(size_t pointer_index) const = 0;
68 virtual float GetTouchMajor(size_t pointer_index) const = 0;
69 virtual float GetTouchMinor(size_t pointer_index) const = 0;
70 virtual float GetOrientation(size_t pointer_index) const = 0;
71 virtual float GetPressure(size_t pointer_index) const = 0;
72 virtual float GetTiltX(size_t pointer_index) const = 0;
73 virtual float GetTiltY(size_t pointer_index) const = 0;
74 virtual ToolType GetToolType(size_t pointer_index) const = 0;
75 virtual int GetButtonState() const = 0;
76 virtual int GetFlags() const = 0;
77 virtual base::TimeTicks GetEventTime() const = 0;
79 // Optional historical data, default implementation provides an empty history.
80 virtual size_t GetHistorySize() const;
81 virtual base::TimeTicks GetHistoricalEventTime(size_t historical_index) const;
82 virtual float GetHistoricalTouchMajor(size_t pointer_index,
83 size_t historical_index) const;
84 virtual float GetHistoricalX(size_t pointer_index,
85 size_t historical_index) const;
86 virtual float GetHistoricalY(size_t pointer_index,
87 size_t historical_index) const;
89 // Get the id of the device which created the event. Currently Aura only.
90 virtual int GetSourceDeviceId(size_t pointer_index) const;
92 // Utility accessor methods for convenience.
93 int GetPointerId() const { return GetPointerId(0); }
94 float GetX() const { return GetX(0); }
95 float GetY() const { return GetY(0); }
96 float GetRawX() const { return GetRawX(0); }
97 float GetRawY() const { return GetRawY(0); }
98 float GetRawOffsetX() const { return GetRawX() - GetX(); }
99 float GetRawOffsetY() const { return GetRawY() - GetY(); }
101 float GetTouchMajor() const { return GetTouchMajor(0); }
102 float GetTouchMinor() const { return GetTouchMinor(0); }
104 // Returns the orientation in radians. The meaning is overloaded:
105 // * For a touch screen or pad, it's the orientation of the major axis
106 // clockwise from vertical. The return value lies in [-PI/2, PI/2].
107 // * For a stylus, it indicates the direction in which the stylus is pointing.
108 // The return value lies in [-PI, PI].
109 // Stylus 3D orientation is returned in GetTiltX/Y. TODO(jkwang):
110 // Cleanup the stylus comment & usage here.
111 float GetOrientation() const { return GetOrientation(0); }
113 float GetPressure() const { return GetPressure(0); }
114 // We have GetTiltX/Y here instead of GetTilt because MotionEvent spec is not
115 // expressive enough for both 2D touch-surface geometry and 3D pen-orientation
116 // geometry, as needed for PointerEvents:
117 // https://w3c.github.io/pointerevents
118 // Both GetTiltX and GetTiltY return angles in **degrees**, in the range
119 // [-90,90]. See the PointerEvent spec link above for details
120 float GetTiltX() const { return GetTiltX(0); }
121 float GetTiltY() const { return GetTiltY(0); }
122 ToolType GetToolType() const { return GetToolType(0); }
124 // O(N) search of pointers (use sparingly!). Returns -1 if |id| nonexistent.
125 int FindPointerIndexOfId(int id) const;
127 // Note that these methods perform shallow copies of the originating events.
128 // They guarantee only that the returned type will reflect the same
129 // data exposed by the MotionEvent interface; no guarantees are made that the
130 // underlying implementation is identical to the source implementation.
131 std::unique_ptr<MotionEvent> Clone() const;
132 std::unique_ptr<MotionEvent> Cancel() const;
135 GESTURE_DETECTION_EXPORT std::ostream& operator<<(
136 std::ostream& stream,
137 const MotionEvent::Action action);
138 GESTURE_DETECTION_EXPORT std::ostream& operator<<(
139 std::ostream& stream,
140 const MotionEvent::ToolType tool_type);
144 #endif // UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_H_