motion_event.h 6.0 KB

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