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 #include "ui/events/gesture_detection/gesture_event_data_packet.h"
7 #include "base/logging.h"
8 #include "ui/events/gesture_detection/motion_event.h"
13 GestureEventDataPacket::GestureSource ToGestureSource(
14 const ui::MotionEvent& event) {
15 switch (event.GetAction()) {
16 case ui::MotionEvent::ACTION_DOWN:
17 return GestureEventDataPacket::TOUCH_SEQUENCE_START;
18 case ui::MotionEvent::ACTION_UP:
19 return GestureEventDataPacket::TOUCH_SEQUENCE_END;
20 case ui::MotionEvent::ACTION_MOVE:
21 return GestureEventDataPacket::TOUCH_MOVE;
22 case ui::MotionEvent::ACTION_CANCEL:
23 return GestureEventDataPacket::TOUCH_SEQUENCE_CANCEL;
24 case ui::MotionEvent::ACTION_POINTER_DOWN:
25 return GestureEventDataPacket::TOUCH_START;
26 case ui::MotionEvent::ACTION_POINTER_UP:
27 return GestureEventDataPacket::TOUCH_END;
29 NOTREACHED() << "Invalid ui::MotionEvent action: " << event.GetAction();
30 return GestureEventDataPacket::INVALID;
35 GestureEventDataPacket::GestureEventDataPacket()
36 : gesture_count_(0), gesture_source_(UNDEFINED) {
39 GestureEventDataPacket::GestureEventDataPacket(
40 base::TimeTicks timestamp,
42 const gfx::PointF& touch_location,
43 const gfx::PointF& raw_touch_location)
44 : timestamp_(timestamp),
46 touch_location_(touch_location),
47 raw_touch_location_(raw_touch_location),
48 gesture_source_(source) {
49 DCHECK_NE(gesture_source_, UNDEFINED);
52 GestureEventDataPacket::GestureEventDataPacket(
53 const GestureEventDataPacket& other)
54 : timestamp_(other.timestamp_),
55 gesture_count_(other.gesture_count_),
56 touch_location_(other.touch_location_),
57 raw_touch_location_(other.raw_touch_location_),
58 gesture_source_(other.gesture_source_) {
59 std::copy(other.gestures_, other.gestures_ + other.gesture_count_, gestures_);
62 GestureEventDataPacket::~GestureEventDataPacket() {
65 GestureEventDataPacket& GestureEventDataPacket::operator=(
66 const GestureEventDataPacket& other) {
67 timestamp_ = other.timestamp_;
68 gesture_count_ = other.gesture_count_;
69 gesture_source_ = other.gesture_source_;
70 touch_location_ = other.touch_location_;
71 raw_touch_location_ = other.raw_touch_location_;
72 std::copy(other.gestures_, other.gestures_ + other.gesture_count_, gestures_);
76 void GestureEventDataPacket::Push(const GestureEventData& gesture) {
77 DCHECK_NE(ET_UNKNOWN, gesture.type());
78 CHECK_LT(gesture_count_, static_cast<size_t>(kMaxGesturesPerTouch));
79 gestures_[gesture_count_++] = gesture;
82 GestureEventDataPacket GestureEventDataPacket::FromTouch(
83 const ui::MotionEvent& touch) {
84 return GestureEventDataPacket(touch.GetEventTime(),
85 ToGestureSource(touch),
86 gfx::PointF(touch.GetX(), touch.GetY()),
87 gfx::PointF(touch.GetRawX(), touch.GetRawY()));
90 GestureEventDataPacket GestureEventDataPacket::FromTouchTimeout(
91 const GestureEventData& gesture) {
92 GestureEventDataPacket packet(gesture.time,
94 gfx::PointF(gesture.x, gesture.y),
95 gfx::PointF(gesture.raw_x, gesture.raw_y));