gesture_event_data_packet.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "ui/events/gesture_detection/gesture_event_data_packet.h"
  5. #include "base/check_op.h"
  6. #include "base/notreached.h"
  7. #include "ui/events/gesture_detection/motion_event.h"
  8. namespace ui {
  9. namespace {
  10. GestureEventDataPacket::GestureSource ToGestureSource(
  11. const ui::MotionEvent& event) {
  12. switch (event.GetAction()) {
  13. case ui::MotionEvent::Action::DOWN:
  14. return GestureEventDataPacket::TOUCH_SEQUENCE_START;
  15. case ui::MotionEvent::Action::UP:
  16. return GestureEventDataPacket::TOUCH_SEQUENCE_END;
  17. case ui::MotionEvent::Action::MOVE:
  18. return GestureEventDataPacket::TOUCH_MOVE;
  19. case ui::MotionEvent::Action::CANCEL:
  20. return GestureEventDataPacket::TOUCH_SEQUENCE_CANCEL;
  21. case ui::MotionEvent::Action::POINTER_DOWN:
  22. return GestureEventDataPacket::TOUCH_START;
  23. case ui::MotionEvent::Action::POINTER_UP:
  24. return GestureEventDataPacket::TOUCH_END;
  25. case ui::MotionEvent::Action::NONE:
  26. case ui::MotionEvent::Action::HOVER_ENTER:
  27. case ui::MotionEvent::Action::HOVER_EXIT:
  28. case ui::MotionEvent::Action::HOVER_MOVE:
  29. case ui::MotionEvent::Action::BUTTON_PRESS:
  30. case ui::MotionEvent::Action::BUTTON_RELEASE:
  31. NOTREACHED();
  32. return GestureEventDataPacket::INVALID;
  33. }
  34. NOTREACHED();
  35. return GestureEventDataPacket::INVALID;
  36. }
  37. } // namespace
  38. GestureEventDataPacket::GestureEventDataPacket()
  39. : gesture_source_(UNDEFINED),
  40. ack_state_(AckState::PENDING),
  41. unique_touch_event_id_(0) {
  42. }
  43. GestureEventDataPacket::GestureEventDataPacket(
  44. base::TimeTicks timestamp,
  45. GestureSource source,
  46. const gfx::PointF& touch_location,
  47. const gfx::PointF& raw_touch_location,
  48. uint32_t unique_touch_event_id)
  49. : timestamp_(timestamp),
  50. touch_location_(touch_location),
  51. raw_touch_location_(raw_touch_location),
  52. gesture_source_(source),
  53. ack_state_(AckState::PENDING),
  54. unique_touch_event_id_(unique_touch_event_id) {
  55. DCHECK_NE(gesture_source_, UNDEFINED);
  56. }
  57. GestureEventDataPacket::GestureEventDataPacket(
  58. const GestureEventDataPacket& other)
  59. : timestamp_(other.timestamp_),
  60. gestures_(other.gestures_),
  61. touch_location_(other.touch_location_),
  62. raw_touch_location_(other.raw_touch_location_),
  63. gesture_source_(other.gesture_source_),
  64. ack_state_(other.ack_state_),
  65. unique_touch_event_id_(other.unique_touch_event_id_) {}
  66. GestureEventDataPacket::~GestureEventDataPacket() {
  67. }
  68. GestureEventDataPacket& GestureEventDataPacket::operator=(
  69. const GestureEventDataPacket& other) {
  70. timestamp_ = other.timestamp_;
  71. gesture_source_ = other.gesture_source_;
  72. touch_location_ = other.touch_location_;
  73. raw_touch_location_ = other.raw_touch_location_;
  74. gestures_ = other.gestures_;
  75. ack_state_ = other.ack_state_;
  76. unique_touch_event_id_ = other.unique_touch_event_id_;
  77. return *this;
  78. }
  79. void GestureEventDataPacket::Push(const GestureEventData& original_gesture) {
  80. DCHECK_NE(ET_UNKNOWN, original_gesture.type());
  81. GestureEventData gesture(original_gesture);
  82. gesture.unique_touch_event_id = unique_touch_event_id_;
  83. gestures_->push_back(gesture);
  84. }
  85. GestureEventDataPacket GestureEventDataPacket::FromTouch(
  86. const ui::MotionEvent& touch) {
  87. return GestureEventDataPacket(touch.GetEventTime(), ToGestureSource(touch),
  88. gfx::PointF(touch.GetX(), touch.GetY()),
  89. gfx::PointF(touch.GetRawX(), touch.GetRawY()),
  90. touch.GetUniqueEventId());
  91. }
  92. GestureEventDataPacket GestureEventDataPacket::FromTouchTimeout(
  93. const GestureEventData& gesture) {
  94. GestureEventDataPacket packet(gesture.time, TOUCH_TIMEOUT,
  95. gfx::PointF(gesture.x, gesture.y),
  96. gfx::PointF(gesture.raw_x, gesture.raw_y),
  97. gesture.unique_touch_event_id);
  98. packet.Push(gesture);
  99. return packet;
  100. }
  101. void GestureEventDataPacket::Ack(bool event_consumed,
  102. bool is_source_touch_event_set_blocking) {
  103. DCHECK_EQ(static_cast<int>(ack_state_), static_cast<int>(AckState::PENDING));
  104. ack_state_ = event_consumed ? AckState::CONSUMED : AckState::UNCONSUMED;
  105. for (auto& gesture : gestures_.container()) {
  106. gesture.details.set_is_source_touch_event_set_blocking(
  107. is_source_touch_event_set_blocking);
  108. }
  109. }
  110. } // namespace ui